一、镜像
1.1 镜像简介
镜像简介
- 镜像是一个Docker的可执行文件,其中包括运行应用程序所需的所有代码内容、依赖库、环境变量和配置文件等。
- 通过镜像可以创建一个或多个容器。
1.2 镜像管理
镜像搜索 - docker search
- 作用:
- 搜索Docker Hub(镜像仓库)上的镜像
- 命令格式:
docker search [OPTIONS] TERM
- 命令参数(OPTIONS):
- -f, --filter filter 根据提供的格式筛选结果
–format string 利用Go语言的format格式化输出结果
–limit int 展示最大的结果数,默认25个
–no-trunc 内容全部显示
- -f, --filter filter 根据提供的格式筛选结果
# 搜索centos相关镜像
C:\Users\lenovo>docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6344 [OK]
ansible/centos7-ansible Ansible on Centos7 132 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 124 [OK]
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 117 [OK]
centos/systemd systemd enabled base container. 92 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 86
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 58 [OK]
tutum/centos Simple CentOS docker image with SSH access 46
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 45
kinogmt/centos-ssh CentOS with SSH 29 [OK]
pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 13
guyton/centos6 From official centos6 container with full up… 10 [OK]
centos/tools Docker image that has systems administration… 7 [OK]
drecom/centos-ruby centos ruby 6 [OK]
pivotaldata/centos Base centos, freshened up a little with a Do… 5
pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated wi… 3
darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
pivotaldata/centos-mingw Using the mingw toolchain to cross-compile t… 3
mamohr/centos-java Oracle Java 8 Docker image based on Centos 7 3 [OK]
mcnaughton/centos-base centos base image 1 [OK]
indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developmen… 1 [OK]
blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
pivotaldata/centos7-dev CentosOS 7 image for GPDB development 0
smartentry/centos centos with smartentry 0 [OK]
pivotaldata/centos6.8-dev CentosOS 6.8 image for GPDB development 0
# 简写
C:\Users\lenovo>docker search cent
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6344 [OK]
ansible/centos7-ansible Ansible on Centos7 132 [OK]
consol/centos-xfce-vnc Centos container with "headless" VNC session… 124 [OK]
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 117 [OK]
centos/systemd systemd enabled base container. 92 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 86
imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 58 [OK]
centurylink/watchtower Automatically update running Docker containe… 46
tutum/centos Simple CentOS docker image with SSH access 46
centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 45
kinogmt/centos-ssh CentOS with SSH 29 [OK]
centrifugo/centrifugo Scalable real-time messaging server in langu… 22 [OK]
pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 13
guyton/centos6 From official centos6 container with full up… 10 [OK]
centos/tools Docker image that has systems administration… 7 [OK]
drecom/centos-ruby centos ruby 6 [OK]
pivotaldata/centos Base centos, freshened up a little with a Do… 5
pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated wi… 3
pivotaldata/centos-mingw Using the mingw toolchain to cross-compile t… 3
darksheer/centos Base Centos Image -- Updated hourly 3 [OK]
indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developmen… 1 [OK]
blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK]
mcnaughton/centos-base centos base image 1 [OK]
pivotaldata/centos7-dev CentosOS 7 image for GPDB development 0
smartentry/centos centos with smartentry 0 [OK]
# 条件过滤: 只要官方镜像
C:\Users\lenovo>docker search -f is-official=true cent
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6344 [OK]
# 限制搜索结果数目
C:\Users\lenovo>docker search --limit 2 centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6344 [OK]
pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 13
# 先限制数量再过滤
C:\Users\lenovo>docker search -f is-official=true --limit 2 cent
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 6344 [OK]
# 查看镜像的完整描述
C:\Users\lenovo>docker search --no-trunc centos-ssh
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - CentOS. 117 [OK]
jdeathe/centos-ssh-apache-php Apache PHP - CentOS. 31 [OK]
kinogmt/centos-ssh CentOS with SSH 29 [OK]
jdeathe/centos-ssh-mysql MySQL Community Server - CentOS. 11 [OK]
stephenlu/centos-ssh-root ssh from centos7 5 [OK]
sickp/centos-sshd A simple OpenSSH 6.6.1 service built atop CentOS 7.2. 5
elcolio/centos-sshd 3 [OK]
komukomo/centos-sshd CentOS[tag] + sshd / tags: 6.5, 6.6, 6.7 3 [OK]
vikas027/centos-ssh-sudo CentOS 6 and 7 with ssh and sudo 3 [OK]
ilemonrain/centos-sshd Docker CentOS with OpenSSH-Server 3 [OK]
jdeathe/centos-ssh-apache-php-fcgi Apache PHP-CGI (FastCGI) - CentOS. 3 [OK]
shijh/centos-ssh-netspeeder-ssr CentOS with SSHD, NetSpeeder, ShadowsocksR installed. 2 [OK]
rsalian/centos-sshpass This docker image has latest centos 7 with sshpass installed 2 [OK]
jdeathe/centos-ssh-varnish Varnish Cache - CentOS. 0 [OK]
kodekloud/centos-ssh-enabled 0
joshhsoj1902/centos-ssh-docker Wraps https://hub.docker.com/r/jdeathe/centos-ssh/ adds docker 0 [OK]
lnterface/centos-ssh Dockerized SSH service, built on top of official Centos images. 0 [OK]
severalnines/centos-ssh Generic Docker base image for ClusterControl 0 [OK]
nlingareddy/centos-sshd 0
jdeathe/centos-ssh-memcached Memcached - CentOS. 0 [OK]
cltapia/centos-ssh-wmos-1 0
stephenlu/centos-ssh-root-jdk from centos-ssh-root build jdk1.7 0 [OK]
indigodatacloud/centos-sshd SSH-enabled of Docker images for CentOS 0 [OK]
frostsky/centos-sshd centos-sshd [tags: 6.7,7.3] 0 [OK]
gerrit91/centos-ssh Builds centos-ssh. 0 [OK]
C:\Users\lenovo>docker search centos-ssh
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 117 [OK]
jdeathe/centos-ssh-apache-php Apache PHP - CentOS. 31 [OK]
kinogmt/centos-ssh CentOS with SSH 29 [OK]
jdeathe/centos-ssh-mysql MySQL Community Server - CentOS. 11 [OK]
stephenlu/centos-ssh-root ssh from centos7 5 [OK]
sickp/centos-sshd A simple OpenSSH 6.6.1 service built atop Ce… 5
elcolio/centos-sshd 3 [OK]
komukomo/centos-sshd CentOS[tag] + sshd / tags: 6.5, 6.6, 6.7 3 [OK]
vikas027/centos-ssh-sudo CentOS 6 and 7 with ssh and sudo 3 [OK]
ilemonrain/centos-sshd Docker CentOS with OpenSSH-Server 3 [OK]
jdeathe/centos-ssh-apache-php-fcgi Apache PHP-CGI (FastCGI) - CentOS. 3 [OK]
shijh/centos-ssh-netspeeder-ssr CentOS with SSHD, NetSpeeder, ShadowsocksR i… 2 [OK]
rsalian/centos-sshpass This docker image has latest centos 7 with s… 2 [OK]
jdeathe/centos-ssh-varnish Varnish Cache - CentOS. 0 [OK]
kodekloud/centos-ssh-enabled 0
joshhsoj1902/centos-ssh-docker Wraps https://hub.docker.com/r/jdeathe/cento… 0 [OK]
lnterface/centos-ssh Dockerized SSH service, built on top of offi… 0 [OK]
severalnines/centos-ssh Generic Docker base image for ClusterControl 0 [OK]
nlingareddy/centos-sshd 0
jdeathe/centos-ssh-memcached Memcached - CentOS. 0 [OK]
stephenlu/centos-ssh-root-jdk from centos-ssh-root build jdk1.7 0 [OK]
cltapia/centos-ssh-wmos-1 0
indigodatacloud/centos-sshd SSH-enabled of Docker images for CentOS 0 [OK]
frostsky/centos-sshd centos-sshd [tags: 6.7,7.3] 0 [OK]
gerrit91/centos-ssh Builds centos-ssh. 0 [OK]
C:\Users\lenovo>
镜像查看 - docker images
/docker image ls
- 作用:
- 列出本地镜像
- 命令格式:
- docker images [OPTIONS] [REPOSITORY[:TAG]]
- 或者 docker image ls [OPTIONS] [REPOSITORY[:TAG]]
- 命令参数(OPTIONS):
- -a, --all 展示所有镜像 (默认隐藏底层的镜像)
–no-trunc 不缩略显示 - -q, --quiet 只显示镜像ID
- -a, --all 展示所有镜像 (默认隐藏底层的镜像)
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 3 weeks ago 209MB
C:\Users\lenovo>docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 3 weeks ago 209MB
# 查看帮助: docker pull --help / docker pull -h
C:\Users\lenovo>docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is multi-platform
capable
-q, --quiet Suppress verbose output
# 下载镜像,默认下载最新版本 docker pull centos[:latest]
C:\Users\lenovo>docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
6ec7b7d162b2: Pull complete
fedd960d3481: Pull complete
7ab947313861: Pull complete
64f92f19e638: Pull complete
3e80b17bff96: Pull complete
014e976799f9: Pull complete
59ae84fee1b3: Pull complete
ffe10de703ea: Pull complete
657af6d90c83: Pull complete
98bfb480322c: Pull complete
6aa3859c4789: Pull complete
1ed875d851ef: Pull complete
Digest: sha256:78800e6d3f1b230e35275145e657b82c3fb02a27b2d8e76aac2f5e90c1c30873
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
C:\Users\lenovo>docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10339 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3832 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 754 [OK]
percona Percona Server is a fork of the MySQL relati… 518 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 86
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 79
centurylink/mysql Image containing mysql. Optimized to be link… 60 [OK]
bitnami/mysql Bitnami MySQL Docker Image 47 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
prom/mysqld-exporter 34 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 34
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 27
centos/mysql-56-centos7 MySQL 5.6 SQL database server 20
circleci/mysql MySQL is a widely used, open-source relation… 19
mysql/mysql-router MySQL Router provides transparent routing be… 17
arey/mysql-client Run a MySQL client from a docker container 16 [OK]
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 10 [OK]
yloeffler/mysql-backup This image runs mysqldump to backup data usi… 7 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK]
jelastic/mysql An image of the MySQL database server mainta… 1
# 下载指定版本的镜像
C:\Users\lenovo>docker pull mysql:5.7
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
# 只列出部分镜像
C:\Users\lenovo>docker images mysql
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
C:\Users\lenovo>docker images mysql:latest
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest a347a5928046 11 days ago 545MB
# 模糊匹配
C:\Users\lenovo>docker images cent*
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 3 weeks ago 209MB
# 若有多个不同版本的镜像
C:\Users\lenovo>docker images mys*
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
C:\Users\lenovo>docker images mys*:latest
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql latest a347a5928046 11 days ago 545MB
C:\Users\lenovo>
镜像下载 - docker pull
- 作用:
- 下载远程仓库(如Docker Hub)中的镜像
- 命令格式:
- docker pull [OPTIONS] NAME[:TAG|@DIGEST]
- 命令参数(OPTIONS):
- -a, --all-tags 下载所有符合给定tag的镜像
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
C:\Users\lenovo>docker pull centos:7
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
# image id相同表示镜像是同一个
C:\Users\lenovo>docker pull centos:8
8: Pulling from library/centos
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:8
docker.io/library/centos:8
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos 8 300e315adb2f 3 weeks ago 209MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
C:\Users\lenovo>
镜像删除 - docker rmi
/docker image rm
- 作用:
- 将本地的一个或多个镜像删除
- 命令格式:
- docker rmi [OPTIONS] IMAGE [IMAGE…]
或者 docker image rm [OPTIONS] IMAGE [IMAGE…]
- docker rmi [OPTIONS] IMAGE [IMAGE…]
- 命令参数(OPTIONS):
- -f, --force 强制删除
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos 8 300e315adb2f 3 weeks ago 209MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
C:\Users\lenovo>docker rmi -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
# 默认删除最新版本: docker rmi ubuntu[:latest]
# 删除指定版本镜像[若有相同的image id但不同的版本号,只会删除镜像中该版本的引用]
# 删除多个镜像用空格分隔
C:\Users\lenovo>docker rmi centos:8
Untagged: centos:8
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
# 通过image id删除镜像,可简写前几位: docker rmi f07[dfa83b528]
C:\Users\lenovo>docker rmi f07
# 强制删除[镜像已经生成容器时使用强制删除]
C:\Users\lenovo>docker rmi -f f07
C:\Users\lenovo>
镜像保存备份 – docker save
- 作用:
- 将本地的一个或多个镜像打包保存成本地tar文件(输出到STDOUT)
- 命令格式:
- docker save [OPTIONS] IMAGE [IMAGE…]
- 命令参数(OPTIONS):
- -o, --output string 指定写入的文件名和路径
C:\Users\lenovo>docker save -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker save [OPTIONS] IMAGE [IMAGE...]
Save one or more images to a tar archive (streamed to STDOUT by default)
Options:
-o, --output string Write to a file, instead of STDOUT
# 默认导出最新镜像,多个镜像导出用空格分开,同样可以使用image id来导出
# 使用 > 重定向
# 注: 使用image id导出时repository和tag信息会丢失[再重新导入时会发现]
# 注:导出时必须指定repository和tag,否则repository和tag信息会丢失,如使用image id导出时未指定repository和tag,信息丢失
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
C:\Users\lenovo>docker save centos centos:7 a347a5928046 > save.tar
C:\Users\lenovo>docker save centos:7 > centos7.tar
C:\Users\lenovo>dir
2021/01/02 12:56 <DIR> .
2021/01/02 12:56 <DIR> ..
2021/01/02 12:56 211,696,640 centos7.tar
# 指定保存路径:
C:\Users\lenovo>docker save centos:7 -o 2centos7.tar
C:\Users\lenovo>dir
2021/01/02 12:59 <DIR> .
2021/01/02 12:59 <DIR> ..
2021/01/02 12:59 211,696,640 2centos7.tar
2021/01/02 12:56 211,696,640 centos7.tar
C:\Users\lenovo>
镜像备份导入 - docker load
- 作用:
- 将save命令打包的镜像导入本地镜像库中
- 命令格式:
- docker load [OPTIONS]
- 命令参数(OPTIONS):
- -i, --input string 指定要打入的文件,如没有指定,默认是STDIN
- -q, --quiet 不打印导入过程信息
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
C:\Users\lenovo>docker rmi centos:7
Untagged: centos:7
Untagged: centos@sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Deleted: sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf
Deleted: sha256:174f5685490326fc0a1c0f5570b8663732189b327007e47ff13d2ca59673db02
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
C:\Users\lenovo>dir
2021/01/02 12:59 <DIR> .
2021/01/02 12:59 <DIR> ..
2021/01/02 12:59 211,696,640 2centos7.tar
2021/01/02 12:56 211,696,640 centos7.tar
C:\Users\lenovo>docker load -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker load [OPTIONS]
Load an image from a tar archive or STDIN
Options:
-i, --input string Read from tar archive file, instead of STDIN
-q, --quiet Suppress the load output
C:\Users\lenovo>docker load -i centos7.tar
174f56854903: Loading layer [==================================================>] 211.7MB/211.7MB
Loaded image: centos:7
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
# docker load -i linux_images.tar
# docker load -i linux_images.tar -q
C:\Users\lenovo>
镜像重命名 – docker tag
- 作用:
- 对本地镜像的NAME、TAG进行重命名,并新产生一个命名后镜像
- 命令格式:
- docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
- 命令参数(OPTIONS):
- 无
C:\Users\lenovo>docker tag --help
Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
C:\Users\lenovo>docker tag centos:7 centosTest:7Test
Error parsing reference: "centosTest:7Test" is not a valid repository/tag: invalid reference format: repository name must be lowercase
# 默认是最新版本docker tag centos[:latest] centos-test:8-test
# 添加一个新的引用指向他[image id确定]
C:\Users\lenovo>docker tag centos:7 centos-test:7-test
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos-test 7-test 8652b9f0cb4c 7 weeks ago 204MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
# 通过image id重命名
C:\Users\lenovo>docker tag 8652 centos7:7.latest
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
centos-test 7-test 8652b9f0cb4c 7 weeks ago 204MB
centos7 7.latest 8652b9f0cb4c 7 weeks ago 204MB
C:\Users\lenovo>docker rmi centos-test:7-test centos7:7.latest
Untagged: centos-test:7-test
Untagged: centos7:7.latest
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
C:\Users\lenovo>docker tag 8652 centos7:7.latest
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos7 7.latest 8652b9f0cb4c 7 weeks ago 204MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
C:\Users\lenovo>
镜像详细信息 – docker image inspect
/docker inspect
- 作用:
- 查看本地一个或多个镜像的详细信息
- 命令格式:
- docker image inspect [OPTIONS] IMAGE [IMAGE…]
- 或者 docker inspect [OPTIONS] IMAGE [IMAGE…]
- 命令参数(OPTIONS):
- -f, --format string 利用特定Go语言的format格式输出结果
C:\Users\lenovo>docker image inspect --help
Usage: docker image inspect [OPTIONS] IMAGE [IMAGE...]
Display detailed information on one or more images
Options:
-f, --format string Format the output using the given Go template
C:\Users\lenovo>docker image inspect centos
[
{
"Id": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"RepoTags": [
"centos:latest"
],
"RepoDigests": [
"centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1"
],
"Parent": "",
"Comment": "",
"Created": "2020-12-08T00:22:53.076477777Z",
"Container": "395e0bfa7301f73bc994efe15099ea56b8836c608dd32614ac5ae279976d33e4",
"ContainerConfig": {
"Hostname": "395e0bfa7301",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) ",
"CMD [\"/bin/bash\"]"
],
"Image": "sha256:6de05bdfbf9a9d403458d10de9e088b6d93d971dd5d48d18b4b6758f4554f451",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"DockerVersion": "19.03.12",
"Author": "",
"Config": {
"Hostname": "",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "sha256:6de05bdfbf9a9d403458d10de9e088b6d93d971dd5d48d18b4b6758f4554f451",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201204",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 209348104,
"VirtualSize": 209348104,
"GraphDriver": {
"Data": {
"MergedDir": "/var/lib/docker/overlay2/4e1486ceffcea4cb9a14394cbafad16282982ff4064b5d187a57f3e832554af4/merged",
"UpperDir": "/var/lib/docker/overlay2/4e1486ceffcea4cb9a14394cbafad16282982ff4064b5d187a57f3e832554af4/diff",
"WorkDir": "/var/lib/docker/overlay2/4e1486ceffcea4cb9a14394cbafad16282982ff4064b5d187a57f3e832554af4/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:2653d992f4ef2bfd27f94db643815aa567240c37732cae1405ad1c1309ee9859"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
# 查看某一个属性:需要装grep命令或者在*nux下使用
C:\Users\lenovo>docker image inspect centos | grep Created
'grep' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
C:\Users\lenovo>docker image inspect -f "{{json .Id}}" centos
"sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55"
C:\Users\lenovo>docker image inspect -f "{{json .Created}}" centos
"2020-12-08T00:22:53.076477777Z"
C:\Users\lenovo>docker image inspect -f "{{json .Data}}" centos
Template parsing error: template: :1:7: executing "" at <.Data>: map has no entry for key "Data"
C:\Users\lenovo>docker image inspect -f "{{json .GraphDriver.Data}}" centos
{"MergedDir":"/var/lib/docker/overlay2/4e1486ceffcea4cb9a14394cbafad16282982ff4064b5d187a57f3e832554af4/merged","UpperDir":"/var/lib/docker/overlay2/4e1486ceffcea4cb9a14394cbafad16282982ff4064b5d187a57f3e832554af4/diff","WorkDir":"/var/lib/docker/overlay2/4e1486ceffcea4cb9a14394cbafad16282982ff4064b5d187a57f3e832554af4/work"}
# 区别docker inspect:可以查看更多信息
C:\Users\lenovo>docker inspect --help
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Return low-level information on Docker objects
Options:
-f, --format string Format the output using the given Go template
-s, --size Display total file sizes if the type is container
--type string Return JSON for specified type
C:\Users\lenovo>docker image inspect --help
Usage: docker image inspect [OPTIONS] IMAGE [IMAGE...]
Display detailed information on one or more images
Options:
-f, --format string Format the output using the given Go template
C:\Users\lenovo>
镜像历史信息 – docker history
- 作用:
- 查看本地一个镜像的历史(历史分层)信息
- 命令格式:
- docker history [OPTIONS] IMAGE
- 命令参数(OPTIONS):
- -H, --human 将创建时间、大小进行优化打印(默认为true)
- -q, --quiet 只显示镜像ID
–no-trunc 不缩略显示
C:\Users\lenovo>docker history -h
Flag shorthand -h has been deprecated, please use --help
Usage: docker history [OPTIONS] IMAGE
Show the history of an image
Options:
--format string Pretty-print images using a Go template
-H, --human Print sizes and dates in human readable format
(default true)
--no-trunc Don't truncate output
-q, --quiet Only show image IDs
C:\Users\lenovo>docker history centos
IMAGE CREATED CREATED BY SIZE COMMENT
300e315adb2f 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7… 209MB
# docker history ubuntu -H=false
C:\Users\lenovo>
二、容器
2.1 容器简介
什么是容器
容器(Container):容器是一种轻量级、可移植、并将应用程序进行的打包的技术,使应用程序可以在几乎任何地方以相同的方式运行
- Docker将镜像文件运行起来后,产生的对象就是容器。容器相当于是镜像运行起来的一个实例。
- 容器具备一定的生命周期。
- 另外,可以借助
docker ps
命令查看运行的容器,如同在linux上利用ps命令查看运行着的进程那样。
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 f07dfa83b528 11 days ago 448MB
mysql latest a347a5928046 11 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
centos7 7.latest 8652b9f0cb4c 7 weeks ago 204MB
C:\Users\lenovo>docker run --rm -dti centos
87f2400ad86412184bbef6ab88bae5adaa732f40ef9a44b6548af96dfbf89dbd
C:\Users\lenovo>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
87f2400ad864 centos "/bin/bash" 10 seconds ago Up 7 seconds eloquent_curie
C:\Users\lenovo>ps -a
sh-4.4# ps -a
PID TTY TIME CMD
28 pts/2 00:00:00 ps
sh-4.4#
2.2 容器与虚拟机
Docker容器与虚拟机相同点
- 容器和虚拟机一样,都会对物理硬件资源进行共享使用。
- 容器和虚拟机的生命周期比较相似(创建、运行、暂停、关闭等等)。
- 容器中或虚拟机中都可以安装各种应用,如redis、mysql、nginx等。也就是说,在容器中的操作,如同在一个虚拟机(操作系统)中操作一样。
- 同虚拟机一样,容器创建后,会存储在宿主机上:linux上位于/var/lib/docker/containers下
Docker容器与虚拟机不同点
注意:容器并不是虚拟机,但它们有很多相似的地方
- 虚拟机的创建、启动和关闭都是基于一个完整的操作系统。一个虚拟机就是一个完整的操作系统。而容器直接运行在宿主机的内核上,其本质上以一系列进程的结合。
- 容器是轻量级的,虚拟机是重量级的。首先容器不需要额外的资源来管理(不需要Hypervisor、Guest OS),虚拟机额外更多的性能消耗;其次创建、启动或关闭容器,如同创建、启动或者关闭进程那么轻松,而创建、启动、关闭一个操作系统就没那么方便了。
- 也因此,意味着在给定的硬件上能运行更多数量的容器,甚至可以直接把Docker运行在虚拟机上。
2.3 容器生命周期
虚拟机的生命周期
容器的生命周期
2.4 容器生命周期管理
容器创建 – docker create
- 作用:
- 利用镜像创建出一个Created 状态的待启动容器
- 命令格式:
- docker create [OPTIONS] IMAGE [COMMAND] [ARG…]
- 命令参数(OPTIONS):查看更多
- -t, --tty 分配一个伪TTY,也就是分配虚拟终端
- -i, --interactive 即使没有连接,也要保持STDIN打开
–name 为容器起名,如果没有指定将会随机产生一个名称
- 命令参数(COMMAND\ARG):
- COMMAND 表示容器启动后,需要在容器中执行的命令,如ps、ls 等命令
- ARG 表示执行 COMMAND 时需要提供的一些参数,如ps 命令的 aux、ls命令的-a等等
# command默认值为Cmd:/bin/bash arg为command的参数
# command arg表示启动镜像时执行的默认命令
# 用法:docker create centos ls -a
# bash命令作用是新开一个终端,退出要exit
C:\Users\lenovo>docker create --help
Usage: docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Create a new container
Options:
--add-host list Add a custom host-to-IP mapping
(host:ip)
-a, --attach list Attach to STDIN, STDOUT or STDERR
--blkio-weight uint16 Block IO (relative weight),
between 10 and 1000, or 0 to
disable (default 0)
--blkio-weight-device list Block IO weight (relative device
weight) (default [])
--cap-add list Add Linux capabilities
--cap-drop list Drop Linux capabilities
--cgroup-parent string Optional parent cgroup for the
container
--cgroupns string Cgroup namespace to use
(host|private)
'host': Run the container in
the Docker host's cgroup namespace
'private': Run the container in
its own private cgroup namespace
'': Use the cgroup
namespace as configured by the
default-cgroupns-mode
option on the daemon (default)
--cidfile string Write the container ID to the file
--cpu-period int Limit CPU CFS (Completely Fair
Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair
Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in
microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in
microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution
(0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution
(0-3, 0,1)
--device list Add a host device to the container
--device-cgroup-rule list Add a rule to the cgroup allowed
devices list
--device-read-bps list Limit read rate (bytes per second)
from a device (default [])
--device-read-iops list Limit read rate (IO per second)
from a device (default [])
--device-write-bps list Limit write rate (bytes per
second) to a device (default [])
--device-write-iops list Limit write rate (IO per second)
to a device (default [])
--disable-content-trust Skip image verification (default true)
--dns list Set custom DNS servers
--dns-option list Set DNS options
--dns-search list Set custom DNS search domains
--domainname string Container NIS domain name
--entrypoint string Overwrite the default ENTRYPOINT
of the image
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
--expose list Expose a port or a range of ports
--gpus gpu-request GPU devices to add to the
container ('all' to pass all GPUs)
--group-add list Add additional groups to join
--health-cmd string Command to run to check health
--health-interval duration Time between running the check
(ms|s|m|h) (default 0s)
--health-retries int Consecutive failures needed to
report unhealthy
--health-start-period duration Start period for the container to
initialize before starting
health-retries countdown
(ms|s|m|h) (default 0s)
--health-timeout duration Maximum time to allow one check to
run (ms|s|m|h) (default 0s)
--help Print usage
-h, --hostname string Container host name
--init Run an init inside the container
that forwards signals and reaps
processes
-i, --interactive Keep STDIN open even if not attached
--ip string IPv4 address (e.g., 172.30.100.104)
--ip6 string IPv6 address (e.g., 2001:db8::33)
--ipc string IPC mode to use
--isolation string Container isolation technology
--kernel-memory bytes Kernel memory limit
-l, --label list Set meta data on a container
--label-file list Read in a line delimited file of labels
--link list Add link to another container
--link-local-ip list Container IPv4/IPv6 link-local
addresses
--log-driver string Logging driver for the container
--log-opt list Log driver options
--mac-address string Container MAC address (e.g.,
92:d0:c6:0a:29:33)
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus
swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness
(0 to 100) (default -1)
--mount mount Attach a filesystem mount to the
container
--name string Assign a name to the container
--network network Connect a container to a network
--network-alias list Add network-scoped alias for the
container
--no-healthcheck Disable any container-specified
HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj int Tune host's OOM preferences (-1000
to 1000)
--pid string PID namespace to use
--pids-limit int Tune container pids limit (set -1
for unlimited)
--platform string Set platform if server is
multi-platform capable
--privileged Give extended privileges to this
container
-p, --publish list Publish a container's port(s) to
the host
-P, --publish-all Publish all exposed ports to
random ports
--pull string Pull image before creating
("always"|"missing"|"never")
(default "missing")
--read-only Mount the container's root
filesystem as read only
--restart string Restart policy to apply when a
container exits (default "no")
--rm Automatically remove the container
when it exits
--runtime string Runtime to use for this container
--security-opt list Security Options
--shm-size bytes Size of /dev/shm
--stop-signal string Signal to stop a container
(default "15")
--stop-timeout int Timeout (in seconds) to stop a
container
--storage-opt list Storage driver options for the
container
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory
-t, --tty Allocate a pseudo-TTY
--ulimit ulimit Ulimit options (default [])
-u, --user string Username or UID (format:
<name|uid>[:<group|gid>])
--userns string User namespace to use
--uts string UTS namespace to use
-v, --volume list Bind mount a volume
--volume-driver string Optional volume driver for the
container
--volumes-from list Mount volumes from the specified
container(s)
-w, --workdir string Working directory inside the container
C:\Users\lenovo>docker create centos ls -a
a47b98b404ea29195f4ea2432b28bdab9912b70b663ea4000422715c816cf919
# 查看容器,默认只显示running状态
C:\Users\lenovo>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 查看所有容器
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a47b98b404ea centos "ls -a" 2 minutes ago Created mystifying_newton
C:\Users\lenovo>docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
-n, --last int Show n last created containers (includes all
states) (default -1)
-l, --latest Show the latest created container (includes all
states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
C:\Users\lenovo>docker create centos
ee3346db835f000a439f55d58c95dab56f2c848c59a8e617e183879727000c2a
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ee3346db835f centos "/bin/bash" 7 seconds ago Created recursing_davinci
a47b98b404ea centos "ls -a" 4 minutes ago Created mystifying_newton
# 创建容器时指定名称:
C:\Users\lenovo>docker create --name centos-test centos
46d0f5054867e4811c965bfd9ba23086a10815f65d248326c8aa4b6fec73b522
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46d0f5054867 centos "/bin/bash" 8 seconds ago Created centos-test
ee3346db835f centos "/bin/bash" 2 minutes ago Created recursing_davinci
a47b98b404ea centos "ls -a" 6 minutes ago Created mystifying_newton
# docker create -ti centos python
C:\Users\lenovo>
容器启动 – docker start
- 作用:
- 将一个或多个处于创建状态或关闭状态的容器启动起来
- 命令格式:
- docker start [OPTIONS] CONTAINER [CONTAINER…]
- 命令参数(OPTIONS):
- -a, --attach 将当前shell的 STDOUT/STDERR 连接到容器上
- -i, --interactive 将当前shell的 STDIN连接到容器上
C:\Users\lenovo>docker start --help
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
Start one or more stopped containers
Options:
-a, --attach Attach STDOUT/STDERR and forward signals
--detach-keys string Override the key sequence for detaching a
container
-i, --interactive Attach container's STDIN
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
C:\Users\lenovo>docker create --name centos-test centos ls -a
364e844201b4ce67b7296d5401785df8132fbe2151aa98f8eb04ec2c62500c82
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
364e844201b4 centos "ls -a" 8 seconds ago Created centos-test
# names或者container id都可以作为container
C:\Users\lenovo>docker start 364e
364e
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
364e844201b4 centos "ls -a" About a minute ago Exited (0) 33 seconds ago centos-test
# running状态去哪了? command(ls -a)执行完成后就结束了
C:\Users\lenovo>docker start -a 364e
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
364e844201b4 centos "ls -a" 48 minutes ago Exited (0) 44 minutes ago centos-test
C:\Users\lenovo>docker create --name python-test centos:7 python
3a3ded481cdf8b80ef78a9643a7807b47e496f96beda7b83835cc9c4aa8f646f
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a3ded481cdf centos:7 "python" 9 seconds ago Created python-test
364e844201b4 centos "ls -a" 48 minutes ago Exited (0) 45 minutes ago centos-test
C:\Users\lenovo>docker start -a python-test
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a3ded481cdf centos:7 "python" 29 seconds ago Exited (0) 5 seconds ago python-test
364e844201b4 centos "ls -a" 49 minutes ago Exited (0) 45 minutes ago centos-test
C:\Users\lenovo>docker create -ti --name python-test-new centos:7 python
a05399a9a96d0eaecd214eb255c6bb850bb8ba9f7f1afa84aaed80b6263c3aae
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a05399a9a96d centos:7 "python" 19 seconds ago Created python-test-new
3a3ded481cdf centos:7 "python" 2 minutes ago Exited (0) 2 minutes ago python-test
364e844201b4 centos "ls -a" 51 minutes ago Exited (0) 47 minutes ago centos-test
# windows下不要尝试...
C:\Users\lenovo>docker start -a python-test-new
# ...输入不了
C:\Users\lenovo>docker start --help
Usage: docker start [OPTIONS] CONTAINER [CONTAINER...]
Start one or more stopped containers
Options:
-a, --attach Attach STDOUT/STDERR and forward signals
--detach-keys string Override the key sequence for detaching a
container
-i, --interactive Attach container's STDIN
# 正常使用
C:\Users\lenovo>docker start -ai python-test-new
Python 2.7.5 (default, Oct 14 2020, 14:45:30)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a05399a9a96d centos:7 "python" 2 minutes ago Exited (0) 14 seconds ago python-test-new
3a3ded481cdf centos:7 "python" 4 minutes ago Exited (0) 4 minutes ago python-test
364e844201b4 centos "ls -a" 53 minutes ago Exited (0) 49 minutes ago centos-test
C:\Users\lenovo>
容器创建并启动 – docker run
- 作用:
- 利用镜像创建并启动一个容器
- 命令格式:
- docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
- 命令参数(OPTIONS):查看更多
- -t, --tty 分配一个伪TTY,也就是分配虚拟终端
- -i, --interactive 即使没有连接,也要保持STDIN打开
–name 为容器起名,如果没有指定将会随机产生一个名称 - -d, --detach 在后台运行容器并打印出容器ID
- –rm 当容器退出运行后,自动删除容器
- 命令参数(COMMAND\ARG):
- COMMAND 表示容器启动后,需要在容器中执行的命令,如ps、ls 等命令
- ARG 表示执行 COMMAND 时需要提供的一些参数,如ps 命令的 aux、ls命令的-a等等
C:\Users\lenovo>docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--add-host list Add a custom host-to-IP mapping
(host:ip)
-a, --attach list Attach to STDIN, STDOUT or STDERR
--blkio-weight uint16 Block IO (relative weight),
between 10 and 1000, or 0 to
disable (default 0)
--blkio-weight-device list Block IO weight (relative device
weight) (default [])
--cap-add list Add Linux capabilities
--cap-drop list Drop Linux capabilities
--cgroup-parent string Optional parent cgroup for the
container
--cgroupns string Cgroup namespace to use
(host|private)
'host': Run the container in
the Docker host's cgroup namespace
'private': Run the container in
its own private cgroup namespace
'': Use the cgroup
namespace as configured by the
default-cgroupns-mode
option on the daemon (default)
--cidfile string Write the container ID to the file
--cpu-period int Limit CPU CFS (Completely Fair
Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair
Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in
microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in
microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution
(0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution
(0-3, 0,1)
-d, --detach Run container in background and
print container ID
--detach-keys string Override the key sequence for
detaching a container
--device list Add a host device to the container
--device-cgroup-rule list Add a rule to the cgroup allowed
devices list
--device-read-bps list Limit read rate (bytes per second)
from a device (default [])
--device-read-iops list Limit read rate (IO per second)
from a device (default [])
--device-write-bps list Limit write rate (bytes per
second) to a device (default [])
--device-write-iops list Limit write rate (IO per second)
to a device (default [])
--disable-content-trust Skip image verification (default true)
--dns list Set custom DNS servers
--dns-option list Set DNS options
--dns-search list Set custom DNS search domains
--domainname string Container NIS domain name
--entrypoint string Overwrite the default ENTRYPOINT
of the image
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
--expose list Expose a port or a range of ports
--gpus gpu-request GPU devices to add to the
container ('all' to pass all GPUs)
--group-add list Add additional groups to join
--health-cmd string Command to run to check health
--health-interval duration Time between running the check
(ms|s|m|h) (default 0s)
--health-retries int Consecutive failures needed to
report unhealthy
--health-start-period duration Start period for the container to
initialize before starting
health-retries countdown
(ms|s|m|h) (default 0s)
--health-timeout duration Maximum time to allow one check to
run (ms|s|m|h) (default 0s)
--help Print usage
-h, --hostname string Container host name
--init Run an init inside the container
that forwards signals and reaps
processes
-i, --interactive Keep STDIN open even if not attached
--ip string IPv4 address (e.g., 172.30.100.104)
--ip6 string IPv6 address (e.g., 2001:db8::33)
--ipc string IPC mode to use
--isolation string Container isolation technology
--kernel-memory bytes Kernel memory limit
-l, --label list Set meta data on a container
--label-file list Read in a line delimited file of labels
--link list Add link to another container
--link-local-ip list Container IPv4/IPv6 link-local
addresses
--log-driver string Logging driver for the container
--log-opt list Log driver options
--mac-address string Container MAC address (e.g.,
92:d0:c6:0a:29:33)
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus
swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness
(0 to 100) (default -1)
--mount mount Attach a filesystem mount to the
container
--name string Assign a name to the container
--network network Connect a container to a network
--network-alias list Add network-scoped alias for the
container
--no-healthcheck Disable any container-specified
HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj int Tune host's OOM preferences (-1000
to 1000)
--pid string PID namespace to use
--pids-limit int Tune container pids limit (set -1
for unlimited)
--platform string Set platform if server is
multi-platform capable
--privileged Give extended privileges to this
container
-p, --publish list Publish a container's port(s) to
the host
-P, --publish-all Publish all exposed ports to
random ports
--pull string Pull image before running
("always"|"missing"|"never")
(default "missing")
--read-only Mount the container's root
filesystem as read only
--restart string Restart policy to apply when a
container exits (default "no")
--rm Automatically remove the container
when it exits
--runtime string Runtime to use for this container
--security-opt list Security Options
--shm-size bytes Size of /dev/shm
--sig-proxy Proxy received signals to the
process (default true)
--stop-signal string Signal to stop a container
(default "15")
--stop-timeout int Timeout (in seconds) to stop a
container
--storage-opt list Storage driver options for the
container
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory
-t, --tty Allocate a pseudo-TTY
--ulimit ulimit Ulimit options (default [])
-u, --user string Username or UID (format:
<name|uid>[:<group|gid>])
--userns string User namespace to use
--uts string UTS namespace to use
-v, --volume list Bind mount a volume
--volume-driver string Optional volume driver for the
container
--volumes-from list Mount volumes from the specified
container(s)
-w, --workdir string Working directory inside the container
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a05399a9a96d centos:7 "python" 4 hours ago Exited (0) 4 hours ago python-test-new
3a3ded481cdf centos:7 "python" 4 hours ago Exited (0) 4 hours ago python-test
364e844201b4 centos "ls -a" 5 hours ago Exited (0) 5 hours ago centos-test
C:\Users\lenovo>docker run centos ls -a
.
..
.dockerenv
bin
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d69f79303b98 centos "ls -a" 8 seconds ago Exited (0) 5 seconds ago flamboyant_hugle
a05399a9a96d centos:7 "python" 4 hours ago Exited (0) 4 hours ago python-test-new
3a3ded481cdf centos:7 "python" 4 hours ago Exited (0) 4 hours ago python-test
364e844201b4 centos "ls -a" 5 hours ago Exited (0) 5 hours ago centos-test
C:\Users\lenovo>docker run centos:7 python
C:\Users\lenovo>docker run -ti centos:7 python
Python 2.7.5 (default, Oct 14 2020, 14:45:30)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
# 前台运行:docker run centos ps -A
# 后台运行:docker run -d centos ps -A
# 容器运行完成后自动删除容器:docker run -rm -d centos ps -A
# 容器运行完成后不自动删除容器:docker run -d centos ps -A
C:\Users\lenovo>
docker run
与 docker create + docker start
docker run
相当于 docker create + docker start –a
前台模式
docker run -d
相当于 docker create + docker start
后台模式
容器暂停 – docker pause
- 作用:
- 暂停一个或多个处于运行状态的容器
- 命令格式:
- docker pause CONTAINER [CONTAINER…]
- 命令参数(OPTIONS):
- 无
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f53ec43c818 centos:7 "python" 18 minutes ago Exited (137) 7 minutes ago vibrant_pasteur
C:\Users\lenovo>docker pause --help
Usage: docker pause CONTAINER [CONTAINER...]
Pause all processes within one or more containers
C:\Users\lenovo>docker start 0f53ec43c818
0f53ec43c818
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f53ec43c818 centos:7 "python" 18 minutes ago Up 4 seconds vibrant_pasteur
C:\Users\lenovo>docker pause 0f53ec43c818
0f53ec43c818
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f53ec43c818 centos:7 "python" 18 minutes ago Up 19 seconds (Paused) vibrant_pasteur
C:\Users\lenovo>docker unpause --help
Usage: docker unpause CONTAINER [CONTAINER...]
Unpause all processes within one or more containers
C:\Users\lenovo>docker unpause 0f53ec43c818
0f53ec43c818
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f53ec43c818 centos:7 "python" 19 minutes ago Up 53 seconds vibrant_pasteur
C:\Users\lenovo>docker stop 0f53ec43c818
0f53ec43c818
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f53ec43c818 centos:7 "python" 20 minutes ago Exited (137) 7 seconds ago vibrant_pasteur
C:\Users\lenovo>
容器取消暂停 – docker unpause
- 作用:
- 取消一个或多个处于暂停状态的容器,恢复运行
- 命令格式:
- docker unpause CONTAINER [CONTAINER…]
- 命令参数(OPTIONS):
- 无
栗子详上。
容器关闭 – docker stop
- 作用:
- 关闭一个或多个处于暂停状态或者运行状态的容器
- 命令格式:
- docker stop [OPTIONS] CONTAINER [CONTAINER…]
- 命令参数(OPTIONS):
- -t, --time int 关闭前,等待的时间,单位秒(默认 10s)
C:\Users\lenovo>docker ps -al
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
C:\Users\lenovo>docker run -dti centos:7 python
59eb31b27325077ba89d9400391ff705729aa70a0ea6db487f49a0b1633c28da
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
59eb31b27325 centos:7 "python" 12 seconds ago Up 10 seconds magical_ritchie
C:\Users\lenovo>docker stop 59eb31b27325
59eb31b27325
C:\Users\lenovo>docker ps -al
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
59eb31b27325 centos:7 "python" About a minute ago Exited (137) 23 seconds ago magical_ritchie
C:\Users\lenovo>docker run -dti centos:7 python
0f53ec43c818531ccef4b35b8fb63364ba7036f03d7effb39e5020c854bc14f9
C:\Users\lenovo>docker ps -al
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f53ec43c818 centos:7 "python" 51 seconds ago Up 49 seconds vibrant_pasteur
C:\Users\lenovo>docker stop -t 0 0f53ec43c818
0f53ec43c818
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f53ec43c818 centos:7 "python" About a minute ago Exited (137) 7 seconds ago vibrant_pasteur
59eb31b27325 centos:7 "python" 2 minutes ago Exited (137) 2 minutes ago magical_ritchie
C:\Users\lenovo>docker start 0f53ec43c818
0f53ec43c818
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f53ec43c818 centos:7 "python" 3 minutes ago Up 13 seconds vibrant_pasteur
59eb31b27325 centos:7 "python" 4 minutes ago Exited (137) 3 minutes ago magical_ritchie
C:\Users\lenovo>docker stop 0f53ec43c818
# 这里使用Ctrl + C
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f53ec43c818 centos:7 "python" 3 minutes ago Up 27 seconds vibrant_pasteur
59eb31b27325 centos:7 "python" 4 minutes ago Exited (137) 4 minutes ago magical_ritchie
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f53ec43c818 centos:7 "python" 3 minutes ago Exited (137) 2 seconds ago vibrant_pasteur
59eb31b27325 centos:7 "python" 5 minutes ago Exited (137) 4 minutes ago magical_ritchie
C:\Users\lenovo>docker stop -t 0 0f53ec43c818
0f53ec43c818
C:\Users\lenovo>docker stop 0f53ec43c818
0f53ec43c818
C:\Users\lenovo>
容器终止 – docker kill
- 作用:
- 强制并立即关闭一个或多个处于暂停状态或者运行状态的容器
- 命令格式:
- docker kill [OPTIONS] CONTAINER [CONTAINER…]
- 命令参数(OPTIONS):
- -s, --signal string 指定发送给容器的关闭信号 (默认“KILL”信号)
C:\Users\lenovo>docker kill --help
Usage: docker kill [OPTIONS] CONTAINER [CONTAINER...]
Kill one or more running containers
Options:
-s, --signal string Signal to send to the container (default "KILL")
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f53ec43c818 centos:7 "python" 8 minutes ago Exited (137) 5 minutes ago vibrant_pasteur
C:\Users\lenovo>docker start 0f53ec43c818
0f53ec43c818
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f53ec43c818 centos:7 "python" 10 minutes ago Up 10 seconds vibrant_pasteur
C:\Users\lenovo>docker kill 0f53ec43c818
0f53ec43c818
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f53ec43c818 centos:7 "python" 10 minutes ago Exited (137) 7 seconds ago vibrant_pasteur
C:\Users\lenovo>
docker stop
和docker kill
的区别
-
前提知识点:
- Linux其中两种终止进程的信号是:SIGTERM和SIGKILL
- SIGKILL信号:无条件终止进程信号。进程接收到该信号会立即终止,不进行清理和暂存工作。该信号不能被忽略、处理和阻塞,它向系统管理员提供了可以杀死任何进程的方法。
- SIGTERM信号:程序终结信号,可以由kill命令产生。与SIGKILL不同的是,SIGTERM信号可以被阻塞和终止,以便程序在退出前可以保存工作或清理临时文件等。
-
docker stop 会先发出SIGTERM信号给进程,告诉进程即将会被关闭。在-t指定的等待时间过了之后,将会立即发出SIGKILL信号,直接关闭容器。
-
docker kill 直接发出SIGKILL信号关闭容器。但也可以通过-s参数修改发出的信号。
-
因此会发现在docker stop的等过过程中,如果终止docker stop的执行,容器最终没有被关闭。而docker kill几乎是立刻发生,无法撤销。
-
此外还有些异常原因也会导致容器被关闭,比如docker daemon重启、容器内部进程运行发生错误等等“异常原因”。
容器重启 – docker restart
- 作用:
- 重启一个或多个处于运行状态、暂停状态、关闭状态或者新建状态的容器
- 该命令相当于stop和start命令的结合
- 命令格式:
- docker restart [OPTIONS] CONTAINER [CONTAINER…]
- 命令参数(OPTIONS):
- -t, --time int 重启前,等待的时间,单位秒(默认 10s)
实则是关闭前等待的时间
- -t, --time int 重启前,等待的时间,单位秒(默认 10s)
C:\Users\lenovo>docker restart --help
Usage: docker restart [OPTIONS] CONTAINER [CONTAINER...]
Restart one or more containers
Options:
-t, --time int Seconds to wait for stop before killing the container
(default 10)
C:\Users\lenovo>
容器删除 – docker container rm
- 作用:
- 删除一个或多个容器
- 命令格式:
- docker container rm [OPTIONS] CONTAINER [CONTAINER…]
或者 docker rm [OPTIONS] CONTAINER [CONTAINER…]
- docker container rm [OPTIONS] CONTAINER [CONTAINER…]
- 命令参数(OPTIONS):
- -f, --force 强行删除容器(会使用 SIGKILL信号)
- -v, --volumes 同时删除绑定在容器上的数据卷
C:\Users\lenovo>docker rm --help
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
-l, --link Remove the specified link
-v, --volumes Remove anonymous volumes associated with the container
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46d0f5054867 centos "/bin/bash" 6 minutes ago Created centos-test
ee3346db835f centos "/bin/bash" 8 minutes ago Created recursing_davinci
a47b98b404ea centos "ls -a" 12 minutes ago Created mystifying_newton
C:\Users\lenovo>docker rm centos-test ee3 a47b98b404ea
centos-test
ee3
a47b98b404ea
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
C:\Users\lenovo>
2.5 容器信息查看
容器详细信息 – docker container inspect
- 作用:
- 查看本地一个或多个容器的详细信息
- 命令格式:
- docker container inspect [OPTIONS] CONTAINER [CONTAINER…]
或者 docker inspect [OPTIONS] CONTAINER [CONTAINER…]
- docker container inspect [OPTIONS] CONTAINER [CONTAINER…]
- 命令参数(OPTIONS):
- -f, --format string 利用特定Go语言的format格式输出结果
- -s, --size 显示总大小
C:\Users\lenovo>docker inspect --help
Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Return low-level information on Docker objects
Options:
-f, --format string Format the output using the given Go template
-s, --size Display total file sizes if the type is container
--type string Return JSON for specified type
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
C:\Users\lenovo>docker run -dti centos:7 bash
f732c03744b5e9d4f7af4bc614477b50bf2adb1317fff55e09c1e03c9cb1d4b8
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f732c03744b5 centos:7 "bash" About a minute ago Up About a minute relaxed_colden
C:\Users\lenovo>docker inspect f732c03744b5
[
{
"Id": "f732c03744b5e9d4f7af4bc614477b50bf2adb1317fff55e09c1e03c9cb1d4b8",
"Created": "2021-01-03T09:19:16.7903198Z",
"Path": "bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2263,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-01-03T09:19:17.3919288Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf",
"ResolvConfPath": "/var/lib/docker/containers/f732c03744b5e9d4f7af4bc614477b50bf2adb1317fff55e09c1e03c9cb1d4b8/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/f732c03744b5e9d4f7af4bc614477b50bf2adb1317fff55e09c1e03c9cb1d4b8/hostname",
"HostsPath": "/var/lib/docker/containers/f732c03744b5e9d4f7af4bc614477b50bf2adb1317fff55e09c1e03c9cb1d4b8/hosts",
"LogPath": "/var/lib/docker/containers/f732c03744b5e9d4f7af4bc614477b50bf2adb1317fff55e09c1e03c9cb1d4b8/f732c03744b5e9d4f7af4bc614477b50bf2adb1317fff55e09c1e03c9cb1d4b8-json.log",
"Name": "/relaxed_colden",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
40,
150
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/a34deffdcbeca0619bc694148d52e162b0678622f81026f4c3ae4fb6611eeb8c-init/diff:/var/lib/docker/overlay2/f1bcb661b333234b53d85773e06fc4ad1b53567b2254ed1fbf4762d35877c01b/diff",
"MergedDir": "/var/lib/docker/overlay2/a34deffdcbeca0619bc694148d52e162b0678622f81026f4c3ae4fb6611eeb8c/merged",
"UpperDir": "/var/lib/docker/overlay2/a34deffdcbeca0619bc694148d52e162b0678622f81026f4c3ae4fb6611eeb8c/diff",
"WorkDir": "/var/lib/docker/overlay2/a34deffdcbeca0619bc694148d52e162b0678622f81026f4c3ae4fb6611eeb8c/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "f732c03744b5",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": true,
"OpenStdin": true,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"bash"
],
"Image": "centos:7",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20201113",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS",
"org.opencontainers.image.created": "2020-11-13 00:00:00+00:00",
"org.opencontainers.image.licenses": "GPL-2.0-only",
"org.opencontainers.image.title": "CentOS Base Image",
"org.opencontainers.image.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "a1eedbdb22296e5081dc1084c181bf39b8b71e2607f30dae4bbbf559c1757c0d",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/a1eedbdb2229",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "0d4f2c5e2d0c574a5cbfffd425bbd07286ff753aade130321ab2f0e0ac02d94a",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "9e0fd76c0d99d2e6c7a83f6b0fa21c9cff97b084f8105b2cb9a06172801d88a0",
"EndpointID": "0d4f2c5e2d0c574a5cbfffd425bbd07286ff753aade130321ab2f0e0ac02d94a",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
C:\Users\lenovo>docker inspect -f "{{json .NetworkSettings.Networks.bridge}}" f732c03744b5
{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"9e0fd76c0d99d2e6c7a83f6b0fa21c9cff97b084f8105b2cb9a06172801d88a0","EndpointID":"0d4f2c5e2d0c574a5cbfffd425bbd07286ff753aade130321ab2f0e0ac02d94a","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02","DriverOpts":null}
C:\Users\lenovo>
容器日志信息 – docker logs
- 作用:
- 查看容器的日志信息——command命令的输出结果,且只能看到主进程信息,子进程信息看不到
- 命令格式:
- docker logs [OPTIONS] CONTAINER
- 命令参数(OPTIONS):
- –details 显示日志的额外信息
- -f, --follow 动态跟踪显示日志信息
- –since string 只显示某事时间节点之后的
- –tail string 显示倒数的行数(默认全部)
- -t, --timestamps 显示timestamps时间
- –until string 只显示某事时间节点之前的
- 注意:
- 容器日志中记录的是容器主进程的输出STDOUT\STDERR
C:\Users\lenovo>docker logs --help
Usage: docker logs [OPTIONS] CONTAINER
Fetch the logs of a container
Options:
--details Show extra details provided to logs
-f, --follow Follow log output
--since string Show logs since timestamp (e.g.
2013-01-02T13:23:37Z) or relative (e.g. 42m for 42
minutes)
-n, --tail string Number of lines to show from the end of the logs
(default "all")
-t, --timestamps Show timestamps
--until string Show logs before a timestamp (e.g.
2013-01-02T13:23:37Z) or relative (e.g. 42m for 42
minutes)
C:\Users\lenovo>docker run -dti centos:7 bash
7d89e02baeac40bf4135e14a931850d0d092f9cc958fe8364f33282016d05780
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d89e02baeac centos:7 "bash" 9 seconds ago Up 8 seconds elegant_babbage
C:\Users\lenovo>docker logs 7d89e02baeac
C:\Users\lenovo>docker run -dti centos:7 python
8d4fbda3593712d03b1bc8335f0fcb1efdd82f18ce64295a6043562822b34c9e
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d4fbda35937 centos:7 "python" 6 seconds ago Up 4 seconds exciting_franklin
7d89e02baeac centos:7 "bash" 51 seconds ago Up 50 seconds elegant_babbage
C:\Users\lenovo>docker logs 8d4fbda35937
Python 2.7.5 (default, Oct 14 2020, 14:45:30)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
C:\Users\lenovo>docker run -dti centos:7 ls -a
a10cfe9ceed67046c00a0292361abf461a9362884bfdcb9fb9e36ec67c198fa3
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a10cfe9ceed6 centos:7 "ls -a" 9 seconds ago Exited (0) 7 seconds ago priceless_austin
8d4fbda35937 centos:7 "python" About a minute ago Up About a minute exciting_franklin
7d89e02baeac centos:7 "bash" About a minute ago Up About a minute elegant_babbage
C:\Users\lenovo>docker logs a10cfe9ceed6
. .dockerenv bin etc lib media opt root sbin sys usr
.. anaconda-post.log dev home lib64 mnt proc run srv tmp var
C:\Users\lenovo>
容器重命名 – docker rename
- 作用:
- 修改容器的名称
- 命令格式:
- docker rename CONTAINER NEW_NAME
- 命令参数(OPTIONS):
- 无
C:\Users\lenovo>docker rename --help
Usage: docker rename CONTAINER NEW_NAME
Rename a container
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a10cfe9ceed6 centos:7 "ls -a" 3 minutes ago Exited (0) 3 minutes ago priceless_austin
8d4fbda35937 centos:7 "python" 4 minutes ago Up 4 minutes exciting_franklin
7d89e02baeac centos:7 "bash" 5 minutes ago Up 5 minutes elegant_babbage
C:\Users\lenovo>docker rename 8d4fbda35937 python-test
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a10cfe9ceed6 centos:7 "ls -a" 4 minutes ago Exited (0) 4 minutes ago priceless_austin
8d4fbda35937 centos:7 "python" 5 minutes ago Up 5 minutes python-test
7d89e02baeac centos:7 "bash" 6 minutes ago Up 6 minutes elegant_babbage
C:\Users\lenovo>
2.6 容器运行时操作
容器连接 – docker attach
- 作用:
- 将当前终端的STDIN、STDOUT、STDERR绑定到正在运行的容器的主进程上实现连接
- 命令格式:
- docker attach [OPTIONS] CONTAINER
- 命令参数(OPTIONS):
- –no-stdin 不绑定STDIN
C:\Users\lenovo>docker attach --help
Usage: docker attach [OPTIONS] CONTAINER
Attach local standard input, output, and error streams to a running container
Options:
--detach-keys string Override the key sequence for detaching a
container
--no-stdin Do not attach STDIN
--sig-proxy Proxy all received signals to the process
(default true)
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
C:\Users\lenovo>docker run -ti centos:7 bash
[root@77f8c47d9cd6 /]# docker ps -a
bash: docker: command not found
[root@77f8c47d9cd6 /]# exit
exit
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
77f8c47d9cd6 centos:7 "bash" 38 seconds ago Exited (127) 9 seconds ago zealous_keldysh
C:\Users\lenovo>docker start 77f8c47d9cd6
77f8c47d9cd6
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
77f8c47d9cd6 centos:7 "bash" About a minute ago Up 7 seconds zealous_keldysh
C:\Users\lenovo>docker attach 77f8c47d9cd6
[root@77f8c47d9cd6 /]# ps -A
PID TTY TIME CMD
1 pts/0 00:00:00 bash
16 pts/0 00:00:00 ps
[root@77f8c47d9cd6 /]# ps -A
PID TTY TIME CMD
1 pts/0 00:00:00 bash
17 pts/0 00:00:00 ps
[root@77f8c47d9cd6 /]# exit
exit
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
77f8c47d9cd6 centos:7 "bash" 3 minutes ago Exited (0) 12 seconds ago zealous_keldysh
C:\Users\lenovo>docker attach 77f8c47d9cd6
You cannot attach to a stopped container, start it first
C:\Users\lenovo>docker start 77f8c47d9cd6
77f8c47d9cd6
C:\Users\lenovo>docker attach 77f8c47d9cd6
[root@77f8c47d9cd6 /]# exit
exit
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
77f8c47d9cd6 centos:7 "bash" 6 minutes ago Exited (0) 37 seconds ago zealous_keldysh
C:\Users\lenovo>docker run -dti centos:7 python
d7205082fdfa94ce4e50ee112ea2608bd8f7c3f893defa92477bfed12b8b28ca
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d7205082fdfa centos:7 "python" 6 seconds ago Up 4 seconds modest_goldberg
77f8c47d9cd6 centos:7 "bash" 6 minutes ago Exited (0) About a minute ago zealous_keldysh
C:\Users\lenovo>docker attach d7205082fdfa
>>>
>>>
>>>
>>> print('hello docker');
hello docker
>>> exit()
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d7205082fdfa centos:7 "python" About a minute ago Exited (0) 5 seconds ago modest_goldberg
77f8c47d9cd6 centos:7 "bash" 7 minutes ago Exited (0) 2 minutes ago zealous_keldysh
C:\Users\lenovo>
容器中执行新命令 – docker exec
- 作用:
- 在容器中运行一个命令
- 命令格式:
- docker exec [OPTIONS] CONTAINER COMMAND [ARG…]
- 命令参数(OPTIONS):
- -d, --detach 后台运行命令
- -i, --interactive 即使没连接容器,也将当前的STDIN绑定上
- -t, --tty 分配一个虚拟终端
- -w, --workdir string 指定在容器中的工作目录
- -e, --env list 设置容器中运行时的环境变量
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
C:\Users\lenovo>docker run -dti centos:7 python
95131a0aea2645ddedda7d47722ab656ca80ccf11d2bb4f49872b2380e61c675
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95131a0aea26 centos:7 "python" 5 seconds ago Up 4 seconds sleepy_shannon
C:\Users\lenovo>docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a
container
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format:
<name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container
# ps -A: 查看容器中所有的进程
C:\Users\lenovo>docker exec 95131a0aea26 ps -A
PID TTY TIME CMD
1 pts/0 00:00:00 python
7 ? 00:00:00 ps
C:\Users\lenovo>docker exec 95131a0aea26 ps -A
PID TTY TIME CMD
1 pts/0 00:00:00 python
13 ? 00:00:00 ps
C:\Users\lenovo>docker exec 95131a0aea26 python
C:\Users\lenovo>docker exec -ti 95131a0aea26 python
Python 2.7.5 (default, Oct 14 2020, 14:45:30)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
# 容器并没有退出
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95131a0aea26 centos:7 "python" 3 minutes ago Up 3 minutes sleepy_shannon
C:\Users\lenovo>docker exec -ti 95131a0aea26 bash
[root@95131a0aea26 /]# ps -a
PID TTY TIME CMD
46 pts/1 00:00:00 ps
[root@95131a0aea26 /]# ps -A
PID TTY TIME CMD
1 pts/0 00:00:00 python
31 pts/1 00:00:00 bash
47 pts/1 00:00:00 ps
[root@95131a0aea26 /]# bash
[root@95131a0aea26 /]# bash
[root@95131a0aea26 /]# ps -A
PID TTY TIME CMD
1 pts/0 00:00:00 python
31 pts/1 00:00:00 bash
48 pts/1 00:00:00 bash
57 pts/1 00:00:00 bash
66 pts/1 00:00:00 ps
[root@95131a0aea26 /]# exit
exit
[root@95131a0aea26 /]# exit
exit
[root@95131a0aea26 /]# exit
exit
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95131a0aea26 centos:7 "python" 8 minutes ago Up 8 minutes sleepy_shannon
# -d后台运行
C:\Users\lenovo>docker exec -dti 95131a0aea26 bash
C:\Users\lenovo>docker exec 95131a0aea26 ps -A
PID TTY TIME CMD
1 pts/0 00:00:00 python
67 pts/1 00:00:00 bash
82 ? 00:00:00 ps
C:\Users\lenovo>docker exec 95131a0aea26 kill -9 67
C:\Users\lenovo>docker exec 95131a0aea26 ps -A
PID TTY TIME CMD
1 pts/0 00:00:00 python
94 ? 00:00:00 ps
# 指定目录下工作
C:\Users\lenovo>docker exec 95131a0aea26 ls
anaconda-post.log
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
C:\Users\lenovo>docker exec -w /root/ 95131a0aea26 ls
anaconda-ks.cfg
C:\Users\lenovo>
三、容器与镜像
3.1 Docker的容器与镜像
Docker容器与镜像的关系
容器提交 – docker commit
- 作用:
- 根据容器生成一个新的镜像
- 命令格式:
- docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
- 命令参数(OPTIONS):
- -a, --author string 作者
- -c, --change list 为创建的镜像加入Dockerfile命令
- -m, --message string 提交信息,类似git commit -m
- -p, --pause 提交时暂停容器 (default true)
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
C:\Users\lenovo>docker run -dti centos bash
d5428e53ce9ecf950970acd68442b13073454d3d66b2ba1044be9d62f117ecbf
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d5428e53ce9e centos "bash" 26 seconds ago Up 24 seconds hopeful_cerf
C:\Users\lenovo>docker exec d5428e53ce9e yum install -y net-tools
CentOS Linux 8 - AppStream 591 kB/s | 6.3 MB 00:10
CentOS Linux 8 - BaseOS 460 kB/s | 2.3 MB 00:05
CentOS Linux 8 - Extras 4.4 kB/s | 8.6 kB 00:01
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
net-tools x86_64 2.0-0.52.20160912git.el8 baseos 322 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 322 k
Installed size: 942 k
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm 216 kB/s | 322 kB 00:01
--------------------------------------------------------------------------------
Total 141 kB/s | 322 kB 00:02
warning: /var/cache/dnf/baseos-f6a80ba95cf937f2/packages/net-tools-2.0-0.52.20160912git.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - BaseOS 722 kB/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 : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Verifying : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Installed:
net-tools-2.0-0.52.20160912git.el8.x86_64
Complete!
C:\Users\lenovo>docker exec d5428e53ce9e ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 6890 bytes 9703168 (9.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3400 bytes 188594 (184.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d5428e53ce9e centos "bash" About a minute ago Up About a minute hopeful_cerf
C:\Users\lenovo>docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
Options:
-a, --author string Author (e.g., "John Hannibal Smith
<hannibal@a-team.com>")
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Commit message
-p, --pause Pause container during commit (default true)
C:\Users\lenovo>docker commit -m 'install net-tools' d5428e53ce9e centos8-net:v1.0
"docker commit" requires at least 1 and at most 2 arguments.
See 'docker commit --help'.
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Create a new image from a container's changes
C:\Users\lenovo>docker commit -m "install net-tools" d5428e53ce9e centos8-net:v1.0
sha256:2df5ef0b1216521232dc1fd4fed203a7893a0beb95023002c1004b2c6c0af99b
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos8-net v1.0 2df5ef0b1216 7 seconds ago 242MB
mysql 5.7 f07dfa83b528 13 days ago 448MB
mysql latest a347a5928046 13 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos7 7.latest 8652b9f0cb4c 7 weeks ago 204MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
C:\Users\lenovo>docker run -dti 2df5ef0b1216 bash
93b78b4871a9a9242f0caa743bdfdcb1f63f2f305e27ee5d8313487f1089a4ec
C:\Users\lenovo>docker exec 2df5ef0b1216 ifconfig
Error: No such container: 2df5ef0b1216
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
93b78b4871a9 2df5ef0b1216 "bash" About a minute ago Up 58 seconds musing_spence
d5428e53ce9e centos "bash" 9 minutes ago Up 9 minutes hopeful_cerf
C:\Users\lenovo>docker exec 93b78b4871a9 ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 10 bytes 796 (796.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
C:\Users\lenovo>
容器导出 – docker export
- 作用:
- 将容器当前的文件系统导出成一个tar文件
- 命令格式:
- docker export [OPTIONS] CONTAINER
- 命令参数(OPTIONS):
- -o, --output string 指定写入的文件,默认是STDOUT
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
C:\Users\lenovo>docker run -dti centos:7 bash
c5cf7302ca92918adff0905a81f5c04e6f85d81ac0b5849111d31d3547248efa
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5cf7302ca92 centos:7 "bash" 7 seconds ago Up 5 seconds eager_colden
C:\Users\lenovo>docker exec c5cf7302ca92 yum -y install net-tools
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirrors.cqu.edu.cn
* extras: mirrors.ustc.edu.cn
* updates: mirrors.cqu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
net-tools x86_64 2.0-0.25.20131004git.el7 base 306 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 306 k
Installed size: 917 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/net-tools-2.0-0.25.20131004git.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for net-tools-2.0-0.25.20131004git.el7.x86_64.rpm is not installed
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-9.2009.0.el7.centos.x86_64 (@CentOS)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : net-tools-2.0-0.25.20131004git.el7.x86_64 1/1
Verifying : net-tools-2.0-0.25.20131004git.el7.x86_64 1/1
Installed:
net-tools.x86_64 0:2.0-0.25.20131004git.el7
Complete!
C:\Users\lenovo>docker exec c5cf7302ca92 ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 8839 bytes 12422773 (11.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3989 bytes 218855 (213.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
C:\Users\lenovo>docker export --help
Usage: docker export [OPTIONS] CONTAINER
Export a container's filesystem as a tar archive
Options:
-o, --output string Write to a file, instead of STDOUT
C:\Users\lenovo>docker export -o centos7-net-tools.tar c5cf7302ca92
C:\Users\lenovo>dir
2021/01/04 22:22 <DIR> .
2021/01/04 22:22 <DIR> ..
2021/01/04 22:22 281,699,840 centos7-net-tools.tar
C:\Users\lenovo>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5cf7302ca92 centos:7 "bash" 3 minutes ago Up 3 minutes eager_colden
C:\Users\lenovo>
容器打包的导入 – docker import
- 作用:
- 从一个tar文件中导入内容创建一个镜像
- 命令格式:
- docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
- 命令参数(OPTIONS):
- -c, --change list 为创建的镜像加入Dockerfile命令
- -m, --message string 导入时,添加提交信息
C:\Users\lenovo>docker import --help
Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Import the contents from a tarball to create a filesystem image
Options:
-c, --change list Apply Dockerfile instruction to the created image
-m, --message string Set commit message for imported image
--platform string Set platform if server is multi-platform capable
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos8-net v1.0 2df5ef0b1216 14 minutes ago 242MB
mysql 5.7 f07dfa83b528 13 days ago 448MB
mysql latest a347a5928046 13 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos7 7.latest 8652b9f0cb4c 7 weeks ago 204MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
C:\Users\lenovo>docker import -m "(import) install net-tools" centos7-net-tools.tar centos-net2:v1.0
sha256:48b2f02b02b746159dadfce601165c56478c58ea905258ba57a508bbfe07ca99
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-net2 v1.0 48b2f02b02b7 7 seconds ago 274MB
centos8-net v1.0 2df5ef0b1216 15 minutes ago 242MB
mysql 5.7 f07dfa83b528 13 days ago 448MB
mysql latest a347a5928046 13 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos7 7.latest 8652b9f0cb4c 7 weeks ago 204MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
C:\Users\lenovo>
commit
和import对比
C:\Users\lenovo>docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-net2 v1.0 48b2f02b02b7 3 minutes ago 274MB
centos8-net v1.0 2df5ef0b1216 18 minutes ago 242MB
mysql 5.7 f07dfa83b528 13 days ago 448MB
mysql latest a347a5928046 13 days ago 545MB
centos latest 300e315adb2f 3 weeks ago 209MB
centos7 7.latest 8652b9f0cb4c 7 weeks ago 204MB
centos 7 8652b9f0cb4c 7 weeks ago 204MB
# import:全新镜像,会丢失父信息
C:\Users\lenovo>docker history centos-net2:v1.0
IMAGE CREATED CREATED BY SIZE COMMENT
48b2f02b02b7 3 minutes ago 274MB (import) install net-tools
# commit:继承,保留所有的历史信息 更多使用
C:\Users\lenovo>docker history centos8-net:v1.0
IMAGE CREATED CREATED BY SIZE COMMENT
2df5ef0b1216 19 minutes ago bash 32.6MB install net-tools
300e315adb2f 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7… 209MB
C:\Users\lenovo>docker history centos:latest
IMAGE CREATED CREATED BY SIZE COMMENT
300e315adb2f 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 3 weeks ago /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7… 209MB
C:\Users\lenovo>
3.2 深入理解Docker的容器与镜像
镜像的Layer
镜像的视角
容器的Layer
容器的视角
容器与镜像的底层关系
容器的运行