1.新建容器
可以使用docker create 命令新建一个容器,例如:
sudo docker create -it ubuntu:latest
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
445a6a12be2b: Pull complete
Digest: sha256:aabed3296a3d45cede1dc866a24476c4d7e093aa806263c27ddaadbdce3c1054
Status: Downloaded newer image for ubuntu:latest
452c370aa4fb62e0eb2aaf993170525e573ac064987a455ef2aed83bd144462f
sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
452c370aa4fb ubuntu:latest "/bin/bash" 49 seconds ago Created confident_bell
04cc20284978 hello-world "/hello" 15 minutes ago Exited (0) 15 minutes ago happy_moore
5a76bd72c898 hello-world "/hello" 15 minutes ago Exited (0) 15 minutes ago mystifying_neumann
232cb88ba121 hello-world "/hello" 16 minutes ago Exited (0) 16 minutes ago flamboyant_easley
83284183907f hello-world "/hello" 16 minutes ago Exited (0) 16 minutes ago flamboyant_grothendieck
使用docker create命令新建的容器处于停止状态,可以使用docker start 命令来启动。
由于容器是整个Docker技术栈的核心,create命令和后续的run命令支持的选项十分复杂,需要在实践中不断体会。
选项主要包括:
1.容器运行模式相关
2.容器环境配置相关
3.与容器资源限制和安全保护相关
create 除了ubuntu:latest 这个之外的都是失败的
2.启动容器
使用docker start 命令来启动一个创建的容器。例如,启动刚创建的ubuntu容器:
(base) tieren@tieren-OptiPlex-7000:~$ sudo docker start ubuntu:latest
Error response from daemon: No such container: ubuntu:latest
Error: failed to start containers: ubuntu:latest
(base) tieren@tieren-OptiPlex-7000:~$ sudo docker start 452c370aa4fb
452c370aa4fb
(base) tieren@tieren-OptiPlex-7000:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
452c370aa4fb ubuntu:latest "/bin/bash" 6 minutes ago Up 7 seconds confident_bell
04cc20284978 hello-world "/hello" 21 minutes ago Exited (0) 21 minutes ago happy_moore
5a76bd72c898 hello-world "/hello" 21 minutes ago Exited (0) 21 minutes ago mystifying_neumann
232cb88ba121 hello-world "/hello" 22 minutes ago Exited (0) 22 minutes ago flamboyant_easley
83284183907f hello-world "/hello" 22 minutes ago Exited (0) 22 minutes ago flamboyant_grothendieck
此时,通过docker ps 命令, 可以查看到一个运行中的容器:
3.新建并启动容器
除了创建容器后通过start命令来启动,也可以直接新建并启动容器。
所需要的命令主要为docker run , 等价于先执行docker create命令,在执行docker start命令。
例如,下面命令输出一个“hello world”,之后容器自动终止:
(base) tieren@tieren-OptiPlex-7000:~$ sudo docker run ubuntu /bin/echo 'hello world'
hello world
(base) tieren@tieren-OptiPlex-7000:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
60c42569af5e ubuntu "/bin/echo 'hello wo…" 19 seconds ago Exited (0) 18 seconds ago modest_brattain
452c370aa4fb ubuntu:latest "/bin/bash" 10 minutes ago Up 4 minutes confident_bell
04cc20284978 hello-world "/hello" 25 minutes ago Exited (0) 25 minutes ago happy_moore
5a76bd72c898 hello-world "/hello" 25 minutes ago Exited (0) 25 minutes ago mystifying_neumann
232cb88ba121 hello-world "/hello" 26 minutes ago Exited (0) 26 minutes ago flamboyant_easley
83284183907f hello-world "/hello" 26 minutes ago Exited (0) 26 minutes ago flamboyant_grothendieck
当利用docker run来创建启动容器时,Docker 在后台运行的标准操作包括:
1.检查本地是否存在指定的镜像,不存在就从公有仓库下载;
2.利用镜像创建一个容器,并启动该容器;
3.分配一个文件系统给容器,并在只读的镜像层外挂载一层可读写层;
4.从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去;
5.从网桥的地址池配置一个IP地址给容器;
6.执行用户指定的应用程序;
7.执行完毕后容器被自动终止。
下面的命令启动一个bash终端,允许用户交互:
sudo docker run -it ubuntu:22.04 /bin/bash
Unable to find image 'ubuntu:22.04' locally
22.04: Pulling from library/ubuntu
Digest: sha256:aabed3296a3d45cede1dc866a24476c4d7e093aa806263c27ddaadbdce3c1054
Status: Downloaded newer image for ubuntu:22.04
root@f395b32ae39e:/# sudo docker ps -a
bash: sudo: command not found
root@f395b32ae39e:/# ll
total 56
drwxr-xr-x 1 root root 4096 Sep 27 09:08 ./
drwxr-xr-x 1 root root 4096 Sep 27 09:08 ../
-rwxr-xr-x 1 root root 0 Sep 27 09:08 .dockerenv*
lrwxrwxrwx 1 root root 7 Aug 16 02:02 bin -> usr/bin/
drwxr-xr-x 2 root root 4096 Apr 18 2022 boot/
drwxr-xr-x 5 root root 360 Sep 27 09:08 dev/
drwxr-xr-x 1 root root 4096 Sep 27 09:08 etc/
drwxr-xr-x 2 root root 4096 Apr 18 2022 home/
lrwxrwxrwx 1 root root 7 Aug 16 02:02 lib -> usr/lib/
lrwxrwxrwx 1 root root 9 Aug 16 02:02 lib32 -> usr/lib32/
lrwxrwxrwx 1 root root 9 Aug 16 02:02 lib64 -> usr/lib64/
lrwxrwxrwx 1 root root 10 Aug 16 02:02 libx32 -> usr/libx32/
drwxr-xr-x 2 root root 4096 Aug 16 02:03 media/
drwxr-xr-x 2 root root 4096 Aug 16 02:03 mnt/
drwxr-xr-x 2 root root 4096 Aug 16 02:03 opt/
dr-xr-xr-x 523 root root 0 Sep 27 09:08 proc/
drwx------ 2 root root 4096 Aug 16 02:06 root/
drwxr-xr-x 5 root root 4096 Aug 16 02:06 run/
lrwxrwxrwx 1 root root 8 Aug 16 02:02 sbin -> usr/sbin/
drwxr-xr-x 2 root root 4096 Aug 16 02:03 srv/
dr-xr-xr-x 13 root root 0 Sep 27 09:08 sys/
drwxrwxrwt 2 root root 4096 Aug 16 02:06 tmp/
drwxr-xr-x 14 root root 4096 Aug 16 02:03 usr/
drwxr-xr-x 11 root root 4096 Aug 16 02:06 var/
root@f395b32ae39e:/# ps
PID TTY TIME CMD
1 pts/0 00:00:00 bash
11 pts/0 00:00:00 ps
root@f395b32ae39e:/# pwd
/
root@f395b32ae39e:/#
root@f395b32ae39e:/#
exit
(base) tieren@tieren-OptiPlex-7000:~$
对于所创建的bash容器,当用户使用exit命令退出bash进程之后,容器也会自动退出。这是因为对于容器来说,当其中的应用退出后,容器的使命完成,也就没有继续运行的必要了。
可以使用docker wait来等待容器退出,并打印出返回结果。
docker wait教程
docker wait 命令可以用于阻塞一个或多个 Docker容器 直到容器停止,然后打印退出代码。
docker wait命令后面的 CONTAINER 可以是容器Id,或者是容器名。
docker wait语法
haicoder(www.haicoder.net)# docker wait [OPTIONS] CONTAINER [CONTAINER...]