Docker基础用法

Docker 基本用法

1. 什么是 Docker?

Docker 是以 「Docker 容器」为资源分割和调度的基本单位,封装整个软件运行时环境,为开发者和系统管理员设计的,用于构建、发布和运行分布式应用的平台。它是一个跨平台、可移植并且简单易用的容器解决方案。

Docker 的源代码托管在 GitHub 上,基于 Go 语言开发并遵从 Apache 2.0 开源协议。

Docker 可在容器内部快速自动化地部署应用,并通过操作系统内核技术(namespaces、groups 等)为容器提供资源隔离与安全保障。

容器技术的优点

  • 1 持续部署与测试;
  • 2 跨云平台支持
  • 3 环境标准化和版本控制;
  • 4 高效资源利用率与隔离;
  • 5 容器跨平台性与镜像;
  • 6 易于理解且易用;
  • 7 应用镜像仓库;

2. OCI&OCF

2.1 OCI

Linux基金会于2015年6月成立OCI(Open Container Initiative)组织,旨在围绕容器格式和运行时制定一个开放的工业化标准。该组织一成立便得到了包括谷歌、微软、亚马逊、华为等一系列云计算厂商的支持。

2.2 OCF

开放容器格式标准( Open Container Format)

制定容器格式标准的宗旨概括来说就是不受上层结构的绑定,如特定的客户端、编排栈等,同时也不受特定的供应商或项目的绑定,即不限于某种特定操作系统、硬件、CPU架构、公有云等。

容器标准化宗旨

标准化容器的宗旨具体分为如下五条。

  • 1 操作标准化:容器的标准化操作包括使用标准容器创建、启动、停止容器,使用标准文件系统工具复制和创建容器快照,使用标准化网络工具进行下载和上传。
  • 2 内容无关:内容无关指不管针对的具体容器内容是什么,容器标准操作执行后都能产生同样的效果。如容器可以用同样的方式上传、启动,不管是PHP应用还是MySQL数据库服务。
  • 3 基础设施无关:无论是个人的笔记本电脑还是AWS S3,亦或是OpenStack,或者其它基础设施,都应该对支持容器的各项操作。
  • 4 为自动化量身定制:制定容器统一标准,是的操作内容无关化、平台无关化的根本目的之一,就是为了可以使容器操作全平台自动化。
  • 5 工业级交付:制定容器标准一大目标,就是使软件分发可以达到工业级交付成为现实。

3. docker镜像与镜像仓库

为什么镜像仓库名字是Registry而不是repository?在docker中仓库的名字是以应用的名称取名的。

Docker基础用法_第1张图片

镜像是静态的,而容器是动态的,容器有其生命周期,镜像与容器的关系类似于程序与进程的关系。镜像类似于文件系统中的程序文件,而容器则类似于将一个程序运行起来的状态,也即进程。所以容器是可以删除的,容器被删除后其镜像是不会被删除的。

4 Docker架构说明

Docker使用C/S体系的架构,Docker客户端与Docker守护进程通信,Docker守护进程负责构建,运行和分发Docker容器。Docker客户端和守护进程可以在同一个系统上运行,也可以将Docker客户端连接到远程Docker守护进程。Docker客户端和守护进程使用REST API通过unix套接字或网络接口进行通信。

下面是Docker核心组件及元素说明:

  • Docker Daemon: dockerd,用来监听Docker API的请求和管理Docker对象,比如镜像、容器、网络和Volume。

  • Docker Client:docker,docker client是我们和Docker进行交互的最主要的方式方法,比如我们可以通过docker run 命令来运行一个容器,然后我们的docker client会把命令发送给上面的Dockerd,主它来负责处理。

  • Docker Registry:用来存储Docker镜像的仓库,Docker hub是官方提供的一个公共仓库,而且Docker默认也是从Docker Hub上查找镜像的。当然你也可以很方便的运行一个私有仓库,当我们使用docker pull或者docker run命令时,就会从我们配置的docker镜像仓库中去拉取镜像,使用docker push时,会将我们构建的镜像推送到对应的镜像仓库中。

  • Images:镜像,镜像是一个只读模板 ,带有创建docker容器的说明。一般来说,镜像会基于另外的一些基础镜像构建并加上一些额外的自定义功能。比如你可以构建一个基于centos的镜像,然后在这个基础镜像上面安装一个Nginx应用,这样就可以构建成属于我们自己的镜像了。

  • Containers:容器,容器是一个镜像的运行实例。可以使用Docker REST API或者CLI来操作容器。容器的实质是进程,但与直接在宿主机执行的进程不同,容器进行运行于属于自己独立的命名空间。因此容器可以拥有自己的root文件系统、自己的网络配置、自己的进程空间,甚至自己的用户id空间。容器内的进程是运行在一个隔离的环境里,使用起来,就好像在一个独立于宿主的系统下操作一样。这种特性使得容器封装的应用比直接在宿主机运行更多案例。

