docker - 为什么docker容器总是重启?

标签 docker docker-container

我使用以下方式在后台运行容器:

docker run --restart always --name lnmp -v /Users/gedongdong/web:/var/www/ -itd lnmp

docker 文件:

FROM alpine:edge
LABEL <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c3a2b6b7abacb1fea4a6a7acada4a7acada4f1f3f2f383f2f5f0eda0acae" rel="noreferrer noopener nofollow">[email protected]</a>

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"]

开始.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 告诉了这一点。一般来说,请记住,当容器的 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 - 为什么docker容器总是重启?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52629450/

相关文章:

docker : where are files downloaded? 上的 RSelenium

docker - 将数据库和应用程序部署到同一容器中的单独容器中有什么好处/缺点?

docker - jHipster无法生成网关Docker镜像

http - Docker 路由/反向代理问题,无法 curl 其他容器

docker - 当多个容器在同一主机上运行时,docker 是否会重用图像?

node.js - Docker 容器内的 Nodemon

Docker Centos7 : Failed to mount tmpfs as/run: Operation not permitted

php - Docker 将 PHP 容器连接到 MySQL

docker - 无法使用 ansible 和 community.general.docker_image 拉取 docker 镜像

networking - Docker:将日志发送到本地主机不起作用,但 0.0.0.0 可以工作,为什么?