[Docker] Storing Container Data in Google Cloud Storage

Introduction

Docker volumes is the preferred method of storing container data locally. Volume support is built directly into Docker, making it an easy tool to use for storage, as well as more portable. However, storing container data in Docker volumes still requires you to back up the data in those volumes on your own.

There is another option - storing your container data in the cloud. It's not a solution for every problem, but after this lab, you'll have another tool at your disposal.

This lab will show you how to mount a Cloud Storage bucket onto your local system as a directory. You will then mount that directory into your Docker container. We will use an httpd container, to serve the contents of that bucket as a webpage, but you can use it to share any common data between containers.

This hands-on lab will demonstrate how flexible Docker can be. You can make changes to your bucket and all of your containers using the Cloud Storage bucket will near-instantly have access to the content.

 

Solution

Log in to the server using the credentials provided:

ssh cloud_user@<PUBLIC_IP_ADDRESS>

Note: You may continue as cloud_user for the duration of the lab. You do not need to elevate to root user privileges to follow this lab guide.

Configuration and Installation

  1. Export the projnum variable:
    export projnum=$(curl http://metadata.google.internal/computeMetadata/v1/project/numeric-project-id -sH "Metadata-Flavor: Google")
  2. Verify that the variable was set successfully:
    echo $projnum
  3. Export the BUCKET variable:
    export BUCKET="widgetfactory-${projnum}"

Prepare the Cloud Storage Bucket

  1. Using gsutil, create a new bucket:
    gsutil mb -l us-central1 -c standard gs://$BUCKET
  2. Verify that the gcsfuse repo is available on the server:
    cat /etc/yum.repos.d/gcsfuse.repo
  3. Install gcsfuse (Note: please wait a few minutes before continuing to allow the lab's startup scripts to release the yum lock).
    sudo yum install -y gcsfuse
  4. Update the fuse.conf file to allow the user to mount the bucket properly:
    sudo sed -ri 's/# user_allow_other/user_allow_other/' /etc/fuse.conf
  5. Configure the directories needed to mount the bucket:
    sudo mkdir /mnt/widget-factory /tmp/gcs
  6. Change ownership of the directories to the cloud_user:
    sudo chown cloud_user: /mnt/widget-factory/ /tmp/gcs
  7. Mount the bucket:
    gcsfuse -o allow_other --temp-dir=/tmp/gcs $BUCKET /mnt/widget-factory/
  8. Copy the website files into the bucket:
    cp -r /home/cloud_user/widget-factory-inc/web/* /mnt/widget-factory/
  9. List the contents of the bucket:
    gsutil ls gs://$BUCKET

Use the GCS Bucket in a Container

  1. Mount the directory into the Docker container:
    sudo docker run -d --name web1 --mount type=bind,source=/mnt/widget-factory,target=/usr/local/apache2/htdocs,readonly -p 80:80 httpd:2.4
  2. Using a web browser, verify connectivity to the container:
    <SERVER_PUBLIC_IP_ADDRESS>
posted @ 2023-01-27 04:35  Zhentiw  阅读(135)  评论(0编辑  收藏  举报