Dockerfile vs Docker Compose

Photo of author

By admin

So you’re about to make your first move for deploying Containers? Now, you may be in a dilemma of choosing either the command line that allows you to deploy Containers one by one through long command options or use certain tools that enable you to set a predefined pattern for configuration files and then deploy containers with the help of short and quick commands.

Well, your decision here will determine the complexities associated with the application/service you are planning to deploy. However, everything boils down to whether you’re choosing dockerfile or docker-compose.yml. Docker Compose would be a common tool for either of the two option. However, you can also opt for choosing both dockerfile or docker-compose.yml, and still, Docker Compose would be required. You can use docker-compose.yml so that it will call upon a Dockerfile to let you create even more complex container rollouts. But before learning that, let’s clear some basics.

How is Docker Containers Created?

For creating Docker containers, you first need to build Docker images that act as a set of instructions. These images are built using dockerfiles that are text-based scripts. All the text files inside the dockerfile are composed of all the commands used for running Containers through Docker image.

Docker image allows individuals to have various users function parallelly under one environment. For sharing Docker images defined with instructions, users can simply upload it on Docker Hub where it can be accessed via the official public Docker Hub website. Any individual can pull these Docker images to build Containers that are consistent with the original Docker Containers.

What is Docker Compose?

Docker Compose is a tool that defines and runs multi-container Docker applications. It makes use of YAML files for configuring the services for the applications. Right after this, a single command is executed for running the services of containers. Docker Compose efficiently runs in every environment such as production, staging, development, testing, and even in CI workflow.

For better understanding, let’s take the example of eCommerce websites. First, users visit the website through internet browsers and perform actions such as logging in, browsing catalogs, adding products to cart, checking out, etc. To successfully execute these activities, various microservices run behind the scenes. These microservices function by running on services in their respective containers. However, developers have to ensure proper collaboration of these containers and this is where Docker Compose plays its role.

It helps in running various containers as a single service and eventually builds proper collaborations among them. Indeed, these containers will work in isolation but will be enabled to collaborate whenever required. Docker Compose files are written easily with the help of a scripting language called YAML, which is a Markup Language. One of the most important benefits of Docker Compose is that it enables users to activate services through a single command. Following are the three simple steps for operating Docker Compose:

  1. Defining the application’s environment through Dockerfile for it to be reproduced in any infrastructure and environment.
  2. Defining services offered by the application in docker-compose.yml to make them function in isolated environments.
  3. Execute docker-compose up command for making your containers get deployed and start functioning. Else, you can run docker-compose up using the docker-compose binary.

Features of Docker Compose

1. Preserving data volume

Docker Compose is efficient in preserving the volume for the services. Right after functioning, it checks the containers from previous runs and copies the volume from old containers to the new ones. Therefore, the data created into those volumes also get conserved.

2. Recreating containers

Docker Compose is used for caching configurations used for creating new containers with old features. On re-starting unmodified services, the existing containers get used for lesser load and quick start. In addition, users would be able to make modifications in their environment quickly.

3. Development environments

For developing software, an individual needs to run applications that are in isolation from each other for better functioning. The Docker command line can be used for achieving the same along with enabling containers to interact with each other at times.

In addition, by making use of the Compose command-line tool an individual can create one or more than one container through a single command, that is docker-compose up.

4. Automated testing environments

Docker Compose enables the Continuous Deployment or Continuous Integration process that eventually results in an automated test suite. Compose offers a testing environment and provides a simpler way to create and destroy isolated testing environments quickly.

What is Dockerfile?

Docker images are built automatically by following the description and details mentioned in the Dockerfile. It is a document containing text commands that users execute on the command line and then create images. Further, an individual can also run the “docker build” command for automatically building images with required commands. The images then created would be assigned with proper context. Context is a set of files specified at certain locations such as PATH or URL. The PATH is the directory of the filesystem stored on your computer locally. Whereas, the URL is the location at Git repositories.

For using files in the build context, the Dockerfile fetches the file which is defined properly with any instruction, such as the COPY instruction. For increasing the docker build’s performance, excluding files and directories by adding a .dockerignore file within the context directory is suggested. Typically, Dockerfile is situated at the root of the context. However, by using the -f flag with the docker build command, an individual would be able to locate a Dockerfile anywhere in the file system.

