Docker for Beginners – Getting Started with 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 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:

  1. The ability to run multiple docker commands together in one go
  2. The ability to automate the docker run commands execution
  3. 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.

For example,

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#
version: '3'

#containers which are being built and run#
services: 
    #container name to be used while creating#
    redis-server:
        #the path of dockerfile to be used#
        build: .
        #the ports on which the container be mapped onto#
        ports:
            - 8000:6379

where the dockerfile which is to be used is as follows:


#base image to be picked#
FROM alpine

#instructions to run on the image#
RUN apk add --update redis

#startup command when the container runs#
CMD ["redis-server"]

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.

wp-content/uploads/2022/05/docker-compose-up.png

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

wp-content/uploads/2022/05/docker-compose-exec.png

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.


Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *