In the previous Docker module, we saw an example of a container running both php-fpm and nginx. Docker Compose allows us to easily run multiple containers alongside each other, and well mirror our production environments. It aims to simplify this process by using a docker-compose.yaml file to define the services, networks, and volues. After this module, you should be comfortable with basic tasks required to interact and manage a local container environment.
For this module, create a new Git repo for your Docker Compose work in Module 3.
If you haven't already, it is recommended that you read through each of the official documentation resources listed below and follow some of the links within each resource to help clarify specific tools and their purposes.
Resources and articles:
- What is a Container?
- Official Docker Engine Docs
- Official docker-compose.yaml Reference
- Official Docker Compose Docs - (common use cases)
-
Create a file called docker-compose.yaml
-
In your docker-compose.yaml file, add a new service called
hello-containerwith the imagehello-world -
Create a new file called docker-compose-module.txt, and paste the output of
docker-compose up.If you look from the top of the output, you should be able to see the process that docker-compose goes through to start the
hello-containerservice. A new image is pulled, started, and exits, just like you would do by hand to start and stop a container. -
Commit your changes and push up.
-
Create a new directory called scripts.
-
In that new directory, create a new executable file called welcome-compose.sh containing the following:
#!/bin/bash
echo 'Hello, Docker Compose!'
- Update the
hello-worldservice in your docker-compose.yaml file to use thenginx:1-alpineimage. We're using this image because it will continue to run and let us be able to exec into the container later on. - In the docker-compose.yaml file, mount the new
./scriptsdirectory as the/home/scriptsdirectory within thehello-containerservice. - Update the
hello-containerservice to run the welcome-docker.sh file and paste the output ofdocker-compose up. - Commit your changes and push up.
- Create a new service called
data-containerthat uses thenginx:1-alpineimage and mounts the same volumes as thehello-containerservice. - Commit and push up.
Note we're manually putting this file in both containers at this time. Now let's use a shared volume to accomplish the same thing.
- Create a new Dockerfile from
alpine:latest- On build, the dockerfile should create a directory
/home/scripts - Copy the
./scriptsdirectory from your machine into the newly created/home/scriptsdirectory in the container
- On build, the dockerfile should create a directory
- Update the docker-compose.yaml file so that the data-container service builds using this newly created Dockerfile
- Add a named volume mount called scriptsdir to docker-compose which will be used by both containers
- Remove your previous volume mounts in both the
data-containerandhello-container. We are now going to use the named volume instead and will be changing these lines. - Mount the named volume in the location that we copied our
./scripts(/home/scripts) into. Do this in both thedata-containerandhome-containerservices. - The
hello-containershould "depend upon" thedata-containerservice coming up first. This will ensure that the./scriptsdirectory is copied first and later we'll be able to access that folder in thehello-containerservice because of the shared, named volume. docker-compose upand exec into thehello-containerto confirm thewelcome-compose.shscript is in/home/scripts- Commit your changes and push up.
When you are done, verify you have pushed your changes to GitHub. Please create a tag called v1.0 with a message of "ready for review" in your docker compose repo. Be sure your tags are pushed to the remote repository and are visible in GitHub.
Create an issue titled Review Module 3 - Docker Compose and assign it to @generationtux-helmsmen.