当前位置:   article > 正文

云计算 之 Docker--Docker 中的容器、镜像、仓库管理_云手机容器管理平台主要分为容器管理、容器镜像管理两个模块。容器管理主要包括底

云手机容器管理平台主要分为容器管理、容器镜像管理两个模块。容器管理主要包括底

前言

本环境是基于 Centos 7.8 系统构建Docker-19.03.13环境
具体构建,请参考 构建Docker-19.03.13


一、docker 包括三个基本概念

在这里插入图片描述

  • 镜像( Image)

镜像:一个特殊的文件系统,Docker镜像就是一个只读的模板
操作系统分为内核和用户空间。对于 Linux 而言,内核启动后,会挂载 root 文件系统为其提供用户空
间支持。而 Docker 镜像(Image),就相当于是一个 root 文件系统。
Docker 镜像是一个特殊的文件系统,除了提供容器运行时所需的程序、库、资源、配置等文件外,还
包含了一些为运行时准备的一些配置参数(如匿名卷、环境变量、用户等)。
镜像不包含任何动态数据,其内容在构建之后也不会被改变。

分层存储的特征还使得镜像的复用、定制变的更为容易。甚至可以用之前构建好的镜像作为基础层,然
后进一步添加新的层,以定制自己所需的内容,构建新的镜像

  • 容器( Container)

容器:镜像运行时的实体,Docker利用容器来运行应用。
镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静
态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等 。
容器的实质是进程,但与直接在宿主执行的进程不同,容器进程运行于属于自己的独立的命名空间。前
面讲过镜像使用的是分层存储,容器也是如此。

容器在整个应用程序生命周期工作流中提供以下优点:隔离性、可移植性、灵活性、可伸缩性和可控性。 最重要的优点是可在开发和运营之间提供隔离

  • 仓库( Repository)

仓库:集中存放镜像文件的地方
镜像构建完成后,可以很容易的在当前宿主上运行,但是, 如果需要在其他服务器上使用这个镜像,我
们就需要一个集中的存储、分发镜像的服务,Docker Registry 就是这样的服务。
一个 Docker Registry 中可以包含多个仓库(Repository);每个仓库可以包含多个标签(Tag);每个标签对应一个镜像。所以说,镜像仓库是 Docker 用来集中存放镜像文件的地方,类似于我们之前常用的代码仓库。
仓库分为公开仓库( Public)和私有仓库( Private) 两种形式。

在这里插入图片描述

二、仓库操作

登录 docker hub 注册账号,创建公有仓库
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在仓库中搜索 MySQL 镜像
在这里插入图片描述
命令行搜索 MySQL 镜像

