2

I'm learning how to build Docker images from Dockerfiles. Here's my current Dockerfile:

FROM ubuntu:14.04
RUN apt-get update && apt-get install -y nginx
CMD ["/usr/sbin/nginx"]

I can build this image, run it, and connect to the container with docker run -t -i -p 80:80 mytestimage /bin/bash/. Then, I can run nginx and connect to the "hello world" page from a web browser.

But I can't get Nginx to run as a daemon (without manually starting it from the container's shell). I've tried docker run -d -p 80:80 mytestimage, but docker ps is empty. What am I doing wrong? I've been looking at the official Nginx Dockerfile, but I'm not sure which parts I need to add to my own Dockerfile.

Joe Mornin
  • 8,766
  • 18
  • 57
  • 82
  • check this topic http://stackoverflow.com/questions/18861300/how-to-run-nginx-within-docker-container-without-halting – Diego Velez Sep 18 '15 at 23:54

3 Answers3

9

you need to add following in Dockerfile

CMD ["nginx", "-g", "daemon off;"]

and the docker container can be run using command : docker run -d -p 8980:80 test-nginx

above port 8980 just an example, this is configured from Dockerfile

spectre007
  • 1,489
  • 11
  • 18
1

The problem is that nginx doesn't run in the foreground so every time your container executes the CMD directive, it returns and then the container exits.

You need to add the following to your /etc/nginx/nginx.conf at the top

daemon off;

Then you can just run your container like this:

docker run -ti -p 80:80 mytestimage
Rico
  • 58,485
  • 12
  • 111
  • 141
0

You must execute nginx on startup. Here is how you do it. add CMD ["nginx", "-g", "daemon off;"] inside dockerFile.

Full guide: How to setup PHP 8, NGINX and php-fpm docker

mick
  • 21
  • 3