In last week’s blog, I covered docker compose files and their significance. For today’s blog, I would like to go in depth on docker-files to explain how they are structured. To give a quick review of what Docker Compose is. Docker Compose is a tool that allows developers to create and run multi-container docker applications. Once again, I am referring to The Definitive Guide to Docker compose, posted by Gabriel Tanner. He does a good job of breaking down the structure of a docker-compose.yml file.
Docker Compose File Structure
By using a docker file, you can list the containers you want and all of the specifications as you would in a docker run command. As explained in last weeks blog, almost every docker-compose file should include the following:
- The version of the compose file
- The services which will be built
- All used volumes
- The networks which connect the different services
Docker files are structured upon indentation. Consider the idea of levels of abstraction.
On the top levels are the version, services, and volumes tag. The next level down is where the containers are listed, then parameter tags such as image, volumes, and ports. And finally on the lowest level, the respective rules for each parameter.
Here is a simple docker-compose.yml file:
Let’s take a look at each tag individually.
Version:
The version tag is used to specify the version of our compose file, based on the Docker engine we are using. For example, if we are using Docker engine release 19.03 or later:
Services:
The service tag acts as a parent tag, as all of the containers are listed underneath it.
Base Image/Build:
The image tag is where you would define the base image of each container. You can define a build using preexisting images available on DockerHub. In this case, we are defining a web1 container using the nginx:mainline image.
You can even define the base image by pointing to a custom Dockerfile:
Volumes:
The volumes tag allows you to designate a directory where the persisting data of a container is managed. Here is an example of a normal volume being specified.
Another way of defining a volume is through path mapping, linking a directory on the host machine to a container destination separated by a : operator.
Ports:
Specifying a port allows you to expose your host machine to the container that is running. In this case, we are defining port 10000 for the host port and we want to expose it to port 80.
Those are all of the main components that you need to structure a Docker Compose file. In this blog post, I simply covered the basic tags and structure. Compose files allow you to manage multiple containers and specify properties to a finer detail. There are tags that I haven’t covered such as dependencies, commands, and environment that Gabriel explains really well.
From the blog CS@Worcester – Null Pointer by vrotimmy and used with permission of the author. All other rights reserved by the author.