Wiki
Manually create a Docker image
Existing English translations were reused. 1 current source blocks remain in Simplified Chinese, without triggering paid translation.
Docker images are built through a Dockerfile file. This file has no extension, and its filename is fixed as Dockerfile.
Essentially,Dockerfile can be regarded as a script file used for automated image building. In our daily use of Linux systems, the various installation and configuration commands executed in the terminal (such as installing software, modifying environment variables, etc.) can all be written into Dockerfile according to certain syntax rules.
However, these ordinary Shell commands need to be used together with specific instructions provided by Docker (such as RUN、COPY、ENV etc.) so that Docker can correctly parse and execute these steps. The process of building an image is essentially executing the instructions defined in Dockerfile step by step from top to bottom, ultimately generating a usable image.
In this way, we can standardize and automate the environment configuration process, thereby achieving environmental consistency and reproducibility.
The following lists some commonly used Dockerfile instructions and their purposes:
1.Common DockerFile Commands
| 指令 | 说明 | 示例 |
|---|---|---|
| FROM | 指定基础镜像,是 Dockerfile 的起点 | FROM ubuntu:22.04 |
| LABEL | 添加元数据(如作者、版本等) | LABEL maintainer="you@example.com" |
| ENV | 设置环境变量 | ENV PORT=8080 |
| ARG | 构建参数,只在构建期间可用 | ARG VERSION=1.0 |
| RUN | 构建镜像时运行命令 | RUN apt-get update && apt-get install -y curl |
| COPY | 复制文件到镜像中 | COPY . /app |
| ADD | 类似 COPY,额外支持解压 .tar 文件或远程 URL(不推荐用于 URL) | ADD archive.tar.gz /data/ |
| WORKDIR | 设置工作目录 | WORKDIR /opt |
| CMD | 设置容器启动时默认命令(可被 docker run 覆盖) | CMD ["node", "index.js"] |
| ENTRYPOINT | 设置容器启动时固定命令(通常用于 CLI 工具等) | ENTRYPOINT ["python3"] |
| EXPOSE | 声明镜像内服务监听的端口(不会自动映射) | EXPOSE 80 |
| VOLUME | 声明数据卷挂载点 | VOLUME ["/data"] |
| USER | 设置后续命令执行的用户 | USER appuser |
| ONBUILD | 当镜像作为其他镜像基础镜像时触发的构建指令 | ONBUILD COPY . /src |
| SHELL | 更改默认 shell,比如将 sh -c 改为 bash -c | SHELL ["/bin/bash", "-c"] |
| HEALTHCHECK | 定义容器运行时的健康检查命令 | HEALTHCHECK CMD curl --fail http://localhost:8080 |
| STOPSIGNAL | 容器停止时发送的信号 | STOPSIGNAL SIGKILL |
2.Create a container yourself
2.1.Create manually
First, create a folder to store yourDockerfile For example, I created a~/UserFolder/MySource/folder underMyDocker.
After creating it, open VS Code in that folder.
cd ~/UserFolder/MySource/MyDocker
code .
Install in the VSCode extensionDev Containers,Container Toolsthese two plugins.

- Extension 1: Microsoft Docker Tools

The Docker extension plugin has evolved into Container Tools; please install Container Tools.

- Plugin 2: Microsoft Docker Remote Development Tools
The following is the remote development plugin.

Next, createDockerfileFile:

