Docker Compose is a tool, which can be used run multiple Docker containers to run the applications. Docker compose can be stated with a YAML file called “docker-compose.yaml” and, with a single command, you have the luxury to manage all these multiple containers. Also, you can define the docker networks for your containers in the same file. Docker Compose is a great fit for an isolated environment with multiple containerized applications, which are running on a single host.
The good side of Docker Compose is it preserves the data, it has the ability to run a new container destroying the previous running one. In this process, it copies the data utilized by the previous container to the new one. Also, it recreates only the changed container, which allows us to maintain the great control of the running unchanged containers. The application with variables, can be defined with the environmental variables, which defined in the Docker compose file.
A Sample Docker Compose File
You can find a sample Docker Compose file here, with two containers connecting to the frontend and back end networks.
The famous method of defining the parameters as list in the yml file. Host and node ports also need to be defined under the service (ex: web). All other parameters such as environmental variables, volumes etc should also be defined as lists.
If you wish to use a $ sign in your environment variables in the docker-compose.yaml file, use “double dollar signs” ($$), I hope this will be really useful for some complex environmental variables.
In my example, two environmental variables defined under “web” service. A mounted EFS volume to an EC2 instance to the “/root/efs” mount point is connected as the “/data/” volume to the container.
I have defined two different isolated networks as “frontend” and “backend” in my file, “web” service is connected to the fronted and “mysql” service is connected to the backend, while “app” service is connected to the both networks. “app” service is the only service which can be talked to both the network.
Few Useful Docker Compose Commands
There are many docker commands which can be used with Docker Compose and, I thought to bring up few useful commands when running Docker Compose containers.
To start all the containers defined in the docker-compose.yaml file, use below command, this will start all the containers in the interactive mode.
If you need to start only a single container defined in the docker-compose file in the interactive mode, use the service name at the end (ex:web)
docker-compose up SERVICE_NAME
If you need to start all the containers in “detached” mode use -d flag
docker-compose up -d
For a single container specify the name at the end
docker-compose up -d SERVICE_NAME
Assume that, you have done a change to a single service and, you need to recreate the container alone while others running use –no-deps flag
docker-compose up -d --no-deps SERVICE_NAME
To run an one-off command inside your container use below command, be low example is to view the environmental variables used in the web service container
docker-compose run SERVICE_NAME env
To stop docker containers use below command, specifying a single service would stop only the specified container
#To Stop All The Containers
#To Stop a Single Container
docker-compose stop web
These are only the very useful commands for your docker-compose environments.
To see how to install Docker Compose follow this well defined Docker documentation.