第 8 節

Manually create a Docker image

0瀏覽次數0訪問次數--跳出率--平均停留

(If you find it troublesome, just go directly to the sections on deploying with various Docker containers) (If someone else has already set one up for you, don't bother doing it yourself.)

DockerFile

|command|Explanation|Here is the translation of the provided Simplified Chinese Markdown fragment into natural American English, following all specified rules.


Example| |:---|:---|:---| |FROM|Specify the base image, which is the starting point of the Dockerfile.|FROM ubuntu:22.04| |LABEL|Add metadata (such as author, version, etc.)|LABEL maintainer="you@example.com"| |ENV|Set environment variables|ENV PORT=8080| |ARG|Build parameters, only available during the build process.|ARG VERSION=1.0| |RUN|When building the image, run the command|RUN apt-get update && apt-get install -y curl| |COPY|Copy files into the image|COPY . /app| |ADD|Similar to COPY, with additional support for extracting .tar files or remote URLs (not recommended for URLs).|ADD archive.tar.gz /data/| |WORKDIR|Set the working directory|WORKDIR /opt| |CMD|Set the default command when the container starts (can be overridden by docker run).|CMD "node", "index.js"| |ENTRYPOINT|Set a fixed command when the container starts (commonly used for CLI tools, etc.)|ENTRYPOINT "python3"| |EXPOSE|Declare the ports that services inside the image listen on (will not be automatically mapped).|EXPOSE 80| |VOLUME|Declare the data volume mount point|VOLUME "/data"| |USER|Set the user for subsequent command execution.|USER appuser| |ONBUILD|Build instructions triggered when an image is used as the base image for other images.|ONBUILD COPY . /src| |SHELL|Change the default shell, for example, change sh -c to bash -c.|SHELL "/bin/bash", "-c"| |HEALTHCHECK|Define the health check command for the container runtime.|HEALTHCHECK CMD curl --fail http://localhost:8080| |STOPSIGNAL|Signal sent when the container stops|STOPSIGNAL SIGKILL|

Create your own container

Manually created

https://github.com/tungchiahui/ros-docker


# DockerFile内容请看Github仓库中的DockerFile

Compiling x86 on an x86 computer:

docker build -t ros-melodic-cuda118-cudnn8-bionic:latest .

docker build -t ros-noetic-focal:latest .

docker build -t ros-humble-jammy:latest .

docker build -t ros-jazzy-noble:latest .

docker build -t ros-humble-opencv411-cuda128-cudnn970-jammy:latest .

docker build -t ros-jazzy-opencv411-cuda128-cudnn970-noble:latest .

The image size is 5GB (for the compressed size, see DockerHub).

The steps to push a Docker image to Docker Hub are as follows:

  1. Create a Docker Hub account

If you don't already have a Docker Hub account, please go to Docker Hub to register a free account.

  1. Log in to Docker Hub

In the terminal, use the following command to log in to your Docker Hub account:

docker login

Enter your Docker Hub username and password for verification.

  1. Tag your image

Docker Hub uses the format <用户名>/<镜像名>:<标签> to identify images. You need to tag your image so it can be pushed to Docker Hub. Use the following command:

docker tag ros-jazzy-noble:latest <你的用户>/ros-jazzy-noble:latest

For example, if your Docker Hub username is tungchiahui, you should run:

docker tag ros-noetic-focal:latest tungchiahui/ros-noetic-focal:latest

docker tag ros-humble-jammy:latest tungchiahui/ros-humble-jammy:latest

docker tag ros-jazzy-noble:latest tungchiahui/ros-jazzy-noble:latest

docker tag ros-humble-opencv411-cuda128-cudnn970-jammy:latest tungchiahui/ros-humble-opencv411-cuda128-cudnn970-jammy:latest

docker tag ros-jazzy-opencv411-cuda128-cudnn970-noble:latest tungchiahui/ros-jazzy-opencv411-cuda128-cudnn970-noble:latest
  1. Push the image to Docker Hub

Use the following command to push the image to Docker Hub:

docker push <你的用户>/ros-noetic-jazzy-noble:latest

For example:

docker push tungchiahui/ros-noetic-focal:latest

docker push tungchiahui/ros-humble-jammy:latest

docker push tungchiahui/ros-jazzy-noble:latest

docker push tungchiahui/ros-humble-opencv411-cuda128-cudnn970-jammy:latest

docker push tungchiahui/ros-jazzy-opencv411-cuda128-cudnn970-noble:latest

docker push tungchiahui/ros-noetic-focal-arm64:latest
  1. Verify the push was successful.

You can verify whether your image has been successfully pushed by visiting your personal page on Docker Hub.

Notes

  • Make sure your image size stays within Docker Hub's limits (typically 10GB).
  • If you plan to make the image public, you can set it as a public repository; if you want only yourself to have access, you can set it as a private repository.

Manual creation (cross-platform multi-architecture build)

If you want to build images on an x86/x64 computer (i.e., native x86 devices) and also build Docker images for ARM64 devices like Raspberry Pi, Jetson, etc. , you need to use Docker's cross-platform build functionality. Here is the complete solution:


  1. Enable Docker Cross-Platform Builds

To simulate an ARM64 environment on an x86 host, the following tools are required:

Step 1: Enable buildx (only needs to be done once)

docker buildx create --name multiarch_builder --use

This will create and enable a builder that supports multi-architecture builds, and it will persist even after a computer restart, so it only needs to be run once.

Step 2: Install QEMU support (Newer versions of Docker Desktop usually come with it, but Linux requires manual installation). If you are using a server or Linux distribution, make sure the qemu emulator is available:

docker run --rm --privileged multiarch/qemu-user-static --reset -p yes

After restarting your computer, it will disappear, so you need to run this command once after each restart, before the buildx command.

Step 3: Build Multi-Architecture Images
Use the following command to build for amd64 and arm64:

docker buildx build --platform linux/amd64,linux/arm64 -t <你的镜像>:<> --push .

# 例子:
docker buildx build \
--platform linux/amd64,linux/arm64 \
 -t tungchiahui/ros:noetic-focal \
 --push \
 .

docker buildx build \
--platform linux/amd64,linux/arm64 \
 -t tungchiahui/ros:humble-jammy \
 --push \
 .

docker buildx build \
--platform linux/amd64,linux/arm64 \
 -t tungchiahui/ros:jazzy-noble \
 --push \
 .

 docker buildx build \
--platform linux/amd64,linux/arm64 \
 -t tungchiahui/opencv:411-cuda128-cudnn970-focal \
 --push \
 .

docker buildx build \
--platform linux/amd64,linux/arm64 \
 -t tungchiahui/opencv:411-cuda128-cudnn971-jammy \
 --push \
 .

 docker buildx build \
--platform linux/amd64,linux/arm64 \
 -t tungchiahui/opencv:411-cuda128-cudnn971-noble \
 --push \
 .

docker buildx build \
--platform linux/amd64,linux/arm64 \
 -t tungchiahui/ros-opencv:noetic-411-cuda128-cudnn970-focal \
 --push \
 .

docker buildx build \
--platform linux/amd64,linux/arm64 \
 -t tungchiahui/ros-opencv:humble-411-cuda128-cudnn970-jammy \
 --push \
 .

 docker buildx build \
--platform linux/amd64,linux/arm64 \
 -t tungchiahui/ros-opencv:jazzy-411-cuda128-cudnn970-noble \
 --push \
 .

  docker buildx build \
--platform linux/amd64,linux/arm64 \
 -t sdutvincirobot/ros-opencv:humble-411-cuda128-cudnn970-jammy \
 --push \
 .

Note: --platform specifies multi-architecture. --push is required because buildx's multi-platform builds cannot be loaded locally by default (unless --load is added, but that only supports a single architecture).

Clear build cache


# 清理BuildKit构建缓存
docker builder prune -f

音乐页