We create a simplest X86 architecture image, and the content of this image is:
- Mirror based on Ubuntu24.04 system
- Comes with CUDA and CuDNN
- Default shell environment is bash
- Change source to a domestic mirror source
- Add rich colors to Shell
# 基于NVIDIA官方CUDA 12.6和CuDNN基础镜像
FROM nvidia/cuda:12.6.0-cudnn-devel-ubuntu24.04
# 设置环境变量以防止交互安装
ENV DEBIAN_FRONTEND=noninteractive
# 强制使用 Bash 作为默认 Shell
SHELL ["/bin/bash", "-c"]
# 先更新包列表并安装CA证书
RUN apt-get update && apt-get install -y ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# 替换为清华大学的DEB822格式镜像源
RUN sed -i 's|http://archive.ubuntu.com/ubuntu/|https://mirrors.tuna.tsinghua.edu.cn/ubuntu/|g' /etc/apt/sources.list.d/ubuntu.sources
# 配置终端颜色和提示符
RUN sed -i 's/#force_color_prompt=yes/force_color_prompt=yes/g' /root/.bashrc && \
# 自定义PS1(带颜色的提示符)
echo 'export PS1="\[\033[1;36m\]\u@\h\[\033[00m\]:\[\033[1;34m\]\w\[\033[00m\]\$ "' >> /root/.bashrc && \
# 给ls、grep等命令添加别名
echo "alias ls='ls --color=auto'" >> /root/.bashrc && \
echo "alias grep='grep --color=auto'" >> /root/.bashrc && \
echo "alias ll='ls -alFh --color=auto'" >> /root/.bashrc && \
# 清理临时文件
rm -rf /tmp/LS_COLORS

Compile an x86 image on an x86 computer (a properly configured network environment is essential; otherwise, the NVIDIA image cannot be pulled):
The command format isdocker build -t 镜像名称 . For example:
docker build -t ubuntu24_cuda126_cudnn:latest .

Then use thedocker imagescommand to view the compiled image.

2.2.Run this image
UseThe parameters of the run command are very important.Use the run command to run the created image.
You should change--nameto the name of the container you want to create, for exampleubuntu24cuda126, and the bottom line should contain the name of the image you just created, for exampleubuntu24_cuda126_cudnn:latest.
The corresponding command for this tutorial should be:
sudo docker run --name=ubuntu24cuda126 \
--gpus all \
-e NVIDIA_DRIVER_CAPABILITIES=all \
-e DISPLAY=$DISPLAY \
-dit \
--privileged \
--net=host \
--group-add audio \
--group-add video \
--group-add dialout \
-e XAUTHORITY=$HOME/.Xauthority \
-e WAYLAND_DISPLAY=$WAYLAND_DISPLAY \
-e XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR \
-e QT_QPA_PLATFORM=xcb \
-v /tmp/.X11-unix:/tmp/.X11-unix:rw \
-v /dev/dri:/dev/dri \
-v $HOME/.Xauthority:$HOME/.Xauthority:ro \
-v /run/user/$(id -u)/wayland-0:/run/user/$(id -u)/wayland-0 \
-v /run/user/$(id -u):/run/user/$(id -u) \
-v $HOME:$HOME \
-w $HOME \
ubuntu24_cuda126_cudnn:latest
The appearance of the following line indicates success.

You can see in the VSCode plugin that the container is already running successfully.

2.3.Open the terminal of the Ubuntu 24 system in the container
The command format isdocker exec -it 容器名称 bash For example:
docker exec -it ubuntu24cuda126 bash
As shown in the figure below, you have already entered the terminal of the Ubuntu 24 system inside the container.

Next, you can install aneofetchand test it.
Note that you are already running a terminal as therootidentity in the Ubuntu 24 system, so you do not need to prefix each command withsudoFor example,sudo apt updatecan be used directly as a replacement forapt update.
apt update
apt install neofetch
neofetch

Then you can also conveniently test the installation status of the NVIDIA driver, CUDA, and cuDNN:
nvidia-smi
nvcc -V
cat /usr/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
As shown in the figure below, the Ubuntu 24 in the container shares the host's NVIDIA driver, with version 595.80. It comes with CUDA12.6 and CuDNN9.3.0

