docker基础用法

1. 什么是docker

docker中的容器:

  • lxc --> libcontainer --> runC

在这里插入图片描述

2. OCI&OCF

2.1 OCI

Open Container-initiative

  • 由Linux基金会主导于2015年6月创立
  • 旨在围绕容器格式和运行时制定一个开放的工业化标准
  • contains two specifications(包含两个规格)
    • the Runtime Specification(runtime-spec)(运行时规范)
    • the Image Specification(image-spec)(形象规范)

2.2 OCF

Open Container Format(打开容器格式)

runC is a CLI tool for spawning and running containers according to the OCI specification(runC是一个CLI工具,用于根据OCI规范生成和运行容器)

  • Containers are started as a child process of runC and can be embedded into various other systems without having to run a daemon(容器作为runC的子进程启动,可以嵌入到各种其他系统中,而不需要运行守护进程)
  • runC is built on libcontainer, the same container technology powering millions of Docker Engine installations(runC是基于libcontainer构建的,同样的容器技术为数百万的Docker引擎安装提供了动力)

docker提供了一个专门容纳容器镜像的站点:https://hub.docker.com/

3. docker架构

在这里插入图片描述

4. docker镜像与镜像仓库

为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。
在这里插入图片描述
镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。

5. docker对象

When you use docker, you are creating and using images, containers, networks, volumes, pluginns, and other objects.(当您使用 docker 时,您正在创建和使用镜像、容器、网络、卷、插件和其他对象。)

  • IMAGES(镜像)

    • An image is a read-only template with instructions for creating a docker container.(镜像是一个只读模板,其中包含创建 docker 容器的说明。)
    • Often, an image is based on another image, with some additional customization.(通常,一个镜像基于另一个镜像,并带有一些额外的自定义。)
    • You might create your own images or you might only use those created by others and published in a registry.(您可以创建自己的镜像,也可以使用其他人创建并在仓库中发布的镜像。)
  • CONTAINERS(容器)

    • A conntainer is a runnable instance of an image.(容器是镜像的可运行实例。)
    • You can create, run, stop, move, or delete a container using the docker API or CLI.(您可以使用 docker API 或 CLI 创建、运行、停止、移动或删除容器。)
    • You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state.(您可以将容器连接到一个或多个网络,为其附加存储,甚至可以根据其当前状态创建新镜像。)

6. 安装及使用docker

6.1 docker安装

[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1919  100  1919    0     0    470      0  0:00:04  0:00:04 --:--:--   470
[root@localhost yum.repos.d]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo
[root@localhost yum.repos.d]# ls
CentOS-Base.repo  docker-ce.repo  redhat.repo
[root@localhost ~]# yum -y install docker-ce

6.2 docker加速

docker-ce的配置文件是/etc/docker/daemon.json,此文件默认不存在,需要我们手动创建并进行配置,而docker的加速就是通过配置此文件来实现的。

docker的加速有多种方式:

  • docker cn
  • 中国科技大学加速器
  • 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)

阿里云加速器:https://www.aliyun.com/
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

[root@localhost ~]# mkdir -p /etc/docker
[root@localhost ~]# cat > /etc/docker/daemon.json <<EOF
> {
>   "registry-mirrors": ["https://faq69nhk.mirror.aliyuncs.com"]
> }
> EOF
[root@localhost ~]# cat /etc/docker/daemon.json 
{
  "registry-mirrors": ["https://faq69nhk.mirror.aliyuncs.com"]
}
[root@localhost ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@localhost ~]# 

//显示整个系统的信息
[root@localhost ~]# docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.6.3-docker)
  scan: Docker Scan (Docker Inc., v0.9.0)

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 0
 Server Version: 20.10.11
 Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc version: v1.0.2-0-g52b36a2
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 4.18.0-193.el8.x86_64
 Operating System: Red Hat Enterprise Linux 8.2 (Ootpa)
 OSType: linux
 Architecture: x86_64
 CPUs: 4
 Total Memory: 7.602GiB
 Name: localhost.localdomain
 ID: 5YGS:R5WZ:KE2O:WC6B:FAUM:HYTR:DLHL:RA5T:ZYHD:XSC4:CZ3L:VFAE
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:
  https://faq69nhk.mirror.aliyuncs.com/   //阿里云加速器
 Live Restore Enabled: false

