Create folder and files
In this tutorial, assume that the relevant configuration files (compose.yaml
, Dockerfile
) of the container we want to create are in a folder named Project
.
Create folder Project
and in this folder create files compose.yaml
, Dockerfile
, and folder SHARE
.
Project
├── compose.yaml
├── Dockerfile
└── SHARE
compose.yaml
Replace #!name_lable
, #!name
and #!name:lable
with your custom string.
# https://docs.docker.com/reference/compose-file/
# 项目名称
name: #!name_lable
# 所有的容器的定义都要在services这个顶级元素下定义
services:
# 服务名称,可以与项目名称保持一致
#!name_lable:
# 设置为true,则容器内PID为1的进程就会是docker提供的一个init程序
# 这样可以使容器能够回收内部的僵尸进程,避免占用资源
init: true
# 容器名称,可以与项目名称保持一致
container_name: #!name_lable
# 实现docker run命令中-i参数的效果
stdin_open: true
# 网络模式设置为host,就可以与主机使用同一个网络,而不是使用docker虚拟网卡的桥接网络,方便设置代理和ssh连接
network_mode: "host"
# 默认用户(若不指定且Dockerfile中有USER指令,则默认用户为Dockerfile中指定的用户)
# user: root
user: #!name
# 设置为true,则容器内的root用户与主机的root用户具有相同的权限
# privileged: true
image: #!name:lable
# 设置容器启动后自动执行的命令,可以为空,为空则执行Dockerfile中指定的命令
# 容器内PID为1的进程退出后,容器就会关闭,为了防止容器自动关闭,就需要让容器一直运行程序
# tail -f /dev/null这个命令可以防止容器启动后自动退出,但因为前面设置了stdin_open,所以可以为空
command: tail -f /dev/null
# command: bash -c "su root && service ssh start && tail -f /dev/null"
# 设置要挂载的目录,可以理解为共享文件夹
volumes:
# 显示GUI所必须的
- "/tmp/.X11-unix:/tmp/.X11-unix:rw"
# 使容器可以读取到外接设备
# 比如主机上插了一个USB相机,挂载/dev后,容器内就也可以使用该相机了
# - "/dev:/dev:rw"
# 将docker-compose.yaml所在目录下的SHARE文件夹(如果不存在则会自动创建一个)共享到容器中
# 目的是方便在容器和主机间共享数据
- "./SHARE:/SHARE:rw"
# 设置容器的默认工作目录
working_dir: /SHARE
# 设置环境变量
environment:
# 显示GUI所必须的
- DISPLAY=$DISPLAY
# 显示GUI所必须的
- QT_X11_NO_MITSHM=1
# 设置语言环境
# - LC_ALL=C.UTF-8
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: all
capabilities: [gpu]
For example:
# https://docs.docker.com/reference/compose-file/
# 项目名称
name: liangchengzhao_aicas
# 所有的容器的定义都要在services这个顶级元素下定义
services:
# 服务名称,可以与项目名称保持一致
liangchengzhao_aicas:
# 设置为true,则容器内PID为1的进程就会是docker提供的一个init程序
# 这样可以使容器能够回收内部的僵尸进程,避免占用资源
init: true
# 容器名称,可以与项目名称保持一致
container_name: liangchengzhao_aicas
# 实现docker run命令中-i参数的效果
stdin_open: true
# 网络模式设置为host,就可以与主机使用同一个网络,而不是使用docker虚拟网卡的桥接网络,方便设置代理和ssh连接
network_mode: "host"
# 默认用户(若不指定且Dockerfile中有USER指令,则默认用户为Dockerfile中指定的用户)
# user: root
user: liangchengzhao
# 设置为true,则容器内的root用户与主机的root用户具有相同的权限
# privileged: true
image: liangchengzhao:aicas
# 设置容器启动后自动执行的命令,可以为空,为空则执行Dockerfile中指定的命令
# 容器内PID为1的进程退出后,容器就会关闭,为了防止容器自动关闭,就需要让容器一直运行程序
# tail -f /dev/null这个命令可以防止容器启动后自动退出,但因为前面设置了stdin_open,所以可以为空
command: tail -f /dev/null
# command: bash -c "su root && service ssh start && tail -f /dev/null"
# 设置要挂载的目录,可以理解为共享文件夹
volumes:
# 显示GUI所必须的
- "/tmp/.X11-unix:/tmp/.X11-unix:rw"
# 使容器可以读取到外接设备
# 比如主机上插了一个USB相机,挂载/dev后,容器内就也可以使用该相机了
# - "/dev:/dev:rw"
# 将docker-compose.yaml所在目录下的SHARE文件夹(如果不存在则会自动创建一个)共享到容器中
# 目的是方便在容器和主机间共享数据
- "./SHARE:/SHARE:rw"
# 设置容器的默认工作目录
working_dir: /SHARE
# 设置环境变量
environment:
# 显示GUI所必须的
- DISPLAY=$DISPLAY
# 显示GUI所必须的
- QT_X11_NO_MITSHM=1
# 设置语言环境
# - LC_ALL=C.UTF-8
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: all
capabilities: [gpu]
Dockerfile
What to install depends on your needs and this is just an exmaple.
# docker build \
# --tag=name:lable \
# --build-arg USER_NAME=$(whoami) \
# --build-arg USER_ID=$(id -u) \
# --build-arg GROUP_ID=$(id -g) \
# --build-arg HTTP_PROXY=http://127.0.0.1:7890 --build-arg HTTPS_PROXY=http://127.0.0.1:7890 \
# --network=host \
# --load .
FROM nvidia/cuda:12.8.0-cudnn-devel-ubuntu22.04
ARG USER_NAME
ARG USER_ID
ARG GROUP_ID
RUN sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list \
&& sed -i 's/security.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list \
&& apt update && apt upgrade -y && DEBIAN_FRONTEND=noninteractive \
apt install \
sudo \
tzdata \
git \
openssh-server \
vim \
zsh \
silversearcher-ag \
fzf \
curl \
tmux \
locales \
-y
RUN sed -i 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen \
&& locale-gen \
&& ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone \
&& dpkg-reconfigure --frontend noninteractive tzdata
RUN addgroup --gid $GROUP_ID $USER_NAME \
&& adduser --disabled-password --gecos "" --uid $USER_ID --gid $GROUP_ID $USER_NAME \
&& echo "$USER_NAME:a" | chpasswd \
&& touch /etc/sudoers.d/$USER_NAME \
&& echo "$USER_NAME ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers.d/$USER_NAME \
&& chmod 0440 /etc/sudoers.d/$USER_NAME
USER ${USER_NAME}
RUN sudo chsh -s /bin/zsh ${USER_NAME} \
&& sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" --unattended \
&& sed -i '/^plugins=(git)$/c\plugins=(\n zsh-syntax-highlighting\n zsh-autosuggestions\n git\n extract\n)' ~/.zshrc \
&& git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions \
&& git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting \
&& git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k \
&& sed -i 's/ZSH_THEME="robbyrussell"/ZSH_THEME="powerlevel10k\/powerlevel10k"/' ~/.zshrc \
&& cd && git clone https://github.com/gpakosz/.tmux.git \
&& ln -s -f .tmux/.tmux.conf \
&& cp .tmux/.tmux.conf.local .
Build image
Replace #!name:lable
with your custom string. Then execute the command in the Project
directory.
docker build \
--tag=name:lable \
--build-arg USER_NAME=$(whoami) \
--build-arg USER_ID=$(id -u) \
--build-arg GROUP_ID=$(id -g) \
--build-arg HTTP_PROXY=http://127.0.0.1:7890 --build-arg HTTPS_PROXY=http://127.0.0.1:7890 \
--network=host \
--load .
For example:
docker build \
--tag=liangchengzhao:aicas \
--build-arg USER_NAME=$(whoami) \
--build-arg USER_ID=$(id -u) \
--build-arg GROUP_ID=$(id -g) \
--build-arg HTTP_PROXY=http://127.0.0.1:7890 --build-arg HTTPS_PROXY=http://127.0.0.1:7890 \
--network=host \
--load .
Create and start container
Execute the command in the Project
directory.
docker compose up -d