Docker Community Forums

Share and learn in the Docker community.

Multiple applications in a single container!

docker

(Manildocker) #1

Hi,

Can i run multiple applications(ex: Python Web App, MySQL etc) in a single Docker container?

Thanks.


(David Maze) #2

You can, but you have to build all the mechanism to keep all of the processes running yourself. Supervisord seems to be a popular solution.

I think it’s generally considered a best practice to run only one “thing” in a container. There are a couple of reasons for this. One is just that it’s simpler (two docker run commands or a docker-compose.yml file, rather than trying to manage supervisord). Another is that you’ll probably need to routinely update your application, and if the application and the database are in separate containers you don’t need to restart one part just because the other’s changed.

If you’re looking at a multi-host setup (like Docker Swarm or Kubernetes) then keeping the database and application separate becomes more important. The database needs some special care; you can’t replicate it and it’s tricky to move it across hosts. If the application is stateless (up to its database connection) you can run multiple copies of it, and you don’t especially care where they run. Those are different enough that you really want them to be separate things so that you can manage them separately.


(Manildocker) #3

Thanks David for your reply.


(Martin Terp) #4

Agree with David, one container pr service is :metal: and should always be the way you build your containers.

BUT

There might be reasons for this not to work, and as David said, supervisor is (for me) the answer.
I just put an example on another post:

( you might need to open that, the format is not great here i can see :joy: )


(Ronbarak) #5

There was an interesting discission on StackOverflow on that topic: https://stackoverflow.com/questions/27664820/running-multiple-applications-in-one-docker-container


(Meemicaudi) #6

I also agree with David. One running application/service per container. David lists some great reasons as to why do set it up that way. The only remaining question is probably how. How to have the database and application needing to connect to the database in separate containers. The answer is through networking.

You should create a new Docker network, and when staring up both containers, assign them to this network. That way, for either to talk to the other, it can be done via there Docker container name instead of IP, which could change.

docker network create --driver bridge

Then, when you run the container, you give it the network to run in.

docker run --network= --name=<container_name> :

Now, in each container started in this network, you can reference the other containers via the container name, and Docker will handle the communication between the containers.

Hope this helps.