Skip to content

Latest commit

 

History

History
64 lines (47 loc) · 4.43 KB

File metadata and controls

64 lines (47 loc) · 4.43 KB

Module 3: Docker Compose

Introduction

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:


Completing the Module

  1. Create a file called docker-compose.yaml

  2. In your docker-compose.yaml file, add a new service called hello-container with the image hello-world

  3. 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-container service. A new image is pulled, started, and exits, just like you would do by hand to start and stop a container.

  4. Commit your changes and push up.

  5. Create a new directory called scripts.

  6. In that new directory, create a new executable file called welcome-compose.sh containing the following:

#!/bin/bash
echo 'Hello, Docker Compose!'
  1. Update the hello-world service in your docker-compose.yaml file to use the nginx:1-alpine image. We're using this image because it will continue to run and let us be able to exec into the container later on.
  2. In the docker-compose.yaml file, mount the new ./scripts directory as the /home/scripts directory within the hello-container service.
  3. Update the hello-container service to run the welcome-docker.sh file and paste the output of docker-compose up.
  4. Commit your changes and push up.
  5. Create a new service called data-container that uses the nginx:1-alpine image and mounts the same volumes as the hello-container service.
  6. 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.

  1. Create a new Dockerfile from alpine:latest
    • On build, the dockerfile should create a directory /home/scripts
    • Copy the ./scripts directory from your machine into the newly created /home/scripts directory in the container
  2. Update the docker-compose.yaml file so that the data-container service builds using this newly created Dockerfile
  3. Add a named volume mount called scriptsdir to docker-compose which will be used by both containers
  4. Remove your previous volume mounts in both the data-container and hello-container. We are now going to use the named volume instead and will be changing these lines.
  5. Mount the named volume in the location that we copied our ./scripts (/home/scripts) into. Do this in both the data-container and home-container services.
  6. The hello-container should "depend upon" the data-container service coming up first. This will ensure that the ./scripts directory is copied first and later we'll be able to access that folder in the hello-container service because of the shared, named volume.
  7. docker-compose up and exec into the hello-container to confirm the welcome-compose.sh script is in /home/scripts
  8. Commit your changes and push up.

Wrapping 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.