Docker常用命令(含详细代码示例)


帮助文档地址https://docs.docker.com/reference/
img

一. 帮助命令


# 显示docker的版本信息
docker version

# 显示docker的系统信息(包括镜像和容器数量)
docker info

# 帮助命令
docker 命令 --help


二. 镜像命令


查看所有镜像

# 查看本地主机所有的镜像
docker images

测试:

[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    bf756fb1ae65   12 months ago   13.3kB

# 解释
REPOSITORY	镜像仓库源
TAG			镜像的标签
IMAGE ID	镜像的ID
CREATED		镜像的创建时间
SIZE		镜像大小

#可选项
--all , -a		列出所有镜像
--quiet , -q	只显示镜像ID

搜索镜像

# 搜索镜像
docker search 镜像名

测试:

[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10414     [OK]       
mariadb                           MariaDB is a community-developed fork of MyS…   3865      [OK]       
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   762                  [OK]
percona                           Percona Server is a fork of the MySQL relati…   524       [OK]       
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   87                   
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   79                   
centurylink/mysql                 Image containing mysql. Optimized to be link…   59                   [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      47                   [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                   [OK]
databack/mysql-backup             Back up mysql databases to... anywhere!         37                   
prom/mysqld-exporter                                                              37                   [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   35                   
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   29                   [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   27                   
circleci/mysql                    MySQL is a widely used, open-source relation…   20                   
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   20                   
arey/mysql-client                 Run a MySQL client from a docker container      17                   [OK]
mysql/mysql-router                MySQL Router provides transparent routing be…   17                   
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]
jelastic/mysql                    An image of the MySQL database server mainta…   1                    
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   1                    [OK]

# 可选项
-f, --filter filter   根据提供的条件过滤输出
# 例如搜索STARS大于3000的
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker search mysql -f STARS=3000
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   10414     [OK]       
mariadb   MariaDB is a community-developed fork of MyS…   3865      [OK]   

下载镜像

# 下载镜像
docker pull 镜像名[:tag]

测试:

# 不指定版本号默认下载最新版latest
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
a076a628af6f: Pull complete	#分层下载,docker image的核心:联合文件系统
f6c208f3f991: Pull complete 
88a9455a9165: Pull complete 
406c9b8427c6: Pull complete 
7c88599c0b25: Pull complete 
25b5c6debdaf: Pull complete 
43a5816f1617: Pull complete 
1a8c919e89bf: Pull complete 
9f3cf4bd1a07: Pull complete 
80539cea118d: Pull complete 
201b3cad54ce: Pull complete 
944ba37e1c06: Pull complete 
Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest	#真实地址

# 指定版本号下载
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Already exists 
f6c208f3f991: Already exists 
88a9455a9165: Already exists 
406c9b8427c6: Already exists 
7c88599c0b25: Already exists 
25b5c6debdaf: Already exists 
43a5816f1617: Already exists 
1831ac1245f4: Pull complete 
37677b8c1f79: Pull complete 
27e4ac3b0f6e: Pull complete 
7227baa8c445: Pull complete 
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

下载完再次查看所有镜像,即可看到刚下载的镜像
image-20210125201641758


删除镜像

# 删除镜像
docker rmi
 `docker rmi -f 镜像id`:删除指定镜像
 `docker rmi -f 镜像id 镜像id 镜像id`:删除指定多个镜像
 `docker rmi -f $(docker iamges -aq)`:删除全部镜像

测试:

[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
mysql         5.7       a70d36bc331a   5 days ago      449MB
mysql         latest    c8562eaf9d81   5 days ago      546MB
hello-world   latest    bf756fb1ae65   12 months ago   13.3kB
#删除指定镜像
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker rmi -f a70d36bc331a 
Untagged: mysql:5.7
Untagged: mysql@sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Deleted: sha256:a70d36bc331a13d297f882d3d63137d24b804f29fa67158c40ad91d5050c39c5
Deleted: sha256:50c77bf7bcddd1f1d97789d80ac2404eec22c860c104e858620d2a2e321f0ef7
Deleted: sha256:14244329b83dfc8982398ee4104a548385652d2bffb957798ff86a419013efd6
Deleted: sha256:6d990477f90af28473eb601a9bca22253f6381e053c5a8edda0a4f027e124a3c
Deleted: sha256:ee0449796df204071589162fc16f8d65586312a40c68d1ba156c93c56f5e5ce8
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
mysql         latest    c8562eaf9d81   5 days ago      546MB
hello-world   latest    bf756fb1ae65   12 months ago   13.3kB
#删除所有镜像
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker rmi -f $(docker images -aq)
Untagged: mysql:latest
Untagged: mysql@sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c
Deleted: sha256:c8562eaf9d81c779cbfc318d6e01b8e6f86907f1d41233268a2ed83b2f34e748
Deleted: sha256:1b649b85960473808c6b812fc30c3f6a3ff1c0ffdcba5c9435daf01cf7d5373a
Deleted: sha256:19cc889447050c16c797fd209fa114ee219de23facb37c00d4137a4ed4aad922
Deleted: sha256:3c793c06a026d276cf56a6a6a75527026ed9eafa7a7d21a438f7d5ed2314148e
Deleted: sha256:1e1cd89a2bc183a7fea3dab0b543e9924278321ad0921c22cc088adbf3c2e77b
Deleted: sha256:83b2015dfd000588c7c947b2d89b3be7a8e5a3abc6ab562668c358033aa779ec
Deleted: sha256:d08533f1e2acc40ad561a46fc6a76b54c739e6b24f077c183c5709e0a6885312
Deleted: sha256:4f9d91a4728e833d1062fb65a792f06e22e425f63824f260c8b5a64b776ddc38
Deleted: sha256:20bf4c759d1b0d0e6286d2145453af4e0e1b7ba3d4efa3b8bce46817ad4109de
Deleted: sha256:a9371bbdf16ac95cc72555c6ad42f79b9f03a82d964fe89d52bdc5f335a5f42a
Deleted: sha256:5b02130e449d94f51e8ff6e5f7d24802246198749ed9eb064631e63833cd8f1d
Deleted: sha256:ab74465b38bc1acb16c23091df32c5b7033ed55783386cb57acae8efff9f4b37
Deleted: sha256:cb42413394c4059335228c137fe884ff3ab8946a014014309676c25e3ac86864
Untagged: hello-world:latest
Untagged: hello-world@sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Deleted: sha256:bf756fb1ae65adf866bd8c456593cd24beb6a0a061dedf42b26a993176745f6b
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE


三. 容器命令


有了镜像才能够创建容器,这里下载一个centos镜像为以下测试做准备

[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete 
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest

新建容器并启动

# 新建容器并启动
docker run [可选参数] image

# 参数说明
--name="Name"	 容器名字,用于区分容器
-d				后台方式运行
-it				使用交互方式运行,进入容器查看内容
-p				指定容器端口(小写)
	-p ip:主机端口:容器端口
	-p 主机端口:容器端口(最常用)
	-P 容器端口
	容器端口
-P				随机执行端口(大写)

测试:

#测试,启动进入容器
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker run -it centos /bin/bash
[root@22d0484830c9 /]# ls	查看容器内的centos,基础版本,很多命令不完善
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

#退出容器到主机
[root@22d0484830c9 /]# exit
exit
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# ls
3000  cp  cp.tar.gz  f.sh  hello.c  install.sh  login.sh  read.sh  time.sh

列出所有运行的容器

# 列出所有运行的容器
docker ps [可选参数]

# 参数说明
-a		列出当前正在运行以及历史运行过的容器
-n=?	显示最近创建的容器
-q		只显示容器编号

测试

[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED              STATUS                          PORTS     NAMES
22d0484830c9   centos         "/bin/bash"   About a minute ago   Exited (0) About a minute ago             condescending_mcnulty
3efe49d28bdd   bf756fb1ae65   "/hello"      7 hours ago          Exited (0) 7 hours ago                    confident_babbage
40fc94e7b0a4   bf756fb1ae65   "/hello"      7 months ago         Exited (0) 7 months ago                   inspiring_lovelace
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker ps -n=1
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
22d0484830c9   centos    "/bin/bash"   4 minutes ago   Exited (0) 3 minutes ago             condescending_mcnulty
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker ps -aq
22d0484830c9
3efe49d28bdd
40fc94e7b0a4

退出容器

# 容器停止并退出
exit

# 容器不停止退出
Ctrl+p+q

删除容器

# 删除指定容器,不能删除正在运行的容器,强制删除用rm -f
docker rm 容器id	

# 删除所有容器
docker rm -f $(docker ps -aq)
docker ps -a -q|xargs docker rm

测试:

[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker ps -aq
6518909c5fb2
3dfd73738e3b
22d0484830c9
3efe49d28bdd
40fc94e7b0a4
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker rm 6518909c5fb2
6518909c5fb2
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker ps -aq
3dfd73738e3b
22d0484830c9
3efe49d28bdd
40fc94e7b0a4
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker rm -f $(docker ps -aq)
3dfd73738e3b
22d0484830c9
3efe49d28bdd
40fc94e7b0a4
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker ps -aq

启动停止容器

docker start 容器id		# 启动容器
docker restart 容器id		# 重启容器
docker stop 容器id		# 停止正在运行的容器
docker kill 容器id		# 强制停止当前容器


四. 常用其他命令


后台启动容器

# 后台启动容器
docker run -d 容器名

测试:

[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker run -d centos
9606d8980f73ff313c64388f5f82e036072cc7191c3305ab80b371e37d8b1ad7
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
9606d8980f73   centos    "/bin/bash"   54 seconds ago   Exited (0) 53 seconds ago             youthful_dubinsky
  • 问题:后台启动容器,再用 docker ps 查看发现容器停止
  • 原因:docker 容器使用后台运行,就必须有一个前台进程;如果仅仅后台启动,没有应用来提供服务,就会自动停止

查看日志

docker logs [可选参数] 容器ID
      --details        显示提供给日志的其他详细信息
  -f, --follow         跟踪日志输出
  -n, --tail string    指定要显示的日志条数 (默认为全部)
  -t, --timestamps     显示时间戳

测试:

# 后台启动容器,同时执行脚本不停打印zsr
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker run -d centos /bin/bash -c "while true;do echo zsr;sleep 1;done"
980daf06de5878e2d3e5c048c7de92817e09e92f20d2d6242ecb15b9b7a5bd02
# 查看运行的容器,此时可以查看到
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
980daf06de58   centos    "/bin/bash -c 'while…"   6 seconds ago   Up 6 seconds             distracted_germain
# 查看日志信息,显示时间戳,指定显示10条日志
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker logs -t -n 10 980daf06de58 
2021-01-25T04:53:47.619357189Z zsr
2021-01-25T04:53:48.621476433Z zsr
2021-01-25T04:53:49.623833490Z zsr
2021-01-25T04:53:50.626253957Z zsr
2021-01-25T04:53:51.628526460Z zsr
2021-01-25T04:53:52.630878536Z zsr
2021-01-25T04:53:53.633046201Z zsr
2021-01-25T04:53:54.635296632Z zsr
2021-01-25T04:53:55.637614256Z zsr
2021-01-25T04:53:56.640358130Z zsr


查看容器中的进程

# 查看容器中的进程
docker top 容器ID

测试:

[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker top 980daf06de58
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                28359               28339               0                   12:52               ?                   00:00:00            /bin/bash -c while true;do echo zsr;sleep 1;done
root                29065               28359               0                   12:58               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

查看镜像元数据

# 查看镜像元数据
docker inspect 容器ID

测试:

[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker inspect 980daf06de58
[
    {
        "Id": "980daf06de5878e2d3e5c048c7de92817e09e92f20d2d6242ecb15b9b7a5bd02",
        "Created": "2021-01-25T04:52:46.079489883Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "while true;do echo zsr;sleep 1;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 28359,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-01-25T04:52:46.469990116Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/980daf06de5878e2d3e5c048c7de92817e09e92f20d2d6242ecb15b9b7a5bd02/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/980daf06de5878e2d3e5c048c7de92817e09e92f20d2d6242ecb15b9b7a5bd02/hostname",
        "HostsPath": "/var/lib/docker/containers/980daf06de5878e2d3e5c048c7de92817e09e92f20d2d6242ecb15b9b7a5bd02/hosts",
        "LogPath": "/var/lib/docker/containers/980daf06de5878e2d3e5c048c7de92817e09e92f20d2d6242ecb15b9b7a5bd02/980daf06de5878e2d3e5c048c7de92817e09e92f20d2d6242ecb15b9b7a5bd02-json.log",
        "Name": "/distracted_germain",
        "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,
            "CgroupnsMode": "host",
            "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": [
                0,
                0
            ],
            "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/c836c661b72e93a97b704d62711facfd82b4f080359405a4382a8dca3f7ce2ed-init/diff:/var/lib/docker/overlay2/e7eaf7928b9265d758185f4ac2e84ab99cb6146cbf37d10a9d9b3ce1ad30ab3e/diff",
                "MergedDir": "/var/lib/docker/overlay2/c836c661b72e93a97b704d62711facfd82b4f080359405a4382a8dca3f7ce2ed/merged",
                "UpperDir": "/var/lib/docker/overlay2/c836c661b72e93a97b704d62711facfd82b4f080359405a4382a8dca3f7ce2ed/diff",
                "WorkDir": "/var/lib/docker/overlay2/c836c661b72e93a97b704d62711facfd82b4f080359405a4382a8dca3f7ce2ed/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "980daf06de58",
            "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",
                "-c",
                "while true;do echo zsr;sleep 1;done"
            ],
            "Image": "centos",
            "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"
            }
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "6a7fa868d1b07689ed1307cd6eaffa3d102c13440bddf41b7eedd526a328f03e",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/6a7fa868d1b0",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "ab462efd8637f15fd7aabe6250429571db2f4dc22e3248278dbe513af5e47e38",
            "Gateway": "172.18.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.18.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:12:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "0524bfb4e90c798594ceca6e4099d5b1407f7dcef80435f023f4a1963a35fbb4",
                    "EndpointID": "ab462efd8637f15fd7aabe6250429571db2f4dc22e3248278dbe513af5e47e38",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

进入当前正在运行的容器

容器通常都是使用后台方式运行的,这时候可能需要进入容器,修改一些配置

# 方式一: 进入容器后开启一个新的终端,可在里面进行操作(常用)
docker exec -it 容器id /bin/bash

# 方式二: 进入容器正在执行的终端,不会启动新的进程
docker attach 容器ID

测试:

# 测试方式一
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS     NAMES
980daf06de58   centos    "/bin/bash -c 'while…"   2 hours ago   Up 2 hours             distracted_germain
# 进入到容器
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker exec -it 980daf06de58 /bin/bash
[root@980daf06de58 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

# 测试方式二
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS     NAMES
980daf06de58   centos    "/bin/bash -c 'while…"   2 hours ago   Up 2 hours             distracted_germain
[root@iZ2ze3zdx4jq8v6hetjjuxZ ~]# docker attach 980daf06de58
zsr
zsr
zsr
zsr
zsr
zsr
zsr
#进入死循环...

从容器内拷贝文件到主机

# 从容器内拷贝文件到主机
docker cp 容器id:容器内路径 目的主机路径

测试:

[root@iZ2ze3zdx4jq8v6hetjjuxZ home]# docker run -it centos /bin/bash
[root@64b40f051756 /]# cd /home
[root@64b40f051756 home]# ls
# 在容器内部新建一个文件
[root@64b40f051756 home]# touch 1.txt
[root@64b40f051756 home]# exit
exit
[root@iZ2ze3zdx4jq8v6hetjjuxZ home]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@iZ2ze3zdx4jq8v6hetjjuxZ home]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                        PORTS     NAMES
64b40f051756   centos    "/bin/bash"              4 minutes ago    Exited (0) 59 seconds ago               beautiful_ganguly
c1b3f151ae12   centos    "/bin/bash"              7 minutes ago    Exited (127) 4 minutes ago              nervous_lamarr
60b1f058de74   centos    "/bin/bash"              14 minutes ago   Exited (0) 12 minutes ago               vibrant_jemison
980daf06de58   centos    "/bin/bash -c 'while…"   2 hours ago      Exited (137) 14 minutes ago             distracted_germain
d77c3cdfb7e2   centos    "/bin/bash -c 'while…"   3 hours ago      Exited (137) 2 hours ago                relaxed_meninsky
7706ccced225   centos    "/bin/bash -c 'whilr…"   3 hours ago      Exited (1) 3 hours ago                  romantic_hopper
6eebf828ab4c   centos    "/bin/bash -c 'whilr…"   3 hours ago      Exited (1) 3 hours ago                  brave_goldberg
df422d73861f   centos    "/bin/bash"              3 hours ago      Exited (0) 3 hours ago                  frosty_panini
744b9b044715   centos    "/bin/bash"              3 hours ago      Exited (127) 3 hours ago                amazing_cori
9606d8980f73   centos    "/bin/bash"              3 hours ago      Exited (0) 3 hours ago                  youthful_dubinsky
[root@iZ2ze3zdx4jq8v6hetjjuxZ home]# cd /home
[root@iZ2ze3zdx4jq8v6hetjjuxZ home]# ls
redis  www  zsr
# 将容器内创建的1.txt拷贝到主机
[root@iZ2ze3zdx4jq8v6hetjjuxZ home]# docker cp 64b40f051756:/home/1.txt /home
[root@iZ2ze3zdx4jq8v6hetjjuxZ home]# ls
1.txt  redis  www  zsr
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页