Docker基础用法_第2张图片

5. docker对象

  • Docker镜像,容器,网络,卷,插件等是Docker对象。

  • 在Dockerland,有图像,有容器。这两者密切相关,但截然不同。但这一切都始于Dockerfile。

  • 一个Dockerfile是你创建镜像时,你建立的一个文件。它包含一堆指令,告知Docker如何构建Docker镜像。

  • 你可以把它与烹饪联系起来。在烹饪中你有食谱。通过配方,您可以了解必须采取的所有步骤,以便生成您想要烹饪的任何内容。烹饪行为正在建立食谱。

  • Dockerfile是构建Docker镜像的配方或蓝图,运行单独的构建命令的行为会从配方生成Docker镜像。

6. 安装及使用docker

6.1 docker安装

//首先配置yum仓库
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

配置docker的yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost ~]# curl -o docker-ce.repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo

[root@localhost ~]# sed -i 's@https://download.docker.com@https://mirrors.tuna.tsinghua.edu.cn/docker-ce@g' docker-ce.repo

[root@Docker ~]# yum clean all
21 文件已删除
[root@Docker ~]# yum -y install docker-ce

6.2 Docker加速

为什么需要加速器?因为docker在拉取镜像时,是去国外拉取的所以会比较慢,使用加速器可以解决这个问题

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

  • docker cn
  • 中国科技大学加速器
  • 阿里云加速器(需要通过阿里云开发者平台注册帐号,免费使用个人私有的加速器)
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://l6paj5i1.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

命令 功能
docker search Search the Docker Hub for images 从Docker Hub镜像仓库上查找镜像
docker pull Pull an image or a repository from a registry 从Docker Hub镜像仓库上拉取镜像
docker images List images 列出本地的镜像
docker create Create a new conntainer 创建一个新的容器(与run命令不同,使用create命令只能创建容器而并不启动)
docker start Start one or more stopped containers 启动容器
docker run Run a command in a new container 根据镜像新建并启动容器
docker attach Attach to a runninng container 进入到一个正在运行容器内(使用exit命令退出后,容器会停止)
docker ps List containers 列出当前所有正在运行的容器
docker logs Fetch the logs of a container 获取容器的日志
docker restart Restart a container 重新启动容器
docker stop Stop one or more running containers 停止正在运行的容器
docker kill Kill one or more running containers 杀死正在运行的容器
docker rm Remove onne or more containers 删除容器
docker exec Run a command in a running container 进入到一个正在运行容器内(使用exit命令退出后,容器会继续运行)
docker info Display system-wide information 显示当前系统信息、docker容器、镜像个数、设置等信息
docker inspect Return low-level information on Docker objects 用于以JSON格式显示容器与镜像的详细信息

7. 命令演示

7.1 docker version命令

[root@localhost ~]# docker version  
Client: Docker Engine - Community    //docker客户端版本
 Version:           20.10.11
 API version:       1.41
 Go version:        go1.16.9
 Git commit:        dea9396
 Built:             Thu Nov 18 00:36:58 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community    //docker服务端版本
 Engine:
  Version:          20.10.11
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.9
  Git commit:       847da18
  Built:            Thu Nov 18 00:35:20 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:   //容器版本
  Version:          1.4.12
  GitCommit:        7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc:
  Version:          1.0.2
  GitCommit:        v1.0.2-0-g52b36a2
 docker-init:  //docker初始化版本
  Version:          0.19.0
  GitCommit:        de40ad0
