欢迎来到赛兔子家园

docker容器管理

docke容器管理

docker run 创建+启动

docker run 镜像名,如果镜像不存在本地,则会在线去下载该镜像

注意:容器内的进行必须处于前台运行状态,否则容器就会直接退出,自己部署一个容器运行,命令不得后天运行,前台运行即可。

如果容器内,什么事也没有做,容器也会挂掉,容器内必须有一个进程在前台运行。

容器使用

运行一个挂掉的容器

docker run centos 该写法会产生多条独立的容器记录,且容器内没有程序在跑,因此容器直接挂掉

[root@localhost ~]# docker run 300e315adb2f
[root@localhost ~]# docker ps -a # 执行一次容器就会生成一条记录
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS                         PORTS                               NAMES
3ecbfde26ff6   300e315adb2f   "/bin/bash"              9 seconds ago   Exited (0) 8 seconds ago                                           jolly_montalcini
c18a45341b9e   nginx          "/docker-entrypoint.…"   2 minutes ago   Exited (0) About a minute ago                                      eloquent_robinson
3bfef5025609   nginx          "/docker-entrypoint.…"   5 hours ago     Up 5 hours                     0.0.0.0:80->80/tcp, :::80->80/tcp   keen_wing
[root@localhost ~]# 

运行容器且进入容器内执行某个命令

docker run -it centos bash

[root@localhost ~]# docker run -it centos bash
[root@a31c419ecec4 /]# cat /etc/redhat-release
CentOS Linux release 8.3.2011

开启一个容器,让它运行某个程序,属于前台运行,会卡主一个终端

[root@localhost ~]# docker run centos ping baidu.com

[root@localhost ~]# docker run centos ping baidu.com
PING baidu.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=46 time=50.8 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=2 ttl=46 time=50.7 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=3 ttl=46 time=53.6 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=4 ttl=46 time=53.8 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=5 ttl=46 time=53.6 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=6 ttl=46 time=54.3 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=7 ttl=46 time=53.8 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=8 ttl=46 time=55.3 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=9 ttl=46 time=53.8 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=10 ttl=46 time=53.6 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=11 ttl=46 time=55.2 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=12 ttl=46 time=57.8 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=13 ttl=46 time=56.4 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=14 ttl=46 time=54.10 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=15 ttl=46 time=57.1 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=16 ttl=46 time=52.4 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=17 ttl=46 time=63.10 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=18 ttl=46 time=53.7 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=19 ttl=46 time=51.0 ms
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=20 ttl=46 time=51.3 ms
^C
--- baidu.com ping statistics ---

运行一个活着的容器,通过docker ps能看到的容器

-d 参数 ,让容器在后台跑着(针对宿主机)

[root@localhost ~]# docker run -d centos ping baidu.com
00be295bf1d1ced0b80cc1982db1469fc133a5ef75d6c53cd001b626d9ef8c3d

docker ps查看进程状态

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND             CREATED          STATUS          PORTS      NAMES
00be295bf1d1   centos    "ping baidu.com"    36 seconds ago   Up 35 seconds      
丰富docker运行的参数

-rm 容器挂掉后自动被删除

[root@localhost ~]# docker run -d --rm centos

--name 容器起个名字

[root@localhost ~]# docker run -d --rm --name tian centos ping baidu.com

查看容器日志

docker logs 容器id

-f 实时日志

查看id=3bfef5025609nginx日志命令:docker logs 3bf

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS                               NAMES
3bfef5025609   nginx     "/docker-entrypoint.…"   5 hours ago   Up 5 hours   0.0.0.0:80->80/tcp, :::80->80/tcp   keen_wing
[root@localhost ~]# docker logs 3bf
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/09/05 10:26:03 [notice] 1#1: using the "epoll" event method

查看实时日志命令:

docker logs -f  3df

[root@localhost ~]# docker logs -f  3bf
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh

查看最新的5条日志:

[root@localhost ~]# docker logs 3bf | tail -5

进入正在运行的容器空间内

进入id=3bfef5025609的nginx容器内

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS   NAMES
3bfef5025609   nginx     "/docker-entrypoint.…" 

[root@localhost ~]# docker exec -it 3bf bash
root@3bfef5025609:/#

查看容器的详细信息,用于高级调试

docker container inspect 容器id

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS                              NAMES
3bfef5025609   nginx     "/docker-entrypoint.…"   5 hours ago   Up 5 hours   0.0.0.0:80->80/tcp, :::80->80/tcp   keen_wing
[root@localhost ~]# docker container inspect 3bf

容器的端口映射

后台运行nginx容器,起个名字。端口映射宿主机的80端口,访问到容器内的80端口

docker run -d --name tian_nginx  -p 80:80 nginx

运行后查看

[root@localhost ~]# clear
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS                               NAMES
3bfef5025609   nginx     "/docker-entrypoint.…"   5 hours ago   Up 5 hours   0.0.0.0:80->80/tcp, :::80->80/tcp   keen_wing

查看容器转发后的端口情况:

docker port id

[root@localhost ~]# docker port 3bf
80/tcp -> 0.0.0.0:80
80/tcp -> :::80

随机端口映射

-P  随机访问一个宿主机的空闲端口,映射到容器内打开的端口

docker run -d --name tian_nginx_random  -P nginx

容器提交

运行centos,在容器内安装vim,然后提交新的镜像。

新镜像,再运行默认就包含了vim。

运行容器:docker run -it centos bash

root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
redis        latest    02c7f2054405   2 days ago     105MB
nginx        latest    822b7ec2aaf2   2 days ago     133MB
ubuntu       latest    fb52e22af1b0   5 days ago     72.8MB
nginx        <none>    08b152afcfae   6 weeks ago    133MB
centos       latest    300e315adb2f   9 months ago   209MB
[root@localhost ~]# docker run -it  centos bash
[root@ca36d58d90e8 /]# vim
bash: vim: command not found
[root@ca36d58d90e8 /]# yum install vim

提交容器

docker commit 容器id  新容器名

[root@localhost ~]# docker commit ca3 tian/centos-vim
sha256:d9589ec2fbbbabfa4d4eb775435968a75f75900593d20eb84bc90b90f511c734
[root@localhost ~]# docker images
REPOSITORY        TAG       IMAGE ID       CREATED          SIZE
tian/centos-vim   latest    d9589ec2fbbb   11 seconds ago   277MB
redis             latest    02c7f2054405   2 days ago       105MB
nginx             latest    822b7ec2aaf2   2 days ago       133MB
ubuntu            latest    fb52e22af1b0   5 days ago       72.8MB
nginx             <none>    08b152afcfae   6 weeks ago      133MB
centos            latest    300e315adb2f   9 months ago     209MB
[root@localhost ~]# 

 

posted on 2021-09-05 18:55  赛兔子  阅读(115)  评论(0编辑  收藏  举报

导航