1.5 Docker 容器管理
之前已经用过已有的镜像启动容器了,还可以正式的创建一个容器,但是不启动。
docker create命令,原有的docker run命令用来创建一个container并创建一个线程来执行它,而docker create命令只是单纯地创建一个container,并不执行,如果我们需要启动这个container的话,用docekr start命令即可。这样我们可以我们可以预先准备好多个container,配置好相应的挂载存储和端口映射,并在需要的时候才启动这些container,而在1.2版本以前,我们只能傻傻地用docker run命令启动一个container,然后用docker stop命令来停止它,这样做显然很麻烦。
root@ubuntu:~# docker create -it ubuntu_with_net_and_wget
3556f94292bd67db3017cd274227811cc27364441f2f5741ea61a5a74188baba
root@ubuntu:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3556f94292bd ubuntu_with_net_and_wget:latest "/bin/bash" 16 seconds ago focused_sammet
611c89a8028f ubuntu-16.04-x86_64:latest "/bin/bash" 34 minutes ago Exited (0) 34 minutes ago sharp_sammet
81678fc4d6c7 ubuntu:latest "/bin/bash" 3 hours ago Up 2 hours happy_newton
root@ubuntu:~# docker start 3556f94292bd
3556f94292bd
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3556f94292bd ubuntu_with_net_and_wget:latest "/bin/bash" About a minute ago Up 10 seconds focused_sammet
81678fc4d6c7 ubuntu:latest "/bin/bash" 3 hours ago Up 2 hours happy_newton
对于docker start,有start就有 stop 和 restart。
docker run -it ubuntu /bin/bash 以某一镜像开启容器,这样进入一个虚拟终端中,我们可以运行一些命令,使用命令exit或者 Ctrl+D退出该bash,当退出后这个容器也会停止。
docker run -d 可以让容器在后台运行
root@ubuntu:~# docker run -itd ubuntu:v1 /bin/bash
c94dfc94f26f0eb8de00b7d634ef732f6f64057efb994040574ce23733314ec1
root@ubuntu:~# docker ps //并没有进入docker界面
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c94dfc94f26f ubuntu:v1 "/bin/bash" 6 seconds ago Up 5 seconds desperate_poincare
3556f94292bd ubuntu_with_net_and_wget:latest "/bin/bash" 12 minutes ago Up 11 minutes focused_sammet
81678fc4d6c7 ubuntu:latest "/bin/bash" 3 hours ago Up 2 hours happy_newton
进入该后台运行的容器
# docker exec -it container_id bash //可以临时打开一个虚拟终端,并且exit后,容器依然运行着
root@ubuntu:~# docker exec -it c94dfc94f26f /bin/bash
root@c94dfc94f26f:/#
root@c94dfc94f26f:/# exitroot@ubuntu:~#
root@ubuntu:~#
还可用 docker attach 命令进入一个后台运行的容器
# docker attach container_id //这种方式想要退出终端,就得用exit了,这样容器也就退出了
# docker attach c94dfc94f26f /bin/bash
后台运行容器并执行命令
root@ubuntu:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3556f94292bd ubuntu_with_net_and_wget:latest "/bin/bash" 30 minutes ago Up 29 minutes focused_sammet
81678fc4d6c7 ubuntu:latest "/bin/bash" 3 hours ago Up 3 hours
root@ubuntu:~# docker run -d ubuntu:v1 bash -c "while :; do echo 123;sleep 1;done"
48b4dc2675b263edff518f260517fd734ebe83605f68a6d3b31036fdf04e9742
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
48b4dc2675b2 ubuntu:v1 "bash -c 'while :; d 14 seconds ago Up 14 seconds sad_banach
3556f94292bd ubuntu_with_net_and_wget:latest "/bin/bash" 32 minutes ago Up 31 minutes focused_sammet
81678fc4d6c7 ubuntu:latest "/bin/bash" 3 hours ago Up 3 hours happy_newton
docker logs 可以获取到容器的运行历史信息,用法如下
# docker logs container_id
docker logs命令查看docker终端输出
root@ubuntu:~# docker logs 48b4dc2675b2
123
123
123
123
123
123
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3556f94292bd ubuntu_with_net_and_wget:latest "/bin/bash" 35 minutes ago Up 33 minutes focused_sammet
81678fc4d6c7 ubuntu:latest "/bin/bash" 3 hours ago Up 3 hours happy_newton
我们可以看到其中有NAMES这一列,这一项可以再运行容器时就指定
root@ubuntu:~# docker run -itd --name name1 ubuntu:v1 /bin/bash
6dec20a19497b255a58521190655688c382ebddebd889218347128b72e0eee6e
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6dec20a19497 ubuntu:v1 "/bin/bash" 7 seconds ago Up 6 seconds name1
3556f94292bd ubuntu_with_net_and_wget:latest "/bin/bash" 40 minutes ago Up 39 minutes focused_sammet
81678fc4d6c7 ubuntu:latest "/bin/bash" 3 hours ago Up 3 hours happy_newton
删除一个容器
#docker rm 6532 //删除一个已停止的容器
#docker rm -f 6532 //删除一个任意状态的容器
还可以在以run形式运行容器时加入- -rm选项,使其退出关闭时就自动删除,不能与-d选项一起用
root@ubuntu:~# docker run -it --rm ubuntu:v1 bash -c "sleep 30"
root@ubuntu:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6dec20a19497 ubuntu:v1 "/bin/bash" 16 hours ago Up 16 hours name1
3556f94292bd ubuntu_with_net_and_wget:latest "/bin/bash" 16 hours ago Up 16 hours focused_sammet
81678fc4d6c7 ubuntu:latest "/bin/bash" 19 hours ago Up 19 hours happy_newton
容器迁移
容器导出
# docker export container_id > file.tar //导出容器,可以迁移到其他机器上,需要导入
root@ubuntu:~# docker export 6dec20a19497 > ubuntu_v1.tar
root@ubuntu:~# ls
ubuntu-16.04-x86_64.tar.gz ubuntu_v1.tar ubuntu_with_net_and_wget.tar
root@ubuntu:~# du -sh ubuntu_v1.tar
111M ubuntu_v1.tar
容器导入
先导入为镜像
root@ubuntu:~# cat ubuntu_v1.tar | docker import - ubuntu_v2
7ccdacdace595efda823ec8baa304648810ac2786c9906edf29596dd2dd2b144
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu_v2 latest 7ccdacdace59 13 seconds ago 110.3 MB
ubuntu-16.04-x86_64 latest be6ed5c5eae2 18 hours ago 500.1 MB
再去以这个镜像去启动容器
root@ubuntu:~# docker run -itd ubuntu_v2 bash
f369b57e550da53a3e0539e5c033aefcd90fef61305fc2eaed925853e5b56d74
评论区