獲取Docker Engine-CentOS社區: https://docs.docker.com/install/linux/docker-ce/centos/
1.操作系統要求
1.1 要安裝 Docker Engine-Community ,您需要一個 CentOS 7 的維護版本.不支持或未測試存檔版本.
1.2 該 centos-extras 庫必須啟用.默認情況下,此存儲庫是啟用的,但是如果已禁用它,則需要 重新啟用它.
1.3 overlay2 建議使用存儲驅動程序.
< centos-extras >
CentOS Extras-在 CentOS 5 和 6 中,為CentOS提供附加功能而不破壞上游兼容性或更新基本組件的軟件包,但未經上游測試或上游產品中沒有提供. 在 CentOS 7 中,這是一個上游存儲庫,以及其他CentOS軟件包.CentOS 開發團隊已經測試了該存儲庫中的每個項目,並且它們都與 CentOS 一起使用. 該存儲庫隨CentOS一起提供,並且默認情況下處於啟用狀態.
------------------------------------------------------------------------------------------ 開啟 CentOS Extra 路徑 /etc/yum.repos.d 文件 CentOS-Base.repo [extras] //extras 模塊 enabled=0 //編輯把 "0" 修改為 "1" ,沒有enabled=0或1,建議添加 enabled=1 啟動 extras 軟件模塊倉庫
#軟件倉庫被配置成 enabled=0 時,yum 在安裝或升級軟件包時不會將該倉庫做為軟件包提供源. ------------------------------------------------------------------------------------------
[root@localhost yum.repos.d]# (cat -n CentOS-Base.repo;pwd) 1 # CentOS-Base.repo 2 # 3 # The mirror system uses the connecting IP address of the client and the 4 # update status of each mirror to pick mirrors that are updated to and 5 # geographically close to the client. You should use this for CentOS updates 6 # unless you are manually picking other mirrors. 7 # 8 # If the mirrorlist= does not work for you, as a fall back you can try the 9 # remarked out baseurl= line instead. 10 # 11 # 12 13 [base] 14 name=CentOS-$releasever - Base 15 mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra 16 #baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/ 17 gpgcheck=1 18 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 19 20 #released updates 21 [updates] 22 name=CentOS-$releasever - Updates 23 mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra 24 #baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/ 25 gpgcheck=1 26 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 27 28 #additional packages that may be useful 29 [extras] 30 name=CentOS-$releasever - Extras 31 mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra 32 #baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/ 33 gpgcheck=1 34 enabled=1 35 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 36 37 #additional packages that extend functionality of existing packages 38 [centosplus] 39 name=CentOS-$releasever - Plus 40 mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra 41 #baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/ 42 gpgcheck=1 43 enabled=0 44 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 45 /etc/yum.repos.d
2.卸載舊版本
sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
較舊的Docker版本稱為docker或docker-engine.如果已安裝這些程序,請卸載它們以及相關的依賴項.
[root@localhost ~]# sudo yum remove docker \ > docker-client \ > docker-client-latest \ > docker-common \ > docker-latest \ > docker-latest-logrotate \ > docker-logrotate \ > docker-engine
3.使用存儲庫安裝
在新主機上首次安裝Docker Engine-Community之前,需要設置Docker存儲庫.之后,您可以從存儲庫安裝和更新Docker.
設置存儲庫
3.1 安裝所需的軟件包.yum-utils提供了yum-config-manager 效用,並device-mapper-persistent-data和lvm2由需要 devicemapper存儲驅動程序.
sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2
[root@localhost ~]# sudo yum install -y yum-utils \ > device-mapper-persistent-data \ > lvm2
3.2 使用以下命令來設置穩定的存儲庫.
sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
[root@localhost ~]# sudo yum-config-manager \ > --add-repo \ > https://download.docker.com/linux/centos/docker-ce.repo
4.安裝 DOCKER ENGINE-CentOS 社區版
安裝最新版本的Docker Engine-Community和containerd,或者轉到下一步安裝特定版本:
sudo yum install -y docker-ce docker-ce-cli containerd.io
[root@localhost ~]# sudo yum install -y docker-ce docker-ce-cli containerd.io
如果提示您接受GPG密鑰,請驗證指紋是否匹配 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35,如果是,則接受它.
5.有多個Docker存儲庫嗎?
如果啟用了多個Docker存儲庫,則在未在yum installor yum update命令中指定版本的情況下進行安裝或更新將始終安裝可能的最高版本,這可能不適合您的穩定性需求.
Docker已安裝但尚未啟動.docker創建該組,但沒有用戶添加到該組.
要安裝特定版本的Docker Engine-Community,請在存儲庫中列出可用版本,然后選擇並安裝:
一種.列出並排序您存儲庫中可用的版本.此示例按版本號(從高到低)對結果進行排序,並被截斷:
yum list docker-ce --showduplicates | sort -r
[root@localhost ~]# yum list docker-ce --showduplicates | sort -r * updates: mirrors.aliyun.com Loading mirror speeds from cached hostfile Loaded plugins: fastestmirror Installed Packages * extras: mirrors.aliyun.com * epel: hkg.mirror.rackspace.com docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.3-3.el7 @docker-ce-stable docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.0-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.9-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.8-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.7-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.5-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable * base: mirrors.aliyun.com Available Packages
返回的列表取決於啟用的存儲庫,並且特定於您的CentOS版本(.el7在此示例中以后綴表示)
通過其完全合格的軟件包名稱安裝特定版本,該軟件包名稱是軟件包名稱(docker-ce)加上版本字符串(第二列),從第一個冒號(:)一直到第一個連字符,並用連字符(-)分隔.例如,docker-ce-18.09.1
sudo yum install -y docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
Docker已安裝但尚未啟動.
6.啟動Docker
[root@localhost ~]# sudo systemctl start docker
7.檢查Docker進程
[root@localhost ~]# pgrep -l docker 7286 dockerd
8.通過運行 hello-world 映像來驗證是否正確安裝了Docker Engine-Community.
[root@localhost ~]# sudo docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
運行命令下載測試圖像並在容器中運行。容器運行時,它會打印參考消息並退出.
Docker Engine-Community已安裝並正在運行.
您需要使用sudo
來運行Docker命令.繼續進行Linux后安裝,以允許非特權用戶運行Docker命令以及其他可選配置步驟.
Docker創建該組,但沒有用戶添加到該組.
1.創建用戶名 [root@localhost ~]# useradd wwweee000 [root@localhost ~]# passwd wwweee000 Changing password for user wwweee000. New password: BAD PASSWORD: The password is a palindrome Retype new password: passwd: all authentication tokens updated successfully. 2.創建docker用戶組名 [root@localhost ~]# sudo groupadd docker groupadd: group 'docker' already exists 3. 應用wwweee000用戶加入docker用戶組 [root@localhost ~]# sudo usermod -aG docker wwweee000 4. 切換或者退出當前賬戶再從新登入 su root 切換到root用戶 su wwweee000 再切換到原來的應用用戶以上配置才生效 [root@localhost ~]# vi /etc/sudoers root ALL=(ALL) ALL wwweee000 ALL=(ALL) ALL [root@localhost ~]# cat -n /etc/sudoers | grep "root\|wwweee000" 2 ## the root user, without needing the root password. 99 ## Allow root to run any commands anywhere 100 root ALL=(ALL) ALL 101 wwweee000 ALL=(ALL) ALL 114 ## cdrom as root [root@localhost ~]# chmod u+w /etc/sudoers [root@localhost ~]# newgrp docker [root@localhost ~]# sudo systemctl restart docker [root@localhost ~]# su wwweee000 [wwweee000@localhost root]$ cd [wwweee000@localhost ~]$ docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ [wwweee000@localhost ~]$ su root Password: [root@localhost wwweee000]# cd [root@localhost ~]# 說明: 這里說下你可以sudoers添加下面四行中任意一條 youuser ALL=(ALL) ALL %youuser ALL=(ALL) ALL youuser ALL=(ALL) NOPASSWD: ALL %youuser ALL=(ALL) NOPASSWD: ALL 第一行:允許用戶youuser執行sudo命令(需要輸入密碼). 第二行:允許用戶組youuser里面的用戶執行sudo命令(需要輸入密碼). 第三行:允許用戶youuser執行sudo命令,並且在執行的時候不輸入密碼. 第四行:允許用戶組youuser里面的用戶執行sudo命令,並且在執行的時候不輸入密碼. 撤銷sudoers文件寫權限,命令: chmod u-w /etc/sudoers
----------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------安裝完成 ---------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------
1.從軟件包安裝(參考)
如果無法使用Docker的存儲庫安裝Docker,則可以下載該.rpm發行版的 文件並手動安裝.
每次升級Docker Engine-Community時,都需要下載一個新文件.
轉到 https://download.docker.com/linux/centos/7/x86_64/stable/Packages/ 並下載.rpm要安裝的Docker版本的文件.
注意:要安裝夜間 或測試(預發布)軟件包,stable 請將上述URL中的單詞更改為nightly或test. 了解每晚和測試頻道.
安裝 Docker Engine-Community ,將下面的路徑更改為您下載Docker軟件包的路徑.
[root@localhost ~]# sudo yum install /path/to/package.rpm
Docker已安裝但尚未啟動.docker創建該組,但沒有用戶添加到該組.
啟動Docker.
[root@localhost ~]# sudo systemctl start docker
通過運行hello-world 映像來驗證是否正確安裝了 Docker Engine-Community .
[root@localhost ~]# sudo docker run hello-world
運行命令下載測試圖像並在容器中運行.容器運行時,它會打印參考消息並退出.
Docker Engine-Community已安裝並正在運行.您需要使用sudo來運行Docker命令.
繼續執行Linux的安裝后步驟,以允許非特權用戶運行Docker命令以及其他可選配置步驟.
升級DOCKER引擎-社區
要升級Docker Engine-Community,請下載更新的軟件包文件,然后使用 代替,並指向新文件,重復 安裝過程.
[root@localhost ~]# yum -y upgradeyum -y install
2.使用便捷腳本進行安裝(參考)
Docker在get.docker.com 和test.docker.com上提供了方便腳本,用於將Docker Engine-Community的邊緣版本和測試版本快速且非交互地安裝到開發環境中.
腳本的源代碼在 docker-install存儲庫中.
不建議在生產環境中使用這些腳本,在使用它們之前,您應該了解潛在的風險:
腳本需要運行root或具有sudo特權.因此,在運行腳本之前,應仔細檢查和審核腳本.
這些腳本嘗試檢測Linux發行版和版本,並為您配置軟件包管理系統.此外,腳本不允許您自定義任何安裝參數.
從Docker的角度或您自己組織的准則和標准的角度來看,這可能導致不支持的配置.
這些腳本將安裝軟件包管理器的所有依賴項和建議,而無需進行確認.這可能會安裝大量軟件包,具體取決於主機的當前配置.
該腳本未提供用於指定要安裝哪個版本的Docker的選項,而是安裝了在“ edge”通道中發布的最新版本.
如果已使用其他機制將Docker安裝在主機上,請不要使用便捷腳本.
本示例使用get.docker.com上的腳本在Linux上安裝最新版本的Docker Engine-Community.
要安裝最新的測試版本,請改用test.docker.com.在下面的每個命令,取代每次出現get用test.
警告:
在本地運行之前,請務必檢查從Internet下載的腳本.
[root@localhost ~]# curl -fsSL https://get.docker.com -o get-docker.sh [root@localhost ~]# sudo sh get-docker.sh <output truncated>
如果要使用Docker作為非root用戶,則現在應考慮使用類似以下方式將用戶添加到“ docker”組:
[root@localhost ~]# sudo usermod -aG docker wwweee000
請記住注銷並重新登錄才能生效!
警告:
將用戶添加到“docker”組后,他們可以運行容器,該容器可用於獲取Docker主機上的根特權.
Docker Engine-CentOS社區已安裝.它會自動在DEB基於發布的版本上啟動.
在 RPM 基於發行版的系統上,您需要使用相應的systemctl或service命令手動啟動它 .
如消息所示,默認情況下,非root用戶無法運行Docker命令.
使用便捷腳本后升級DOCKER
如果使用便捷腳本安裝了Docker ,則應直接使用包管理器升級 Docker.
重新運行便利程序腳本沒有任何好處,並且如果嘗試重新添加已經添加到主機的存儲庫,則可能導致問題.
卸載Docker Engine-社區
卸載Docker軟件包:
[root@localhost ~]# sudo yum remove docker-ce
主機上的映像,容器,卷或自定義配置文件不會自動刪除.要刪除所有圖像,容器和卷:
[root@localhost ~]# sudo rm -rf /var/lib/docker
您必須手動刪除所有已編輯的配置文件.
----------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------overlay2 建議使用存儲驅動程序.------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------
1.docker默認的存儲目錄是/var/lib/docker [root@localhost ~]# ls -lh /var/lib/docker total 8.0K drwx------ 2 root root 24 Oct 10 01:10 builder drwx--x--x 4 root root 92 Oct 10 01:10 buildkit drwx------ 7 root root 4.0K Oct 10 02:05 containers drwx------ 3 root root 22 Oct 10 01:10 image drwxr-x--- 3 root root 19 Oct 10 01:10 network drwx------ 17 root root 4.0K Oct 10 02:05 overlay2 drwx------ 4 root root 32 Oct 10 01:10 plugins drwx------ 2 root root 6 Oct 10 01:52 runtimes drwx------ 2 root root 6 Oct 10 01:10 swarm drwx------ 2 root root 6 Oct 10 02:05 tmp drwx------ 2 root root 6 Oct 10 01:10 trust drwx------ 2 root root 25 Oct 10 01:10 volumes 2.啟動nginx容器 [root@localhost ~]# docker run -d nginx //會下載nginx鏡像並運行一個nginx鏡像容器進程 Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx b8f262c62ec6: Pull complete e9218e8f93b1: Pull complete 7acba7289aa3: Pull complete Digest: sha256:aeded0f2a861747f43a01cf1018cf9efe2bdd02afd57d2b11fcc7fcadc16ccd1 Status: Downloaded newer image for nginx:latest c6970c0f854135f75998ed1bdc0c7cc2ebea86acd3fe02451063cde641b8a4d1 3.查找 Docker Hub 上的 nginx 鏡像 [root@localhost ~]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 12037 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1671 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 742 [OK] linuxserver/nginx An Nginx container, brought to you by LinuxS… 78 bitnami/nginx Bitnami nginx Docker Image 71 [OK] tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 56 [OK] nginxdemos/hello NGINX webserver that serves a simple page co… 29 [OK] jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 24 [OK] jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 24 nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 22 privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 18 [OK] schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 17 [OK] blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 12 [OK] centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 11 centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 10 nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 9 nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 7 sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 5 [OK] 1science/nginx Nginx Docker images that include Consul Temp… 5 [OK] mailu/nginx Mailu nginx frontend 4 [OK] pebbletech/nginx-proxy nginx-proxy sets up a container running ngin… 2 [OK] travix/nginx NGinx reverse proxy 2 [OK] ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 1 [OK] centos/nginx-110-centos7 Platform for running nginx 1.10 or building … 0 wodby/nginx Generic nginx 0 [OK] 4.拉取官方的鏡像 [root@localhost ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx Digest: sha256:aeded0f2a861747f43a01cf1018cf9efe2bdd02afd57d2b11fcc7fcadc16ccd1 Status: Image is up to date for nginx:latest docker.io/library/nginx:latest 4.查到本地鏡像列表里 REPOSITORY 為 nginx 的鏡像 [root@localhost ~]# docker images nginx REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f949e7d76d63 2 weeks ago 126MB 5.查看網卡信息(多了個docker0網卡) [root@localhost ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 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 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:f2:3d:bf brd ff:ff:ff:ff:ff:ff inet 192.168.1.3/24 brd 192.168.1.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet6 fe80::9a8:d0da:266f:5ac7/64 scope link noprefixroute valid_lft forever preferred_lft forever 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:32:18:de:d4 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:32ff:fe18:ded4/64 scope link valid_lft forever preferred_lft forever 6.啟動一個 Nginx 容器 [root@localhost ~]# docker run --name nginx_name -p 8081:80 -d nginx cd5b2b7338504b1e0a5dc074b796e37b4959fd93ec5260c14f0b3284929c1396 nginx_name 容器名稱設置不要使用- the -d設置容器在在后台一直運行 the -p 端口進行映射,將本地 8081 端口映射到容器內部的 80 端口 7c82f5afa73e2d8d031ed95d109b4e1cd7e39292591a3dd9dd94caeb772079b1 為容器的 ID [root@localhost ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 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 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:f2:3d:bf brd ff:ff:ff:ff:ff:ff inet 192.168.1.3/24 brd 192.168.1.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet6 fe80::9a8:d0da:266f:5ac7/64 scope link noprefixroute valid_lft forever preferred_lft forever 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:32:18:de:d4 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:32ff:fe18:ded4/64 scope link valid_lft forever preferred_lft forever 19: vethd3e3ef9@if18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default link/ether 82:2d:54:0d:34:da brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet6 fe80::802d:54ff:fe0d:34da/64 scope link valid_lft forever preferred_lft forever 7.查找nginx運行進程信息 [root@localhost ~]# docker ps|grep nginx cd5b2b733850 nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:8081->80/tcp runoob-nginx-test [root@localhost ~]# docker top nginx_name UID PID PPID C STIME TTY TIME CMD root 17118 17101 0 03:10 ? 00:00:00 nginx: master process nginx -g daemon off; 101 17167 17118 0 03:11 ? 00:00:00 nginx: worker process [root@localhost ~]# ps -ef | grep nginx root 17118 17101 0 03:10 ? 00:00:00 nginx: master process nginx -g daemon off; 101 17167 17118 0 03:11 ? 00:00:00 nginx: worker process root 17360 10468 0 03:13 pts/0 00:00:00 grep --color=auto nginx 8.查看Docker進程 列出所有在運行的容器信息 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cd5b2b733850 nginx "nginx -g 'daemon of…" 5 minutes ago Up 4 minutes 0.0.0.0:8081->80/tcp nginx_name 9.列出最近創建的5個容器信息 [root@localhost ~]# docker ps -n 5 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cd5b2b733850 nginx "nginx -g 'daemon of…" 5 minutes ago Up 5 minutes 0.0.0.0:8081->80/tcp nginx_name 10.列出所有創建的容器ID [root@localhost ~]# docker ps -a -q cd5b2b733850 11.確認使用 overlay2 使用存儲驅動程序 [root@localhost ~]# ls -lh /var/lib/docker/image/ total 0 drwx------ 5 root root 81 Oct 10 02:05 overlay2 12.查看overlay2目錄 [root@localhost ~]# tree -L 2 /var/lib/docker/image/overlay2/ /var/lib/docker/image/overlay2/ ├── distribution │ ├── diffid-by-digest │ └── v2metadata-by-diffid ├── imagedb │ ├── content │ └── metadata ├── layerdb │ ├── mounts │ ├── sha256 │ └── tmp └── repositories.json 10 directories, 1 file 13.停止 nginx_name [root@localhost ~]# docker stop nginx_name nginx_name 14.刪除 nginx_name 容器 [root@localhost ~]# docker rm nginx_name nginx_name 15.刪除后查看網卡信息 [root@localhost ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 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 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:f2:3d:bf brd ff:ff:ff:ff:ff:ff inet 192.168.1.3/24 brd 192.168.1.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet6 fe80::9a8:d0da:266f:5ac7/64 scope link noprefixroute valid_lft forever preferred_lft forever 3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:32:18:de:d4 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:32ff:fe18:ded4/64 scope link valid_lft forever preferred_lft forever 15.切換普通用戶wwweee000 [root@localhost ~]# su wwweee000 [wwweee000@localhost root]$ cd [wwweee000@localhost ~]$ docker run --name nginx_name_wwweee000 -p 8082:80 -d nginx 79f9741f257ac4fc8079b4283d9830cec8d2a5d85fde530bf08aec3f5a1970a3 [wwweee000@localhost ~]$ docker ps |grep nginx_name_wwweee000 79f9741f257a nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:8082->80/tcp nginx_name_wwweee000 [wwweee000@localhost ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 79f9741f257a nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:8082->80/tcp nginx_name_wwweee000 ae4026623f4a nginx "nginx -g 'daemon of…" 6 minutes ago Up 6 minutes 0.0.0.0:8081->80/tcp nginx_name [wwweee000@localhost ~]$ ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 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 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:f2:3d:bf brd ff:ff:ff:ff:ff:ff inet 192.168.1.3/24 brd 192.168.1.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet6 fe80::9a8:d0da:266f:5ac7/64 scope link noprefixroute valid_lft forever preferred_lft forever 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:32:18:de:d4 brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:32ff:fe18:ded4/64 scope link valid_lft forever preferred_lft forever 27: vethb5b02c6@if26: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default link/ether 2e:82:6a:01:a6:a5 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet6 fe80::2c82:6aff:fe01:a6a5/64 scope link valid_lft forever preferred_lft forever 29: veth13521f0@if28: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default link/ether 22:d1:38:0a:0f:3d brd ff:ff:ff:ff:ff:ff link-netnsid 1 inet6 fe80::20d1:38ff:fe0a:f3d/64 scope link valid_lft forever preferred_lft forever 16.普通用戶也可以停止root用戶docker容器 [wwweee000@localhost ~]$ docker stop nginx_name nginx_name [wwweee000@localhost ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 79f9741f257a nginx "nginx -g 'daemon of…" 4 minutes ago Up 4 minutes 0.0.0.0:8082->80/tcp nginx_name_wwweee000 [wwweee000@localhost ~]$ docker restart nginx_name nginx_name [wwweee000@localhost ~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 79f9741f257a nginx "nginx -g 'daemon of…" 12 minutes ago Up 12 minutes 0.0.0.0:8082->80/tcp nginx_name_wwweee000 ae4026623f4a nginx "nginx -g 'daemon of…" 16 minutes ago Up 12 seconds 0.0.0.0:8081->80/tcp nginx_name docker ps // 查看所有正在運行容器 docker stop containerId // containerId 是容器的ID docker ps -a // 查看所有容器 docker ps -a -q // 查看所有容器ID docker stop $(docker ps -a -q) // stop停止所有容器 docker rm $(docker ps -a -q) // remove刪除所有容器 docker kill 容器ID或容器名 //直接關閉容器 17.設置用戶管理root用戶組的容器進行操作(參考) https://www.jb51.net/article/149430.htm