[root@localhost ~]# 

7.2 docker info(显示整个系统的信息)

[root@localhost ~]# docker info  
Client:                     #docker客户端版本信息
 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:              #docker服务端版本信息
 Containers: 1       #容器个数
  Running: 0         #运行容器个数
  Paused: 0          #暂停容器状态个数
  Stopped: 1         #停止状态个数
 Images: 2           #镜像个数
 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-257.el8.x86_64      # 内核版本号
 Operating System: CentOS Stream 8          # 操作系统
 OSType: linux                              #操作系统 类型
 Architecture: x86_64                       #架构
 CPUs: 4                                    # CPU核心数
 Total Memory: 3.622GiB                     #总内存 
 Name: localhost.localdomain
 ID: FO7P:WGML:I2OS:PRY6:QYST:7RGF:XMF7:BTKO:SU72:V47B:YS4W:ABBQ
 Docker Root Dir: /var/lib/docker           #docker默认目录
 Debug Mode: false
 Registry: https://index.docker.io/v1/      # 仓库地址
 Labels:                                    #标签
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Registry Mirrors:                          #加速器
  https://l6paj5i1.mirror.aliyuncs.com/
 Live Restore Enabled: false

[root@localhost ~]# 

7.3 docker search(在docker hub中搜索镜像)

