Docker:从入门到入门

参考b站【狂神说Java】Docker最新超详细版教程通俗易懂

官方帮助文档

一、Docker的安装

本教程选用系统:Ubuntu 20.04

1.1 环境查看

huif@huif-Ubuntu20:~$ uname -r
5.8.0-43-generic
#系统版本
huif@huif-Ubuntu20:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.2 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.2 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal

1.2 安装

#1、卸载旧版本
~$ sudo apt-get remove docker docker-engine docker.io containerd runc

 #2、安装需要的包
~$ sudo apt-get update
~$ sudo apt-get install \
   apt-transport-https \
   ca-certificates \
   curl \
   gnupg \
   lsb-release

#3、加入官方的GPS key
~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

#4、设置稳定版镜像 国外的有点慢,建议用阿里云地址https://d4uds4zs.mirror.aliyuncs.com
~$ echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

#5.安装
~$ sudo apt-get update
~$ sudo apt-get install docker-ce docker-ce-cli containerd.io

#6、启动docker
~$ systemctl start docker
~$ sudo docker version
Client: Docker Engine - Community
 Version:           20.10.8
 API version:       1.41
 Go version:        go1.16.6
 Git commit:        3967b7d
 Built:             Fri Jul 30 19:54:27 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.8
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.6
  Git commit:       75249d8
  Built:            Fri Jul 30 19:52:33 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.9
  GitCommit:        e25210fe30a0a703442421b0f60afac609f950a3
 runc:
  Version:          1.0.1
  GitCommit:        v1.0.1-0-g4144b63
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
  
#7、测试
~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete 
Digest: sha256:0fe98d7debd9049c50b597ef1f85b7c1e8cc81f59c8d623fcb2250e8bec85b38
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

#8、查看镜像
~$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   5 months ago   13.3kB

1.3 卸载镜像

#1、卸载依赖
sudo apt-get purge docker-ce docker-ce-cli containerd.io
#2、删除资源
sudo rm -rf /var/lib/docker  #默认资源中心
sudo rm -rf /var/lib/containerd

1.4 阿里云镜像加速

以下为配置阿里云镜像加速器
在这里插入图片描述

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://d4uds4zs.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

通过将用户添加到docker用户组可以将sudo去掉,命令如下

sudo groupadd docker #添加docker用户组
sudo gpasswd -a $USER docker #将登陆用户加入到docker用户组中
newgrp docker #更新用户组

1.4 底层原理

1.4.1 Docker是怎么工作的?

Docker是一个Client-Server结构的系统,Docker的守护进程运行在主机上。通过Socket从客户端访问
Docker-Server接受到Docker-Client的指令,就会执行这个命令
在这里插入图片描述

1.4.2 Docker VS VM

  1. Docker的抽象层更少
  2. Docker利用的是宿主机的内核,VM需要是Guest OS。
    在这里插入图片描述

二、Docker的常用命令

2.1 帮助命令

命令说明
docker version查看版本信息
docker version显示docker的系统信息,包括镜像和容器的数量
docker --help帮助命令,查看所有指令信息
systemctl start docker启动Docker

帮助文档的地址

2.2 镜像命令

命令说明
docker images查看镜像
docker search搜索镜像
docker pull拉取下载镜像
docker rmi删除镜像

docker images 查看镜像

~$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
hello-world   latest    d1165f221234   5 months ago   13.3kB
# 解释
REPOSITORY   镜像的仓库源
TAG          镜像的标签
IMAGE ID     镜像的id
CREATED      镜像的创建时间
SIZE         镜像的大小
#可选项
  -a, --all             Show all images (default hides intermediate images)
  -q, --quiet           Only show image IDs

docker search 搜索镜像

dockerhub 搜索镜像

~$ docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11258     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4277      [OK]       
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   836                  [OK]
#可选项,通过搜索来过滤   #搜索STARS>3000的mysql
~$ docker search mysql --filter=STARS=3000  
NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql     MySQL is a widely used, open-source relation…   11258     [OK]       
mariadb   MariaDB Server is a high performing open sou…   4277      [OK] 

docker pull 拉取下载镜像

~$ docker pull mysql  #等价于docker pull docker.io/library/mysql:latest
Using default tag: latest #如果不写tag,默认是latest
latest: Pulling from library/mysql
33847f680f63: Pull complete #分层下载
5cb67864e624: Pull complete 
1a2b594783f5: Pull complete 
b30e406dd925: Pull complete 
48901e306e4c: Pull complete 
603d2b7147fd: Pull complete 
802aa684c1c4: Pull complete 
715d3c143a06: Pull complete 
6978e1b7a511: Pull complete 
f0d78b0ac1be: Pull complete 
35a94d251ed1: Pull complete 
36f75719b1a9: Pull complete 
Digest: sha256:8b928a5117cf5c2238c7a09cd28c2e801ac98f91c3f8203a8938ae51f14700fd#签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest#真实地址

#指定版本下载
~$ docker pull mysql:5.7
5.7: Pulling from library/mysql
33847f680f63: Already exists 
5cb67864e624: Already exists 
1a2b594783f5: Already exists 
b30e406dd925: Already exists 
48901e306e4c: Already exists 
603d2b7147fd: Already exists 
802aa684c1c4: Already exists 
5b5a19178915: Pull complete 
f9ce7411c6e4: Pull complete 
f51f6977d9b2: Pull complete 
aeb6b16ce012: Pull complete 
Digest: sha256:be70d18aedc37927293e7947c8de41ae6490ecd4c79df1db40d1b5b5af7d9596
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

docker rmi 删除镜像

命令含义
docker rmi -f 镜像d删除指定的镜像
docker rmi -f 镜像id 镜像id 镜像id删除多个镜像
docker rmi -f $(docker images -aq)删除全部的镜像
~$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         5.7       8cf625070931   3 weeks ago    448MB
mysql         latest    c60d96bd2b77   3 weeks ago    514MB
hello-world   latest    d1165f221234   5 months ago   13.3kB

#按ID删除
~$ docker rmi -f 8cf625070931 
Untagged: mysql:5.7
Untagged: mysql@sha256:be70d18aedc37927293e7947c8de41ae6490ecd4c79df1db40d1b5b5af7d9596
Deleted: sha256:8cf6250709314f2fcd2669e8643f5d3bdebfe715bddb63990c8c96e5d261d6fc
Deleted: sha256:452fe6896278c26338d547f8d1092011d923785247c46629b374d3477fe28c84
Deleted: sha256:bd40bf60af5d06e6b93eaf5a648393d97f70998faa3bfa1b85af55b5a270cb35
Deleted: sha256:c43e9e7d1e833650e0ed54be969d6410efa4e7fa6e27a236a44a2b97e412ee93
Deleted: sha256:70f18560bbf492ddb2eadbc511c58c4d01e51e8f5af237e3dbb319632f16335b

~$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
mysql         latest    c60d96bd2b77   3 weeks ago    514MB
hello-world   latest    d1165f221234   5 months ago   13.3kB

#删除所有
docker rmi -f $(docker images -aq)
Untagged: mysql:latest
Untagged: mysql@sha256:8b928a5117cf5c2238c7a09cd28c2e801ac98f91c3f8203a8938ae51f14700fd
Deleted: sha256:c60d96bd2b771a8e3cae776e02e55ae914a6641139d963defeb3c93388f61707
Deleted: sha256:5c8c91273faab368a6d659156f2569fa9f40b0e0139222fdf9eef073df4b3797
Deleted: sha256:33d8196a776f42a16f10395b66f10f91443b1fb194bca2a9b8dfb0deff5babb8
Deleted: sha256:3ec63323025213e3cabf17ac7933506dc5520ec49226a9764418f77ea60d35c8
Deleted: sha256:1f129b005b51b049ac84ed0775b82096d480b7d9308a9a137697f37346562266
Deleted: sha256:80ed209bd0434faa1ce31fbaab8508124dddf8f6502c5736ee4b8e46697a8477
Deleted: sha256:e53f0d35c77064014a5c1c1332d84d5f421a58418ca9c208bc470691c0e483e3
Deleted: sha256:75209fb28131d5537e73406ff0f6f508f3eb1f4d86c43d1d16df76fd28b9cc35
Deleted: sha256:34a01bee1a62a01034ffc3da48a3cb45716a0cf2e264c26663e02288e81c7ec2
Deleted: sha256:9f8bca37a56017fd3462d4fc329b0b20f97c2dd4c15e55a8e6ad1c023ab5552b
Deleted: sha256:c8a6e3f9a2412c28cd8c48e2c7bed5e7fbaa0ab6649add2dbe8641cb29b967f6
Deleted: sha256:0a26eacdbd862e75d064d817e8a5bcf5e060c2680c10f77ffa52757c0b8c3328
Deleted: sha256:814bff7343242acfd20a2c841e041dd57c50f0cf844d4abd2329f78b992197f4
Untagged: hello-world:latest
Untagged: hello-world@sha256:0fe98d7debd9049c50b597ef1f85b7c1e8cc81f59c8d623fcb2250e8bec85b38
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726

2.3 容器命令

说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像来学习

~$ 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"   容器名字 tomacat01, tomacat02,用来区分容器
-d              后台方式运行
-it             使用交互方式运行,进入容器查看内容
-P              指定容器的端口,分三种情况
					1. ip:主机端口:容器端口
					2. 主机端口:容器端口
					3. 容器端口
-p              随机指定端口
#测试,启动并进入容器
huif@huif-Ubuntu20:~$ docker run -it centos /bin/bash
#查看容器内的列表
[root@bbd7d27d3ff9 /]# ls   #
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@bbd7d27d3ff9 /]# exit
exit
huif@huif-Ubuntu20:~$ 

docker ps 查看正在运行的容器

命令说明
docker ps查看正在运行的容器,-a:查看历史运行过的容器; -n= x x x :显示最近 x x x条创建的容器;-q:只显示容器的编号
huif@huif-Ubuntu20:~$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
#查看所有运行过的容器
huif@huif-Ubuntu20:~$ docker ps -a   
CONTAINER ID   IMAGE          COMMAND       CREATED         STATUS                          PORTS     NAMES
bbd7d27d3ff9   centos         "/bin/bash"   2 minutes ago   Exited (0) About a minute ago             keen_jepsen
9507a13e5fd3   d1165f221234   "/hello"      4 hours ago     Exited (0) 4 hours ago                    gracious_fermi
huif@huif-Ubuntu20:~$ docker ps -a -n=1
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
bbd7d27d3ff9   centos    "/bin/bash"   16 minutes ago   Exited (0) 15 minutes ago             keen_jepsen
huif@huif-Ubuntu20:~$ docker ps -aq
bbd7d27d3ff9
9507a13e5fd3

exit退出容器

exit   #停止容器运行并退出
ctrl + p + q   #不停止容器运行并退出

docker rm删除容器

命令说明
docker rm 容器id删除指定的容器,不能删除正在运行的容器,如果要强制删除:rm -f
docker rm -f $(docker ps -aq)强制删除所有的容器
docker pa -a|xargs docker rm删除所有的容器
huif@huif-Ubuntu20:~$ docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS                      PORTS     NAMES
879a0fa95418   centos         "/bin/bash"   5 minutes ago    Up 5 minutes                          brave_heisenberg
bbd7d27d3ff9   centos         "/bin/bash"   28 minutes ago   Exited (0) 26 minutes ago             keen_jepsen
9507a13e5fd3   d1165f221234   "/hello"      5 hours ago      Exited (0) 5 hours ago                gracious_fermi
huif@huif-Ubuntu20:~$ docker rm bbd7d27d3ff9   #删除bbd7d27d3ff9 的容器
bbd7d27d3ff9
huif@huif-Ubuntu20:~$ docker ps -aq
879a0fa95418
9507a13e5fd3
huif@huif-Ubuntu20:~$ docker rm $(docker ps -aq) #无法删除正在运行的容器
9507a13e5fd3
Error response from daemon: You cannot remove a running container 879a0fa954187094e9cfe69a06bba142fb8796febf83190e77e3ad72096e46d8. Stop the container before attempting removal or force remove
huif@huif-Ubuntu20:~$ docker ps -aq
879a0fa95418


start , restart ,stop ,kill 启动和停止容器的操作

命令说明
docker start 容器id启动容器
docker restart 容器id停止容器
docker stop 容器id停止当前正在运行的容器
docker kill 容器id强制停止正在运行的容器
huif@huif-Ubuntu20:~$ docker start 879a0fa95418
879a0fa95418
huif@huif-Ubuntu20:~$ docker ps 
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
879a0fa95418   centos    "/bin/bash"   13 minutes ago   Up 13 minutes             brave_heisenberg
huif@huif-Ubuntu20:~$ docker stop 879a0fa95418 
879a0fa95418
huif@huif-Ubuntu20:~$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

docker run 后台启动容器

docker run -d 容器id

huif@huif-Ubuntu20:~$ docker run -d centos
b875f70fcebac48db00559b23243cfea6d3767962f4d2b826f7c94dd1d6e8bd8
huif@huif-Ubuntu20:~$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

问题: run之后发现centos停止了
常见的坑: docker 容器使用后台运行,就必须有一个前台进程,docker发现没有应用,就会自动停止
解决:可以用-it交互

docker logs 查看日志

docker logs -tf --tail 条数 容器id

#输入shell脚本
huif@huif-Ubuntu20:~$ docker run -d centos /bin/bash -c "for((i=1;i<20;i++));do echo 'you are so beautiful';sleep 1;done"
8e30746667e11528a5f157737cab226badc34b3c39c806694cb692d664637fe0
#查看正在运行的容器
huif@huif-Ubuntu20:~$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
8e30746667e1   centos    "/bin/bash -c 'for((…"   4 seconds ago   Up 3 seconds             silly_tesla
#打印10条日志
huif@huif-Ubuntu20:~$ docker logs -ft --tail 10 8e30746667e1 
2021-08-13T01:26:51.267596495Z you are so beautiful
2021-08-13T01:26:52.273498639Z you are so beautiful
2021-08-13T01:26:53.278449513Z you are so beautiful
2021-08-13T01:26:54.283725388Z you are so beautiful
2021-08-13T01:26:55.289850633Z you are so beautiful
2021-08-13T01:26:56.296083630Z you are so beautiful
2021-08-13T01:26:57.301224351Z you are so beautiful
2021-08-13T01:26:58.306065218Z you are so beautiful
2021-08-13T01:26:59.311384021Z you are so beautiful
2021-08-13T01:27:00.316057480Z you are so beautiful
huif@huif-Ubuntu20:~$ 
docker top 查看容器中进程信息

docker top 容器id

huif@huif-Ubuntu20:~$ docker top b664e51c737a 
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                275767              275744              0                   09:34               ?                   00:00:00            /bin/bash -c for((i=1;i<20;i++));do echo 'you are so beautiful';sleep 1;done
root                275834              275767              0                   09:34               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
huif@huif-Ubuntu20:~$ 
docker inspect 查看镜像源数据

docker inspect 容器id

huif@huif-Ubuntu20:~$ docker inspect b664e51c737a 
[
    {
        "Id": "b664e51c737a5bd2ced0a6ad61cb0a282c16099855de7273a15782ea477a1efe",
        "Created": "2021-08-13T01:34:34.200153104Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "for((i=1;i<20;i++));do echo 'you are so beautiful';sleep 1;done"
        ],
        "State": {
            "Status": "exited",
            "Running": false,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 0,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-08-13T01:34:34.642094996Z",
            "FinishedAt": "2021-08-13T01:34:53.735943271Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/b664e51c737a5bd2ced0a6ad61cb0a282c16099855de7273a15782ea477a1efe/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/b664e51c737a5bd2ced0a6ad61cb0a282c16099855de7273a15782ea477a1efe/hostname",
        "HostsPath": "/var/lib/docker/containers/b664e51c737a5bd2ced0a6ad61cb0a282c16099855de7273a15782ea477a1efe/hosts",
        "LogPath": "/var/lib/docker/containers/b664e51c737a5bd2ced0a6ad61cb0a282c16099855de7273a15782ea477a1efe/b664e51c737a5bd2ced0a6ad61cb0a282c16099855de7273a15782ea477a1efe-json.log",
        "Name": "/strange_nobel",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "docker-default",
        "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/9a3e0c2901ca762b6d5e3bf58f2f3269c41e7ab3b55c5203bf3f92dd781c34af-init/diff:/var/lib/docker/overlay2/82a5a9eda333fce8e93c80f0983860bdc8106b7916c5550a70c59ee5685b430a/diff",
                "MergedDir": "/var/lib/docker/overlay2/9a3e0c2901ca762b6d5e3bf58f2f3269c41e7ab3b55c5203bf3f92dd781c34af/merged",
                "UpperDir": "/var/lib/docker/overlay2/9a3e0c2901ca762b6d5e3bf58f2f3269c41e7ab3b55c5203bf3f92dd781c34af/diff",
                "WorkDir": "/var/lib/docker/overlay2/9a3e0c2901ca762b6d5e3bf58f2f3269c41e7ab3b55c5203bf3f92dd781c34af/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "b664e51c737a",
            "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",
                "for((i=1;i<20;i++));do echo 'you are so beautiful';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": "41181dc147d8f348197e31d125a779aef6cd551aa0d064f4639e7fcd64861cbf",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/41181dc147d8",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "70fc9dba8dd58ea220281a6b22abd9bb949a005c71c5523cb214cf750c656928",
                    "EndpointID": "",
                    "Gateway": "",
                    "IPAddress": "",
                    "IPPrefixLen": 0,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "",
                    "DriverOpts": null
                }
            }
        }
    }
]
docker exec和docker attach :进入当前正在进行的容器

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

