Powered By GitBook
docker-compose-on-container-optimized-os
Running Docker Compose on a Container-Optimized OS instance.
Tim Swast | Developer Programs Engineer | Google
Contributed by Google employees.
This tutorial guides you through running Docker Compose in a container on a Container-Optimized OS instance.

Objectives

    Run Docker Compose with Docker on Container-Optimized OS.
    Create an alias for the containerized docker-compose command.

Before you begin

    1.
    Create or select a Google Cloud project from the Cloud Console projects page.
    2.
    ​Enable billing​
    for your project.

Costs

This tutorial uses billable components of Google Cloud, including Compute Engine.
Use the Pricing Calculator to estimate the costs for your usage.

Setting up the virtual machine

Create a new Compute Engine instance using the Container-Optimized OS stable image.
    1.
    Open the Cloud Console.
    3.
    Select the desired Zone, such as "us-central1-f".
    4.
    Select the desired Machine type, such as "micro" (f1-micro).
    5.
    Change the Boot disk to "Container-Optimized OS stable".
    6.
    Check the box to allow HTTP traffic in the Firewall section.
    7.
    Click the Create button to create the Compute Engine instance.

Getting the sample code

    1.
    After the instance is created, click the SSH button to open a terminal
    connected to the machine.
    2.
    Download the Hello World sample using the git command.
    1
    git clone https://github.com/docker/dockercloud-hello-world.git
    2
    cd dockercloud-hello-world
    Copied!

Running Docker Compose

​Container-Optimized OS has many benefits, but since it is locked down by default, it is difficult to run software that is not bundled in a container. For example, the general instructions for installing Docker Compose will not work because very few parts of the filesystem are mounted as executable. Instead, you can run Docker Compose image.
    1.
    Download and run the Docker Compose image. Check the tags for Docker Compose to use the latest version.
    1
    docker run docker/compose:1.24.0 version
    Copied!
    2.
    Ensure that your location is a writable directory.
    Many directories are mounted as read-only in the Container-Optimized OS. Change to a writable directory such as your home directory.
    1
    $ pwd
    2
    /home/username/dockercloud-hello-world
    Copied!
    3.
    Run the Docker Compose command to run the sample code.
    So that the Docker Compose container has access to the Docker daemon, mount the Docker socket with the -v /var/run/docker.sock:/var/run/docker.sock option.
    To make the current directory available to the container, use the -v "$PWD:$PWD" option to mount it as a volume and the -w="$PWD" to change the working directory.
    1
    docker run --rm \
    2
    -v /var/run/docker.sock:/var/run/docker.sock \
    3
    -v "$PWD:$PWD" \
    4
    -w="$PWD" \
    5
    docker/compose:1.24.0 up
    Copied!
    4.
    With the docker run command still running, open the Cloud Console instances page. Click the link to your instance's External IP address.
    You should see a "Hello World" message appear.
    5.
    With the SSH window open, press Control-C on your keyboard to stop the sample application.

Making an alias to Docker Compose

The docker run ... docker/compose:1.24.0 up command is equivalent to running the docker-compose up command on systems where Docker Compose is installed by the usual method. So that you don't have to remember or type this long command, create an alias for it.
    1.
    Add a docker-compose alias to your shell configuration file, e.g. .bashrc.
    1
    echo alias docker-compose="'"'docker run --rm \
    2
    -v /var/run/docker.sock:/var/run/docker.sock \
    3
    -v "$PWD:$PWD" \
    4
    -w="$PWD" \
    5
    docker/compose:1.24.0'"'" >> ~/.bashrc
    Copied!
    2.
    Reload the Bash configuration.
    1
    source ~/.bashrc
    Copied!
    3.
    Change back to the sample directory if you aren't already there.
    1
    cd ~/dockercloud-hello-world
    Copied!
    4.
    Run the sample code using the new docker-compose alias.
    1
    docker-compose up
    Copied!
    You should be able to view the "Hello World" message at your instance's external IP address.

Next steps

    [Set up the Google Cloud logging driver for Docker to upload your containers'
    logs to Stackdriver Logging](/community/tutorials/docker-gcplogs-driver).
    [Use CoreOS Toolbox to run other tools in the Container-Optimized
    OS](/container-optimized-os/docs/how-to/toolbox).
    Learn how to orchestrate containers using
    ​Kubernetes. [Use Kompose to transform Docker
    Compose configuration into Kubernetes
Last modified 7mo ago