0%

Docker 管理笔记

前言

本文记录 docker 中的常用命令。

宿主机(也许这个叫法不对哈哈)的环境:Ubuntu 18.04。

21-12-15 补充记录了通过子进程PID查找 Container ID 的方法。

正文

安装

首先是 docker 的安装。完全参考了下面的链接,没有问题。

https://www.cnblogs.com/ellisonzhang/p/13902451.html

需要注意的是,未安装 Docker Compose,似乎需要 python,由于想避免宿主机环境混乱,没有安装。

初始配置

首先为了方便配置,将管理员用户加入 docker 组中。这一步可以忽略,但是所有的命令都需要加上 sudo 前缀,较为不便。命令中,-a 表示追加,必须与 -G 一起使用,不加这个前缀时会将原来组成员替换,-G表示附加组,似乎就是除去新建用户生成的组外都是附加组。

1
usermod -a -G docker username

添加后需要重新登陆才能正常使用 docker。

下面的命令可以查看每个用户所属的组。

1
getent passwd | cut -d : -f 1 | xargs groups

下一步,我们先 pull 下来一个官方镜像,为了稳定性,这里我们使用 18.04。

1
docker pull ubuntu:18.04

使用下面命令查看目前本地已有的镜像。

1
docker images

下面的命令查看正在监听端口,在分配端口映射时方便使用。

1
netstat -nlp

使用下面的命令运行镜像。-it 表示交互,但是不知道为什么执行命令并没有交互,似乎和-d命令冲突了;-p 是端口映射,将本地的xx端口映射到容器的22端口便于ssh连接;–name 表示容器的名字,用于标识;–restart 是Restart policy to apply when a container exits (default “no”);-d 是Run container in background and print container ID; 最后是 image:TAG ,和要执行的命令。

1
docker run -it -p 40500:22 --name Ubuntu-18.04-test --restart=always -d ubuntu:18.04 bash

使用 docker ps 命令查看现在在运行的容器。

1
2
3
username@device_name:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd7e12345678 ubuntu:18.04 "bash" 3 minutes ago Up 3 minutes 0.0.0.0:40500->22/tcp, :::40500->22/tcp Ubuntu-18.04-test

使用下面的命令进入容器的bash。其中 -it 是交互,bd7 是容器的ID的前三位,bash 是要执行的命令。

1
docker exec -it bd7 bash

进去以后就进行正常的配置了,需要什么安装什么。需要注意的是,此时尝试 apt 安装会无法安装,需要先 update。

1
apt-get update

然后安装软件,这里因为默认的Image太轻量级了,什么都没有,需要一点一点安装。

1
apt-get install vim

接下来配置源

1
vi /etc/apt/sources.list

结果不知道为什么清华tuna的景象配置会报错。

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
27
28
29
30
31
32
33
34
root@bd7e12345678:/# apt update
Ign:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic InRelease
Ign:2 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-updates InRelease
Ign:3 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-backports InRelease
Ign:4 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-security InRelease
Err:5 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic Release
Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown. Could not handshake: Error in the certificate verification. [IP: 101.6.15.130 443]
Err:6 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-updates Release
Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown. Could not handshake: Error in the certificate verification. [IP: 101.6.15.130 443]
Err:7 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-backports Release
Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown. Could not handshake: Error in the certificate verification. [IP: 101.6.15.130 443]
Err:8 https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-security Release
Certificate verification failed: The certificate is NOT trusted. The certificate issuer is unknown. Could not handshake: Error in the certificate verification. [IP: 101.6.15.130 443]
Reading package lists... Done
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/bionic/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/bionic-updates/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/bionic-backports/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/bionic-security/InRelease: No system certificates available. Try installing ca-certificates.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/bionic/Release: No system certificates available. Try installing ca-certificates.
E: The repository 'https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/bionic-updates/Release: No system certificates available. Try installing ca-certificates.
E: The repository 'https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-updates Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/bionic-backports/Release: No system certificates available. Try installing ca-certificates.
E: The repository 'https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-backports Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
W: https://mirrors.tuna.tsinghua.edu.cn/ubuntu/dists/bionic-security/Release: No system certificates available. Try installing ca-certificates.
E: The repository 'https://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-security Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

简单搜了一下没找到解决方案,但是换成163的源好了。

接下来安装其他的包。

1
2
3
4
$ apt install openssh-server
$ apt install sudo
$ apt install dialog
$ apt install gcc

安装完毕后 exit 命令退出容器。

为了便于之后新建容器,不至于每次都要安装ssh等,将当前容器保存镜像。-a 表示作者,-m 表示附带信息,后面bd7 是容器的ID前三位,最后一部分是新建镜像的名字与TAG。

1
$ docker commit -a "c405_whl" -m "Added ssh and gcc, 163 mirror configured" bd7 imageToCreate:tagToCreate

使用 docker images 查看是否新建成功。

生成容器并配置用户

接下来用同样的方法新建第二个容器,但此时使用之前保存的镜像新建容器。

1
docker run -it -p 40501:22 --name Ubuntu-18.04-XXX --restart=always -d imageToCreate:tagToCreate bash

进入容器命令:

1
docker exec -it bd7 bash

进入容器后,新建用户,然后将其加入管理员。最后启动 ssh 服务器。

1
2
3
adduser username
adduser username sudo
service ssh start

通过子进程 PID 查询容器 ID

Reference:https://stackoverflow.com/questions/24406743/coreos-get-docker-container-name-by-pid

使用 cat /proc/<process-pid>/cgroup命令确认进程所属的容器 ID。

然后使用下面的命令查询容器对应的容器名称。注意将 ${containerId} 修改成前面查询到的容器ID。(其实只用输入前3个字符基本就可以了,因为一般通过前三个字符就能唯一确定一个容器)

1
docker inspect --format '{{.Name}}' "${containerId}" | sed 's/^\///'