$ docker build -f /path/to/a/Dockerfile

Main Instructions of the Dockerfile

1. FROM

This instruction is used for analyzing new build stages along with setting base images for succeeding instructions. Every ideal dockerfile is created with the FROM instruction at the start. This instruction can be used with any docker image and it would be simple for users to get started if they pull images from Public Repositories.

FROM [–platform=<platform>] <image> [AS <name>]

Or

FROM [–platform=<platform>] <image>[:<tag>] [AS <name>]

Or

FROM [–platform=<platform>] <image>[@<digest>] [AS <name>]

2. RUN

The RUN command will carry out commands present in the new layers of current docker images for processing the results. The resulting image would further be used in the next steps of the Dockerfile.

The addition of RUN instruction will generate images that will obey all the basic concepts of Docker. In addition, the containers will be enabled for being created from any point in the image’s history. Its function is somewhat similar to source control that stores all the previous versions that can be rolled out at any point in time.

RUN has 2 forms:

  • RUN <command> (shell form, the command is run in a shell, which by default is /bin/sh -c on Linux or cmd /S /C on Windows)
  • RUN [“executable”, “param1”, “param2”] (exec form)

3. CMD

CMD instruction is created for providing default configurations to existing Containers. The default configurations can be executed and omitted and users have to specify ENTRYPOINT instructions along with it. A dockerfile can only have one CMD. But if there’s more than one CMD, then only the last one will function.

If CMD is utilized for providing default configurations to the ENTRYPOINT, then instructions for both CMD and ENTRYPOINT would be defined by JSON array format. CMD command has the following three patterns:

  • CMD [“executable”,”param1″,”param2″] (exec form, this is the preferred form)
  • CMD [“param1″,”param2”] (as default parameters to ENTRYPOINT)
  • CMD command param1 param2 (shell form)

4. LABEL

This instruction is used for adding metadata to Docker images. LABEL is a key-value pair and for including spaces while writing this command, an individual has to insert quotes and backslashes. Following is an example of the format for executing this instruction:

LABEL <key>=<value> <key>=<value> <key>=<value> …

Docker images can have multiple LABELs that are specified through single lines. In older versions of Docker, this instruction also reduces the size of the image, but this feature is removed in the recent versions.

Docker Compose Vs Dockerfile: What’s the Difference?

The difference between these two Docker terminologies is extremely easy to figure out. A Dockerfile is simply a text file that is composed of certain commands for enabling users to create the Docker image, whereas Docker Compose is a tool that defines and runs applications based on multiple containers.

Docker Compose is used for defining services that are used for making applications in docker-compose.yml, which helps them to function collaboratively in isolated environments. Therefore, an application would be able to function seamlessly by the execution of just one command, i.e. docker-compose up. Docker Compose further makes use of Dockerfile if the build command is added into the docker-compose.yml file. For every Docker image created, there has to be one suitable Dockerfile associated with the same. Then the compose assembles all the images for building Containers. Following is an example of a Dockerfile.

FROM centos:latest

LABEL maintainer=”collabnix”

RUN yum update -y && \

yum install -y httpd net-tools && \

mkdir -p /run/httpd

EXPOSE 80

ENTRYPOINT apachectl “-DFOREGROUND”

Conclusion

Using Docker for building Containers to run applications takes an individual through various steps. One such step includes the usage of Docker Compose and Dockerfile. These are used for different purposes while Deploying Containers but people often get confused with their usage. For understanding the key difference between the two, you should be well-versed with the complete process of how Containers are created and that is exactly why this blog starts with a brief overview of the Container creation process.

Nonetheless, you simply need to understand that Docker Compose is a tool that helps in defining and running applications with multiple Docker containers. It makes use of YAML files for configuring the services for the applications. Docker Compose efficiently runs in every environment such as production, staging, development, testing, and even in CI workflow. On the other hand, Dockerfile is a document that is composed of several text commands that are utilized by users for creating images.

Leave a Comment