Centos7 docker基础2-容器
一、创建容器
docker create -it centos
#-i 即使未连接,也保持stdin打开
#-t 分配给伪tty
#create 只创建 不包含自动启动容器
#本地没有centos的镜像,将自动下载
[root@centos7 ~]# docker create -it centos
Unable to find image 'centos:latest' locally
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
8ba884070f61: Pull complete
Digest: sha256:a799dd8a2ded4a83484bbae769d97655392b3f86533ceb7dd96bbac929809f3c
Status: Downloaded newer image for docker.io/centos:latest
a33622e3f977f846aac151b0e0250d42d37f6f7276c76208c16fe8faf9578a40
[root@centos7 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu 14.04 40446eeefc1a About an hour ago 215 MB
docker.io/rdocker8/test latest 8e861c8118a3 2 hours ago 64.2 MB
docker.io/ubuntu latest 4c108a37151f 4 weeks ago 64.2 MB
docker.io/centos latest 9f38484d220f 4 months ago 202 MB #下载了我好久
[root@centos7 ~]#
Create a new container
[root@centos7 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a33622e3f977 centos "/bin/bash" 22 minutes ago Created unruffled_keller
0b2d5edef671 ubuntu:latest "/bin/bash" 2 hours ago Exited (0) 2 hours ago optimistic_nightingale
[root@centos7 ~]#
容器内操作
输出一个“hello world”
docker run centos /bin/echo "hello world"
[root@centos7 ~]# docker run centos /bin/echo "hello world"
hello world #运行完就直接exit了
[root@centos7 ~]#
进入交互tty
docker run -t -i centos:latest /bin/bash
[root@centos7 ~]# docker run -t -i centos:latest /bin/bash
[root@23b2ea559a54 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 08:48 ? 00:00:00 /bin/bash
root 14 1 0 08:48 ? 00:00:00 ps -ef
[root@23b2ea559a54 /]# ll -h /
total 12K
-rw-r--r-- 1 root root 12K Mar 5 17:36 anaconda-post.log
lrwxrwxrwx 1 root root 7 Mar 5 17:34 bin -> usr/bin
drwxr-xr-x 5 root root 360 Jul 19 08:48 dev
drwxr-xr-x 1 root root 66 Jul 19 08:48 etc
drwxr-xr-x 2 root root 6 Apr 11 2018 home
lrwxrwxrwx 1 root root 7 Mar 5 17:34 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Mar 5 17:34 lib64 -> usr/lib64
drwxr-xr-x 2 root root 6 Apr 11 2018 media
drwxr-xr-x 2 root root 6 Apr 11 2018 mnt
drwxr-xr-x 2 root root 6 Apr 11 2018 opt
dr-xr-xr-x 110 root root 0 Jul 19 08:48 proc
dr-xr-x--- 2 root root 114 Mar 5 17:36 root
drwxr-xr-x 1 root root 21 Jul 19 08:48 run
lrwxrwxrwx 1 root root 8 Mar 5 17:34 sbin -> usr/sbin
drwxr-xr-x 2 root root 6 Apr 11 2018 srv
dr-xr-xr-x 13 root root 0 Jul 19 08:40 sys
drwxrwxrwt 7 root root 132 Mar 5 17:36 tmp
drwxr-xr-x 13 root root 155 Mar 5 17:34 usr
drwxr-xr-x 18 root root 238 Mar 5 17:34 var
[root@23b2ea559a54 /]#
按ctrl+d 或 exit 可退出容器
守护态运行
-d:后台运行容器,并返回容器ID
-h “mars”: 指定容器的hostname;
[root@centos7 ~]# docker run -d -h docker001 centos:latest /bin/bash -c "while true;do echo hello world;sleep 1;done"
[root@centos7 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd14a6ab787b centos:latest "/bin/bash -c 'whi..." 22 seconds ago Up 21 seconds nervous_nightingale
查看日志:
docker logs bd14a6ab787b
docker logs -f bd14a6ab787b #加 -f 可以动态监听[root@centos7 ~]# docker logs bd14a6ab787b #容器ID hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world [root@centos7 ~]#
终止守护态的容器
docker stop 容器ID
[root@centos7 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd14a6ab787b centos:latest "/bin/bash -c 'whi..." 23 minutes ago Up 23 minutes nervous_nightingale
23b2ea559a54 centos:latest "/bin/bash" 3 hours ago Up 3 hours hardcore_davinci
[root@centos7 ~]# docker stop bd14a6ab787b #关闭容器
bd14a6ab787b
[root@centos7 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23b2ea559a54 centos:latest "/bin/bash" 3 hours ago Up 3 hours hardcore_davinci
[root@centos7 ~]#
docker ps -a -q
-a:显示所有容器
-q:只显示容器的ID
[root@centos7 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd14a6ab787b centos:latest "/bin/bash -c 'whi..." 28 minutes ago Exited (137) 3 minutes ago nervous_nightingale
23b2ea559a54 centos:latest "/bin/bash" 3 hours ago Up 3 hours hardcore_davinci
f75113ef9dfa centos "/bin/bash" 3 hours ago Exited (0) 3 hours ago thirsty_goodall
b118b57a47db centos "/bin/echo 'hello ..." 4 hours ago Exited (0) 4 hours ago sleepy_hodgkin
a33622e3f977 centos "/bin/bash" 4 hours ago Created unruffled_keller
0b2d5edef671 ubuntu:latest "/bin/bash" 6 hours ago Exited (0) 6 hours ago optimistic_nightingale
[root@centos7 ~]# docker ps -a -q
bd14a6ab787b
23b2ea559a54
f75113ef9dfa
b118b57a47db
a33622e3f977
0b2d5edef671
[root@centos7 ~]#
使用docker start 容器ID 可以启动容器
使用docker restart 容器ID 可以重启容器
二、进入容器:
有三种命令可以进容器:
- docker attach
- docker exec
及nsenter工具 这个要另外装
我先学习exec吧
[root@centos7 /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23b2ea559a54 centos:latest "/bin/bash" 5 hours ago Up 5 hours hardcore_davinci
[root@centos7 /]# docker exec -ti 23b2ea559a54 /bin/bash
[root@23b2ea559a54 /]# cd /
[root@23b2ea559a54 /]# ll
三、删除容器
使用docker rm 容器ID
[root@centos7 /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd14a6ab787b centos:latest "/bin/bash -c 'whi..." About an hour ago Exited (137) About an hour ago nervous_nightingale
23b2ea559a54 centos:latest "/bin/bash" 5 hours ago Up 5 hours hardcore_davinci
f75113ef9dfa centos "/bin/bash" 5 hours ago Exited (0) 5 hours ago thirsty_goodall
b118b57a47db centos "/bin/echo 'hello ..." 5 hours ago Exited (0) 5 hours ago sleepy_hodgkin
a33622e3f977 centos "/bin/bash" 5 hours ago Created unruffled_keller
0b2d5edef671 ubuntu:latest "/bin/bash" 7 hours ago Exited (0) 7 hours ago optimistic_nightingale
[root@centos7 /]# docker rm 0b2d5edef671 #容器没有运行时,可以直接运行这个命令
0b2d5edef671
[root@centos7 /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd14a6ab787b centos:latest "/bin/bash -c 'whi..." About an hour ago Exited (137) About an hour ago nervous_nightingale
23b2ea559a54 centos:latest "/bin/bash" 5 hours ago Up 5 hours hardcore_davinci
f75113ef9dfa centos "/bin/bash" 5 hours ago Exited (0) 5 hours ago thirsty_goodall
b118b57a47db centos "/bin/echo 'hello ..." 5 hours ago Exited (0) 5 hours ago sleepy_hodgkin
a33622e3f977 centos "/bin/bash" 5 hours ago Created unruffled_keller
[root@centos7 /]#
如果正在运行 可以先执行stop操作,再执行rm操作;
强制删除正在运行的容器 要加-f (强制参数)
[root@centos7 /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23b2ea559a54 centos:latest "/bin/bash" 5 hours ago Up 5 hours hardcore_davinci
[root@centos7 /]# docker rm 23b2ea559a54
Error response from daemon: You cannot remove a running container 23b2ea559a540e744787c0c7d5eaf41dba763de678e5fb52d810a4ff7e221027.
Stop the container before attempting removal or use -f #提示你使用-f
[root@centos7 /]# docker rm -f 23b2ea559a54
23b2ea559a54
[root@centos7 /]#
四、导入和导出容器
不管是否运行,都可以使用docker export 执行导出容器
[root@centos7 /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd14a6ab787b centos:latest "/bin/bash -c 'whi..." 2 hours ago Exited (137) About an hour ago nervous_nightingale
f75113ef9dfa centos "/bin/bash" 5 hours ago Exited (0) 5 hours ago thirsty_goodall
b118b57a47db centos "/bin/echo 'hello ..." 5 hours ago Exited (0) 5 hours ago sleepy_hodgkin
a33622e3f977 centos "/bin/bash" 6 hours ago Created unruffled_keller
[root@centos7 /]# docker export bd14a6ab787b > centos_for_stop.tar
[root@centos7 /]# ll -h centos_for_stop.tar
-rw-r--r-- 1 root root 201M 7月 19 22:08 centos_for_stop.tar
[root@centos7 /]#
导入使用 dock import 导入文件 成 镜像???
[root@centos7 /]# cat centos_for_stop.tar | docker import - centos1:v1.0
sha256:f1420ae8ab97a6f6f0615a2c20a90e2ced90ef76b03076b7117fed71dbbda4d1
[root@centos7 /]# docker ps -a #在容器没有看到导入的
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd14a6ab787b centos:latest "/bin/bash -c 'whi..." 2 hours ago Exited (137) About an hour ago nervous_nightingale
f75113ef9dfa centos "/bin/bash" 5 hours ago Exited (0) 5 hours ago thirsty_goodall
b118b57a47db centos "/bin/echo 'hello ..." 5 hours ago Exited (0) 5 hours ago sleepy_hodgkin
a33622e3f977 centos "/bin/bash" 6 hours ago Created unruffled_keller
[root@centos7 /]# docker images #在镜像中 找到导入的centos1
REPOSITORY TAG IMAGE ID CREATED SIZE
centos1 v1.0 f1420ae8ab97 21 seconds ago 202 MB
ubuntu 14.04 40446eeefc1a 7 hours ago 215 MB
docker.io/rdocker8/test latest 8e861c8118a3 8 hours ago 64.2 MB
docker.io/ubuntu latest 4c108a37151f 4 weeks ago 64.2 MB
docker.io/centos latest 9f38484d220f 4 months ago 202 MB
[root@centos7 /]#
看这句话 意思是不管从镜像还是容器导出 导入的都要成镜像
#我以为清空所有容器,将有容器之前的镜像在导入,就会看到之前的容器 看来我想错了。
[root@centos7 /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos1 v1.0 f1420ae8ab97 5 minutes ago 202 MB
ubuntu 14.04 40446eeefc1a 7 hours ago 215 MB
docker.io/rdocker8/test latest 8e861c8118a3 8 hours ago 64.2 MB
docker.io/ubuntu latest 4c108a37151f 4 weeks ago 64.2 MB
docker.io/centos latest 9f38484d220f 4 months ago 202 MB
[root@centos7 /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd14a6ab787b centos:latest "/bin/bash -c 'whi..." 2 hours ago Exited (137) About an hour ago nervous_nightingale
f75113ef9dfa centos "/bin/bash" 5 hours ago Exited (0) 5 hours ago thirsty_goodall
b118b57a47db centos "/bin/echo 'hello ..." 5 hours ago Exited (0) 5 hours ago sleepy_hodgkin
a33622e3f977 centos "/bin/bash" 6 hours ago Created unruffled_keller
[root@centos7 /]# docker save -o testimport.tar docker.io/centos:latest
[root@centos7 /]# ll -h testimport.tar
-rw------- 1 root root 200M 7月 19 22:22 testimport.tar
[root@centos7 /]# docker rm bd14a6ab787b b118b57a47db a33622e3f977 f75113ef9dfa
[root@centos7 /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@centos7 /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos1 v1.0 f1420ae8ab97 8 minutes ago 202 MB
ubuntu 14.04 40446eeefc1a 7 hours ago 215 MB
docker.io/rdocker8/test latest 8e861c8118a3 8 hours ago 64.2 MB
docker.io/ubuntu latest 4c108a37151f 4 weeks ago 64.2 MB
docker.io/centos latest 9f38484d220f 4 months ago 202 MB
[root@centos7 /]# docker image rm 9f38484d220f
Untagged: docker.io/centos:latest
Untagged: docker.io/centos@sha256:a799dd8a2ded4a83484bbae769d97655392b3f86533ceb7dd96bbac929809f3c
Deleted: sha256:9f38484d220fa527b1fb19747638497179500a1bed8bf0498eb788229229e6e1
Deleted: sha256:d69483a6face4499acb974449d1303591fcbb5cdce5420f36f8a6607bda11854
[root@centos7 /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos1 v1.0 f1420ae8ab97 8 minutes ago 202 MB
ubuntu 14.04 40446eeefc1a 7 hours ago 215 MB
docker.io/rdocker8/test latest 8e861c8118a3 8 hours ago 64.2 MB
docker.io/ubuntu latest 4c108a37151f 4 weeks ago 64.2 MB
[root@centos7 /]# docker load < testimport.tar
d69483a6face: Loading layer [==================================================>] 209.5 MB/209.5 MB
Loaded image: docker.io/centos:latest
[root@centos7 /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos1 v1.0 f1420ae8ab97 9 minutes ago 202 MB
ubuntu 14.04 40446eeefc1a 7 hours ago 215 MB
docker.io/rdocker8/test latest 8e861c8118a3 8 hours ago 64.2 MB
docker.io/ubuntu latest 4c108a37151f 4 weeks ago 64.2 MB
docker.io/centos latest 9f38484d220f 4 months ago 202 MB
[root@centos7 /]# docker ps -a #我以为清空所有容器,将有容器之前的镜像在导入,就会看到之前的容器 看来我想错了。
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@centos7 /]#
教程参考于:http://www.bdkyr.com/
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。
文章标题:Centos7 docker基础2-容器
本文作者:wangzhirui
发布时间:2019-08-22, 14:32:45
最后更新:2025-02-27, 02:04:07
原始链接:https://wangzhirui.com/2019/08/22/Centos7-docker基础2-容器/转载请保留原文链接及作者。