如何配置Docker确保CDH容器重启或停止后不丢失工作成果?
Hey there! I totally get the frustration of losing your work after a restart—let’s get this sorted out so your CDH setup stays intact no matter what. The issue here boils down to two things: temporary container storage (by default, Docker doesn’t save data inside containers when they stop/restart) and missing auto-restart configuration (your container wasn’t set to fire up again when your Mac rebooted).
1. Persist Your CDH Data with Docker Volumes or Bind Mounts
First, we need to make sure your CDH data (configs, HDFS storage, Cloudera Manager settings, etc.) lives outside the container, so it survives stops, restarts, or even container deletion.
Option 1: Use Docker Volumes (Recommended)
Docker volumes are managed by Docker itself, so they’re more reliable and avoid permission headaches on Mac. Here’s how to set it up:
- Create a dedicated volume for CDH:
docker volume create cdh-persistent-storage - When starting your CDH container, mount this volume to the critical directories inside the container. For example, if your CDH image stores core data in
/var/lib/cloudera-scm-serverand/hdfs-data, your run command would look like:
Note: Check your CDH image’s documentation to find exactly which directories need persistence—these are just common examples.docker run -d \ --name cdh-container \ -v cdh-persistent-storage:/var/lib/cloudera-scm-server \ -v cdh-persistent-storage:/hdfs-data \ your-cdh-image-name
Option 2: Use Bind Mounts (Manual Local Directory)
If you prefer to store data in a specific folder on your Mac, use a bind mount:
- Create a local directory to hold your CDH data:
mkdir -p ~/Documents/cdh-data - Mount this directory when starting the container:
Pro tip: On Mac, if you run into permission errors, make sure the local directory has read/write access for all users (or adjust the container user’s permissions via the image settings).docker run -d \ --name cdh-container \ -v ~/Documents/cdh-data:/var/lib/cloudera-scm-server \ -v ~/Documents/cdh-data:/hdfs-data \ your-cdh-image-name
2. Configure Auto-Restart for Your CDH Container
Next, we’ll make sure the container automatically starts up when your Mac (or Docker Desktop) reboots.
Set Auto-Restart When Creating a New Container
Add the --restart flag to your run command. There are two useful options:
--restart always: The container will restart every time Docker starts, even if you manually stopped it before rebooting.--restart unless-stopped: The container will restart on Docker startup only if you didn’t manually stop it (this is safer if you sometimes need to keep the container off).
Example with both persistence and auto-restart:
docker run -d \ --name cdh-container \ --restart unless-stopped \ -v cdh-persistent-storage:/var/lib/cloudera-scm-server \ your-cdh-image-name
Update an Existing Container’s Restart Policy
If you already have a container set up and don’t want to recreate it, you can update its restart policy:
docker update --restart unless-stopped cdh-container
3. Recover Your Lost Data (If Needed)
If you want to salvage the work from your old container:
- Start the old container (if it’s stopped):
docker start cdh-container-old - Copy the critical data from the container to your local machine:
docker cp cdh-container-old:/var/lib/cloudera-scm-server ~/cdh-recovery - Stop and delete the old container:
docker stop cdh-container-old && docker rm cdh-container-old - Create a new persistent container and copy the recovered data into it:
docker run -d \ --name cdh-container \ --restart unless-stopped \ -v cdh-persistent-storage:/var/lib/cloudera-scm-server \ your-cdh-image-name docker cp ~/cdh-recovery/. cdh-container:/var/lib/cloudera-scm-server
Final Checks
After setting this up:
- Reboot your Mac and verify the container starts automatically (
docker psshould show it running). - Make a small change in your CDH setup (e.g., create a test HDFS directory), stop the container, start it again, and confirm the change is still there.
内容的提问来源于stack exchange,提问作者miteshsc

