Running Nginx on Docker

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.

  • Mesos/Marathon on EC2 agents hostnames not accessible
  • How do I provide credentials to the docker awslogs driver using Docker for Mac?
  • Docker “Can't connect to local MySQL server through socket”
  • Sharing docker registry images among gcloud projects
  • docker-registry disk full and no ideas to diet it
  • Xdebug time out while running debug session (PhpFarm | phpFcgi)
  • 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.

  • How to change location of “.docker” directory
  • Docker container with ASP.NET Core libraries
  • Auto setup a docker Image with ci runner
  • How do I fix hostname resolution on DigitalOcean's Ubuntu Docker 16.04 server?
  • Mesos+ZooKeeper don't work fine
  • docker.local hostname in /etc/hosts not resolving on OSX for docker machine (causing rspec tests to fail)
  • 2 Solutions collect form web for “Running Nginx on Docker”

    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

    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
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.