Knoldus Blog Audio
If you have a need to define a service that requires more than one docker container to function, Docker Compose is the right tool for you. In this blog, you’ll see how easily you can run a multi-container docker application using Docker Compose.
Docker Compose – Introduction
Docker Compose is basically a docker tool to define and run multi-container Docker applications. Each of these containers run in isolation but can interact with each other when required. With Compose, you use a docker-compose.yaml file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. It’s an excellent tool for development, testing, CI workflows, and staging environments.
Some of the benefits provided by Docker Compose are:
- Multiple isolated environments on a single host
- Quick and easy configuration due to YAML scripts
- Scale up selected services when required
- Orchestrate multiple containers that work together
Docker Compose relies on Docker Engine. So before installing it make sure you have Docker Engine installed on your system.
On desktop systems like Docker Desktop for Mac and Windows, Docker Compose is included as part of those desktop installs. You don’t need to install it manually.
On Linux systems, you’ll need to
- Install Docker Engine
- Run the following command to download the current stable release of Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
3. Apply executable permissions to the binary:
sudo chmod +x /usr/local/bin/docker-compose
Now test the installation using the following command
Creating a docker-compose.yml file to Run a Multi-container Docker Application
Let’s say you have an application that requires an NGINX web server and Redis database, you can create a docker-compose.yml file that can run both the containers as a service without the need to start each of them separately.
Now, let’s look at the docker-compose.yml file for this purpose:
version: '3' services: web: image: nginx ports: - 8080:80 database: image: redis
Note: The structure of this YAML file must be consistent, so indent carefully.
Let’s understand the meaning of each of the keywords used here:
version ‘3’: This denotes that we are using version 3 of the Compose file.
services: This section defines all the different containers we will create. In the above example, we have two services, web and database.
weband database: These are the names of our services. Containers will be created with the names we provide.
ports: This is used to map the container’s ports to the host machine.
image: To run a service using a pre-built image, we specify the image location using the
There are many more advanced keywords that you can use in production, but for now, let’s just get started with the necessary clauses.
Building our Application
Now we have our docker-compose.yml file constructed, so it’s time to build our application. As this is a very simple application, it will basically deploy two containers — the web server and the database.
So now, to build the stack, go back to your terminal window and run the following command:
The above command will deploy both the web and database containers in attached mode, so you won’t get your bash prompt returned. If you want to run them in detached mode, use the below command:
docker-compose up -d
To make sure the containers are running, use the command:
Now to see your application up and running, go to your browser and type the following url http://localhost:8080
To stop all the running containers, use the following command:
Congrats! you have just deployed your first container stack using Docker Compose. This was a very simple example but it will definitely help you get started with your workflow.
Hope you find this blog useful. For more such blogs, visit https://blog.knoldus.com
References: Docker Compose Documentation