Following my recent beginners guide to Docker post I thought it would be useful to go into a bit more detail on what docker compose files are and why I use them.
Normally starting a container involves invoking the docker run command followed by all the commands required to start the container. This is fine for one off containers however if you create a container and define a few environment variables you need to make a note of all these variables if you ever want to take the container down and re-create it.
To make it easier to store all the commands required to start containers it’s best to use docker compose files. These files allow us to write all requirements once which can then be backed up and moved to different docker hosts to bring up the container with the exact same environment variables without having to remember them or inspect the container to get all the details.
In order to create a docker compose file you need to create a YAML file called docker-compose.yml. It’s best to store this file in a directory with the name of the stack/container you wish to create.
Launching a docker-compose file
After customizing your file you can run your container/stack in the background
docker-compose up -d
If you want to run your stack in the foreground so you can see the logs as it is running simply run
docker-compose up
If you want to stop your stack and remove the containers run
docker-compose down
Example docker-compose.yml
Below is an example compose file that will create an apache server instance with a volume called htdocs mounted into the container.
version: '3.3' services: httpd: restart: always container_name: myApacheServer image: httpd environment: - PUID=1000 - PGID=1000 volumes: - htdocs:/usr/local/apache2/htdocs/ volumes: htdocs:
- restart – This defines the restart policy for the container
- container_name – We are manually defining this containers name. If we did not then it would inherit the name of the folder that this docker-compose.yml was created in
- image – The name of the image from Docker Hub you wish to use for this container
- environment – Define your environment variables
- volumes – The first reference to volumes is defining that we want the volume of htdocs to be mounted into the containers /usr/local/apache2/htdocs. The reference to htdocs under the volumes section tells docker to either mount the existing volume called htdocs or create a new volume in this name if it does not exist.
When creating these files use spaces not tabs. YAML does not like tabs and you will receive an error such as
ERROR: yaml.scanner.ScannerError: while scanning for the next token found character '\t' that cannot start any token in "./docker-compose.yml", line 8, column 1
Multiple Containers
So we have one container running but this is where compose files come in to their own. You can add multiple containers into this one file, where this becomes important is that they will all share the same Docker network meaning they are able to communicate with each other.
A simple example of this would be extending our file above to include a SQL server along with our Apache server.
version: '3.3' services: httpd: restart: always container_name: myApacheServer image: httpd environment: - PUID=1000 - PGID=1000 volumes: - htdocs:/usr/local/apache2/htdocs/ mariadb: restart: unless-stopped container_name: mariadb image: mariadb:latest environment: - MYSQL_ROOT_PASSWORD=myPassword volumes: - mariadb-db-data:/var/lib/mysql volumes: htdocs: mariadb-db-data:
References
A helpful website to generate docker-compose files from the docker run commands is https://www.composerize.com/.
Leave a Reply