Docker compose and Docker logos on a thumbnail

Docker vs Docker Compose, what’s the difference?

This is a question asked by a student who is taking our course: Build a Backend REST API with Python & Django – Advanced.


A student asked: What is the difference between Docker and Docker Compose? Why do we need both a Dockerfile and docker-compose.yml file in the project?


When working with Docker, there are two concepts: Image and Container.

Images are like mini operating systems stored in a file which is built specifically with our application in mind. Think of it like a custom operating system which is sitting on your hard disk when your computer is switched off.

Containers are running instances of your image.

Imagine you had a shared hard disk (or even CD/DVD if you are old school) which had an operating system which can run on multiple machines. The files on the disk are the “image”, and those files running on a machine are a “container”.

This is how Docker works, you have files on the machine which are known as the image, and running instances of those files are referred to as the container. Images can also be uploaded and shared for other users to download and run on their machine too.

This brings us to Docker vs Docker Compose.

Docker is the underlying technology which manages (creates, stores or shares) images, and runs containers.

We provide the Dockerfile to tell Docker how to create our images. For example, we say: starts from the Python 3 base image, then install these requirements, then creates these folders, then switch to this user, etc… (I’m oversimplifying the actual steps, but this is just to explain the point).

Once we have done that, we can create an image using Docker, by running docker build .. If you run that, Docker will execute each step in our Dockerfile and store the result as an image on the system.

Once the image is build, you can run it manually using something like this:

docker run <IMAGE_ID>

However, if you need to setup volumes, you need to run them like this:

docker run -v /path/to/vol:/path/to/vol -p 8000:8000 <IMAGE_ID>

Often applications need multiple images to run. For example, you might have an application and a database, and you may also need to setup networks and shared volumes between them.

So you would need to write the above commands with the appropriate configurations and ID’s for each container you want to run, every time you want to start your service…

As you might expect, this could become complex, tedious and difficult to manage very quickly…

This is where Docker Compose comes in…

Docker Compose is a tool used to define how Docker runs our images in a yaml file which is can be stored with our code and reused.

So, if we need to run our app image as a container, share port 8000 and map a volume, we can do this:

      context: .
      - 8000:8000
      - app/:/app

Then, every time we need to start our app, we just run docker-compose up, and Docker Compose will handle all the complex docker commands behind the scenes.

So basically, the purpose of Docker Compose is to configure how our running service should work together to serve our application.

When we run docker-compose build, Docker Compose will run all the necessary docker build commands, to build all images needed for our project and tag them appropriately to keep track of them in the system.

In summary, Docker is the underlying technology used to create images and run them as containers, and Docker Compose is a tool that configures how Docker should run multiple containers to serve our application.

1 reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *