Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dockerized nginx is not starting

Tags:

docker

nginx

I have tried following some tutorials and documentation on dockerizing my web server, but I am having trouble getting the service to run via the docker run command.

This is my Dockerfile:

FROM ubuntu:trusty

#Update and install stuff
RUN apt-get update
RUN apt-get install -y python-software-properties aptitude screen htop nano nmap nginx

#Add files
ADD src/main/resources/ /usr/share/nginx/html

EXPOSE 80
CMD service nginx start

I create my image:

docker build -t myImage .

And when I run it:

docker run -p 81:80 myImage

it seems to just stop:

docker ps -a

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
90e54a254efa        pms-gui:latest      /bin/sh -c service n   3 seconds ago       Exit 0                                  prickly_bohr

I would expect this to be running with port 81->80 but it is not. Running

docker start 90e

does not seem to do anything.

I also tried entering it directly

docker run -t -i -p 81:80 myImage /bin/bash

and from here I can start the service

service nginx start

and from another tab I can see it is working as intended (also in my browser):

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                NAMES
408237a5e10b        myImage:latest      /bin/bash           12 seconds ago      Up 11 seconds       0.0.0.0:81->80/tcp   mad_turing 

So I assume it is something I am doing wrong with my Dockerfile? Could anyone help me out with this, I am quite new to Docker. Thank you!

SOLUTION: Based on the answer from Ivant I found another way to start nginx in the foreground. My Dockerfile CMD now looks like:

CMD /usr/sbin/nginx -g "daemon off;"
like image 261
Solvemon Avatar asked Jun 16 '14 10:06

Solvemon


People also ask

How do I run nginx in foreground?

In a development environment, using "master_process off", nginx can run in the foreground without the master process and can be terminated simply with ^C (SIGINT). This is somewhat similar to running Apache with an 'X' command-line option. However you should NEVER run nginx in production with "master_process off".


4 Answers

As of now, the official nginx image uses this to run nginx (see the Dockerfile):

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

In my case, this was enough to get it to start properly. There are tutorials online suggesting more awkward ways of accomplishing this but the above seems quite clean.

like image 190
Ben Creasy Avatar answered Oct 23 '22 07:10

Ben Creasy


Docker container runs as long as the command you specify with CMD, ENTRTYPOINT or through the command line is running. In your case the service command finishes right away and the whole container is shut down.

One way to fix this is to start nginx directly from the command line (make sure you don't run it as a daemon).

Another option is to create a small script which starts the service and then sleeps forever. Something like:

#!/bin/bash
service nginx start
while true; do sleep 1d; done

and run this instead of directly running the service command.

A third option would be to use something like runit or similar program, instead of the normal service.

like image 28
ivant Avatar answered Oct 23 '22 06:10

ivant


Using docker-compose:

To follow the recommended solution, add to docker-compose.yml:

command: nginx -g "daemon off"

I also found I could simply add to nginx.conf:

daemon off;

...and continue to use in docker-compose.yml:

command: service nginx start

...although it would make the config file less portable outside docker.

like image 7
macsteps Avatar answered Oct 23 '22 06:10

macsteps


Docker as a very nice index of offical and user images. When you want to do something, chances are someone already did it ;)

Just search for 'nginx' on index.docker.io, you will see, there is an official nginx image: https://registry.hub.docker.com/_/nginx/

There you have a full guide to help you start your webserver.

Feel free to take a look at other users nginx image to see variants :)

The idea is to start nginx in foreground mode.

like image 5
creack Avatar answered Oct 23 '22 05:10

creack