无互联网局域网内多树莓派Docker应用部署工作流问询
Absolutely, you can build Docker images on your PC_A and deploy them to offline Raspberry Pi devices in your local network—this is totally feasible with the right setup. Let’s walk through your questions and the step-by-step workflow, including how to leverage docker-machine and Docker Swarm as you’ve already researched.
1. Can I build images on PC_A and deploy to offline LAN Raspberry Pis?
Yes, definitely! The key caveat here is architecture compatibility: most PCs run x86_64, while Raspberry Pis use ARM (typically armv7 or arm64). You’ll need to build multi-architecture images (or ARM-specific images) on PC_A so they run correctly on the Pi hardware.
You don’t need internet access for the deployment phase once all prerequisites are in place—just a working LAN connection between PC_A and your Pis.
2. Required Configurations & Step-by-Step Workflow
Let’s break this down into actionable steps, covering both individual Pi deployment and Swarm cluster setup:
Prerequisites (Do These First, with Internet Access)
- Prepare Raspberry Pis:
- Install Docker on each Pi while you have internet: Download the appropriate
.debpackages for your Pi’s OS (e.g., Raspberry Pi OS) and transfer them via USB/SD card. Install with:sudo dpkg -i docker-ce_*.deb docker-ce-cli_*.deb containerd.io_*.deb sudo systemctl start docker sudo systemctl enable docker sudo usermod -aG docker pi # Let the pi user run Docker without sudo - Enable SSH on each Pi (via
raspi-configorsudo systemctl enable ssh). - Ensure all Pis and PC_A are on the same LAN, and you can ping each Pi from PC_A.
- Install Docker on each Pi while you have internet: Download the appropriate
- Prepare PC_A:
- Install Docker (with buildx support for multi-architecture builds).
- Set up SSH key-based authentication to your Pis (so you don’t enter passwords repeatedly):
ssh-keygen # Accept defaults ssh-copy-id pi@<PI_IP_ADDRESS> # Repeat for each Pi
Option 1: Deploy to Individual Pis with docker-machine
docker-machine lets you manage Docker daemons on remote Pis directly from PC_A.
- Add Pis as Docker Machines:
For each Pi, run:docker-machine create \ --driver generic \ --generic-ip-address=<PI_IP> \ --generic-ssh-user=pi \ pi-node-<NUMBER> - Build ARM-Compatible Images on PC_A:
Use Docker Buildx to build an ARM-specific image (replacelinux/arm/v7withlinux/arm64if using Pi 4+/64-bit OS):# Initialize Buildx (if not already done) docker buildx create --name multiarch --use docker buildx inspect --bootstrap # Build and load the ARM image to your local Docker docker buildx build --platform linux/arm/v7 -t my-pi-app:latest --load . - Transfer & Load Image to Pis:
You can either:- Export the image as a tar file and copy it to each Pi:
docker save -o my-pi-app.tar my-pi-app:latest docker-machine scp my-pi-app.tar pi-node-<NUMBER>:/home/pi/ docker $(docker-machine config pi-node-<NUMBER>) load -i /home/pi/my-pi-app.tar - Or directly push the image to a local registry (if you set one up on a Pi, but this requires LAN connectivity only).
- Export the image as a tar file and copy it to each Pi:
- Run Containers on Pis:
From PC_A, execute commands on the remote Pi’s Docker daemon:docker $(docker-machine config pi-node-<NUMBER>) run -d --name my-app my-pi-app:latest
Option 2: Deploy to a Docker Swarm Cluster (For 5 Pis)
Swarm makes managing a cluster of Pis easier—you can deploy services across all nodes from PC_A.
- Initialize Swarm on One Pi (Manager Node):
SSH into one Pi and run:
Copy thedocker swarm init --advertise-addr <MANAGER_PI_IP>docker swarm joincommand that’s output (you’ll need this for worker nodes). - Join Worker Pis to the Swarm:
On each remaining Pi, run the join command from step 1 (e.g.):docker swarm join --token <SWARM_WORKER_TOKEN> <MANAGER_PI_IP>:2377 - Connect PC_A to the Swarm Manager:
From PC_A, use SSH to connect to the Swarm manager’s Docker daemon:
Now any Docker command you run on PC_A will target the Swarm cluster.export DOCKER_HOST=ssh://pi@<MANAGER_PI_IP> - Deploy Your Service:
Create adocker-compose.ymlfile on PC_A (example):
Deploy the stack to the Swarm:version: '3.8' services: my-pi-service: image: my-pi-app:latest deploy: replicas: 5 # One instance per Pi placement: max_replicas_per_node: 1 ports: - "8080:8080"
Note: Ensure thedocker stack deploy -c docker-compose.yml my-pi-stackmy-pi-app:latestimage is loaded on every Pi before deployment (use the tar transfer method from Option 1).
Key Tips for Offline Success
- Batch Operations: Write a simple bash script to transfer and load images to all Pis at once (loop through IP addresses with
scpanddocker-machinecommands). - Local Registry (Optional): If you have a Pi with extra storage, set up a local Docker registry (
docker run -d -p 5000:5000 --restart always registry:2). Then push your ARM image tolocalhost:5000/my-pi-appfrom PC_A, and pull it on Pis using the registry’s LAN IP—this avoids transferring tar files individually.
内容的提问来源于stack exchange,提问作者Julius s

