This note details the process of creating and configuring a development environment with CUDA support using Docker, from initializing the container and installing the necessary packages such as vim, nano, git, openssh-server, etc., to configuring SSH to allow root to log in and set the root password, and configuring the nvcc environment variable for the user to ensure that the CUDA toolchain is available.

It also describes how to clear the command history to maintain privacy and save the configured container as a new image version (4.0).

The documentation then describes the process of creating a new user based on this customized image, including adding the user to the host, obtaining his or her UID and GID, and then starting a new Docker container with this information while mapping the user’s home directory inside the container.

Finally, the note mention changing the ownership of the user’s folder to the newly created user and suggest future improvements that can be made to automate this series of steps by writing Dockerfiles and scripts. The entire process demonstrates the complete flow of building a Docker development environment from scratch suitable for deep learning or other GPU-accelerated tasks.

修改docker权限

1
newgrp docker

从原始容器中创建模板

创建并进入容器

1
2
3
docker run --gpus all -itd --name username --hostname host-57 -p 10023:22  dockerpull.org/nvidia/cuda:12.6.3-cudnn-devel-ubuntu24.04 

docker exec -it username /bin/bash

更新并安装包

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
apt update

apt install vim 5 69

apt install nano

apt install git

apt install openssh-server

apt install sudo

apt install python-is-python3

apt install python3-pip

apt install iputils-ping

apt install --reinstall ubuntu-standard

删除unminimize提示

1
rm -f /etc/update-motd.d/60-unminimize

修改ssh

1
2
3
vim /etc/ssh/sshd_config

PermitRootLogin yes

设置root密码

1
host12345

配置用户nvcc环境

1
vim /etc/skel/.bashrc
1
2
3
4
5
# Set up CUDA environment variables
if [ -d /usr/local/cuda/bin ]; then
    export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}
    export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
fi

清除历史记录

1
2
3
echo -n "" > ~/.bash_history
history -r
history -c

保存容器到镜像(4.0)

1
2
3
docker ps

docker commit -a cjy -m 保存容器到镜像 docker_id newstudent:4.0

从模板中创建新用户

在主机中创建新用户

1
adduser --home /media/disk/home/username username

获取用户UID和GID

1
id username

从模板中创建容器并进入

1
2
3
docker run -u uid:gid --gpus all -itd --name username --hostname host-57 -p 10000:22 -p 10001-10009:10001-10009 -v /media/disk/home/username:/home/username newstudent:4.0

docker exec -it username /bin/bash

更改用户的UID和GID

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
su

# 创建用户组GID
groupadd -g gid username

# 创建用户
adduser --uid uid --gid gid username

# 复制.bashrc到用户home目录
cp -r /etc/skel/. /home/lxy/

# 重启ssh
service ssh restart

# 清除历史记录
echo -n "" > ~/.bash_history
history -r
history -c

# 切换到用户
su username

# 清除历史记录
echo -n "" > ~/.bash_history
history -r
history -c

将用户文件夹所有者从root修改为用户自身

1
2
# 一般文件权限755,文件夹权限644
chown -R 1003:1003 /home/username

TODO: 使用Dockerfile和脚本化