xorLogic
xorLogic

Reputation: 417

What should I do if exposing ports in a Dockerfile do not take effect?

I have the following Dockerfile to run an Nginx server but I can't seem to get Docker to expose port 80 thru my host machine so I can access it externally:

FROM ubuntu:latest
EXPOSE 80
RUN apt-get update
RUN apt-get -y install apt-utils
RUN apt-get -y dist-upgrade
RUN apt-get -y install nginx 
CMD service nginx start

If I run the following command after building the image, docker run -p 80:80 -d nginxserver, I can get the correct global settings to take effect, however my newly created Docker container does not run persistently and it exits after a brief second.

If I try docker run -it /bin/bash -d nignxserver, this will allow my Docker container to work, however I won't be able to connect to the Nginx server outside the host machine.

If I try docker run -p 80:80 -it /bin/bash -d nignxserver, this will fail with the following error message:

docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"-it\": executable file not found in $PATH": unknown.

What would be the correct solution here?

Upvotes: 0

Views: 342

Answers (2)

Rawkode
Rawkode

Reputation: 22592

You need to run with --privileged to listen on ports under 1024.

Also, service nginx start exist immediately (This is covered by David Maze)

You should instead use CMD nginx

Upvotes: 1

David Maze
David Maze

Reputation: 159781

The best solution is just to use the standard nginx image, if you're not really going to customize the image at all.

If you're writing a custom image, you should broadly assume commands like service just don't work. The CMD of the image you show (assuming it's successful) attempts to launch nginx as a background service; once it's started in the background, the container's main process has finished and the container exits. The CMD generally needs to launch the single process that the container runs in the foreground.

In terms of your various docker run gyrations, the options always come in the same order:

docker run \
  -d -p 80:80 \           # docker-specific options
  nginxserver \           # the image name
  nginx -g 'daemon off;'  # the command to run and its options

If you specify an alternate command (like /bin/bash) that runs instead of the main container process, and if the container normally would have run a network server, you get the shell instead. /bin/bash is a command and not an argument to -it; the same breakdown would be

docker run \
  --rm -i -t \            # docker-specific options
  nginxserver \           # the image name
  /bin/bash               # the command to run and its options 

Upvotes: 1

Related Questions