[root@localhost ~]# docker search nginx
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                             Official build of Nginx.                        15899     [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                  
linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   160                  
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   146                  [OK]
jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        144                  [OK]
alfg/nginx-rtmp                   NGINX, nginx-rtmp-module and FFmpeg from sou…   110                  [OK]
nginxdemos/hello                  NGINX webserver that serves a simple page co…   79                   [OK]
privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   60                   [OK]
nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo…   57                   
nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  54                   
nginxproxy/nginx-proxy            Automated Nginx reverse proxy for docker con…   28                   
staticfloat/nginx-certbot         Opinionated setup for automatic TLS certs lo…   25                   [OK]
nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NGIN…   22                   
schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   19                   [OK]
centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   16                   
centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   13                   
bitwarden/nginx                   The Bitwarden nginx web server acting as a r…   11                   
flashspys/nginx-static            Super Lightweight Nginx Image                   11                   [OK]
mailu/nginx                       Mailu nginx frontend                            9                    [OK]
webdevops/nginx                   Nginx container                                 9                    [OK]
ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          3                    [OK]
wodby/nginx                       Generic nginx                                   1                    [OK]
arnau/nginx-gate                  Docker image with Nginx with Lua enabled on …   1                    [OK]
[root@localhost ~]# 

7 .4 docker pull(拉取镜像)

从Docker Hub镜像仓库上拉取镜像(默认最新版) ,没有指定镜像的版本,默认就是拉取最新版的镜像

[root@localhost ~]# docker pull httpd   #httpd后不跟任何版本,代表默认拉取最新版latest
Using default tag: latest
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
docker.io/library/httpd:latest

7.5 docker images(列出系统当前镜像)

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
busybox      latest    d23834f29b38   33 hours ago   1.24MB
httpd        latest    ad17c88403e2   12 days ago    143MB
nginx        latest    ea335eea17ab   2 weeks ago    141MB
[root@localhost ~]# 

7.6 docker image history(查看指定镜像的历史)

[root@localhost ~]# docker image history httpd
IMAGE          CREATED       CREATED BY                                      SIZE      COMMENT
ad17c88403e2   12 days ago   /bin/sh -c #(nop)  CMD ["httpd-foreground"]     0B        
      12 days ago   /bin/sh -c #(nop)  EXPOSE 80                    0B        
      12 days ago   /bin/sh -c #(nop) COPY file:c432ff61c4993ecd…   138B      
      12 days ago   /bin/sh -c #(nop)  STOPSIGNAL SIGWINCH          0B        
      12 days ago   /bin/sh -c set -eux;   savedAptMark="$(apt-m…   60.5MB    
      2 weeks ago   /bin/sh -c #(nop)  ENV HTTPD_PATCHES=           0B        
      2 weeks ago   /bin/sh -c #(nop)  ENV HTTPD_SHA256=20e01d81…   0B        
      2 weeks ago   /bin/sh -c #(nop)  ENV HTTPD_VERSION=2.4.51     0B        
      2 weeks ago   /bin/sh -c set -eux;  apt-get update;  apt-g…   2.6MB     
      2 weeks ago   /bin/sh -c #(nop) WORKDIR /usr/local/apache2    0B        
      2 weeks ago   /bin/sh -c mkdir -p "$HTTPD_PREFIX"  && chow…   0B        
      2 weeks ago   /bin/sh -c #(nop)  ENV PATH=/usr/local/apach…   0B        
      2 weeks ago   /bin/sh -c #(nop)  ENV HTTPD_PREFIX=/usr/loc…   0B        
      2 weeks ago   /bin/sh -c #(nop)  CMD ["bash"]                 0B        
      2 weeks ago   /bin/sh -c #(nop) ADD file:a2405ebb9892d98be…   80.4MB    
[root@localhost ~]# 

7.7 docker image inspect(查看指定镜像的详细信息)

[root@localhost ~]# docker image inspect httpd
[
    {
        "Id": "sha256:ad17c88403e2cedd27963b98be7f04bd3f903dfa7490586de397d0404424936d",
        "RepoTags": [
            "httpd:latest"
        ],
        "RepoDigests": [
            "httpd@sha256:1d71eef54c08435c0be99877c408637f03112dc9f929fba3cccdd15896099b02"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-11-19T01:46:03.31426493Z",
        "Container": "6f074d2d7f78e1c0b56f7cc03a636840ba57f407dd7059e32b460a1d74654f48",
        "ContainerConfig": {
            "Hostname": "6f074d2d7f78",
            "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": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"httpd-foreground\"]"
            ],
            "Image": "sha256:3c6cc8d7517ea98ccc2625425b999f56c3e6ca85a8149e76a37061d1bac6f76f",
            "Volumes": null,
            "WorkingDir": "/usr/local/apache2",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {},
            "StopSignal": "SIGWINCH"
        },
        "DockerVersion": "20.10.7",
        "Author": "",
        "Config": {
            "Hostname": "",
            "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": "sha256:3c6cc8d7517ea98ccc2625425b999f56c3e6ca85a8149e76a37061d1bac6f76f",
            "Volumes": null,
            "WorkingDir": "/usr/local/apache2",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": null,
            "StopSignal": "SIGWINCH"
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 143493815,
        "VirtualSize": 143493815,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/3172e6b059843d3fe21e561381f2ed56277b468dfae90ff7225020bd40628af6/diff:/var/lib/docker/overlay2/74d01c832625f85510ae687747e9803b79f053374a005f0975bb09c822de8a5d/diff:/var/lib/docker/overlay2/184702c58e7e17e85e78bd46cefcfcd0b56fc08fc1a7d6ef06a455b04f2feb4d/diff:/var/lib/docker/overlay2/ae92e4ea7b72287d899fe98a2c8967f156e5df0da17a86129c21616d0b873719/diff",
                "MergedDir": "/var/lib/docker/overlay2/1371050f2283bd8162e9427502725d9bb83d2f8b62987f4b24306f6801da4889/merged",
                "UpperDir": "/var/lib/docker/overlay2/1371050f2283bd8162e9427502725d9bb83d2f8b62987f4b24306f6801da4889/diff",
                "WorkDir": "/var/lib/docker/overlay2/1371050f2283bd8162e9427502725d9bb83d2f8b62987f4b24306f6801da4889/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:e1bbcf243d0e7387fbfe5116a485426f90d3ddeb0b1738dca4e3502b6743b325",
                "sha256:d1fa9a83a17ca27aed9c6f00895de04253c4a8b481b7d56454bffd0013e2d108",
                "sha256:5fd2cf4ae3b8043df1023e06620c6b9e523950ba005abeeda9b01642a9743de4",
                "sha256:205f5d3fcbd98d06b20afe58c4d2f258de943802878cefb31c288ff4162cde74",
                "sha256:b1dcc93155ae800e979fece05841c8b6456f1f64d0d7c2b26c8eb3f438eb730d"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
[root@localhost ~]# 

7.8 删除镜像

[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
busybox      latest    d23834f29b38   33 hours ago   1.24MB
httpd        latest    ad17c88403e2   12 days ago    143MB
nginx        latest    ea335eea17ab   2 weeks ago    141MB

[root@localhost ~]# docker rmi nginx
Untagged: nginx:latest
Untagged: nginx@sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e
Deleted: sha256:ea335eea17ab984571cd4a3bcf90a0413773b559c75ef4cda07d0ce952b00291
Deleted: sha256:cc284e9b1cbed75793782165a07a0c2139d8ec0116d1d562c0e2e504ed586238
Deleted: sha256:6207e091bef7f1c94a109cb455ba163d53d7c2c641de65e71d3a0f33c0ebd8ae
Deleted: sha256:97a18ff8c6973f64d763f004cad932319a1428e0502c0ec3e671e78b2f14256b
Deleted: sha256:319130834f01416a2e8f9a4f2b2fa082c702ac21f16e0e2a206e23d53a0a3bae
Deleted: sha256:1bc375f72973dc110c9629a694bc7476bf878d244287c0214e6436afd6a9d1b0
[root@localhost ~]# 

7.9 docker create(创建容器)

此命令只创建容器,而不会运行容器

[root@localhost ~]# docker create httpd
e500e0c073202569d14dfc72001f155c056113f2d6b745e3d0d1d94d12d09f18
[root@localhost ~]# 

7.10 docker ps (列出容器)

[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
e500e0c07320   httpd     "httpd-foreground"   About a minute ago   Created             zealous_kowalevski
37ffb8444994   busybox   "sh"                 9 hours ago          Created             elegant_lewin
[root@localhost ~]# 

7.11 docker start(启动容器)

[root@localhost ~]# docker ps -a 
CONTAINER ID   IMAGE     COMMAND              CREATED              STATUS    PORTS     NAMES
e500e0c07320   httpd     "httpd-foreground"   About a minute ago   Created             zealous_kowalevski
37ffb8444994   busybox   "sh"                 9 hours ago          Created             elegant_lewin


[root@localhost ~]# docker start e500e0c07320
e500e0c07320

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS     NAMES
e500e0c07320   httpd     "httpd-foreground"   2 minutes ago   Up 6 seconds   80/tcp    zealous_kowalevski
[root@localhost ~]# 

7.12 docker attach(进入容器)

在当前shell下attach连接指定运行镜像,以这种方式进入容器,此时容器会一直占用前台,如果退出容器,容器就会停止

[root@localhost ~]# docker attach e500e0c07320
   

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# 

7.1.3 docker exec(进入容器)

用这个命令进入容器后台运行就算是退出了容器,容器也不会停止运行

[root@localhost ~]# docker start e500e0c07320
e500e0c07320
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS         PORTS     NAMES
e500e0c07320   httpd     "httpd-foreground"   8 minutes ago   Up 6 seconds   80/tcp    zealous_kowalevski
[root@localhost ~]# 


[root@localhost ~]# docker exec -it e500e0c07320 /bin/bash
root@e500e0c07320:/usr/local/apache2# exit
exit

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND              CREATED         STATUS              PORTS     NAMES
e500e0c07320   httpd     "httpd-foreground"   9 minutes ago   Up About a minute   80/tcp    zealous_kowalevski
[root@localhost ~]# 

7.1.4 docker inspect(查看容器的信息详细)

通过查看详细信息可以查看其容器的IP地址

[root@localhost ~]# docker inspect e500e0c07320
[
    {
        "Id": "e500e0c073202569d14dfc72001f155c056113f2d6b745e3d0d1d94d12d09f18",
        "Created": "2021-12-01T12:58:36.109519823Z",
        "Path": "httpd-foreground",
        "Args": [],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 18451,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-12-01T13:06:53.458803326Z",
            "FinishedAt": "2021-12-01T13:06:46.282602134Z"
        },
......

7.1.5 docker logs(查看容器日志)

[root@localhost ~]# docker logs e500e0c07320
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 13:01:25.782955 2021] [mpm_event:notice] [pid 1:tid 139769029520704] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations
[Wed Dec 01 13:01:25.783306 2021] [core:notice] [pid 1:tid 139769029520704] AH00094: Command line: 'httpd -D FOREGROUND'
[Wed Dec 01 13:04:22.034028 2021] [mpm_event:notice] [pid 1:tid 139769029520704] AH00491: caught SIGTERM, shutting down
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 13:04:55.117962 2021] [mpm_event:notice] [pid 1:tid 140630165085504] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations
[Wed Dec 01 13:04:55.118120 2021] [core:notice] [pid 1:tid 140630165085504] AH00094: Command line: 'httpd -D FOREGROUND'
[Wed Dec 01 13:06:46.229688 2021] [mpm_event:notice] [pid 1:tid 140630165085504] AH00491: caught SIGTERM, shutting down
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 13:06:53.469192 2021] [mpm_event:notice] [pid 1:tid 139793421331776] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations
[Wed Dec 01 13:06:53.469444 2021] [core:notice] [pid 1:tid 139793421331776] AH00094: Command line: 'httpd -D FOREGROUND'
[root@localhost ~]# 

7.1.6 docker stop(停止容器)

[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS     NAMES
e500e0c07320   httpd     "httpd-foreground"   21 minutes ago   Up 13 minutes   80/tcp    zealous_kowalevski
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# docker stop e500e0c07320
e500e0c07320
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# 

7.17 docker restart(重启容器)

[root@localhost ~]# docker ps -a 
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                      PORTS     NAMES
e500e0c07320   httpd     "httpd-foreground"   22 minutes ago   Exited (0) 35 seconds ago             zealous_kowalevski
37ffb8444994   busybox   "sh"                 10 hours ago     Created                               elegant_lewin
[root@localhost ~]# 
[root@localhost ~]# docker restart e500e0c07320
e500e0c07320
[root@localhost ~]# 
[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS         PORTS     NAMES
e500e0c07320   httpd     "httpd-foreground"   23 minutes ago   Up 3 seconds   80/tcp    zealous_kowalevski
[root@localhost ~]# 

7.18 docker kill(杀死正在运行的容器)

[root@localhost ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS         PORTS     NAMES
e500e0c07320   httpd     "httpd-foreground"   23 minutes ago   Up 3 seconds   80/tcp    zealous_kowalevski
[root@localhost ~]# 
[root@localhost ~]# docker kill e500e0c07320
e500e0c07320
[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
e500e0c07320   httpd     "httpd-foreground"   23 minutes ago   Exited (137) 6 seconds ago             zealous_kowalevski
37ffb8444994   busybox   "sh"                 10 hours ago     Created                                elegant_lewin
[root@localhost ~]# 

7.19 docker rm(删除容器)

删除一个或者多个容器,只能删除停止状态的容器或使用-f参数强制删除

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS                       PORTS     NAMES
e500e0c07320   httpd     "httpd-foreground"   26 minutes ago   Exited (137) 2 minutes ago             zealous_kowalevski
37ffb8444994   busybox   "sh"                 10 hours ago     Created                                elegant_lewin

[root@localhost ~]# docker rm e500e0c07320
e500e0c07320

[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED        STATUS    PORTS     NAMES
37ffb8444994   busybox   "sh"      10 hours ago   Created             elegant_lewin
[root@localhost ~]# 

7.20 docker run(创建并启动容器)

[root@localhost ~]# docker run -it nginx /bin/bash  ##拉取一个nginx的镜像,然后创建容器并运行,-it表示以交互的方式进入终端,/bin/bash表示终端的环境
Unable to find image 'nginx:latest' locally   
latest: Pulling from library/nginx
eff15d958d66: Already exists 
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
root@fb245d8da0f2:/# 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
root@fb245d8da0f2:/# exit
exit

你可能感兴趣的:(运维,Linux系统,docker,Linux系统,运维)