docker玩過不少次,但每次都是從零開始,因此此次而打算記錄下來,方便後面查詢。
docker的技術基礎是LXC虛擬化技術,虛擬化技術有不少,有硬件層面和軟件層面的,我在
2018年第13周-虛擬化技術理解大概有描述。其中LXC就是軟件層面的虛擬化技術。其內存利用率很高,這是由於全部虛擬機(在docker裏叫Container)都共享宿主的操做系統實例,,不存在因爲運行多個操做系統實例所形成的性能損耗。下圖是docker與其餘虛擬技術的對比圖。
Docker的目標是「Build, Ship and Run Any App, Anywhere」,基於這個目標,將你須要發佈的應用組件及須要的環境配置都封裝成一個image(鏡像),而後基於鏡像建立Container(容器)並啓動運行。這樣就能夠達到組建級別的一次封裝,屢次運行。是否是跟JVM很像,一次編譯,多處運行。python
歷史本版的Docker可能被叫爲docker或docker-engine. 若是它們有安裝 ,卸載它以及它們的依賴. 命令以下:linux
$ sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-selinux \ docker-engine-selinux \ docker-engine
/var/lib/docker/目錄裏的內容將被保留, 以及images, containers, volumes, 和networks將會被保留.
Docker CE包如今叫 docker-cedocker
你能夠從不少地方安裝Docker CE, 這取決於你的需求:json
在安裝Docker CE以前, 你須要設置Docker的yum倉庫. 而後在安裝.segmentfault
$ sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2
我centos7系統安裝時選擇的SOFTWARE SELECTION是Infrastuctue Server (Server for operating network infrastuctue Server)centos
[root@amd usb]# sudo yum install -y yum-utils \ > device-mapper-persistent-data \ > lvm2 Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirror.lzu.edu.cn * extras: mirrors.zju.edu.cn * updates: mirrors.zju.edu.cn Package yum-utils-1.1.31-46.el7_5.noarch already installed and latest version Package device-mapper-persistent-data-0.7.3-3.el7.x86_64 already installed and latest version Package 7:lvm2-2.02.177-4.el7.x86_64 already installed and latest version Nothing to do
sudo yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo
[root@amd usb]# sudo yum-config-manager \ > --add-repo \ > https://download.docker.com/linux/centos/docker-ce.repo Loaded plugins: fastestmirror, langpacks adding repo from: https://download.docker.com/linux/centos/docker-ce.repo grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo repo saved to /etc/yum.repos.d/docker-ce.repo
$ sudo yum-config-manager --enable docker-ce-edge $ sudo yum-config-manager --enable docker-ce-test
經過yum-config-manager命令關閉edge和test倉庫.bash
sudo yum-config-manager --disable docker-ce-edge
sudo yum install docker-ce
若是須要確認GPG key, 確認fingerprint是否匹配060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35, 匹配上了則確認服務器
多個docker庫的狀況下
若是你有多個docker庫啓用了, 並且安裝或升級時不指定版本, 那麼yum install或yum update命令則總會安裝儘量高的版本, 這也許不符合你穩定的要求.
到此位置, docker已經安裝, 但還未啓動. Linux的docker group也建立了, 但沒有users在這組裏.
安裝過程:
[root@amd ~]# sudo yum install docker-ce Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirror.lzu.edu.cn * extras: mirrors.zju.edu.cn * updates: mirrors.zju.edu.cn base | 3.6 kB 00:00:00 docker-ce-stable | 2.9 kB 00:00:00 epel | 3.2 kB 00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/2): epel/x86_64/updateinfo | 935 kB 00:00:05 (2/2): epel/x86_64/primary | 3.6 MB 00:00:14 epel 12742/12742 Resolving Dependencies --> Running transaction check ---> Package docker-ce.x86_64 0:18.06.1.ce-3.el7 will be installed --> Processing Dependency: container-selinux >= 2.9 for package: docker-ce-18.06.1.ce-3.el7.x86_64 --> Processing Dependency: libcgroup for package: docker-ce-18.06.1.ce-3.el7.x86_64 --> Processing Dependency: libltdl.so.7()(64bit) for package: docker-ce-18.06.1.ce-3.el7.x86_64 --> Running transaction check ---> Package container-selinux.noarch 2:2.68-1.el7 will be installed --> Processing Dependency: policycoreutils-python for package: 2:container-selinux-2.68-1.el7.noarch ---> Package libcgroup.x86_64 0:0.41-15.el7 will be installed ---> Package libtool-ltdl.x86_64 0:2.4.2-22.el7_3 will be installed --> Running transaction check ---> Package policycoreutils-python.x86_64 0:2.5-22.el7 will be installed --> Processing Dependency: setools-libs >= 3.3.8-2 for package: policycoreutils-python-2.5-22.el7.x86_64 --> Processing Dependency: libsemanage-python >= 2.5-9 for package: policycoreutils-python-2.5-22.el7.x86_64 --> Processing Dependency: audit-libs-python >= 2.1.3-4 for package: policycoreutils-python-2.5-22.el7.x86_64 --> Processing Dependency: python-IPy for package: policycoreutils-python-2.5-22.el7.x86_64 --> Processing Dependency: libqpol.so.1(VERS_1.4)(64bit) for package: policycoreutils-python-2.5-22.el7.x86_64 --> Processing Dependency: libqpol.so.1(VERS_1.2)(64bit) for package: policycoreutils-python-2.5-22.el7.x86_64 --> Processing Dependency: libapol.so.4(VERS_4.0)(64bit) for package: policycoreutils-python-2.5-22.el7.x86_64 --> Processing Dependency: checkpolicy for package: policycoreutils-python-2.5-22.el7.x86_64 --> Processing Dependency: libqpol.so.1()(64bit) for package: policycoreutils-python-2.5-22.el7.x86_64 --> Processing Dependency: libapol.so.4()(64bit) for package: policycoreutils-python-2.5-22.el7.x86_64 --> Running transaction check ---> Package audit-libs-python.x86_64 0:2.8.1-3.el7_5.1 will be installed ---> Package checkpolicy.x86_64 0:2.5-6.el7 will be installed ---> Package libsemanage-python.x86_64 0:2.5-11.el7 will be installed ---> Package python-IPy.noarch 0:0.75-6.el7 will be installed ---> Package setools-libs.x86_64 0:3.3.8-2.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ===================================================================================================================================================================================================================== Package Arch Version Repository Size ===================================================================================================================================================================================================================== Installing: docker-ce x86_64 18.06.1.ce-3.el7 docker-ce-stable 41 M Installing for dependencies: audit-libs-python x86_64 2.8.1-3.el7_5.1 updates 75 k checkpolicy x86_64 2.5-6.el7 base 294 k container-selinux noarch 2:2.68-1.el7 extras 36 k libcgroup x86_64 0.41-15.el7 base 65 k libsemanage-python x86_64 2.5-11.el7 base 112 k libtool-ltdl x86_64 2.4.2-22.el7_3 base 49 k policycoreutils-python x86_64 2.5-22.el7 base 454 k python-IPy noarch 0.75-6.el7 base 32 k setools-libs x86_64 3.3.8-2.el7 base 619 k Transaction Summary ===================================================================================================================================================================================================================== Install 1 Package (+9 Dependent packages) Total size: 42 M Installed size: 46 M Is this ok [y/d/N]: y Downloading packages: warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-18.06.1.ce-3.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY Retrieving key from https://download.docker.com/linux/centos/gpg Importing GPG key 0x621E9F35: Userid : "Docker Release (CE rpm) <docker@docker.com>" Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35 From : https://download.docker.com/linux/centos/gpg Is this ok [y/N]: y Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : libcgroup-0.41-15.el7.x86_64 1/10 Installing : audit-libs-python-2.8.1-3.el7_5.1.x86_64 2/10 Installing : setools-libs-3.3.8-2.el7.x86_64 3/10 Installing : libtool-ltdl-2.4.2-22.el7_3.x86_64 4/10 Installing : python-IPy-0.75-6.el7.noarch 5/10 Installing : checkpolicy-2.5-6.el7.x86_64 6/10 Installing : libsemanage-python-2.5-11.el7.x86_64 7/10 Installing : policycoreutils-python-2.5-22.el7.x86_64 8/10 Installing : 2:container-selinux-2.68-1.el7.noarch 9/10 setsebool: SELinux is disabled. Installing : docker-ce-18.06.1.ce-3.el7.x86_64 10/10 Verifying : libcgroup-0.41-15.el7.x86_64 1/10 Verifying : docker-ce-18.06.1.ce-3.el7.x86_64 2/10 Verifying : policycoreutils-python-2.5-22.el7.x86_64 3/10 Verifying : libsemanage-python-2.5-11.el7.x86_64 4/10 Verifying : 2:container-selinux-2.68-1.el7.noarch 5/10 Verifying : checkpolicy-2.5-6.el7.x86_64 6/10 Verifying : python-IPy-0.75-6.el7.noarch 7/10 Verifying : libtool-ltdl-2.4.2-22.el7_3.x86_64 8/10 Verifying : setools-libs-3.3.8-2.el7.x86_64 9/10 Verifying : audit-libs-python-2.8.1-3.el7_5.1.x86_64 10/10 Installed: docker-ce.x86_64 0:18.06.1.ce-3.el7 Dependency Installed: audit-libs-python.x86_64 0:2.8.1-3.el7_5.1 checkpolicy.x86_64 0:2.5-6.el7 container-selinux.noarch 2:2.68-1.el7 libcgroup.x86_64 0:0.41-15.el7 libsemanage-python.x86_64 0:2.5-11.el7 libtool-ltdl.x86_64 0:2.4.2-22.el7_3 policycoreutils-python.x86_64 0:2.5-22.el7 python-IPy.noarch 0:0.75-6.el7 setools-libs.x86_64 0:3.3.8-2.el7 Complete!
a. 列出並排序有哪些版本在倉庫裏, 如下是例子, 從高級到低級.網絡
[root@amd ~]# yum list docker-ce --showduplicates | sort -r * updates: mirrors.zju.edu.cn Loading mirror speeds from cached hostfile Loaded plugins: fastestmirror, langpacks Installed Packages * extras: mirrors.zju.edu.cn docker-ce.x86_64 18.06.1.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: mirror.lzu.edu.cn Available Packages
這列表內容取決於哪一個倉庫被啓動, 而且指定了你CentOS版本(在這例子裏是.el7)
b. 安裝指定版本, 需指定全限定包名, 也就是包名(docker-ce)加版本字符串(第2列), 以橫槓(-)隔開, 例如: docker-ce-18.03.0.ceapp
sudo yum install docker-ce-<VERSION STRING>
Dokcer
到此位置, docker已經安裝, 但還未啓動. Linux的docker group也建立了, 但沒有users在這組裏.
$ sudo systemctl start docker
sudo docker run hello-world
此命令會下載一個test鏡像, 而後運行在一個容器裏, 當容器運行時, 它會輸出信息, 而後退出.
Docker CE已經安裝而且運行着, 你須要用sudo在運行Docker. 能夠繼續教程Linux postinstall 來運行沒有權限的用戶運行Docker命令.
就在yum中直接選擇新版本安裝便可
docker search centos
docker pull centos
docker images
docker run -ti centos /bin/bash
docker ps -a
docker run -tid --name can_download --net host centos /bin/bash
docker run -tid --name my-net-centos --network my-net centos /bin/bash
docker exec -it my-net-centos /bin/bash
docker cp jdk-8u172-linux-x64.tar.gz f858312b67c20f1ff5303ae8f8333133c8791d434ee30c0e3395ccfb6b345d4f:/root/jdk-8u172-linux-x64.tar.gz
nescafe@nescafe-PC:~$ docker images Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.38/images/json: dial unix /var/run/docker.sock: connect: permission denied
摘自docker mannual上的一段話
Manage Docker as a non-root user
The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The docker daemon always runs as the root user.
If you don’t want to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.
大概的意思就是:docker進程使用Unix Socket而不是TCP端口。而默認狀況下,Unix socket屬於root用戶,須要root權限才能訪問。
使用sudo獲取管理員權限,運行docker命令
docker守護進程啓動的時候,會默認賦予名字爲docker的用戶組讀寫Unix socket的權限,所以只要建立docker用戶組,並將當前用戶加入到docker用戶組中,那麼當前用戶就有權限訪問Unix socket了,進而也就能夠執行docker相關命令
sudo groupadd docker #添加docker用戶組 sudo gpasswd -a $USER docker #將登錄用戶加入到docker用戶組中 newgrp docker #更新用戶組 docker ps #測試docker命令是否可使用sudo正常使用