How to start docker container as server

Jeroen picture Jeroen · Jul 11, 2014 · Viewed 41.6k times · Source

I would like to run a docker container that hosts a simple web application, however I do not understand how to design/run the image as a server. For example:

docker run -d -p 80:80 ubuntu:14.04 /bin/bash

This will start and immediately shutdown the container. Instead we can start it interactively:

docker run -i -p 80:80 ubuntu:14.04 /bin/bash

This works, but now I have to keep open the interactive shell for every container that is running? I would rather just start it and have it running in the background. A hack would be using a command that never returns:

docker run -d -p 80:80 {image} tail -F /var/log/kern.log

But now I cannot connect to the shell anymore, to inspect what is going on if the application is acting up.

Is there a way to start the container in the background (as we would do for a vm), in a way that allows for attaching/detaching a shell from the host? Or am I completely missing the point?

Answer

Ben Whaley picture Ben Whaley · Jul 12, 2014

The final argument to docker run is the command to run within the container. When you run docker run -d -p 80:80 ubuntu:14.04 /bin/bash, you're running bash in the container and nothing more. You actually want to run your web application in a container and to keep that container alive, so you should do docker run -d -p 80:80 ubuntu:14.04 /path/to/yourapp.

But your application probably depends on some configuration in order to run. If it reads its configuration from environment variables, you can use the -e key=value arguments with docker run. If your application needs a configuration file to be in place, you should probably use a Dockerfile to set up the configuration first.

This article provides a nice complete example of running a node application in a container.