huif@huif-Ubuntu20:~$ docker run -d centos /bin/bash -c "for((i=1;i<200;i++));do echo 'you are so beautiful';sleep 1;done"
fa1c7faa49822f003b8106aa616a3affa1f81409e74680bd7a637dd49bc3a42b
huif@huif-Ubuntu20:~$ docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
fa1c7faa4982   centos    "/bin/bash -c 'for((…"   7 seconds ago   Up 6 seconds             distracted_hawking
huif@huif-Ubuntu20:~$ docker exec -it fa1c7faa4982 /bin/bash
[root@fa1c7faa4982 /]#  exit
exit

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

huif@huif-Ubuntu20:~$ docker attach fa1c7faa4982
you are so beautiful
you are so beautiful
you are so beautiful
you are so beautiful
you are so beautiful
you are so beautiful
you are so beautiful
you are so beautiful
you are so beautiful

2.4容器与宿主机间文件相互拷贝

2.4.1 docker cp 从容器内拷贝文件到主机

docker cp 容器id:容器内路径 目的地主机

#1. 启动容器
huif@huif-Ubuntu20:~$ docker run -it centos /bin/bash
#2. 查看容器内镜像
[root@55feb5a5b7ab /]# huif@huif-Ubuntu20:~$ docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
55feb5a5b7ab   centos    "/bin/bash"   22 seconds ago   Up 22 seconds             strange_kepler
#3. 在容器内的/home目录下创建fanqie.java
[root@55feb5a5b7ab /]# cd /home
[root@55feb5a5b7ab home]# ls
[root@55feb5a5b7ab home]# touch fanqie.java
#4. 退出容器回到主机
[root@55feb5a5b7ab home]# exit
exit
#5. 将容器内的文件拷贝到主机
huif@huif-Ubuntu20:/home$ sudo docker cp 55feb5a5b7ab:/home/fanqie.java /home
huif@huif-Ubuntu20:/home$ ls
fanqie.java  huif  lost+found  fanqie.java
huif@huif-Ubuntu20:/home$ 
#

2.4 2 从主机拷贝文件到容器

docker cp 文件路径 容器名:容器路径

附录:所有常用命令汇总

命令说明
docker version查看版本信息
docker version显示docker的系统信息,包括镜像和容器的数量
docker --help帮助命令,查看所有指令信息
systemctl start docker启动Docker
镜像命令
docker images查看镜像
docker search搜索镜像,搜索条件可选docker search mysql --filter=STARS=3000
docker pull拉取下载镜像
docker rmi删除镜像
docker rmi -f 镜像d删除指定的镜像
docker rmi -f 镜像id 镜像id 镜像id删除多个镜像
docker rmi -f $(docker images -aq)删除全部的镜像
容器命令
docker run [可选参数] image新建容器并启动,–name=“Name” 容器名字 tomacat01, tomacat02,用来区分容器;-d:后台方式运行;-it :使用交互方式运行,进入容器查看内容;-P :指定容器的端口,分三种情况:(1)p:主机端口:容器端口(2)主机端口:容器端口(3)容器端口;-p :随机指定端口
docker ps查看正在运行的容器,-a:查看历史运行过的容器; -n= x x x :显示最近 x x x条创建的容器;-q:只显示容器的编号
exit停止容器运行并退出
ctrl + p + q不停止容器运行并退出
docker rm 容器id删除指定的容器,不能删除正在运行的容器,如果要强制删除:rm -f
docker rm -f $(docker ps -aq)强制删除所有的容器
docker pa -a|xargs docker rm删除所有的容器
docker start 容器id启动容器
docker restart 容器id停止容器
docker stop 容器id停止当前正在运行的容器
docker kill 容器id强制停止正在运行的容器
docker run -d centos后台启动容器
docker logs -tf --tail 条数 容器id查看指定条数的日志
docker top 容器id查看容器中进程信息
docker inspect 容器id查看镜像源数据
docker exec -it 容器id /bin/bash进入容器后开启一个新的终端,可以在里面操作
docker attach 容器id进入容器正在执行的终端,不会启动新的进程
docker cp 容器id:容器内路径 目的地主机将容器内的文件拷贝到主机
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值