Dockerfile vs Docker-Compose

Gupta Nishtha
4 min readJul 1, 2021
Dockerfile and Docker-Compose File

Did you recently start to learn about docker and wonder why some articles use docker to build images while some create docker-compose.yml? What makes them different if both have images specified in them? Are these files the same or just differ in their writing styles?
Don’t worry! This article will provide clarity on these questions you have been pondering upon.

Before we discuss the above questions, let us recall quickly.

Docker is a platform that provides a control interface between the host operating system and containerized applications. It enables developers or DevOps teams to build, deploy, and manage necessary code and dependencies that are necessary for them to function in any computing environment by using containers. As a result, containerized applications run reliably when moved from one computing environment to another. It is container-based technology and takes lower resources of the host system.

Now, what would be the next step to make the container enter into execution mode? Yes, you are right, run the Docker Images.

Docker Images are similar to a virtual machine template. It is packed executable files that include all the stuffs like operating systems, software packages, drivers, configuration files, and helper scripts wrapped in one bundle to run the containers.
In a nutshell, consider images as apps and containers as processors that run those apps. Running apps won’t alter the scripts or files included inside the app likewise, the container doesn’t alter images.

Now, can you guess the platform which facilitates the build of such Docker Images? It’s nothing but Dockerfile.

Dockerfile

Dockerfile is a text file that comprises instructions on how to build images. The first line in the file specifies the base image with FROM, i.e. FROM java. Subsequent RUN lines in the Dockerfile provide the additional steps that the docker build will execute in a shell, within the context of the FROM image, to create the new image.
The build command is run on the command line to build a new image using the following syntax:

docker build <options> <directory path or URL>

The directory path will be the address of the directory where we want to set up our container.

Dockerfile

Docker-Compose
A Compose file or a YAML file is a text file that defines services that make up your app and run together in an isolated environment. In addition to services, it defines networking, and volume details. The default name of the YAML file is docker-compose.yml.
The docker-compose.yml file defines a group of images to download and run together as a part of a combined service. For example, the docker-compose.yml for a java based application could consist of a web server image, an application image, and a database image and would define the way how these images communicate with each other.

Now, you might wonder how exactly these two commands differ.

docker build .docker compose build [SERVICE...]

So, the Docker-Compose build will read your docker-compose.yml, look for all services containing the build: statement and run a Docker build for each one. Each build: can specify a Dockerfile, a context, and args to pass to docker.

docker-compose.yml

Conclusion
Dockerfile
is a text template similar to Compose file that comprises instructions on how to build images. This text file is converted into an image and then into a container, which is nothing but a running instance of an image. Compose files connect all these created containers and run them together as a complete application instead of running each container independently.

Thus, Docker’s workflow is to build a suitable Dockerfile for each image you wish to create, then use compose to assemble the images using the build command.

Let me know in the comment section if you have any questions or suggestions.

If you like this article and learned something from this don’t forget to hit the clap button and share it with your friends.

Happy learning!!

--

--