Running Docker Compose with Docker on a Google Cloud VM Instance

It is recommended to deploy docker-compose containers on a Container-Optimized OS which is Google’s recommended OS for hosting and running containers on GCP. Container-Optimized OS is free to use on GCE. Container Optimized OS has the following features:

  • Optimized for Containers
  • Built to Scale
  • Minimal OS, Maximum Security
  • Open Source and maintained by Google

Setting up the virtual machine

Create a new Compute Engine instance using the Container-Optimized OS stable image.

  1. Open the Cloud Console.
  2. Create a new Compute Engine instance.
  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.

SSH into the COOS and Clone your repository

git clone https://github.com/you/your_repository.git</pre> <!-- /wp:preformatted -->  <!-- wp:paragraph --> <strong>cd your_repository</strong> <!-- /wp:paragraph -->  <!-- wp:heading {"level":3} --> <h3>Install docker compose</h3> <!-- /wp:heading -->  <!-- wp:code --> <pre class="wp-block-code"><code>sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-(uname -s)-(uname -m)" -o /usr/local/bin/docker-compose  sudo chmod +x /usr/local/bin/docker-compose</code></pre> <!-- /wp:code -->  <!-- wp:heading --> <h2 id="running_docker_compose">Running Docker Compose</h2> <!-- /wp:heading -->  <!-- wp:paragraph --> <a href="https://cloud.google.com/container-optimized-os">Container-Optimized OS</a> has many <a href="https://cloud.google.com/container-optimized-os/docs/concepts/features-and-benefits">benefits</a>, but since it is <a href="https://cloud.google.com/container-optimized-os/docs/concepts/security">locked down by default</a>, it is difficult to run software that is not bundled in a container. For example, the general instructions for installing <a href="https://docs.docker.com/compose/">Docker Compose</a> will not work because very few parts of the filesystem are mounted as executable. Instead, you can run <a href="https://hub.docker.com/r/docker/compose/">Docker Compose image</a>. <!-- /wp:paragraph -->  <!-- wp:list {"ordered":true} --> <ol><li>Download and run the Docker Compose image. Check the <a href="https://hub.docker.com/r/docker/compose/tags/">tags for Docker Compose</a> to use the latest version.<code>docker run docker/compose:1.24.0 version</code></li><li>Ensure that your location is a writable directory.Many directories are <a href="https://cloud.google.com/container-optimized-os/docs/concepts/disks-and-filesystem">mounted as read-only in the Container-Optimized OS</a>. Change to a writable directory such as your home directory.<code> pwd
/home/username/dockercloud-hello-world
  • 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"</code> to change the working directory.<code>docker run --rm \    -v /var/run/docker.sock:/var/run/docker.sock \    -v "PWD:PWD" \    -w="PWD"
        docker/compose:1.24.0 up
  • 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.
  • 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.echo alias docker-compose="'"'docker run --rm
          -v /var/run/docker.sock:/var/run/docker.sock
          -v "PWD:PWD"
          -w="$PWD"
          docker/compose:1.24.0'"'" >> ~/.bashrc
    2. Reload the Bash configuration.source ~/.bashrc
    3. Change back to the sample directory if you aren't already there.cd ~/dockercloud-hello-world
    4. Run the sample code using the new docker-compose alias.docker-compose up
      You should be able to view the "Hello World" message at your instance's external IP address.

    Source: https://cloud.google.com/community/tutorials/docker-compose-on-container-optimized-os

    Open chat
    Powered by