Docker 常见问题 (FAQ)-2015
2019獨角獸企業重金招聘Python工程師標準>>>
內核需求
- rhel/centos 要求內核在 2.6.32-431 (系統版本6.5) 及以上
- debian/ubuntu 要求內核在 3.8 以上
修改鏡像/容器文件的存儲位置
方法一 修改 docker daemon 的啟動參數
-g, --graph=""Path to use as the root of the Docker runtime. Default is /var/lib/docker.如?docker -d --graph=/opt/docker
docker daemon 的啟動參數修改方法
rhel/centos 下, 默認啟動參數在?/etc/sysconfig/docker, 如:
6.x:
other_args="--graph=/opt/docker "7.x: (update: 2015-01-21)
OPTIONS="--graph=/opt/docker "debian/ubuntu 下, 默認啟動參數在?/etc/default/docker, 如:
DOCKER_OPTS="--graph=/opt/docker "方法二 指定掛載目錄
mount -o bind /var/lib/docker /opt/docker永久修改在需要在 /etc/fatab 添加:
/opt/docker /var/lib/docker none bind 0 0方法三 軟連接
ln -s /var/lib/docker /opt/docker容器剛運行就退出了(Exited)
很多人發現剛剛執行了一個?docker run?的命令, 再用?docker ps -a?的時候, 剛剛那個容器就已經 Exited 了, 比如
docker run -d <IMAGE> service sshd start上述命令一執行完就會發現容器就退出了, 那是因為?service sshd start?不是一個前臺進程, 這個進程執行完以后就會退出, 所以這里必須使用前臺方式(nodaemon)運行進程, 上述例子中要使用
docker run -d <IMAGE> /usr/sbin/sshd -D其他應用程序也要使用對應的 nodaemon 的參數, 沒有的話就要加一個不會退出的命令, 如:
docker run -d <IMAGE> <CMD> && tail -f xxx.logselinux (Permission denied)
當開啟 selinux 經常會遇到 Permission denied 錯誤, 如:
docker run -i -v /data:/data ubuntu bash然后運行?ls /data?時就會報如下錯誤
ls: cannot open directory .: Permission denied解決方案有兩個:
一是關閉宿主機的 selinux
二是在宿主機上添加 selinux 規則, 以上面為例:
chcon -Rt svirt_sandbox_file_t /data其他情形下 如果是因為 selinux 引起 Permission denied 也要添加相應的規則或者直接關閉 selinux
容器固定 IP
pipework
- OS: rhel/centos 6.x
在 rhel/centos 6.x 下使用 pipework 時會報如下錯誤:
Object "netns" is unknown, try "ip help".原因是 rhel/centos 6.x 的 iproute 包默認并不支持?ip netns?命令, 所以這里需要安裝新的 iproute 包
這里使用 RDO 的源:
yum install -y https://repos.fedorapeople.org/repos/openstack/openstack-icehouse/rdo-release-icehouse-4.noarch.rpm yum install -y iproute注意:?在這里最新的 RDO 源(openstack-juno)已經不支持 rhel/centos 6.x 了, 如有更新可以到這個目錄下查看
如果已經安裝了 openstack-juno 的需先卸載
進入容器
因為容器本身其實就是把進程/資源隔離了, 嚴格意義上講不存在所謂的進入容器, 通常這里的所說的進入容器指的是在容器的 namespace 內執行 shell
小于 1.3 版本
小于 1.3 版本的可以使用 nsenter:
https://github.com/jpetazzo/nsenter
1.3 版本以上
如果 docker 版本已經在 1.3 以上了, 那么可以用?docker exec?這個命令:
docker exec -it <CONTAINER ID> bash注意
ssh
不建議使用 ssh 進入容器, 關于為什么不建議使用, 請參考如下文章:
- http://blog.docker.com/2014/06/why-you-dont-need-to-run-sshd-in-docker/
- http://www.oschina.net/translate/why-you-dont-need-to-run-sshd-in-docker
docker attach
為什么執行?docker attach?卡住了?
首先要明確的是?docker attach?不是一個用來進入容器的命令, 或者說他不是用來在容器內運行一個 bash(shell) 的命令, 它是用來連接到容器中運行中的進程, 也就是容器的?CMD, 容器內 PID=1 的那個進程, 如果這個進程沒有 stdout/stderr 那么你將看不到任何輸出, 如果它沒有接收 stdin 你也無法發送指令給它. 這也就是為什么你運行一個 bash 的容器, 就可以 attach 進去執行命令, 而你運行一個 mysql server 的容器就無法操作的原因
如何退出 attach 的容器
這里要使用?CTRL-P CTRL-Q?來退出容器, 如果使用?CTRL+C?那么會導致容器結束(Exited), 因為它會發送?SIGKILL?給容器的進程, 然后這個容器就 Exited 了, 當然這里可以使用?docker attach --sig-proxy=false?防止發送?SIGKILL?給進程
https://docs.docker.com/reference/commandline/cli/#attach
You can detach from the container again (and leave it running) with CTRL-p CTRL-q (for a quiet exit), or CTRL-c which will send a SIGKILL to the container, or CTRL-\ to get a stacktrace of the Docker client when it quits. When you detach from the container's process the exit code will be returned to the client.
Docker 私有庫自簽名 SSL 報錯 (Invalid registry endpoint ... unknown CA certificate)
docker 升級到 1.3 以后使用?docker pull/push?等命令時必須要求 registry 使用 SSL, 否則就會報如下錯誤
Error: Invalid registry endpoint https://registry.xx.com/v1/: Get https://registry.xx.com/v1/_ping: EOF. If this private registry supports only HTTP or HTTPS with an unknown CA certificate, please add `--insecure-registry registry.xx.com` to the daemon's arguments. In the case of HTTPS, if you have access to the registry's CA certificate, no need for the flag; simply place the CA certificate at /etc/docker/certs.d/registry.xx.com/ca.crt
這里是因為你使用了自簽名的證書導致系統不信任, 如報錯信息所描述, 解決方法有兩個
一是添加?--insecure-registry registry.xx.com?參數到 docker daemon
二是把私有 docker registry 的 CA 證書放到指定的位置(/etc/docker/certs.d/registry.xx.com/ca.crt)
這里的 CA 證書是指你簽發 registry.xx.com 這個域名的 SSL 證書所用的 CA
證書的制作方法可參見?http://www.lsproc.com/post/easyrsa-generate-ssl-cert/
docker daemon 重啟后某些容器無法啟動 (device or resource busy)
當重啟 docker daemon 有時候會導致某些容器無法啟動, 手動啟動容器會報如下錯誤
Error response from daemon: Cannot start container 5e9bde9b409b: Error getting container 5e9bde9b409b001bcc685c0b478e925a53a03bab8d8ef3210bf24aa39410e30d from driver devicemapper: Error mounting '/dev/mapper/docker-253:0-267081-5e9bde9b409b001bcc685c0b478e925a53a03bab8d8ef3210bf24aa39410e30d' on '/var/lib/docker/devicemapper/mnt/5e9bde9b409b001bcc685c0b478e925a53a03bab8d8ef3210bf24aa39410e30d': device or resource busy目前來看這應該是一個 bug:?docker/docker#5684,?docker/docker#4036
解決辦法有兩個:
一是發現報錯后, 在啟動容器之前手動 umount:
umount /var/lib/docker/devicemapper/mnt/5e9bde9b409b001bcc685c0b478e925a53a03bab8d8ef3210bf24aa39410e30d二是 docker daemon stop 時先停止容器運行, 修改 /etc/init.d/docker 如下
stop() {[ -x $exec ] || exit 5echo -n $"Stopping $prog: "if [[ -n $($exec ps -q) ]]; then$exec stop $($exec ps -q) > /dev/nullfikillproc -p $pidfile -d 300 $progretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retval }不過這樣因為會停止所有容器, 所以重啟速度會變慢
另外重啟后所有容器默認都不會啟動, 如果讓容器在 daemon 啟動時自動開啟, 那么需要在 docker run 時添加參數?--restart always, 如:
docker run -d --restart always <IMAGE> <CMD>docker 1.6.x 關閉 iptables 時不能 link 容器
2015-06-08 更新
使用 docker 1.6, daemon 參數關閉了 iptables 選項時(--iptables=false), link 容器時會報錯:
Cannot start container 35437aa2c224d580d7555a2f900b95d6d5ab282bb91c34e96ae0cda106557ff0: (exit status 1)
解決方法一是 daemon 打開 iptables(--iptables=true), 或者手動創建 DOCKER clain:
iptables -N DOCKERbug 詳情在此:?https://github.com/docker/docker/issues/12701
docker-compose 使用2進制方式安裝后每過一段時間就不可用
2015-10-11 更新
使用 docker-compose 的過程中發現每過一段時間2進制文件就不可用, 報如下錯誤
Cannot open self /usr/bin/docker-compose or archive /usr/bin/docker-compose.pkg
解決方法為(rhel/centos):
echo "-b /usr/bin/docker-compose" > /etc/prelink.conf.d/docker-compose.confdebian/ubuntu 也找到?prelink?相應的配置文件地址增加上述配置即可
bug 詳情在此:?https://github.com/docker/compose/issues/1135
延伸閱讀
- http://knktc.com/2014/08/09/docker-cheat-sheet/
- http://csaba.palfi.me/random-docker-tips/
- http://dockerone.com/article/59
- http://dockerpool.com/article/1413082493
轉載于:https://my.oschina.net/u/3362827/blog/1505518
總結
以上是生活随笔為你收集整理的Docker 常见问题 (FAQ)-2015的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用Kotlin开发android平台语音
- 下一篇: 自定义ServicesLoader来实现