[root@localhost ~]# 

7. docker常用操作

命令功能
docker search在Docker Hub中搜索镜像
docker pull从注册表中拉出映像或存储库
docker images列出所有镜像
docker create创建一个新的容器
docker start启动一个或多个停止的容器
docker run在新容器中运行命令
docker attach附加到一个运行的容器
docker ps列表正在运行的容器
docker ps -a列出所有的容器
docker logs获取容器的日志
docker restart重启一个容器
docker stop停止一个或多个运行中的容器
docker kill杀死一个或多个运行中的容器
docker rm删除一个或多个停止的容器
docker exec在运行容器中运行命令
docker info显示整个系统的信息
docker inspect返回Docker对象的低级信息
docker version查看Docker版本信息

7.1 docker常用操作示例

7.1.1 docker search

//在Docker Hub中搜索nginx镜像
[root@localhost ~]# docker search nginx
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                             Official build of Nginx.                        15893     [OK]       
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2098                 [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   819                  [OK]
jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   285                  
······

7.1.2 docker pull

//拉取镜像
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
eff15d958d66: Pull complete 
1e5351450a59: Pull complete 
2df63e6ce2be: Pull complete 
9171c7ae368c: Pull complete 
020f975acd28: Pull complete 
266f639b35ad: Pull complete 
Digest: sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# 

7.1.3 docker images

//查看已安装的镜像
[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    ea335eea17ab   2 weeks ago   141MB
[root@localhost ~]# 

7.1.4 docker create

//创建一个容器
[root@localhost ~]# docker create nginx
03aa723b790c92036c29cc2225d5a9b5e3ea63ea495a59df48009a9c7d15421f
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS    PORTS     NAMES
03aa723b790c   nginx     "/docker-entrypoint.…"   19 seconds ago   Created             naughty_bardeen
[root@localhost ~]# 

7.1.5 docker start

//启动容器
[root@localhost ~]# docker start 03aa723b790c
03aa723b790c
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
03aa723b790c   nginx     "/docker-entrypoint.…"   2 minutes ago   Up 3 seconds   80/tcp    naughty_bardeen
[root@localhost ~]# 

7.1.6 docker stop

//停止容器
[root@localhost ~]# docker stop 03aa723b790c
03aa723b790c
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# 

7.1.7 docker restart

//重启容器
[root@localhost ~]# docker restart 03aa723b790c
03aa723b790c
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
03aa723b790c   nginx     "/docker-entrypoint.…"   5 minutes ago   Up 5 seconds   80/tcp    naughty_bardeen
[root@localhost ~]# 

7.1.8 docker kill

//杀死一个容器
[root@localhost ~]# docker kill 03aa723b790c
03aa723b790c
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# 

7.1.9 docker run

//运行一个容器
docker run [options] image [command]
options:
  -a stdin: 指定标准输入输出内容类型,可选stdin/stdout/stderr
  -d: 运行一个容器,在后台运行或者打印容器id
  -i: 以交互模式运行容器,通常与-t同时使用
  -t: 为容器重新分配一个伪输入终端
  -P: 随机端口映射,容器内部端口随机映射到主机的端口
  -p: 指定端口映射,例:宿主机端口:容器端口
  --name: 为容器指定一个名称
  --dns: 指定容器使用的DNS服务器
  -v: 绑定一个卷
  
[root@localhost ~]# docker run -it nginx /bin/sh
# ls
bin   docker-entrypoint.d   home   media  proc	sbin  tmp
boot  docker-entrypoint.sh  lib    mnt	  root	srv   usr
dev   etc		    lib64  opt	  run	sys   var
# exit
[root@localhost ~]# 

[root@localhost ~]# docker run -d httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
eff15d958d66: Already exists 
ba1caf8ba86c: Pull complete 
ab86dc02235d: Pull complete 
0d58b11d2867: Pull complete 
e88da7cb925c: Pull complete 
Digest: sha256:1d71eef54c08435c0be99877c408637f03112dc9f929fba3cccdd15896099b02
Status: Downloaded newer image for httpd:latest
d224c77a7e875f0696654592a45de6fc70ca4b8ad95fb17ce8cda8255a770087
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS     NAMES
d224c77a7e87   httpd     "httpd-foreground"   12 seconds ago   Up 11 seconds   80/tcp    awesome_swirles
[root@localhost ~]# 

7.1.10 docker inspect

//获取一个容器的信息
[root@localhost ~]# docker inspect d224c77a7e87 
[
    {
        "Id": "d224c77a7e875f0696654592a45de6fc70ca4b8ad95fb17ce8cda8255a770087",
        "Created": "2021-12-01T11:30:44.627107396Z",
        "Path": "httpd-foreground",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 4626,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-12-01T11:30:45.604888464Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:ad17c88403e2cedd27963b98be7f04bd3f903dfa7490586de397d0404424936d",
        "ResolvConfPath": "/var/lib/docker/containers/d224c77a7e875f0696654592a45de6fc70ca4b8ad95fb17ce8cda8255a770087/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/d224c77a7e875f0696654592a45de6fc70ca4b8ad95fb17ce8cda8255a770087/hostname",
        "HostsPath": "/var/lib/docker/containers/d224c77a7e875f0696654592a45de6fc70ca4b8ad95fb17ce8cda8255a770087/hosts",
        "LogPath": "/var/lib/docker/containers/d224c77a7e875f0696654592a45de6fc70ca4b8ad95fb17ce8cda8255a770087/d224c77a7e875f0696654592a45de6fc70ca4b8ad95fb17ce8cda8255a770087-json.log",
        "Name": "/awesome_swirles",
        "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/5832a07b782cf7272cee8a2a1ddb3582a1141afe780065b7c71905e0793160eb-init/diff:/var/lib/docker/overlay2/2ce6d18ceaf276ffd402767bd001187aa803d94b96d78e4349efa0caad71b9bd/diff:/var/lib/docker/overlay2/14548a9bb2246684399b70ac59f04475ba35bf698a9a7c8edc0d0197fe360a5a/diff:/var/lib/docker/overlay2/f44ab05a147f8558a3c19a1a6d07d262d459daa72d4c03cb1ee68f4ec0dd0233/diff:/var/lib/docker/overlay2/daf8a67763b43a5208b50ae739fe5643e9c79e06c31eb7f99a5b7bf9dfcb92aa/diff:/var/lib/docker/overlay2/336ff9a30043fb7aee33f99a279338b48b50d47932d5bb5b6f07cfe486a12e4c/diff",
                "MergedDir": "/var/lib/docker/overlay2/5832a07b782cf7272cee8a2a1ddb3582a1141afe780065b7c71905e0793160eb/merged",
                "UpperDir": "/var/lib/docker/overlay2/5832a07b782cf7272cee8a2a1ddb3582a1141afe780065b7c71905e0793160eb/diff",
                "WorkDir": "/var/lib/docker/overlay2/5832a07b782cf7272cee8a2a1ddb3582a1141afe780065b7c71905e0793160eb/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "d224c77a7e87",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/apache2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "HTTPD_PREFIX=/usr/local/apache2",
                "HTTPD_VERSION=2.4.51",
                "HTTPD_SHA256=20e01d81fecf077690a4439e3969a9b22a09a8d43c525356e863407741b838f4",
                "HTTPD_PATCHES="
            ],
            "Cmd": [
                "httpd-foreground"
            ],
            "Image": "httpd",
            "Volumes": null,
            "WorkingDir": "/usr/local/apache2",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {},
            "StopSignal": "SIGWINCH"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "5227fd3086201b8a16b03ce6351138a079b9fd54f29e0dac47c63a56b07cc1cd",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/5227fd308620",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "f849b710e5b40813bb76e4e81e244ee152df63bf5b916b6fd1945a23dd33737f",
            "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": "b312a3b5fc7278bcbb9438e3acea83300e3d0edc6553d49e08c9a98fa9dc3d11",
                    "EndpointID": "f849b710e5b40813bb76e4e81e244ee152df63bf5b916b6fd1945a23dd33737f",
                    "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
                }
            }
        }
    }
]
[root@localhost ~]# curl 172.17.0.2
<html><body><h1>It works!</h1></body></html>
[root@localhost ~]# 

7.1.11 docker logs

//查看一个容器的日志
[root@localhost ~]# docker logs d224c77a7e87 
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
[Wed Dec 01 11:30:45.613770 2021] [mpm_event:notice] [pid 1:tid 139883583319360] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations
[Wed Dec 01 11:30:45.613999 2021] [core:notice] [pid 1:tid 139883583319360] AH00094: Command line: 'httpd -D FOREGROUND'
172.17.0.1 - - [01/Dec/2021:11:33:15 +0000] "GET / HTTP/1.1" 200 45
[root@localhost ~]# 

7.1.11 docker rm

//删除一个容器
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                        PORTS     NAMES
d224c77a7e87   httpd     "httpd-foreground"       5 minutes ago    Up 5 minutes                  80/tcp    awesome_swirles
4b50a8610a3a   nginx     "/docker-entrypoint.…"   8 minutes ago    Exited (0) 8 minutes ago                intelligent_williams
ae51fe7c633f   nginx     "/docker-entrypoint.…"   9 minutes ago    Exited (127) 8 minutes ago              focused_elbakyan
98bfd6062ea9   nginx     "/docker-entrypoint.…"   9 minutes ago    Exited (130) 9 minutes ago              recursing_johnson
03aa723b790c   nginx     "/docker-entrypoint.…"   19 minutes ago   Exited (137) 12 minutes ago             naughty_bardeen
[root@localhost ~]# docker rm 4b50a8610a3a
4b50a8610a3a
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                        PORTS     NAMES
d224c77a7e87   httpd     "httpd-foreground"       5 minutes ago    Up 5 minutes                  80/tcp    awesome_swirles
ae51fe7c633f   nginx     "/docker-entrypoint.…"   9 minutes ago    Exited (127) 9 minutes ago              focused_elbakyan
98bfd6062ea9   nginx     "/docker-entrypoint.…"   9 minutes ago    Exited (130) 9 minutes ago              recursing_johnson
03aa723b790c   nginx     "/docker-entrypoint.…"   19 minutes ago   Exited (137) 12 minutes ago             naughty_bardeen
[root@localhost ~]# 

