In a previous article, we have looked at how a dockerfile can help us build a customized image as per our requirements on top an available base image and use the created image in rapid replication and deployment of application clones running under separate containers. While running a container out of a dockerfile, we have seen how our command sophisticates itself specifying how the container must be generated out of the image and how it must be executed. As the complexities grow in the container instructions, the docker run command grows out in size making it hard to understand or compose. To solve this, there’s another way of running containers out of a dockerfile, with all the instructions laid out in a readable manner and making our run operation quick and simple. Enter docker-compose.
What is docker-compose?
a docker-compose is a command which comes out of the box along with the docker installation, that helps in keeping instructions to run a container out of an image simple without missing out on any instruction which needs to be passed on while creating a container. In other words, think of docker-compose as a script file which contains all the necessary instructions that are needed for a container while being created, and all we need to do it just elaborate on all the needed instructions in that script file and just run it with a single command.
In short, docker-compose provides us with:
- The ability to run multiple docker commands together in one go
- The ability to automate the docker run commands execution
- The simplicity of writing complex command over a simple script, and execute with a built-in feature
The Anatomy of a Docker-Compose file:
A docker-compose file is a yaml file, with all the instructions given for a docker run command laid out in a formatted and segmented manner. There are segments, which specify what kind of operation it is, and all the sub commands under that segment are indented with no braces or paranthesis; but tabs and spaces.
Each option to be specified for the run command is present, and is passed the values in the form of key:value pairs, with one key:value pair per line.
To create and run a container for running a redis server out of a customized image we created using alpine linux base image, via docker-compose. We compose the yaml file as below:
-- docker-compose.yaml --
#version of docker-compose#
#containers which are being built and run#
#container name to be used while creating#
#the path of dockerfile to be used#
#the ports on which the container be mapped onto#
where the dockerfile which is to be used is as follows:
#base image to be picked#
#instructions to run on the image#
RUN apk add --update redis
#startup command when the container runs#
Rule: In this case, the dockerfile and the docker-compose.yaml files must reside on the same directory since we have mentioned the build path as "." (current dir)
To run this setup, we just give the below command:
> docker-compose up
When docker encounters this command, it first looks up for a docker-compose.yaml file in the directory of execution and then steps up a docker build and docker run out of the dockerfile specified. Then we can see that the services specified in the docker-compose are built and are running.
To check for the processes running out of a docker-compose, we use the command:
> docker-compose ps
This shows up all the processes and their states, which are built via docker-compose. And to stop these services together, we use:
> docker-compose down
In our example, to connect to the redis-server container created from the same container, we can run in another command prompt the docker exec onto the docker container as:
> docker exec -it redis-server_redis-server_1 sh
This way, we can use docker-compose to simplify the process of creating and running containers out of images defined from a dockerfile. Using this approach, we can actually build multiple containers and multi levels of containers, which we shall look in the next articles.