Docker容器管理总结

Posted jks212454

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker容器管理总结相关的知识,希望对你有一定的参考价值。

一、Docker容器理解

1.Docker的核心概念

容器是Docker的核心概念,容器是一个或一组应用。

2.容器的运行状态

①docker利用容器容器运行应用程序
②容器是镜像的运行实例,可以被stop、strat、rm、run
③每个容器都是相互隔离的,保证平台的安全
④容器可以看作是一个简易版的Linux环境(有root权限,进程,用户空间,网络)
⑤镜像时只读的,容器在启动的时候创建一层可写层。

二、Dockerfile理解

dockerfile面向开发,docker image作为交付标准,docker container涉及部署和运维,三者结合起来完成docker体系。

FROM ubuntu:14.04  #选择基础镜像
ADD run.sh  # 添加文件镜像,这一层镜像只有一个内容,就是这个文件
VOLUME /data # 设定存储目录,并未添加文件,只是更新了镜像的json文件,便于启动时候读取该层信息
CMD ["./run.sh"] # 更新json文件,设定程序入口

三、容器使用及操作

1.进入容器空间内

[root@node1 ~]# docker images
REPOSITORY                   TAG        IMAGE ID       CREATED         SIZE
centos_volume                latest     6cc85e0cee71   25 hours ago    203MB
centos_curl_new              latest     d61875c20211   26 hours ago    471MB
centos_curl                  latest     ded6737bf511   26 hours ago    471MB
my_nginx                     latest     ecda82f680dc   35 hours ago    133MB
ittest/centos-vim-7.8.2003   latest     91e91af846d6   46 hours ago    386MB
ubuntu                       latest     c29284518f49   10 days ago     72.8MB
nginx                        latest     4cdc5dd7eaad   2 weeks ago     133MB
redis                        latest     08502081bff6   4 weeks ago     105MB
fedora                       latest     055b2e5ebc94   2 months ago    178MB
centos                       latest     300e315adb2f   7 months ago    209MB
centos                       7.8.2003   afb6fca791e0   14 months ago   203MB
opensuse                     latest     d9e50bf28896   2 years ago     111MB
[root@node1 ~]# docker run -it ubuntu bash
root@664e138829e0:/# cat /etc/os-release 
NAME="Ubuntu"
VERSION="20.04.2 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.2 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
root@664e138829e0:/# 

2.后台运行容器

[root@node1 ~]# docker run  --name it_nginx  -d --restart=always -p 8080:80 nginx
6013d118362f16f746810c1297d9136ffb28d532dca2bf449e4c4040acde5b7c
[root@node1 ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                   NAMES
6013d118362f   nginx      "/docker-entrypoint.…"   3 seconds ago   Up 2 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   it_nginx
5fbf299d43ea   my_nginx   "/docker-entrypoint.…"   35 hours ago    Up 35 hours    0.0.0.0:80->80/tcp, :::80->80/tcp       eager_pike

3.前台运行容器(针对宿主机)

①前台运行容器

[root@node1 ~]# docker run -P nginx
/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/07/24 14:50:03 [notice] 1#1: using the "epoll" event method
2021/07/24 14:50:03 [notice] 1#1: nginx/1.21.1
2021/07/24 14:50:03 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6) 
2021/07/24 14:50:03 [notice] 1#1: OS: Linux 4.18.0-80.el8.x86_64
2021/07/24 14:50:03 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/07/24 14:50:03 [notice] 1#1: start worker processes
2021/07/24 14:50:03 [notice] 1#1: start worker process 31

② 新开终端查看容器状态