2.4.Push the image to Docker Hub
The steps to push a Docker image to Docker Hub are as follows:
- Create a Docker Hub account
If you do not yet have a Docker Hub account, go to Docker Hub to register for a free account.
- Log in to Docker Hub
Log in to your Docker Hub account in the terminal using the following command:
docker login
Enter your Docker Hub username and password for verification.
- Tag your image
Docker Hub uses the format <用户名>/<镜像名>:<标签> to identify images. You need to tag your image so that it can be pushed to Docker Hub. Use the following command:
docker tag 镜像名:标签 <你的用户名>/新镜像名:标签
For example, if your Docker Hub username is tungchiahui, you should run:
docker tag ubuntu24_cuda126_cudnn:latest tungchiahui/ubuntu24_cuda126_cudnn:latest
- Push the image to Docker Hub
Push the image to Docker Hub using the following command:
docker push <你的用户名>/镜像名:标签
For example:
docker push tungchiahui/ubuntu24_cuda126_cudnn:latest
- Verify push success
You can verify whether your image has been successfully pushed by visiting your personal page on Docker Hub.
Precautions
-
Ensure that your image size is within the limits of Docker Hub (typically 10GB).
-
If you plan to make the image public, you can set it as a public repository; if you want only yourself to be able to access it, you can set it as a private repository.


2.5.Manual creation (cross-platform multi-architecture build) (advanced, no need to learn for now)
If you want to build images on x86/x64 for the local x86 device, and also want to build Docker images for ARM64 devices such as Raspberry Pi and Jetson , you need to use Docker's cross-platform build feature . The following is the complete solution:
Enable Docker cross-platform build
Emulating an ARM64 environment on an x86 host requires the following tools:
- Enable buildx (only needs to be done once)
docker buildx create --name multiarch_builder --use
This creates and enables a builder that supports multi-architecture builds and persists after the computer restarts, so it only needs to be run once.
- Install QEMU support (generally included in newer versions of Docker Desktop, but must be installed on Linux). If you are using a server or a Linux distribution, ensure that the QEMU emulator is available:
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
After the computer restarts, it will disappear, so you need to run this command once before the buildx command each time the computer restarts.
- Modify the Dockerfile to a multi-architecture version
# 支持多架构,使用变量指定架构
ARG TARGETARCH
# 基于NVIDIA官方CUDA 12.6和CuDNN基础镜像
FROM --platform=linux/${TARGETARCH} nvidia/cuda:12.6.0-cudnn-devel-ubuntu24.04
# 设置环境变量以防止交互安装
ENV DEBIAN_FRONTEND=noninteractive
# 强制使用 Bash 作为默认 Shell
SHELL ["/bin/bash", "-c"]
# 先更新包列表并安装CA证书
RUN apt-get update && apt-get install -y ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# 替换为清华大学的DEB822格式镜像源
RUN if [ "$(dpkg --print-architecture)" = "amd64" ]; then \
sed -i 's|http://archive.ubuntu.com/ubuntu/|https://mirrors.tuna.tsinghua.edu.cn/ubuntu/|g' /etc/apt/sources.list.d/ubuntu.sources; \
elif [ "$(dpkg --print-architecture)" = "arm64" ]; then \
sed -i 's|http://ports.ubuntu.com/ubuntu-ports/|https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/|g' /etc/apt/sources.list.d/ubuntu.sources; \
fi
# 配置终端颜色和提示符
RUN sed -i 's/#force_color_prompt=yes/force_color_prompt=yes/g' /root/.bashrc && \
# 自定义PS1(带颜色的提示符)
echo 'export PS1="\[\033[1;36m\]\u@\h\[\033[00m\]:\[\033[1;34m\]\w\[\033[00m\]\$ "' >> /root/.bashrc && \
# 给ls、grep等命令添加别名
echo "alias ls='ls --color=auto'" >> /root/.bashrc && \
echo "alias grep='grep --color=auto'" >> /root/.bashrc && \
echo "alias ll='ls -alFh --color=auto'" >> /root/.bashrc && \
# 清理临时文件
rm -rf /tmp/LS_COLORS
- Building multi-architecture images Use the following command to build amd64 and arm64:
docker buildx build --platform linux/amd64,linux/arm64 -t <你的镜像名>:<标签> --push .
Example:
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t tungchiahui/ubuntu24_cuda126_cudnn:latest \
--push \
.
Note: --platform specifies multiple architectures. --push is required because buildx's multi-platform build cannot be loaded locally by default (unless --load is added, but that only supports a single architecture).


2.6.Clear build cache
# 清理BuildKit构建缓存
docker builder prune -f