[root@node01 ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   10097               [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3705                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   737                                     [OK]
percona                           Percona Server is a fork of the MySQL relati…   511                 [OK]                
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   84                                      
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   77                                      
centurylink/mysql                 Image containing mysql. Optimized to be link…   60                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      45                                      [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   35                                      
prom/mysqld-exporter                                                              31                                      [OK]
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   30                                      [OK]
databack/mysql-backup             Back up mysql databases to... anywhere!         30                                      
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   26                                      
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   20                                      
circleci/mysql                    MySQL is a widely used, open-source relation…   19                                      
mysql/mysql-router                MySQL Router provides transparent routing be…   17                                      
arey/mysql-client                 Run a MySQL client from a docker container      15                                      [OK]
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   10                                      [OK]
yloeffler/mysql-backup            This image runs mysqldump to backup data usi…   7                                       [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   3                                       
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1                                       
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   1                                       [OK]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

从仓库中下载镜像

#下载 busybox 镜像
[root@node01 ~]# docker pull busybox
Using default tag: latest
latest: Pulling from library/busybox
9758c28807f2: Pull complete 
Digest: sha256:a9286defaba7b3a519d585ba0e37d0b2cbee74ebfe590960b0b1d6a5e97d1e1d
Status: Downloaded newer image for busybox:latest
docker.io/library/busybox:latest

#下载我们自己创建的镜像
[root@node01 ~]# docker pull wan012q/docker01
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

查看下载下来的镜像

[root@node01 ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              f0b02e9d092d        10 days ago         1.23MB
  • 1
  • 2
  • 3

仓库的登录、退出登录

#登录仓库
[root@node01 ~]# docker login -u wan012q -pabc200888
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

#退出容器的登录
[root@node01 ~]# docker logout 
Removing login credentials for https://index.docker.io/v1/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

将本地镜像上传到公有仓库

公有仓库
在这里插入图片描述

#镜像打标记
[root@node01 ~]# docker tag busybox:latest wan012q/docker01:busybox_v1
#查看本地现有的镜像
[root@node01 ~]# docker image ls 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              f0b02e9d092d        10 days ago         1.23MB
wan012q/docker01    busybox_v1          f0b02e9d092d        10 days ago         1.23MB
#上传镜像
[root@node01 ~]# docker push wan012q/docker01:busybox_v1
The push refers to repository [docker.io/wan012q/docker01]
d2421964bad1: Mounted from library/busybox 
busybox_v1: digest: sha256:c9249fdf56138f0d929e2080ae98ee9cb2946f71498fc1484288e6a935b5e5bc size: 527
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

查看结果
在这里插入图片描述

三、 镜像操作

在这里插入图片描述
在仓库中搜索nginx镜像

[root@node01 ~]# docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        13911               [OK]                
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1900                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   791                                     [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   127                                     
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   101                                     
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   99                                      [OK]
bitnami/nginx                      Bitnami nginx Docker Image                      90                                      [OK]
alfg/nginx-rtmp                    NGINX, nginx-rtmp-module and FFmpeg from sou…   77                                      [OK]
jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        64                                      [OK]
nginxdemos/hello                   NGINX webserver that serves a simple page co…   62                                      [OK]
nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         42                                      
privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & Al…   39                                      [OK]
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  19                                      
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 …   15                                      
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       15                                      
raulr/nginx-wordpress              Nginx front-end for the official wordpress:f…   13                                      [OK]
centos/nginx-18-centos7            Platform for running nginx 1.8 or building n…   13                                      
staticfloat/nginx-certbot          Opinionated setup for automatic TLS certs lo…   13                                      [OK]
bitwarden/nginx                    The Bitwarden nginx web server acting as a r…   7                                       
mailu/nginx                        Mailu nginx frontend                            7                                       [OK]
flashspys/nginx-static             Super Lightweight Nginx Image                   6                                       [OK]
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr…   6                                       [OK]
ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                                       [OK]
wodby/nginx                        Generic nginx                                   1                                       [OK]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

搜索结果解释

参数说明
NAME镜像名称
DESCRIPTION镜像说明
STARS点赞数量(受欢迎程度)
OFFICIAL是否是官方的
AUTOMATED是否是自动构建的

拉取nginx-alpine镜像

[root@node01 ~]# docker pull nginx:1.14-alpine
1.14-alpine: Pulling from library/nginx
bdf0201b3a05: Pull complete 
3d0a573c81ed: Pull complete 
8129faeb2eb6: Pull complete 
3dc99f571daf: Pull complete 
Digest: sha256:485b610fefec7ff6c463ced9623314a04ed67e3945b9c08d7e53a47f6d108dc7
Status: Downloaded newer image for nginx:1.14-alpine
docker.io/library/nginx:1.14-alpine
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

查看镜像nginx:1.14-alpine的详细信息

[root@node01 ~]# docker image inspect nginx:1.14-alpine 
[
    {
        "Id": "sha256:8a2fb25a19f5dc1528b7a3fabe8b3145ff57fe10e4f1edac6c718a3cf4aa4b73",
        "RepoTags": [
            "nginx:1.14-alpine"
        ],
        "RepoDigests": [
            "nginx@sha256:485b610fefec7ff6c463ced9623314a04ed67e3945b9c08d7e53a47f6d108dc7"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2019-04-10T01:08:46.210666872Z",
        "Container": "0a597833eefb7f60fd434dcc4438c1bd0d514a42f5925513841ec5c3295d62d4",
        "ContainerConfig": {
            "Hostname": "0a597833eefb",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.14.2"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"nginx\" \"-g\" \"daemon off;\"]"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:aff1bb24fb4907c200a9e7cbabe65ec4b5490bc511ab7f1f69b2946f709a27cb",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "DockerVersion": "18.06.1-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.14.2"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "ArgsEscaped": true,
            "Image": "sha256:aff1bb24fb4907c200a9e7cbabe65ec4b5490bc511ab7f1f69b2946f709a27cb",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 16032814,
        "VirtualSize": 16032814,
        "GraphDriver": {
            "Data": {
                "LowerDir": "/var/lib/docker/overlay2/0afec5029a73fa8e3fe1b929547a3062231091e13a175c5feb4f8615d0c01b99/diff:/var/lib/docker/overlay2/ccdd05f4cb175a568354340ce363a0d847b41e22dffe80037948d1296d961a32/diff:/var/lib/docker/overlay2/2af829e9b6451a53c5f747e55886e843daff2dfd77233c8ae3e3286e25f5a2eb/diff",
                "MergedDir": "/var/lib/docker/overlay2/0cb9be8148524543d03c8154eb2dbe3ad5a25fded931d4f8f9e83665a448ca0d/merged",
                "UpperDir": "/var/lib/docker/overlay2/0cb9be8148524543d03c8154eb2dbe3ad5a25fded931d4f8f9e83665a448ca0d/diff",
                "WorkDir": "/var/lib/docker/overlay2/0cb9be8148524543d03c8154eb2dbe3ad5a25fded931d4f8f9e83665a448ca0d/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:a464c54f93a9e88fc1d33df1e0e39cca427d60145a360962e8f19a1dbf900da9",
                "sha256:5ac9a5170bf254da6d1b54edbfde4da4070e5c43a85a0402ec7defdcec5c3657",
                "sha256:b2cbae4b8c158c49c6590cc69a305c36dfb133321c3ab1e9c3b764f54c27158a",
                "sha256:076c58d2644f0d4c4a80d460bc2dfb21469f4fdb5c3d259438e0a4a1b182fa87"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110

保存镜像

[root@node01 ~]# docker image save busybox -o buaybox.tar
  • 1

导入镜像

[root@node01 ~]# docker image rm busybox:latest 
Untagged: busybox:latest
Untagged: busybox@sha256:a9286defaba7b3a519d585ba0e37d0b2cbee74ebfe590960b0b1d6a5e97d1e1d
[root@node01 ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
wan012q/docker01    busybox_v1          f0b02e9d092d        10 days ago         1.23MB
nginx               1.14-alpine         8a2fb25a19f5        18 months ago       16MB
[root@node01 ~]# docker image import buaybox.tar 
sha256:1c26751958da198b989b2b08384c134ca11077becca89fdcfc5cec9f9cc05fd6
[root@node01 ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              1c26751958da        2 seconds ago       1.45MB
wan012q/docker01    busybox_v1          f0b02e9d092d        10 days ago         1.23MB
nginx               1.14-alpine         8a2fb25a19f5        18 months ago       16MB
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
[root@node01 ~]# docker image rmi 1c26751958da 
Deleted: sha256:1c26751958da198b989b2b08384c134ca11077becca89fdcfc5cec9f9cc05fd6
Deleted: sha256:e272adc323d6eadc54a5f1f9a350d251e019eb84f24aabe52347039aec82896a
[root@node01 ~]# docker image load -i buaybox.tar
d2421964bad1: Loading layer   1.45MB/1.45MB
Loaded image: busybox:latest
[root@node01 ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
busybox             latest              f0b02e9d092d        10 days ago         1.23MB
nginx               1.14-alpine         8a2fb25a19f5        18 months ago       16MB
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

四、 容器操作

在这里插入图片描述

创建容器
方法一:创建容器后启动(不推荐)

[root@node01 ~]# docker create busybox
14379b0ed03f8a63be06703594bd031dfc1d1db633e9b68e84fbcfb0763c3800
[root@node01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@node01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
14379b0ed03f        busybox             "sh"                8 seconds ago       Created                                 vibrant_blackwell
[root@node01 ~]# docker start 14379b0ed03f
14379b0ed03f
[root@node01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@node01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
14379b0ed03f        busybox             "sh"                53 seconds ago      Exited (0) 29 seconds ago                       vibrant_blackwell
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

方法二:通过运行创建容器

[root@node01 ~]# docker run nginx:1.14-alpine 
[root@node01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
147631182ea4        nginx:1.14-alpine   "nginx -g 'daemon of…"   14 seconds ago      Up 13 seconds       80/tcp              eager_lehmann
  • 1
  • 2
  • 3
  • 4

注:容器退出后,停止运行

#创建容器,退出后,停止告知运行
[root@node01 ~]# docker run -it busybox /bin/sh
/ # 
#创建容器,放在后台运行
[root@node01 ~]# docker run -d nginx:1.14-alpine 
49b6c21411285a76a82713d58b0a416814df9a32e0574172a159ff36d46264f7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

退出时,容器还在运行
Ctrl + p + q

[root@node01 ~]# docker run -it nginx:1.14-alpine /bin/sh
/ # [root@node01 ~]# 
  • 1
  • 2

创建容器时,指定名字

[root@node01 ~]# docker run --name=nginx1 nginx:1.14-alpine
[root@node01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
0a2a2d538c1f        nginx:1.14-alpine   "nginx -g 'daemon of…"   6 seconds ago       Up 6 seconds        80/tcp              nginx1
  • 1
  • 2
  • 3
  • 4

创建容器,运行,结束后,自动删除

[root@node01 ~]# docker run --rm -it nginx:1.14-alpine /bin/sh
/ # 

[root@node01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3b783d031102        nginx:1.14-alpine   "/bin/sh"           11 seconds ago      Up 10 seconds       80/tcp              cool_cerf

退出登录后
[root@node01 ~]# docker run --rm -it nginx:1.14-alpine /bin/sh
/ # exit
[root@node01 ~]# 
[root@node01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAM
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

容器的关闭、停止

#创建容器nginx
[root@node01 ~]# docker run -d --name=nginx nginx:1.14-alpine 
3dc088a9821f9419b191e5f989bd790a7103389161f3b8941a6dcdaf2eca30b1
#查看容器状态
[root@node01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
3dc088a9821f        nginx:1.14-alpine   "nginx -g 'daemon of…"   12 seconds ago      Up 12 seconds       80/tcp              nginx
#停止容器
[root@node01 ~]# docker stop nginx
nginx
#查看容器状态
[root@node01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@node01 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
3dc088a9821f        nginx:1.14-alpine   "nginx -g 'daemon of…"   34 seconds ago      Exited (0) 7 seconds ago                       nginx
5a6a2d294f3d        nginx:1.14-alpine   "nginx -g 'daemon of…"   3 minutes ago       Exited (0) 2 minutes ago                       affectionate_johnson
0a2a2d538c1f        nginx:1.14-alpine   "nginx -g 'daemon of…"   9 minutes ago       Exited (0) 8 minutes ago                       nginx1
#启动容器
[root@node01 ~]# docker start nginx
nginx
#重启容器
[root@node01 ~]# docker restart nginx1
nginx1
#查看容器状态
[root@node01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
3dc088a9821f        nginx:1.14-alpine   "nginx -g 'daemon of…"   55 seconds ago      Up 7 seconds        80/tcp              nginx
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

查看容器 nginx的详细信息

[root@node01 ~]# docker inspect nginx
[
    {
        "Id": "3dc088a9821f9419b191e5f989bd790a7103389161f3b8941a6dcdaf2eca30b1",
        "Created": "2020-10-24T15:39:36.093956012Z",
        "Path": "nginx",
        "Args": [
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 19292,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2020-10-24T15:40:23.543972408Z",
            "FinishedAt": "2020-10-24T15:40:03.203496406Z"
        },
        "Image": "sha256:8a2fb25a19f5dc1528b7a3fabe8b3145ff57fe10e4f1edac6c718a3cf4aa4b73",
        "ResolvConfPath": "/var/lib/docker/containers/3dc088a9821f9419b191e5f989bd790a7103389161f3b8941a6dcdaf2eca30b1/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/3dc088a9821f9419b191e5f989bd790a7103389161f3b8941a6dcdaf2eca30b1/hostname",
        "HostsPath": "/var/lib/docker/containers/3dc088a9821f9419b191e5f989bd790a7103389161f3b8941a6dcdaf2eca30b1/hosts",
        "LogPath": "/var/lib/docker/containers/3dc088a9821f9419b191e5f989bd790a7103389161f3b8941a6dcdaf2eca30b1/3dc088a9821f9419b191e5f989bd790a7103389161f3b8941a6dcdaf2eca30b1-json.log",
        "Name": "/nginx",
        "RestartCount": 0,
        "Driver": "overlay2",
        "Platform": "linux",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": null,
        "HostConfig": {
            "Binds": null,
            "ContainerIDFile": "",
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {},
            "RestartPolicy": {
                "Name": "no",
                "MaximumRetryCount": 0
            },
            "AutoRemove": false,
            "VolumeDriver": "",
            "VolumesFrom": null,
            "CapAdd": null,
            "CapDrop": null,
            "Capabilities": null,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "private",
            "Cgroup": "",
            "Links": null,
            "OomScoreAdj": 0,
            "PidMode": "",
            "Privileged": false,
            "PublishAllPorts": false,
            "ReadonlyRootfs": false,
            "SecurityOpt": null,
            "UTSMode": "",
            "UsernsMode": "",
            "ShmSize": 67108864,
            "Runtime": "runc",
            "ConsoleSize": [
                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/8df304dfa35b230db4be4375a64ecc173fe60b094d294c741cc18a0c96dd2e43-init/diff:/var/lib/docker/overlay2/0cb9be8148524543d03c8154eb2dbe3ad5a25fded931d4f8f9e83665a448ca0d/diff:/var/lib/docker/overlay2/0afec5029a73fa8e3fe1b929547a3062231091e13a175c5feb4f8615d0c01b99/diff:/var/lib/docker/overlay2/ccdd05f4cb175a568354340ce363a0d847b41e22dffe80037948d1296d961a32/diff:/var/lib/docker/overlay2/2af829e9b6451a53c5f747e55886e843daff2dfd77233c8ae3e3286e25f5a2eb/diff",
                "MergedDir": "/var/lib/docker/overlay2/8df304dfa35b230db4be4375a64ecc173fe60b094d294c741cc18a0c96dd2e43/merged",
                "UpperDir": "/var/lib/docker/overlay2/8df304dfa35b230db4be4375a64ecc173fe60b094d294c741cc18a0c96dd2e43/diff",
                "WorkDir": "/var/lib/docker/overlay2/8df304dfa35b230db4be4375a64ecc173fe60b094d294c741cc18a0c96dd2e43/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "3dc088a9821f",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "80/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "NGINX_VERSION=1.14.2"
            ],
            "Cmd": [
                "nginx",
                "-g",
                "daemon off;"
            ],
            "Image": "nginx:1.14-alpine",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "maintainer": "NGINX Docker Maintainers <docker-maint@nginx.com>"
            },
            "StopSignal": "SIGTERM"
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "f6c02746179a1ae0d1d913288bda58fe1e460a10914a8bcc3180a6dda69cded1",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/f6c02746179a",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "055a09643d5a2333874cc4dc0997b682bbc92a02a2cee9e7a0096193a4e0cb8f",
            "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": "8baa1345522f83775dbbafa9129f807dc84f3603e55567278bde4a918819c65f",
                    "EndpointID": "055a09643d5a2333874cc4dc0997b682bbc92a02a2cee9e7a0096193a4e0cb8f",
                    "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
                }
            }
        }
    }
]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211

删除容器
注:停止容器后,删除容器

[root@node01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
acca5d142dee        nginx:1.14-alpine   "nginx -g 'daemon of…"   3 minutes ago       Up 3 minutes        0.0.0.0:80->80/tcp   nginx2
3dc088a9821f        nginx:1.14-alpine   "nginx -g 'daemon of…"   15 minutes ago      Up 14 minutes       80/tcp               nginx
[root@node01 ~]# docker stop nginx2
nginx2
[root@node01 ~]# docker rm nginx2
nginx2
[root@node01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
3dc088a9821f        nginx:1.14-alpine   "nginx -g 'daemon of…"   16 minutes ago      Up 15 minutes       80/tcp              nginx
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

注:强制删除运行中的容器

[root@node01 ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
090322c06e5b        nginx:1.14-alpine   "nginx -g 'daemon of…"   6 seconds ago       Up 5 seconds        80/tcp              nginx2
3dc088a9821f        nginx:1.14-alpine   "nginx -g 'daemon of…"   18 minutes ago      Up 17 minutes       80/tcp              nginx
[root@node01 ~]# docker rm -f nginx2 nginx
nginx2
nginx
[root@node01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAME
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

注:将运行、没有运行的所有容器都删除掉

[root@node01 ~]# docker ps -aq
5a6a2d294f3d
0a2a2d538c1f
[root@node01 ~]# docker rm -f `docker ps -aq`
5a6a2d294f3d
0a2a2d538c1f
[root@node01 ~]# docker ps -aq
[root@node01 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

运行容器后进入,交互式界面

[root@node01 ~]# docker run --name=nginx1 nginx:1.14-alpine
[root@node01 ~]# docker exec -it nginx1 /bin/sh
/ # 
/ # ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
56: eth0@if57: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever
/ # 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

查看容器的访问日志

[root@node01 ~]# docker logs nginx1
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/529870
推荐阅读
相关标签
  

闽ICP备14008679号