理論+實操: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

2.2.2 部署19版docker

  • 安裝依賴包
[root@nginx ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
  • 設置阿里雲鏡像源
[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	//新增
  • 安裝docker-ce
[root@nginx yum.repos.d]# yum install -y docker-ce
  • 開啓docker服務,並開啓自啓
[root@nginx yum.repos.d]# systemctl start docker
[root@nginx yum.repos.d]# systemctl enable docker

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
  • 配置緩存加速
[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

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

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

docker images——查看當前docker下的下載鏡像信息

[root@nginx docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

docker ps -a——查看當前docker下的容器狀態

-a 列出最近一次啓動的容器

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

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                                       

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"}}}

此時再次查看images

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

latest代表標籤

docker inspect IMAGE-id——查看目標鏡像的詳細信息

其中包含大小,端口號

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

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

相當於重新複製一個別名,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

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

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

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

上傳鏡像到倉庫中
在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述
登錄本地倉庫

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

在這裏插入圖片描述

[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

查看當前容器

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

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 

啓動容器 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  

進入容器內部 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

一次性運行容器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

此時也多了一個鏡像

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

查看容器,發現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

終止容器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 

使一次性執行容器 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 

關閉死循環容器

這種會異常退出

[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 

進入容器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 ~]# 

容器導出 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

容器導入 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   

刪除容器 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 ~]# 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章