December 3, 2020
Docker is known for its use of OS-level virtualization and for the container system it employs to make creating, deploying, and running applications much easier for developers. These small execution environments have the merit of being very lightweight and to run in isolation from one another while still sharing the same operating system kernel. Docker puts several tools at the disposal of developers to help them create containerized applications, including Dockerfile, Docker image, Docker run, Docker Hub, Docker Engine, and more. It also provides solutions to streamline behaviors between containers and makes it possible to hitch them together to create application stacks. But what is Docker Compose? Essentially, it was designed to simplify multi-container application development and testing.
Docker Compose is a command-line tool that uses a specially formatted descriptor file to take multiple containers and assemble them into applications which can then be run on a single host. The application’s services are configured using YAML files. Maybe you wonder what is Docker Compose main benefit? Most certainly the fact that it allows users to run commands on several containers at the same time. This means that developers can write a YAML configuration file for their application service and subsequently use just one command to start it. Originally developed for Linux, this tool is now available to most operating systems, including Windows and macOS.
What is Docker Compose used for, concretely, and what makes it so popular?
One of the main benefits of Docker Compose is its huge portability. The command
docker-compose up alone is enough to bring up a whole development environment, which can then be torn down by utilizing
docker-compose down. This means developers can centralize their development environments and deploy applications effortlessly.
Testing, in the form of unit and E2E tests, is one of Docker Compose’s other main features. It allows for convenient and repeatable testing without mandating a change in the environment. It makes it possible to run an environment very similar to the production circumstances instead of having to test the application on a local or host operating system. Docker Compose can accommodate several isolated environments on one single host, allowing developers to rely on the same machine to run multiple copies of a given environment. Using Docker Compose also means they do not need to take the risk of having different services and projects interfere with one another.
What is Docker Compose’s most common use-case scenario?
Docker Compose could be considered a workflow improvement tool. As previously stated, it simplifies testing processes by making it possible to use any machine with Docker installed to run applications in an isolated environment. Yet, it now supports deployment and can be used to manage the rolling out of several containers on a host system.
With a single command, Docker Compose allows a developer to bring up a project together with all the services it requires to run smoothly. For instance, it could be used to install a headless CMS such as Strapi. To that end, both Docker and Docker Compose need to be installed on the Mac, Windows, or Linux machine you wish to use. There is a very helpful tutorial on how to install Strapi using Docker available from Strapi itself, which involves creating a ‘docker-compose.yaml’ file and pulling images before running the stack. Once these steps are done, you can start using your containers, keeping in mind that any computer deleted will result in the loss of the corresponding data. To prevent this, establishing data persistence is possible thanks to user-defined volumes.
It provides an isolated environment where projects can be experimented with. You can have as many containers as you like and swap them around to arrange them into a network. By default, a single network will be created for each container, ensuring complete isolation. With that said, more complex topologies can be configured wherever necessary using a top-level network key. If you wish to experiment before trying your hand at your own project, you will find source code to study on Github, whether you want to start from scratch or use Docker Compose for a pre-existing project.
In answer to the question: ‘What is Docker Compose?’, one could argue that it is one of the most conclusive alternatives to the complicated task that building, running and connecting dockerfiles can represent when dealing with multiple containers. Federating all their containers under a single command is a dream come true for many developers, especially when they want to run several applications inside said containers. Machine-optimized and human-readable at the same time, the YAML rules declared within Docker Compose’s configuration file provide a compelling solution to turn the countless code lines of a project into one digestible snapshot.
Do you want to use Docker and Docker Compose to deploy Strapi to your favorite cloud? Check out this tutorial on how to run a Strapi dev stack on with Docker Compose?
Victor Kane, a member of the Strapi community shows you how to run a Strapi dev stack with Docker compose
April 14, 2020
Integrate Strapi with Python to analyze the data of your headless cms. Use tools like pandas to work with your Strapi data.
December 1, 2020
Sign up for the Strapi newsletter to keep up with the latest news from the Strapi community!