[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 @   Zhentiw  阅读(140)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-01-27 [Yarn] Use yarn up to Update Dependencies In A Yarn Workspace
2020-01-27 [Yarn] Use Yarn dlx to Execute Packages without Installing Them
2020-01-27 [Yarn] Install Yarn2
2020-01-27 [Tools] Install npm packages globally without sudo on macOS and Linux
2019-01-27 [TypeScript] Dynamically initialize class properties using TypeScript decorators
2016-01-27 [Javascript] Web APIs: Persisting browser data with window.localStorage
2016-01-27 [Javascript] The JSON.stringify API
点击右上角即可分享
微信分享提示