为什么 docker 容器总是重启?
Why docker container always restarting?
我运行一个容器在后台使用:
docker run --restart always --name lnmp -v /Users/gedongdong/web:/var/www/ -itd lnmp
码头文件:
FROM alpine:edge
LABEL author=gedongdong2010@163.com
RUN mkdir -p /run/nginx && mkdir -p /shell
RUN echo http://mirrors.aliyun.com/alpine/edge/main > /etc/apk/repositories && \
echo http://mirrors.aliyun.com/alpine/edge/community >> /etc/apk/repositories && \
apk update && apk add --no-cache nginx
COPY vhosts.conf /etc/nginx/conf.d/
COPY start.sh /shell
RUN chmod -R 777 /shell
EXPOSE 80 443 6379
CMD ["/shell/start.sh"]
start.sh:
nginx -c /etc/nginx/nginx.conf
tail -f /dev/null
vhosts.conf:
server {
listen 80;
server_name docker.test;
root /var/www;
index index.html;
}
当我使用 docker ps -a
:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a3910c0dc29 lnmp "/shell/start.sh" 16 minutes ago Restarting (1) 50 seconds ago lnmp
docker ps -a
为什么我的容器总是重启?
将 #!/bin/sh
添加到您的 start.sh 文件
#!/bin/sh
nginx -c /etc/nginx/nginx.conf
tail -f /dev/null
为什么容器总是重启:
正如 Henry 在他的评论中指出的那样,您的设置 --restart always
就是这么说的。通常,请记住当容器 stops/crashes 的 PID 1
时,容器退出。例如,您的容器显示如下内容:
(注意问题所在的 PID 1
行)
docker container exec -it lnmp top -n 1 -b
Mem: 2846060K used, 3256768K free, 62108K shrd, 83452K buff, 1102096K cached
CPU: 2% usr 2% sys 0% nic 95% idle 0% io 0% irq 0% sirq
Load average: 0.09 0.24 0.27 1/892 41
PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND
10 9 nginx S 15372 0% 0 0% nginx: worker process
12 9 nginx S 15372 0% 5 0% nginx: worker process
17 9 nginx S 15372 0% 1 0% nginx: worker process
11 9 nginx S 15372 0% 7 0% nginx: worker process
18 9 nginx S 15372 0% 5 0% nginx: worker process
15 9 nginx S 15372 0% 4 0% nginx: worker process
14 9 nginx S 15372 0% 1 0% nginx: worker process
16 9 nginx S 15372 0% 4 0% nginx: worker process
9 1 root S 14924 0% 6 0% nginx: master process nginx -c /etc/nginx/nginx.conf
1 0 root S 1592 0% 1 0% {start.sh} /bin/sh /shell/start.sh
34 0 root R 1532 0% 4 0% top -n 1 -b
13 1 root S 1524 0% 2 0% tail -f /dev/null
对我来说,这是因为权限问题。我机器的密码被更改了,在 Docker 机器上,它没有更新。
要在终端中调试它,请编写以下代码:
logs <your container's name>
我运行一个容器在后台使用:
docker run --restart always --name lnmp -v /Users/gedongdong/web:/var/www/ -itd lnmp
码头文件:
FROM alpine:edge
LABEL author=gedongdong2010@163.com
RUN mkdir -p /run/nginx && mkdir -p /shell
RUN echo http://mirrors.aliyun.com/alpine/edge/main > /etc/apk/repositories && \
echo http://mirrors.aliyun.com/alpine/edge/community >> /etc/apk/repositories && \
apk update && apk add --no-cache nginx
COPY vhosts.conf /etc/nginx/conf.d/
COPY start.sh /shell
RUN chmod -R 777 /shell
EXPOSE 80 443 6379
CMD ["/shell/start.sh"]
start.sh:
nginx -c /etc/nginx/nginx.conf
tail -f /dev/null
vhosts.conf:
server {
listen 80;
server_name docker.test;
root /var/www;
index index.html;
}
当我使用 docker ps -a
:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a3910c0dc29 lnmp "/shell/start.sh" 16 minutes ago Restarting (1) 50 seconds ago lnmp
docker ps -a 为什么我的容器总是重启?
将 #!/bin/sh
添加到您的 start.sh 文件
#!/bin/sh
nginx -c /etc/nginx/nginx.conf
tail -f /dev/null
为什么容器总是重启:
正如 Henry 在他的评论中指出的那样,您的设置 --restart always
就是这么说的。通常,请记住当容器 stops/crashes 的 PID 1
时,容器退出。例如,您的容器显示如下内容:
(注意问题所在的 PID 1
行)
docker container exec -it lnmp top -n 1 -b
Mem: 2846060K used, 3256768K free, 62108K shrd, 83452K buff, 1102096K cached
CPU: 2% usr 2% sys 0% nic 95% idle 0% io 0% irq 0% sirq
Load average: 0.09 0.24 0.27 1/892 41
PID PPID USER STAT VSZ %VSZ CPU %CPU COMMAND
10 9 nginx S 15372 0% 0 0% nginx: worker process
12 9 nginx S 15372 0% 5 0% nginx: worker process
17 9 nginx S 15372 0% 1 0% nginx: worker process
11 9 nginx S 15372 0% 7 0% nginx: worker process
18 9 nginx S 15372 0% 5 0% nginx: worker process
15 9 nginx S 15372 0% 4 0% nginx: worker process
14 9 nginx S 15372 0% 1 0% nginx: worker process
16 9 nginx S 15372 0% 4 0% nginx: worker process
9 1 root S 14924 0% 6 0% nginx: master process nginx -c /etc/nginx/nginx.conf
1 0 root S 1592 0% 1 0% {start.sh} /bin/sh /shell/start.sh
34 0 root R 1532 0% 4 0% top -n 1 -b
13 1 root S 1524 0% 2 0% tail -f /dev/null
对我来说,这是因为权限问题。我机器的密码被更改了,在 Docker 机器上,它没有更新。
要在终端中调试它,请编写以下代码:
logs <your container's name>