当前位置:   article > 正文

理论+实操:docker入门初体验,申请阿里镜像加速器_阿里云docker镜像加速申请

阿里云docker镜像加速申请

文章目录


前言:

Docker 核心概念

Docker 镜像操作

Docker 容器操作

Docker 资源控制

Docker 数据卷管理

一:Docker概述

官方网站:www.docker.com

官方文档:docs.docker.com

docker是一种轻量级的”虚拟机“

在linux容器里运行的开源工具

容器内的应用程序之间相互隔离、互不影响

便于应用拆分、解耦

例:LNMP——部署到同一台主机中

​ linux ——容器1

​ nginx——容器2

​ mysql——容器3

​ php-fpm——容器4

应用之间不受影响,可以方便迁移

1.1 docker概念:

daocker是一个开源的应用容器引擎(相当于vm 15就是虚拟机的引擎),让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中;然后发布到任何流行的linux或windows机器上,也可以实现虚拟化

容器是完全使用沙箱机制,相互之间不会有任何接口

docker不需要虚拟出来硬件,比如虚拟cpu、虚拟磁盘;docker是进程之间的隔离

1.2 docker设计的目标:

提供简单的应用程序打包工具

开发人员和运维人员职责逻辑分离

多环境保持一致性

1.3 docker的组成:

  • docker Client ——客户端
  • docker Daemon——守护进程
  • docker Images——镜像
  • docker container ——容器
  • docker registry ——镜像仓库

在这里插入图片描述
输入的指令交由客户端,客户端转发给守护进程处理

守护进程识别处理命令,比如说制造镜像images;也可以从仓库(regisstry,分为公有和私有)中拉取,从这中间需要一个加速器,后面去配置一个阿里云加速器

利用镜像images,直接制造容器containors

容器会开放端口,用以客户访问

彼此进程之间是相互隔离的,采用的就是解耦的思想

docker可以在秒级别的层面去扩展收缩节点

容器内的应用可以直接打包成镜像上传到仓库内

1.4 docker的使用场景

打包应用程序简化部署——做镜像文件

可脱离底层硬件任意迁移

例:服务器从腾讯云迁移到阿里云

应用程序打包和发布

应用程序隔离

持续集成

部署微服务

快速搭建测试环境

提供paas(平台即服务)

1.5 docker版本:

社区版(community edition,CE)

企业版(Enterprise edition,EE) 增值服务

二: docker与虚拟机的区别

openstack的云主机搭建是基于IAAS基础设施即服务的层面搭建

docker(k8s)的容器containors是基于PAAS平台即服务的层面搭建

区别于VMware Workstation Pro、kvm、exsi、hyper-v 微软、Virtual box的硬件虚拟化,docker是基于进程的隔离

在这里插入图片描述

app 应用

bins/libs 进程

guest OS 安装虚拟机系统(guest OS)

docker engine/安装虚拟化引擎 hypervisor/监控平台

openrating system 宿主系统

infrastructure 硬件层面

备注:

  • docker 没有虚拟系统层面

  • hypervisor会侵占5%的cpu资源

  • docker engine 几乎忽略,只损耗关于网络通信的资源

  • docker的安全性低,因为是在宿主系统直接安装

  • 虚拟机因为有一个虚拟机系统,所以安全性要高

在这里插入图片描述

备注:

  • 磁盘占用,docker占用空间少,因为他是个进程级别

  • docker主要支持linux

docker支持平台

  • linux(centos、debian、fedora、oracle linux、rhel、suse、ubuntu)
  • mac
  • windows

二:docker的核心概念及安装方式

镜像

容器

仓库

2.1 centos安装docker的两种方式

  • 使用curl 获得docker的安装脚本进行安装
  • 使用yum仓库来安装docker

2.2 实操演示

2.2.1 关闭防火墙,核心防护

[root@nginx ~]# systtemctl stop firewalld
-bash: systtemctl: command not found
[root@nginx ~]# systemctl stop firewalld
[root@nginx ~]# systemctl disable firewalld
[root@nginx ~]# setenforce 0
setenforce: SELinux is disabled
[root@nginx ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2.2.2 部署19版docker

  • 安装依赖包
[root@nginx ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
  • 1
  • 设置阿里云镜像源
[root@nginx ~]# cd /etc/yum.repos.d/
[root@nginx yum.repos.d]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@nginx yum.repos.d]# ls
docker-ce.repo	//新增
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 安装docker-ce
[root@nginx yum.repos.d]# yum install -y docker-ce
  • 1
  • 开启docker服务,并开启自启
[root@nginx yum.repos.d]# systemctl start docker
[root@nginx yum.repos.d]# systemctl enable docker
  • 1
  • 2

2.2.3 使用阿里云设置镜像加速器

打开https://help.aliyun.com/document_detail/60750.html

在这里插入图片描述

登录,点击容器镜像服务控制台

在这里插入图片描述

前往开通

在这里插入图片描述

在这里插入图片描述
设置密码

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://fk2yrsh1.mirror.aliyuncs.com"]
}
EOF
  • 1
  • 2
  • 3
  • 4
  • 5
  • 配置缓存加速
[root@nginx yum.repos.d]# cd /etc/docker/
You have new mail in /var/spool/mail/root
[root@nginx docker]# ls
key.json
[root@nginx docker]# tee /etc/docker/daemon.json <<-'EOF'
> {
>   "registry-mirrors": ["https://fk2yrsh1.mirror.aliyuncs.com"]
> }
> EOF
{
  "registry-mirrors": ["https://fk2yrsh1.mirror.aliyuncs.com"]
}
[root@nginx docker]# ls
daemon.json  key.json
[root@nginx docker]# systemctl daemon-reload
[root@nginx docker]# systemctl restart docker
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

2.2.4 网络优化

[root@nginx docker]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf 
[root@nginx docker]# sysctl -p
net.ipv4.ip_forward = 1
[root@nginx docker]# systemctl restart network
[root@nginx docker]# systemctl restart docker
  • 1
  • 2
  • 3
  • 4
  • 5

2.2.5 命令演示

docker version——查看当前docker版本信息
[root@nginx docker]# docker version
Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b
 Built:             Wed Mar 11 01:27:04 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:25:42 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

  • 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
docker images——查看当前docker下的下载镜像信息
[root@nginx docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

  • 1
  • 2
  • 3
docker ps -a——查看当前docker下的容器状态

-a 列出最近一次启动的容器

[root@nginx docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
  • 1
  • 2
docker search nginx——查看仓库内nginx服务的可安装images

此时下载镜像默认是从公有仓库,从公有仓库下载需要使用镜像加速

STARS越高,代表该镜像被使用率越高

jwilder/nginx-proxy 含义是作者/软件名

[root@nginx docker]# which docker
/usr/bin/docker
[root@nginx docker]# docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        12946               [OK]                
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   1767                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   764                                     [OK]
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   102                                     
bitnami/nginx                      Bitnami nginx Docker Image                      81                                      [OK]
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   67                                      [OK]
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   50                                      
nginxdemos/hello                   NGINX webserver that serves a simple page co…   47                                      [OK]
jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        37                                      [OK]
nginx/unit                         NGINX Unit is a dynamic web and application …   36                                      
nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         28                                      
privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & Al…   23                                      [OK]
schmunk42/nginx-redirect           A very simple container to redirect HTTP tra…   18                                      [OK]
centos/nginx-18-centos7            Platform for running nginx 1.8 or building n…   13                                      
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  13                                      
centos/nginx-112-centos7           Platform for running nginx 1.12 or building …   13                                      
blacklabelops/nginx                Dockerized Nginx Reverse Proxy Server.          13                                      [OK]
raulr/nginx-wordpress              Nginx front-end for the official wordpress:f…   12                                      [OK]
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       10                                      
sophos/nginx-vts-exporter          Simple server that scrapes Nginx vts stats a…   7                                       [OK]
mailu/nginx                        Mailu nginx frontend                            6                                       [OK]
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr…   4                                       [OK]
ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          1                                       [OK]
wodby/nginx                        Generic nginx                                   0                                       [OK]
centos/nginx-110-centos7           Platform for running nginx 1.10 or building …   0                                       

  • 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
docker pull nginx——下载想要使用的镜像,拉取镜像

AUFS (联合文件系统)若干层下载

c499e6d256d6: Pull complete
74cda408e262: Pull complete
ffadbd415ab7: Pull complete

下载信息存放在/var/lib/docker/image/overlay2/下

[root@nginx docker]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
c499e6d256d6: Pull complete 
74cda408e262: Pull complete 
ffadbd415ab7: Pull complete 
Digest: sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@nginx docker]# cd /var/lib/docker/image/overlay2/
[root@nginx overlay2]# ls
distribution  imagedb  layerdb  repositories.json
[root@nginx overlay2]# cat repositories.json 
{"Repositories":{"nginx":{"nginx:latest":"sha256:ed21b7a8aee9cc677df6d7f38a641fa0e3c05f65592c592c9f28c42b3dd89291","nginx@sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596":"sha256:ed21b7a8aee9cc677df6d7f38a641fa0e3c05f65592c592c9f28c42b3dd89291"}}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

此时再次查看images

[root@nginx overlay2]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        8 days ago          127MB

  • 1
  • 2
  • 3
  • 4

latest代表标签

docker inspect IMAGE-id——查看目标镜像的详细信息

其中包含大小,端口号

[root@nginx overlay2]# docker inspect ed21b7a8aee9
[
    {
        "Id": "sha256:ed21b7a8aee9cc677df6d7f38a641fa0e3c05f65592c592c9f28c42b3dd89291",
        "RepoTags": [
            "nginx:latest"

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
docker tag nginx:latest nginx:web——添加新标签
[root@nginx overlay2]# docker tag nginx:latest nginx:web
[root@nginx overlay2]# docker images	//可以使用grep 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        8 days ago          127MB
nginx               web                 ed21b7a8aee9        8 days ago          127MB

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

相当于重新复制一个别名,ID号一样的

docker rmi nginx:web——删除镜像别名
[root@nginx overlay2]# docker rmi nginx:latest
Untagged: nginx:latest
[root@nginx overlay2]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               web                 ed21b7a8aee9        8 days ago          127MB
  • 1
  • 2
  • 3
  • 4
  • 5
docker save -o nginx nginx:web——导出镜像命名为nginx,存放在当前目录下
[root@nginx opt]# docker save -o nginx nginx:web
[root@nginx opt]# ls -lh nginx
-rw------- 1 root root 125M Apr  8 20:39 nginx
  • 1
  • 2
  • 3
docker rmi 镜像ID(或者仓库:标签)——删除镜像

docker rmi ed21b7a8aee9

[root@nginx opt]# docker rmi nginx:web
Untagged: nginx:web
Untagged: nginx@sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596
Deleted: sha256:ed21b7a8aee9cc677df6d7f38a641fa0e3c05f65592c592c9f28c42b3dd89291
Deleted: sha256:8a305f371a6c3c445a1dfc500c1364743868a269ab8cdaf95902692e82168352
Deleted: sha256:d079ef06ec1f10a8050887365f9a940b39547ba6bcc46b16a463e740984f3223
Deleted: sha256:c3a984abe8a88059915bb6c7a1d249fd1ccc16d931334ac8816540b0eb686b45
[root@nginx opt]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
docker load < nginx——导入当前目录下的镜像
[root@nginx opt]# docker load < nginx
[root@nginx opt]# docker load < nginx
c3a984abe8a8: Loading layer [==================================================>]  72.48MB/72.48MB
99134ec7f247: Loading layer [==================================================>]  58.11MB/58.11MB
d37eecb5b769: Loading layer [==================================================>]  3.584kB/3.584kB
Loaded image: nginx:web
[root@nginx opt]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               web                 ed21b7a8aee9        8 days ago          127MB
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

上传镜像到仓库中
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
登录本地仓库

docker tag nginx:web registry.cn-hangzhou.aliyuncs.com/nginx_gsy/docker_gsy:web

docker tag 仓库名:标签 私有仓库地址/命名空间/仓库名:镜像版本号

[root@nginx opt]# docker login --username=压马路一起 registry.cn-hangzhou.aliyuncs.com
//登录
Password: 输入密码
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@nginx opt]# docker tag nginx:web registry.cn-hangzhou.aliyuncs.com/nginx_gsy/docker_gsy:web
//上传镜像
[root@nginx opt]# docker push registry.cn-hangzhou.aliyuncs.com/nginx_gsy/docker_gsy:web
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/nginx_gsy/docker_gsy]
d37eecb5b769: Pushed 
99134ec7f247: Pushed 
c3a984abe8a8: Pushed 
web: digest: sha256:7ac7819e1523911399b798309025935a9968b277d86d50e5255465d6592c0266 size: 948

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在这里插入图片描述

[root@nginx opt]# docker images
REPOSITORY                                               TAG                 IMAGE ID            CREATED             SIZE
nginx                                                    web                 ed21b7a8aee9        8 days ago          127MB
registry.cn-hangzhou.aliyuncs.com/nginx_gsy/docker_gsy   web                 ed21b7a8aee9        8 days ago          127MB

  • 1
  • 2
  • 3
  • 4
  • 5

查看当前容器

[root@nginx opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
  • 1
  • 2

2.2.6 容器的操作

容器创建 docker create -it nginx:web /bin/bash

此时容器为created状态

-i 让容器的标准型输入保持打开

-t 让容器开启一个伪终端

[root@nginx opt]# docker create -it nginx:web /bin/bash
0baf68712aedaf52156085b6fc0567c840533a6b0061dcc658b7f5df11c17d7d
[root@nginx opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0baf68712aed        nginx:web           "/bin/bash"         15 seconds ago      Created 
  • 1
  • 2
  • 3
  • 4
  • 5
启动容器 docker start container-id

此时容器为up状态,端口开启

[root@nginx opt]# docker start 0baf68712aed 
0baf68712aed
You have new mail in /var/spool/mail/root
[root@nginx opt]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
0baf68712aed        nginx:web           "/bin/bash"         About a minute ago   Up 3 seconds        80/tcp  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
进入容器内部 docker exec -it 0baf68712aed /bin/bash
[root@nginx opt]# docker exec -it 0baf68712aed /bin/bash
root@0baf68712aed:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
  • 1
  • 2
  • 3
一次性运行容器centos7,执行查看命令 docker run cetnos:7 /usr/bin/bash -c ls /

-c commad的意思

先去找镜像,如果没有,就去仓库下载,之后执行一次-c的命令

[root@nginx ~]# docker run centos:7 /usr/bin/bash -c ls /
Unable to find image 'centos:7' locally
7: Pulling from library/centos
ab5ef0e58194: Pull complete 
Digest: sha256:4a701376d03f6b39b8c2a8f4a8e499441b0d567f9ab9d58e4991de4472fb813c
Status: Downloaded newer image for centos:7
anaconda-post.log
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
  • 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

此时也多了一个镜像

[root@nginx ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        8 days ago          127MB
centos              7                   5e35e350aded        4 months ago        203MB
  • 1
  • 2
  • 3
  • 4

查看容器,发现centos:7的状态是exited(0)指退出状态,(0)为正常退出

-c的ls命令执行完了,成功执行就正常退出

[root@nginx ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
a75d7c0e9f65        centos:7            "/usr/bin/bash -c ls…"   2 minutes ago       Exited (0) 2 minutes ago                        hungry_moser
  • 1
  • 2
  • 3
终止容器up状态 docker stop 容器-id
[root@nginx ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              ed21b7a8aee9        8 days ago          127MB
centos              7                   5e35e350aded        4 months ago        203MB
[root@nginx ~]# docker create -it nginx:latest /bin/bash
d080f27d8e0fb1269480a3bf7977e2104e4f580f5bfd68488a348e667b31d70e
[root@nginx ~]# docker start d080f27d8e0f
[root@nginx ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                     PORTS               NAMES
d080f27d8e0f        nginx:latest        "/bin/bash"              About a minute ago   Up 3 seconds               80/tcp              jovial_cerf
a75d7c0e9f65        centos:7            "/usr/bin/bash -c ls…"   7 minutes ago        Exited (0) 6 minutes ago                       hungry_moser
[root@nginx ~]# docker stop d080f27d8e0f
d080f27d8e0f
[root@nginx ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                     PORTS               NAMES
d080f27d8e0f        nginx:latest        "/bin/bash"              About a minute ago   Exited (0) 1 second ago                        jovial_cerf
a75d7c0e9f65        centos:7            "/usr/bin/bash -c ls…"   7 minutes ago        Exited (0) 7 minutes ago 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
使一次性执行容器 docker run ,持续性在后台运行

写个死循环语句,让其保持UP状态

-d 给一个守护进程

[root@nginx ~]# docker run -d centos:7 /bin/bash -c "while true;do echo hello;done"
f9197f3ebbf773559e7dc653796f83847ca43ceb99aa130d5aa8068065cbf082
[root@nginx ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
f9197f3ebbf7        centos:7            "/bin/bash -c 'while…"   11 seconds ago      Up 10 seconds 
  • 1
  • 2
  • 3
  • 4
  • 5
关闭死循环容器

这种会异常退出

[root@nginx ~]# docker stop f9197f3ebbf7
f9197f3ebbf7
[root@nginx ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                       PORTS               NAMES
f9197f3ebbf7        centos:7            "/bin/bash -c 'while…"   About a minute ago   Exited (137) 2 seconds ago 
  • 1
  • 2
  • 3
  • 4
  • 5
进入容器docker exec -it 容器-id /bin/bash

首先目标容器是一个up状态

然后进入docker exec it

[root@nginx ~]# docker start d080f27d8e0f 
d080f27d8e0f
[root@nginx ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
f9197f3ebbf7        centos:7            "/bin/bash -c 'while…"   3 minutes ago       Exited (137) 2 minutes ago                       hopeful_blackburn
d080f27d8e0f        nginx:latest        "/bin/bash"              7 minutes ago       Up 1 second                  80/tcp  
[root@nginx ~]# docker exec -it d080f27d8e0f  /bin/bash
root@d080f27d8e0f:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@d080f27d8e0f:/# exit
exit
[root@nginx ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
容器导出 docker export 容器-id > nginx_c
[root@nginx ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
f9197f3ebbf7        centos:7            "/bin/bash -c 'while…"   7 minutes ago       Exited (137) 6 minutes ago                       hopeful_blackburn
d080f27d8e0f        nginx:latest        "/bin/bash"              11 minutes ago      Up 4 minutes                 80/tcp              jovial_cerf
a75d7c0e9f65        centos:7            "/usr/bin/bash -c ls…"   17 minutes ago      Exited (0) 17 minutes ago  
[root@nginx ~]# docker export a75d7c0e9f65 > nginx_c	导出,此时这个节点使exited状态
[root@nginx ~]# ls -lh nginx_c
-rw-r--r-- 1 root root 202M Apr  8 22:22 nginx_c
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
容器导入 cat nginx_c | docker import - nginx:c

(会生成镜像,而不会创建容器)

[root@nginx ~]# cat nginx_c | docker import - nginx:c
sha256:525c40797065e97c8c9238a486de8109292a8016cc081283ed32c8d32d6a8c27
[root@nginx ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               c                   525c40797065        33 seconds ago      203MB
nginx               latest              ed21b7a8aee9        8 days ago          127MB
centos              7                   5e35e350aded        4 months ago        203MB
[root@nginx ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
f9197f3ebbf7        centos:7            "/bin/bash -c 'while…"   12 minutes ago      Exited (137) 11 minutes ago                       hopeful_blackburn
d080f27d8e0f        nginx:latest        "/bin/bash"              16 minutes ago      Up 9 minutes                  80/tcp              jovial_cerf
a75d7c0e9f65        centos:7            "/usr/bin/bash -c ls…"   22 minutes ago      Exited (0) 22 minutes ago   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
删除容器 docker rm 容器-id

批量删除容器

容器必须先停止,再删除

[root@nginx ~]# docker ps -a | awk '{print "docker rm "$1}' | bash
Error: No such container: CONTAINER
f9197f3ebbf7
Error response from daemon: You cannot remove a running container d080f27d8e0fb1269480a3bf7977e2104e4f580f5bfd68488a348e667b31d70e. Stop the container before attempting removal or force remove
a75d7c0e9f65
[root@nginx ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d080f27d8e0f        nginx:latest        "/bin/bash"         21 minutes ago      Up 13 minutes       80/tcp              jovial_cerf
[root@nginx ~]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/964302
推荐阅读
相关标签
  

闽ICP备14008679号