Docker容器
Posted 特昂糖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker容器相关的知识,希望对你有一定的参考价值。
容器
一.容器的操作命令
在运行镜像中,后面需要带一些指令的信息,这些指令的信息具体汇总为如下: • -d:后台运行 • -it:交互式命令 • --rm:容器挂掉后自动被删除 • --name:给容器起一个名字 • -p:端口映射
1.查看容器信息
容器信息的查看: #linux docker ps -a | grep 容器ID #windows docker ps -a | findstr 容器ID #查看容器的详细的信息 docker container inspect 容器ID
如:
C:\\Users>docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f6987c8d6ed5 2 days ago 141MB alpine/git latest c6b70534b534 4 weeks ago 27.4MB hello-world latest feb5d9fea6a5 3 months ago 13.3kB centos 7.8.2003 afb6fca791e0 19 months ago 203MB C:\\Users>docker run -it --rm centos Unable to find image \'centos:latest\' locally latest: Pulling from library/centos a1d0c7532777: Pull complete Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 Status: Downloaded newer image for centos:latest [root@68ba367e5b40 /]# date Fri Dec 24 02:21:53 UTC 2021 [root@68ba367e5b40 /]# exit exit C:\\Users>docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f6987c8d6ed5 2 days ago 141MB alpine/git latest c6b70534b534 4 weeks ago 27.4MB hello-world latest feb5d9fea6a5 3 months ago 13.3kB centos latest 5d0da3dc9764 3 months ago 231MB centos 7.8.2003 afb6fca791e0 19 months ago 203MB
2.查看容器日志
1、运行镜像 docker run -it --rm centos 2、查看运行中的容器的ID信息 docker ps -a 3、获取到容器的ID后,查看容器实时的日志信息 docker logs 容器ID 4、比如在Linux中安装vim的编辑器,安装的命令为:yum install -y vim,那么在容器的日志记录中也会实时的显示安装它的全部信息
运行容器
C:\\Users\\特昂糖>docker run -it centos [root@875e8725e5eb /]# date Fri Dec 24 03:02:05 UTC 2021 [root@875e8725e5eb /]# exit exit C:\\Users\\特昂糖>docker run -it --rm centos [root@783a0336bfbe /]# date Fri Dec 24 03:06:21 UTC 2021 [root@783a0336bfbe /]# yum install -y vim Failed to set locale, defaulting to C.UTF-8 CentOS Linux 8 - AppStream 1.6 MB/s | 8.4 MB 00:05 CentOS Linux 8 - BaseOS 32 kB/s | 3.6 MB 01:56 CentOS Linux 8 - Extras 310 B/s | 10 kB 00:34 Dependencies resolved. ======================================================================================================================== Package Architecture Version Repository Size ========================================================================================================================Installing: vim-enhanced x86_64 2:8.0.1763-16.el8 appstream 1.4 M Installing dependencies: gpm-libs x86_64 1.20.7-17.el8 appstream 39 k vim-common x86_64 2:8.0.1763-16.el8 appstream 6.3 M vim-filesystem noarch 2:8.0.1763-16.el8 appstream 49 k which x86_64 2.21-16.el8 baseos 49 k Transaction Summary ========================================================================================================================Install 5 Packages Total download size: 7.8 M Installed size: 30 M Downloading Packages: CentOS Linux 8 - AppStream 194% [==========================================================================(1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm 200 kB/s | 39 kB 00:00 (2/5): vim-filesystem-8.0.1763-16.el8.noarch.rpm 79 kB/s | 49 kB 00:00 (3/5): vim-enhanced-8.0.1763-16.el8.x86_64.rpm 871 kB/s | 1.4 MB 00:01 (4/5): vim-common-8.0.1763-16.el8.x86_64.rpm 1.9 MB/s | 6.3 MB 00:03 (5/5): which-2.21-16.el8.x86_64.rpm 16 kB/s | 49 kB 00:03 ------------------------------------------------------------------------------------------------------------------------Total 1.5 MB/s | 7.8 MB 00:05 warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY CentOS Linux 8 - AppStream 1.5 MB/s | 1.6 kB 00:00 Importing GPG key 0x8483C65D: Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>" Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial Key imported successfully Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : which-2.21-16.el8.x86_64 1/5 Installing : vim-filesystem-2:8.0.1763-16.el8.noarch 2/5 Installing : vim-common-2:8.0.1763-16.el8.x86_64 3/5 Installing : gpm-libs-1.20.7-17.el8.x86_64 4/5 Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64 4/5 Installing : vim-enhanced-2:8.0.1763-16.el8.x86_64 5/5 Running scriptlet: vim-enhanced-2:8.0.1763-16.el8.x86_64 5/5 Running scriptlet: vim-common-2:8.0.1763-16.el8.x86_64 5/5 Verifying : gpm-libs-1.20.7-17.el8.x86_64 1/5 Verifying : vim-common-2:8.0.1763-16.el8.x86_64 2/5 Verifying : vim-enhanced-2:8.0.1763-16.el8.x86_64 3/5 Verifying : vim-filesystem-2:8.0.1763-16.el8.noarch 4/5 Verifying : which-2.21-16.el8.x86_64 5/5 Installed: gpm-libs-1.20.7-17.el8.x86_64 vim-common-2:8.0.1763-16.el8.x86_64 vim-enhanced-2:8.0.1763-16.el8.x86_64 vim-filesystem-2:8.0.1763-16.el8.noarch which-2.21-16.el8.x86_64 Complete!
2.1查看实时日志:docker logs -f 容器ID
C:\\Users\\特昂糖>docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 783a0336bfbe centos "/bin/bash" 14 seconds ago Up 12 seconds happy_feynman 875e8725e5eb centos "/bin/bash" 24 minutes ago Exited (0) 3 minutes ago hardcore_goldwasser C:\\Users\\特昂糖>docker logs 783a0336bfbe [root@783a0336bfbe /]# date Fri Dec 24 03:06:21 UTC 2021 [root@783a0336bfbe /]# yum install -y vim Failed to set locale, defaulting to C.UTF-8 CentOS Linux 8 - AppStream 1.6 MB/s | 8.4 MB 00:05 CentOS Linux 8 - BaseOS 32 kB/s | 3.6 MB 01:56 CentOS Linux 8 - Extras 310 B/s | 10 kB 00:34 Dependencies resolved. ======================================================================================================================== Package Architecture Version Repository Size ========================================================================================================================Installing: vim-enhanced x86_64 2:8.0.1763-16.el8 appstream 1.4 M Installing dependencies: gpm-libs x86_64 1.20.7-17.el8 appstream 39 k vim-common x86_64 2:8.0.1763-16.el8 appstream 6.3 M vim-filesystem noarch 2:8.0.1763-16.el8 appstream 49 k which x86_64 2.21-16.el8 baseos 49 k Transaction Summary ========================================================================================================================Install 5 Packages Total download size: 7.8 M Installed size: 30 M Downloading Packages: CentOS Linux 8 - AppStream 194% [==========================================================================(1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm 200 kB/s | 39 kB 00:00 (2/5): vim-filesystem-8.0.1763-16.el8.noarch.rpm 79 kB/s | 49 kB 00:00 (3/5): vim-enhanced-8.0.1763-16.el8.x86_64.rpm 871 kB/s | 1.4 MB 00:01 (4/5): vim-common-8.0.1763-16.el8.x86_64.rpm 1.9 MB/s | 6.3 MB 00:03 (5/5): which-2.21-16.el8.x86_64.rpm 16 kB/s | 49 kB 00:03 ------------------------------------------------------------------------------------------------------------------------Total 1.5 MB/s | 7.8 MB 00:05 warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY CentOS Linux 8 - AppStream 1.5 MB/s | 1.6 kB 00:00 Importing GPG key 0x8483C65D: Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>" Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial Key imported successfully Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : which-2.21-16.el8.x86_64 1/5 Installing : vim-filesystem-2:8.0.1763-16.el8.noarch 2/5 Installing : vim-common-2:8.0.1763-16.el8.x86_64 3/5 Installing : gpm-libs-1.20.7-17.el8.x86_64 4/5 Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64 4/5 Installing : vim-enhanced-2:8.0.1763-16.el8.x86_64 5/5 Running scriptlet: vim-enhanced-2:8.0.1763-16.el8.x86_64 5/5 Running scriptlet: vim-common-2:8.0.1763-16.el8.x86_64 5/5 Verifying : gpm-libs-1.20.7-17.el8.x86_64 1/5 Verifying : vim-common-2:8.0.1763-16.el8.x86_64 2/5 Verifying : vim-enhanced-2:8.0.1763-16.el8.x86_64 3/5 Verifying : vim-filesystem-2:8.0.1763-16.el8.noarch 4/5 Verifying : which-2.21-16.el8.x86_64 5/5 Installed: gpm-libs-1.20.7-17.el8.x86_64 vim-common-2:8.0.1763-16.el8.x86_64 vim-enhanced-2:8.0.1763-16.el8.x86_64 vim-filesystem-2:8.0.1763-16.el8.noarch which-2.21-16.el8.x86_64 Complete!
#如果需要退出实时日志,按Ctrl+C即可
2.2查看容器运行的所有日志:docker logs 容器ID
C:\\Users\\特昂糖>docker logs 0f065c0baee3 [root@0f065c0baee3 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@0f065c0baee3 /]# yum install -y vim Failed to set locale, defaulting to C.UTF-8 CentOS Linux 8 - AppStream 2.1 MB/s | 8.4 MB 00:03 CentOS Linux 8 - BaseOS 1.2 MB/s | 3.6 MB 00:03 CentOS Linux 8 - Extras 12 kB/s | 10 kB 00:00 Dependencies resolved. ======================================================================================================================== Package Architecture Version Repository Size ========================================================================================================================Installing: vim-enhanced x86_64 2:8.0.1763-16.el8 appstream 1.4 M Installing dependencies: gpm-libs x86_64 1.20.7-17.el8 appstream 39 k vim-common x86_64 2:8.0.1763-16.el8 appstream 6.3 M vim-filesystem noarch 2:8.0.1763-16.el8 appstream 49 k which x86_64 2.21-16.el8 baseos 49 k Transaction Summary ========================================================================================================================Install 5 Packages Total download size: 7.8 M Installed size: 30 M Downloading Packages: CentOS Linux 8 - BaseOS 195% [==========================================================================(1/5): gpm-libs-1.20.7-17.el8.x86_64.rpm 344 kB/s | 39 kB 00:00 (2/5): vim-filesystem-8.0.1763-16.el8.noarch.rpm 819 kB/s | 49 kB 00:00 (3/5): which-2.21-16.el8.x86_64.rpm 174 kB/s | 49 kB 00:00 (4/5): vim-enhanced-8.0.1763-16.el8.x86_64.rpm 2.6 MB/s | 1.4 MB 00:00 (5/5): vim-common-8.0.1763-16.el8.x86_64.rpm 3.3 MB/s | 6.3 MB 00:01 ------------------------------------------------------------------------------------------------------------------------Total 2.4 MB/s | 7.8 MB 00:03 warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/gpm-libs-1.20.7-17.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY CentOS Linux 8 - AppStream 1.6 MB/s | 1.6 kB 00:00 Importing GPG key 0x8483C65D: Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>" Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial Key imported successfully Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : which-2.21-16.el8.x86_64 1/5 Installing : vim-filesystem-2:8.0.1763-16.el8.noarch 2/5 Installing : vim-common-2:8.0.1763-16.el8.x86_64 3/5 Installing : gpm-libs-1.20.7-17.el8.x86_64 4/5 Running scriptlet: gpm-libs-1.20.7-17.el8.x86_64 4/5 Installing : vim-enhanced-2:8.0.1763-16.el8.x86_64 5/5 Running scriptlet: vim-enhanced-2:8.0.1763-16.el8.x86_64 5/5 Running scriptlet: vim-common-2:8.0.1763-16.el8.x86_64 5/5 Verifying : gpm-libs-1.20.7-17.el8.x86_64 1/5 Verifying : vim-common-2:8.0.1763-16.el8.x86_64 2/5 Verifying : vim-enhanced-2:8.0.1763-16.el8.x86_64 3/5 Verifying : vim-filesystem-2:8.0.1763-16.el8.noarch 4/5 Verifying : which-2.21-16.el8.x86_64 5/5 Installed: gpm-libs-1.20.7-17.el8.x86_64 vim-common-2:8.0.1763-16.el8.x86_64 vim-enhanced-2:8.0.1763-16.el8.x86_64 vim-filesystem-2:8.0.1763-16.el8.noarch which-2.21-16.el8.x86_64 Complete!
2.3查看容器日志文件最后几行
docker logs --tail 行数 容器id 查看最新的几行日志文件(日志文件最后的内容)
docker logs 容器ID | tail -10:查看最新的10行日志文件(日志文件最后的内容) #仅仅适用于Linux的系统
C:\\Users\\特昂糖>docker logs --tail 3 ee58acd8d92e bash: vim: command not found [root@ee58acd8d92e /]# ls anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
2.4查看容器日志最前面几行
docker logs 容器ID | head -10:查看日志文件最前面的10行 #仅仅适用于Linux的系统
3.容器的提交
容器的提交解决了什么? 解决了容器中安装的环境,但是必须在容器退出前要进行提交,如果没有提交,那么退出后,安装的环境等于再次安装。 退出vim的步骤: 1、按下esc键 2、按下shift+: 3、:后面输入q!,按下回车键,就会退出 容器提交的步骤: 1、运行镜像,到容器后
C:\\Users\\特昂糖>docker run -it --rm centos [root@44a63a5040a1 /]#
2、在容器里面进行具体的操作(比如安装软件,比如搭建环境等等),但是容器千万不能退出
[root@44a63a5040a1 /]# yum install -y mysql Failed to set locale, defaulting to C.UTF-8 CentOS Linux 8 - AppStream 3.1 MB/s | 8.4 MB 00:02 CentOS Linux 8 - BaseOS 1.7 MB/s | 3.6 MB 00:02 CentOS Linux 8 - Extras 8.3 kB/s | 10 kB 00:01 Dependencies resolved. ======================================================================================================================== Package Architecture Version Repository Size ========================================================================================================================Installing: mysql x86_64 8.0.26-1.module_el8.4.0+915+de215114 appstream 12 M Installing dependencies: libedit x86_64 3.1-23.20170329cvs.el8 baseos 102 k mariadb-connector-c-config noarch 3.1.11-2.el8_3 appstream 15 k mysql-common x86_64 8.0.26-1.module_el8.4.0+915+de215114 appstream 134 k Enabling module streams: mysql 8.0 Transaction Summary ========================================================================================================================Install 4 Packages Total download size: 12 M Installed size: 63 M Downloading Packages: CentOS Linux 8 - AppStream 195% [==========================================================================(1/4): mysql-common-8.0.26-1.module_el8.4.0+915+de215114.x86_64.rpm 479 kB/s | 134 kB 00:00 (2/4): libedit-3.1-23.20170329cvs.el8.x86_64.rpm 86 kB/s | 102 kB 00:01 (3/4): mariadb-connector-c-config-3.1.11-2.el8_3.noarch.rpm 981 B/s | 15 kB 00:15 [MIRROR] mysql-8.0.26-1.module_el8.4.0+915+de215114.x86_64.rpm: Curl error (7): Couldn\'t connect to server for http://mirrors.bfsu.edu.cn/centos/8.5.2111/AppStream/x86_64/os/Packages/mysql-8.0.26-1.module_el8.4.0%2b915%2bde215114.x86_64.rpm [Failed to connect to mirrors.bfsu.edu.cn port 80: Connection refused] (4/4): mysql-8.0.26-1.module_el8.4.0+915+de215114.x86_64.rpm 510 kB/s | 12 MB 00:23 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 494 kB/s | 12 MB 00:25 warning: /var/cache/dnf/appstream-02e86d1c976ab532/packages/mariadb-connector-c-config-3.1.11-2.el8_3.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY CentOS Linux 8 - AppStream 1.6 MB/s | 1.6 kB 00:00 Importing GPG key 0x8483C65D: Userid : "CentOS (CentOS Official Signing Key) <security@centos.org>" Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial Key imported successfully Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : libedit-3.1-23.20170329cvs.el8.x86_64 1/4 Installing : mariadb-connector-c-config-3.1.11-2.el8_3.noarch 2/4 Installing : mysql-common-8.0.26-1.module_el8.4.0+915+de215114.x86_64 3/4 Installing : mysql-8.0.26-1.module_el8.4.0+915+de215114.x86_64 4/4 Running scriptlet: mysql-8.0.26-1.module_el8.4.0+915+de215114.x86_64 4/4 Verifying : mariadb-connector-c-config-3.1.11-2.el8_3.noarch 1/4 Verifying : mysql-8.0.26-1.module_el8.4.0+915+de215114.x86_64 2/4 Verifying : mysql-common-8.0.26-1.module_el8.4.0+915+de215114.x86_64 3/4 Verifying : libedit-3.1-23.20170329cvs.el8.x86_64 4/4 Installed: libedit-3.1-23.20170329cvs.el8.x86_64 mariadb-connector-c-config-3.1.11-2.el8_3.noarch mysql-8.0.26-1.module_el8.4.0+915+de215114.x86_64 mysql-common-8.0.26-1.module_el8.4.0+915+de215114.x86_64 Complete!
3、打开一个新的控制台,查看到容器的ID docker ps -a
C:\\Users\\特昂糖>docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 44a63a5040a1 centos "/bin/bash" About a minute ago Up About a minute determined_carson
4、对容器进行提交
docker commit 容器ID 镜像新的名称 如下:
C:\\Users\\特昂糖>docker commit 44a63a5040a1 centos_mysql
sha256:d26cfd7f4d199ce81bc18a070cc08bb820f78a6e4b1a98c8eebbf1a9f4d7309e
5、待容器提交成功后,之前的容器进行退出
[root@44a63a5040a1 /]# exit exit
6、查看所有的镜像,就会发现存在centos_mysql的镜像
C:\\Users\\特昂糖>docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_mysql latest d26cfd7f4d19 About a minute ago 336MB nginx latest f6987c8d6ed5 3 days ago 141MB alpine/git latest c6b70534b534 4 weeks ago 27.4MB hello-world latest feb5d9fea6a5 3 months ago 13.3kB centos latest 5d0da3dc9764 3 months ago 231MB centos 7.8.2003 afb6fca791e0 19 months ago 203MB
7、运行新的镜像文件,如:
C:\\Users\\特昂糖>docker run -it --rm centos_mysql [root@9b03c0dcea7e /]# date Fri Dec 24 07:36:40 UTC 2021
4.镜像的停止和启动
1、获取nginx的镜像:docker pull nginx 2、端口映射:-p 80(宿主机的端口):80(容器里面的端口) 3、后台方式运行容器: docker run -d -p 80:80 nginx eede621c9dd01419b85c999dcacb6386790db9a2dbcc3287d020d3cd148738d6 4、查询容器的端口:docker port 容器的ID docker port eede621c9dd0 80/tcp -> 0.0.0.0:80 5、下面演示容器的停止和启动 localhost:~ liwangping$ docker port eede621c9dd0 80/tcp -> 0.0.0.0:80 localhost:~ liwangping$ docker stop eede621c9dd0 eede621c9dd0 localhost:~ liwangping$ docker port eede621c9dd0 localhost:~ liwangping$ docker start eede621c9dd0 eede621c9dd0 localhost:~ liwangping$ docker port eede621c9dd0 80/tcp -> 0.0.0.0:80
以上是关于Docker容器的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 Rider 调试使用 docker 容器和 docker-compose 构建的 .net 核心应用程序。容器退出代码 139
vscode 远程连接 docker 容器进行 C++ 代码调试实践