目录
1. 安装
1.1 LINUX
参考
- https://docs.docker.com/engine/install/ubuntu/
- https://mirror.tuna.tsinghua.edu.cn/help/docker-ce/
# 卸载之前的安装
sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
# 安装依赖
sudo apt-get update -y
sudo apt-get install -y ca-certificates curl gnupg lsb-release
# 添加GPG
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# 设置仓库
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 安装最新引擎
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
# 运行样例
sudo docker run -rm hello-world
1.2 Windows
待补充
2. 常用配置
2.1 普通权限运行
sudo usermod -aG docker $USER
newgrp docker
docker run --rm hello-world
2.2 开机自启动
# 开启
sudo systemctl enable docker.service
sudo systemctl enable containerd.service
# 关闭
sudo systemctl disable docker.service
sudo systemctl disable containerd.service
2.3 3 更换Docker镜像源
# 方式一:命令添加
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://docker.mirrors.sjtug.sjtu.edu.cn",
"https://cr.console.aliyun.com",
"http://mirror.azure.cn/"
]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
# 方式二:手动添加
sudo vim /etc/docker/daemon.json
{
"registry-mirrors" : [ "https://docker.mirrors.sjtug.sjtu.edu.cn" ],
}
sudo systemctl daemon-reload
sudo systemctl restart docker
2.4 更改默认存储位置
sudo docker info | grep "Docker Root Dir" # 查看 docker 存储位置
sudo systemctl stop docker # 停止 docker 服务
sudo rsync -avzP /var/lib/docker/ /home/docker # 复制原有数据到新位置
sudo rm -rf /var/lib/docker # 删除原始存储文件夹
sudo ln -s /home/docker /var/lib/ # 指定新的路径之软连接到新位置
sudo sed -i '1a"data-root": "/home/docker",' > /etc/docker/daemon.json # 指定新的路径之配置文件指定新位置
sudo systemctl daemon-reload # 重启守护进程
sudo systemctl restart docker # 重启docker
sudo systemctl status docker # 查看运行状态
sudo docker info | grep "Docker Root Dir" # 查看最新存储位置
3. 显示带UI的软件
# 1. 宿主机开放X server权限
xhost +
# 2. 创建容器,使用 -v 参数挂载宿主机的/tmp/.X11-unix目录,该目录中存放着unix套接字文件;使用 -e 参数设置容器的DISPLAY环境变量与宿主机一致
docker run -it --rm -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY ubuntu:20.04
apt update
apt install gedit
gedit
4.5 GitLab
4.5.1 创建容器
# 1. 拉取镜像
docker pull gitlab/gitlab-ce
# 2. 创建宿主机数据卷文件夹
mkdir -p ${HOME}/.docker/volumes/gitlab/logs # 日志
mkdir -p ${HOME}/.docker/volumes/gitlab/data # 数据
mkdir -p ${HOME}/.docker/volumes/gitlab/config # 配置
# 3. 创建容器
docker run \
--detach \
--publish 50080:80 \
--publish 50022:22 \
--publish 50443:443 \
--name gitlab \
--hostname localhost \
--privileged=true \
--restart always \
--volume ${HOME}/.docker/volumes/gitlab/config:/etc/gitlab \
--volume ${HOME}/.docker/volumes/gitlab/logs:/var/log/gitlab \
--volume ${HOME}/.docker/volumes/gitlab/data:/var/opt/gitlab \
--volume /etc/localtime:/etc/localtime:ro \
--env 'GITLAB_HOST=192.168.1.88' \
--env 'GITLAB_PORT=50080' \
--env 'GITLAB_SSH_PORT=50022' \
gitlab/gitlab-ce
# 浏览器中输入:
# http://172.17.0.1:50080
4.5.2 克隆路径问题
vim ${HOME}/.docker/volumes/gitlab/config/gitlab.rb
# 方式一:
external_url 'http://192.168.1.88:50080'
gitlab_rails['gitlab_ssh_host'] = '192.168.1.88'
gitlab_rails['gitlab_shell_ssh_port'] = 50022
nginx['listen_port'] = 80
# 方式二:
创建容器时不对 80 端口进行映射,而是直接映射 external_url 中的端口(即原本 -p 9080:80 修改成 -p 9080:9080),同时不要设置 nginx['listen_port'],因为 nginx 会自动监听 external_url 中的端口,若 external_url 未指定端口,则监听 80。
docker restart gitlab
4.5.3 获取默认密码
GitLab初始化时将初始秘方放在/etc/gitlab/initial_root_password
,且该文件在首次执行reconfigure后自动删除。由于做了数据卷挂载,因此只需要到宿主机的${HOME}/.docker/volumes/gitlab/config/initial_root_password
文件中获取。
sudo cat ${HOME}/.docker/volumes/gitlab/config/initial_root_password