[root@node1 ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED              STATUS              PORTS                                     NAMES
2b1f409cc1ca   nginx      "/docker-entrypoint.…"   About a minute ago   Up About a minute   0.0.0.0:49154->80/tcp, :::49154->80/tcp   hardcore_hopper
6013d118362f   nginx      "/docker-entrypoint.…"   17 minutes ago       Up 17 minutes       0.0.0.0:8080->80/tcp, :::8080->80/tcp     it_nginx
5fbf299d43ea   my_nginx   "/docker-entrypoint.…"   35 hours ago         Up 35 hours         0.0.0.0:80->80/tcp, :::80->80/tcp         eager_pike

③访问web

④前台nginx日志查看

4.容器运行原理

docker后台的操作默认流程:
①检查本地是否有该镜像,没有就下载
②利用镜像创建且启动一个容器
③分配容器文件系统,在只读的镜像层挂载读写层
④宿主机的网桥接口会分配一个虚拟接口到容器中
⑤容器获得地址池的IP地址
⑥容器获得地址池的IP地址
⑦执行用户指定的程序
⑧若程序没有进程在运行。容器执行完毕后立即终止

5.查看容器内日志

[root@node1 ~]# docker logs -f 6013d118362f 
/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

四、容器常用命令

1.查看容器状态

[root@node1 ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                   NAMES
6013d118362f   nginx      "/docker-entrypoint.…"   25 minutes ago   Up 25 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   it_nginx
5fbf299d43ea   my_nginx   "/docker-entrypoint.…"   35 hours ago     Up 35 hours     0.0.0.0:80->80/tcp, :::80->80/tcp       eager_pike
[root@node1 ~]# docker container  ls
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                   NAMES
6013d118362f   nginx      "/docker-entrypoint.…"   25 minutes ago   Up 25 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   it_nginx
5fbf299d43ea   my_nginx   "/docker-entrypoint.…"   35 hours ago     Up 35 hours     0.0.0.0:80->80/tcp, :::80->80/tcp       eager_pike
[root@node1 ~]# docker ps -a
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS                      PORTS                                   NAMES
2b1f409cc1ca   nginx             "/docker-entrypoint.…"   8 minutes ago    Exited (0) 4 minutes ago                                            hardcore_hopper
6013d118362f   nginx             "/docker-entrypoint.…"   25 minutes ago   Up 25 minutes               0.0.0.0:8080->80/tcp, :::8080->80/tcp   it_nginx
664e138829e0   ubuntu            "bash"                   28 minutes ago   Exited (0) 26 minutes ago                                           elastic_herschel
447041a55a0d   centos_volume     "/bin/bash"              26 hours ago     Exited (0) 26 hours ago                                             exciting_burnell
3e02f032ab3d   centos_curl_new   "curl -s cip.cc -I"      26 hours ago     Exited (0) 26 hours ago                                             ecstatic_villani
76df5f73a16d   centos_curl       "pwd"                    26 hours ago     Exited (0) 26 hours ago                                             recursing_ride
a03d7954369c   centos_curl       "-I"                     26 hours ago     Created                                                             inspiring_brahmagupta
82c511283e6d   centos_curl       "curl -s cip.cc"         26 hours ago     Exited (0) 26 hours ago                                             adoring_hermann
5fbf299d43ea   my_nginx          "/docker-entrypoint.…"   35 hours ago     Up 35 hours                 0.0.0.0:80->80/tcp, :::80->80/tcp       eager_pike

2.停止启动容器

[root@node1 ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                                   NAMES
6013d118362f   nginx      "/docker-entrypoint.…"   26 minutes ago   Up 26 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   it_nginx
5fbf299d43ea   my_nginx   "/docker-entrypoint.…"   35 hours ago     Up 35 hours     0.0.0.0:80->80/tcp, :::80->80/tcp       eager_pike
[root@node1 ~]# docker stop 6013d118362f 
6013d118362f
[root@node1 ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED        STATUS        PORTS                               NAMES
5fbf299d43ea   my_nginx   "/docker-entrypoint.…"   35 hours ago   Up 35 hours   0.0.0.0:80->80/tcp, :::80->80/tcp   eager_pike
[root@node1 ~]# docker start 6013d118362f 
6013d118362f
[root@node1 ~]# docker ps
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS         PORTS                                   NAMES
6013d118362f   nginx      "/docker-entrypoint.…"   26 minutes ago   Up 2 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   it_nginx
5fbf299d43ea   my_nginx   "/docker-entrypoint.…"   35 hours ago     Up 35 hours    0.0.0.0:80->80/tcp, :::80->80/tcp       eager_pike

3.进入容器内

[root@node1 ~]# docker exec  -it 6013d118362f bash
root@6013d118362f:/# exit
exit
[root@node1 ~]# 

4.删除容器

docker rm 容器ID
docker rm `docker ps -aq`
docker rm -f 容器ID 强制杀死容器

5.查看容器进程信息

[root@node1 ~]# docker top 6013d118362f 
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                32218               32200               0                   22:59               ?                   00:00:00            nginx: master process nginx -g daemon off;
101                 32263               32218               0                   22:59               ?                   00:00:00            nginx: worker process
[root@node1 ~]# ps aux |grep 32218
root      32218  0.0  0.2  10652  5812 ?        Ss   22:59   0:00 nginx: master process nginx -g daemon off;
root      32408  0.0  0.0  12112  1072 pts/0    R+   23:04   0:00 grep --color=auto 32218

6.查看容器资源信息

[root@node1 ~]# docker stats 6013d118362f
CONTAINER ID   NAME       CPU %     MEM USAGE / LIMIT    MEM %     NET I/O       BLOCK I/O     PIDS
6013d118362f   it_nginx   0.00%     2.562MiB / 1.92GiB   0.13%     2.37kB / 0B   1.37MB / 0B   2

7.查看容器具体信息

[root@node1 ~]# 
[root@node1 ~]# docker inspect  6013d118362f

8.获取容器内地址

[root@node1 ~]# docker inspect  --format '{{.NetworkSettings.IPAddress}}' 6013d118362f
172.17.0.3

以上是关于Docker容器管理总结的主要内容,如果未能解决你的问题,请参考以下文章

Docker基本管理(容器及镜像操作)

Docker学习总结(68)—— Docker 数据卷相关知识总结

Docker删除报错:Error response from daemon: conflict: unable to delete 08b152afcfae (must be forced)(代码片段

docker学习总结六

Docker学习总结(60)——Docker-Compose 基础知识回顾总结

Docker学习总结(60)——Docker-Compose 基础知识回顾总结