dongdong
dongdong

Reputation: 25

Why docker container always restarting?

I run a container in the background using:

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

dockerfile:

FROM alpine:edge
LABEL [email protected]

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;
}

when i use 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 Why is my container always restarting?

Upvotes: 2

Views: 6009

Answers (2)

james25
james25

Reputation: 351

For me, it happened because of the permission issue. My machine's password was changed and in the Docker machine, it was not updated.

To debug it in terminal write the code below:

logs <your container's name>

Upvotes: 0

tgogos
tgogos

Reputation: 25250

Add #!/bin/sh to your start.sh file

#!/bin/sh
nginx -c /etc/nginx/nginx.conf
tail -f /dev/null

Why the container was always restarting:

As Henry has pointed out in his comments, your setting --restart always told so. In general, keep in mind that when the PID 1 of the container stops/crashes, then the container exits. For example your container shows something like this:

(notice the PID 1 line, where the problem was)

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

Upvotes: 2

Related Questions