I’m having a Dockerfile
FROM centos:7
ENV container docker
RUN yum -y update && yum -y install net-tools && yum -y install initscripts && yum -y install epel-release && yum -y install nginx && yum clean all
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
# expose Nginx ports http/https
EXPOSE 80 443
RUN curl https://www.sheldonbrown.com/web_sample1.html > /usr/share/nginx/index.html
RUN mv /usr/share/nginx/index.html /usr/share/nginx/html/index.html -f
RUN mkdir -p /local/nginx
RUN touch /local/nginx/start.sh
RUN chmod 755 /local/nginx/start.sh
RUN echo "#!/bin/bash" >> /local/nginx/start.sh
RUN echo "nginx" >> /local/nginx/start.sh
RUN echo "tail -f /var/log/nginx/access.log" >> /local/nginx/start.sh
ENTRYPOINT ["/bin/bash", "-c", "/local/nginx/start.sh"]
I’m building it with docker build -t "my_nginx" .
And then running it with docker run -i -t --rm -p 8888:80 --name nginx "my_nginx"
https://localhost:8888/ shows the page but no logging is shown.
- If I press Ctrl-C, nginx is stopped but the tail on the logging is shown.
- If I press Ctrl-C again, the container is gone.
Question: How can I let nginx run AND show the tail on the logging (which is preferably also visible using the "docker logs"-command)
2
Answers
You should remove the
tail
command from the Dockerfile, run the container withdocker run -it -d --rm -p 8888:80 --name nginx "my_nginx"
and then usedocker logs -f nginx
.The easiest way to accomplish this is just to use the Docker Hub
nginx
image, which deals with this for you. A Dockerfile that could be as little asIf you look at its Dockerfile it actually uses symbolic links to cause Nginx’s "normal" logs to go to the container stdout
That gets around most of the mechanics in your Dockerfile: you can just run
nginx
as the main container command without trying to use a second process to cat logs. You can basically trim out the entire last half, and getYet another possibility here (with any of these images) is to mount your own volume over the container’s
/var/log/nginx
directory. That gives you your own host-visible directory of logs that you can inspect at your convenience.(In the shell script you construct in the Dockerfile, you use the Nginx
daemon off
directive to run as a foreground process, which means thenginx
command will never exit on its own. That in turn means the script never advances to thetail
line, which is why you don’t get logs out.)