7.1.13 docker rm $(docker ps -aq)

//删除多个容器
[root@localhost ~]# docker ps -aq
d224c77a7e87
ae51fe7c633f
98bfd6062ea9
03aa723b790c
[root@localhost ~]# docker rm $(docker ps -aq)
ae51fe7c633f
98bfd6062ea9
03aa723b790c
Error response from daemon: You cannot remove a running container d224c77a7e875f0696654592a45de6fc70ca4b8ad95fb17ce8cda8255a770087. Stop the container before attempting removal or force remove
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS     NAMES
d224c77a7e87   httpd     "httpd-foreground"   7 minutes ago   Up 7 minutes   80/tcp    awesome_swirles
[root@localhost ~]# 

7.1.14 docker attach

//连接到正在运行的容器上
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED              STATUS          PORTS     NAMES
d5f81cac59be   httpd     "httpd-foreground"   About a minute ago   Up 13 seconds   80/tcp    bold_bassi
[root@localhost ~]# docker attach d5f81cac59be
^C[Wed Dec 01 11:42:07.244368 2021] [mpm_event:notice] [pid 1:tid 139743543156032] AH00491: caught SIGTERM, shutting down
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]#


7.1.15 docker exec

//进入容器
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS         PORTS     NAMES
d224c77a7e87   httpd     "httpd-foreground"   15 minutes ago   Up 8 seconds   80/tcp    awesome_swirles
[root@localhost ~]# docker exec -it d224c77a7e87 /bin/bash
root@d224c77a7e87:/usr/local/apache2# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs	modules
root@d224c77a7e87:/usr/local/apache2# exit
exit
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS              PORTS     NAMES
d224c77a7e87   httpd     "httpd-foreground"   16 minutes ago   Up About a minute   80/tcp    awesome_swirles
[root@localhost ~]# 

8. docker event state

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彭宇栋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值