Can somebody explain it with some examples? Why multi-container docker apps are built? while you can contain your app in a single docker container. When you make a multi-container app you have to do networking. Is not it easy to run a single image of a single container rather than two images of two containers?
-
Different parts of the app have different scaling needs, for one. – Sergio Tulentsev Feb 08 '21 at 10:49
-
can you please explain it with examples? – Shakir Aqeel Feb 08 '21 at 10:50
-
2When you have multiple independent products, that needs to run in some env, it makes little sense to have them all run in a single container. Single point of failure, they all pollute each others env, no isolation or easy way to monitor them. – Mansoor Feb 08 '21 at 10:53
-
3Maybe later, but here's some food for thought: if you put your database server (postgres, for example) in the same container as your webserver and, as your app grows, you find yourself in need of more webserver instances, what do you do? Do you spin up more containers from that image? What happens to the database if you do that? – Sergio Tulentsev Feb 08 '21 at 10:53
2 Answers
There are several good reasons for this:
It's easier to reuse prebuilt images. If you need MySQL, or Redis, or an Nginx reverse proxy, these all exist as standard images on Docker Hub, and you can just include them in a multi-container Docker Compose setup. If you tried to put them into a single image, you'd have to install and configure them yourself.
The Docker tooling is built for single-purpose containers. If you want the logs of a multi-process container, docker logs
will generally print out the supervisord
logs, which aren't what you want; if you want to restart one of those containers, the docker stop; docker rm; docker run
sequence will delete the whole thing. Instead with a multi-process container you need to use debugging tools like docker exec
to do anything, which is harder to manage.
You can upgrade one part without affecting the rest. Upgrading the code in a container usually involves building a new image, stopping and deleting the old container, and running a new container from the new image. The "deleting the old container" part is important, and routine; if you need to delete your database to upgrade your application, you risk losing data.
You can scale one part without affecting the rest. More applicable in a cluster environment like Docker Swarm or Kubernetes. If your application is overloaded (especially in production) you'd like to run multiple copies of it, but it's hard to run multiple copies of a standard relational database. That essentially requires you to run these separately, so you can run one proxy, five application servers, and one database.
Setting up a multi-container application shouldn't be especially difficult; the easiest way is to use Docker Compose, which will deal with things like creating a network for you.

- 130,717
- 29
- 175
- 215
-
I can't understand the second point. As we can save data in volumes, data will not be destroyed with the image. So, how the removal of one image could be dangerous? – Shakir Aqeel Feb 08 '21 at 11:57
-
1You can simulate this workflow on your local system. Make a change to your application code, save it in your editor, then reboot the whole system. You're right, you probably won't lose data, but there's no reason to restart the things you haven't changed. – David Maze Feb 08 '21 at 12:12
For the sake of simplification, I would say you can run only one application with a public entry point (like API) in a single container. Actually, this approach is recommended by Docker official documentation.
Microservices
Because of this single constraint, you cannot run microservices that require their own entry points in a single docker container. It could be more a discussion on the advantages of Monolith application vs Microservices.
Database
Even if you decide to run the Monolith application only, still you need to connect some database there. As you noticed, Docker has an additional network-configuration layer, so if you want to run Database and application locally, the easiest way is to use docker-compose to run both images (Database and your Application) inside one, automatically configured network.
# Application definition
application: <your app definition>
# Database definition
database:
image: mysql:5.7
In my example, you can just connect to your DB via https://database:<port>
URL from your main app (plus credentials eventually) and it will work.
Scalability
However, why we should split images for the database from the application? One word - scalability. For development purposes, you want to have your local DB, maybe with docker because it is handy. For production purposes, you will put the application image to run somewhere (Kubernetes, Docker-Swarm, Azure App Services, etc.). To handle multiple requests at the same time, you want to run multiple instances of your application. However what about the database? You cannot connect to the internal instance of DB hosted in the same container, because other instances of your app in other containers will have a completely different set of data (without synchronization).
Most often you are electing to use a separate Database server - no matter if running it on the container or fully manged databases (like Azure CosmosDB or Mongo Atlas), but with your own configuration, scaling, and synchronization dedicated for DB only. Your app just needs to worry about the proper URL to that. Most cloud providers are exposing such services out of the box, so you are not worrying about the configuration by yourself.
Easy to change
Last but not least argument is about changing the initial setup overtime. You might change the database provider, or upgrade the version of the image in the future (such things are required from time to time). When you separate images, you can modify one without touching others. It is decreasing the cost of maintenance significantly.
Also, you can add additional services very easy - different logging aggregator? No Problem, additional microservice running out-of-the-box? Easy.

- 1,836
- 18
- 26
-
okay, but why people contain their serverside app in two containers i.e one container for DB and another one for their core app? – Shakir Aqeel Feb 08 '21 at 11:02
-
2