参考链接:https://blog.csdn.net/laughing1997/article/details/84305615

docker的官方安装教程:https://docs.docker.com/engine/install/centos/

1.1.1安装Docker

1.1.1.1第一步:安装工具包

  1. $ sudo yum install -y yum-utils #安装工具包,缺少这些依赖将无法完成

执行结果:

  1. Loaded plugins: fastestmirror, langpacks
  2. base | 3.6 kB 00:00:00
  3. epel | 4.3 kB 00:00:00
  4. extras | 3.4 kB 00:00:00
  5. update | 3.4 kB 00:00:00
  6. (1/3): epel/7/x86_64/updateinfo | 797 kB 00:00:00
  7. (2/3): epel/7/x86_64/primary_db | 4.7 MB 00:00:00
  8. (3/3): update/7/x86_64/primary_db | 4.8 MB 00:00:00
  9. Loading mirror speeds from cached hostfile
  10. Package yum-utils-1.1.31-40.el7.noarch already installed and latest version
  11. Nothing to do

1.1.1.2第二步:设置远程仓库

  1. $sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
  2. 执行结果:
  3. Loaded plugins: fastestmirror, langpacks
  4. adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
  5. grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
  6. repo saved to /etc/yum.repos.d/docker-ce.repo

1.1.1.3第三步:安装

  1. $ sudo yum install docker-ce
  2. 执行结果:
  3. Loaded plugins: fastestmirror, langpacks
  4. docker-ce-stable | 2.9 kB 00:00:00
  5. docker-ce-stable/x86_64/primary_db | 4.8 kB 00:00:00
  6. Loading mirror speeds from cached hostfile
  7. Resolving Dependencies
  8. --> Running transaction check
  9. ---> Package docker-ce.x86_64 0:17.03.1.ce-1.el7.centos will be installed
  10. --> Processing Dependency: docker-ce-selinux >= 17.03.1.ce-1.el7.centos for package: docker-ce-17.03.1.ce-1.el7.centos.x86_64
  11. --> Processing Dependency: libcgroup for package: docker-ce-17.03.1.ce-1.el7.centos.x86_64
  12. --> Processing Dependency: libseccomp.so.2()(64bit) for package: docker-ce-17.03.1.ce-1.el7.centos.x86_64
  13. --> Running transaction check
  14. ---> Package docker-ce-selinux.noarch 0:17.03.1.ce-1.el7.centos will be installed
  15. --> Processing Dependency: policycoreutils-python for package: docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch
  16. ---> Package libcgroup.x86_64 0:0.41-11.el7 will be installed
  17. ---> Package libseccomp.x86_64 0:2.3.1-2.el7 will be installed
  18. --> Running transaction check
  19. ---> Package policycoreutils-python.x86_64 0:2.5-11.el7_3 will be installed
  20. --> Processing Dependency: setools-libs >= 3.3.8-1 for package: policycoreutils-python-2.5-11.el7_3.x86_64
  21. --> Processing Dependency: libsemanage-python >= 2.5-5 for package: policycoreutils-python-2.5-11.el7_3.x86_64
  22. --> Processing Dependency: audit-libs-python >= 2.1.3-4 for package: policycoreutils-python-2.5-11.el7_3.x86_64
  23. --> Processing Dependency: python-IPy for package: policycoreutils-python-2.5-11.el7_3.x86_64
  24. --> Processing Dependency: libqpol.so.1(VERS_1.4)(64bit) for package: policycoreutils-python-2.5-11.el7_3.x86_64
  25. --> Processing Dependency: libqpol.so.1(VERS_1.2)(64bit) for package: policycoreutils-python-2.5-11.el7_3.x86_64
  26. --> Processing Dependency: libapol.so.4(VERS_4.0)(64bit) for package: policycoreutils-python-2.5-11.el7_3.x86_64
  27. --> Processing Dependency: checkpolicy for package: policycoreutils-python-2.5-11.el7_3.x86_64
  28. --> Processing Dependency: libqpol.so.1()(64bit) for package: policycoreutils-python-2.5-11.el7_3.x86_64
  29. --> Processing Dependency: libapol.so.4()(64bit) for package: policycoreutils-python-2.5-11.el7_3.x86_64
  30. --> Running transaction check
  31. ---> Package audit-libs-python.x86_64 0:2.6.5-3.el7_3.1 will be installed
  32. ---> Package checkpolicy.x86_64 0:2.5-4.el7 will be installed
  33. ---> Package libsemanage-python.x86_64 0:2.5-5.1.el7_3 will be installed
  34. ---> Package python-IPy.noarch 0:0.75-6.el7 will be installed
  35. ---> Package setools-libs.x86_64 0:3.3.8-1.1.el7 will be installed
  36. --> Finished Dependency Resolution
  37. Dependencies Resolved
  38. =====================================================================================================================
  39. Package Arch Version Repository Size
  40. =====================================================================================================================
  41. Installing:
  42. docker-ce x86_64 17.03.1.ce-1.el7.centos docker-ce-stable 19 M
  43. Installing for dependencies:
  44. audit-libs-python x86_64 2.6.5-3.el7_3.1 update 70 k
  45. checkpolicy x86_64 2.5-4.el7 base 290 k
  46. docker-ce-selinux noarch 17.03.1.ce-1.el7.centos docker-ce-stable 28 k
  47. libcgroup x86_64 0.41-11.el7 base 65 k
  48. libseccomp x86_64 2.3.1-2.el7 base 56 k
  49. libsemanage-python x86_64 2.5-5.1.el7_3 update 104 k
  50. policycoreutils-python x86_64 2.5-11.el7_3 update 445 k
  51. python-IPy noarch 0.75-6.el7 base 32 k
  52. setools-libs x86_64 3.3.8-1.1.el7 base 612 k
  53. Transaction Summary
  54. =====================================================================================================================
  55. Install 1 Package (+9 Dependent packages)
  56. Total download size: 20 M
  57. Installed size: 24 M
  58. Is this ok [y/d/N]: y
  59. Downloading packages:
  60. (1/10): audit-libs-python-2.6.5-3.el7_3.1.x86_64.rpm | 70 kB 00:00:00
  61. (2/10): checkpolicy-2.5-4.el7.x86_64.rpm | 290 kB 00:00:00
  62. (3/10): libseccomp-2.3.1-2.el7.x86_64.rpm | 56 kB 00:00:00
  63. (4/10): libcgroup-0.41-11.el7.x86_64.rpm | 65 kB 00:00:00
  64. (5/10): policycoreutils-python-2.5-11.el7_3.x86_64.rpm | 445 kB 00:00:00
  65. (6/10): setools-libs-3.3.8-1.1.el7.x86_64.rpm | 612 kB 00:00:00
  66. (7/10): libsemanage-python-2.5-5.1.el7_3.x86_64.rpm | 104 kB 00:00:00
  67. (8/10): python-IPy-0.75-6.el7.noarch.rpm | 32 kB 00:00:00
  68. warning: /var/cache/yum/x86_64/7/docker-ce-stable/packages/docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch.rpm: Header V4 RSA/SHA512 Signature, key ID 621e9f35: NOKEY
  69. Public key for docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch.rpm is not installed
  70. (9/10): docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch.rpm | 28 kB 00:00:00
  71. (10/10): docker-ce-17.03.1.ce-1.el7.centos.x86_64.rpm | 19 MB 00:00:00
  72. ---------------------------------------------------------------------------------------------------------------------
  73. Total 23 MB/s | 20 MB 00:00:00
  74. Retrieving key from https://download.docker.com/linux/centos/gpg
  75. Importing GPG key 0x621E9F35:
  76. Userid : "Docker Release (CE rpm) <docker@docker.com>"
  77. Fingerprint: 060a 61c5 1b55 8a7f 742b 77aa c52f eb6b 621e 9f35
  78. From : https://download.docker.com/linux/centos/gpg
  79. Is this ok [y/N]: y
  80. Running transaction check
  81. Running transaction test
  82. Transaction test succeeded
  83. Running transaction
  84. Installing : libcgroup-0.41-11.el7.x86_64 1/10
  85. Installing : setools-libs-3.3.8-1.1.el7.x86_64 2/10
  86. Installing : checkpolicy-2.5-4.el7.x86_64 3/10
  87. Installing : libsemanage-python-2.5-5.1.el7_3.x86_64 4/10
  88. Installing : audit-libs-python-2.6.5-3.el7_3.1.x86_64 5/10
  89. Installing : python-IPy-0.75-6.el7.noarch 6/10
  90. Installing : policycoreutils-python-2.5-11.el7_3.x86_64 7/10
  91. Installing : docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch 8/10
  92. setsebool: SELinux is disabled.
  93. libsemanage.semanage_direct_install_info: Overriding docker module at lower priority 100 with module at priority 400.
  94. Installing : libseccomp-2.3.1-2.el7.x86_64 9/10
  95. Installing : docker-ce-17.03.1.ce-1.el7.centos.x86_64 10/10
  96. Verifying : libseccomp-2.3.1-2.el7.x86_64 1/10
  97. Verifying : python-IPy-0.75-6.el7.noarch 2/10
  98. Verifying : audit-libs-python-2.6.5-3.el7_3.1.x86_64 3/10
  99. Verifying : libsemanage-python-2.5-5.1.el7_3.x86_64 4/10
  100. Verifying : docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch 5/10
  101. Verifying : libcgroup-0.41-11.el7.x86_64 6/10
  102. Verifying : policycoreutils-python-2.5-11.el7_3.x86_64 7/10
  103. Verifying : docker-ce-17.03.1.ce-1.el7.centos.x86_64 8/10
  104. Verifying : checkpolicy-2.5-4.el7.x86_64 9/10
  105. Verifying : setools-libs-3.3.8-1.1.el7.x86_64 10/10
  106. Installed:
  107. docker-ce.x86_64 0:17.03.1.ce-1.el7.centos
  108. Dependency Installed:
  109. audit-libs-python.x86_64 0:2.6.5-3.el7_3.1 checkpolicy.x86_64 0:2.5-4.el7
  110. docker-ce-selinux.noarch 0:17.03.1.ce-1.el7.centos libcgroup.x86_64 0:0.41-11.el7
  111. libseccomp.x86_64 0:2.3.1-2.el7 libsemanage-python.x86_64 0:2.5-5.1.el7_3
  112. policycoreutils-python.x86_64 0:2.5-11.el7_3 python-IPy.noarch 0:0.75-6.el7
  113. setools-libs.x86_64 0:3.3.8-1.1.el7
  114. Complete!

报错:

  1. 错误:docker-ce conflicts with 2:docker-1.13.1-161.git64e9980.el7_8.x86_64
  2. 错误:docker-ce-cli conflicts with 2:docker-1.13.1-161.git64e9980.el7_8.x86_64

原因:安装了旧版本的docker导致版本冲突,需把原来的docker删除
解决:

  1. sudo yum -y remove docker

报错:

  1. Transaction check error:
  2. file /usr/bin/docker from install of docker-ce-cli-1:19.03.8-3.el7.x86_64 conflicts with file from package docker-common-2:1.13.1-161.git64e9980.el7_8.x86_64
  3. file /usr/bin/dockerd from install of docker-ce-3:19.03.8-3.el7.x86_64 conflicts with file from package docker-common-2:1.13.1-161.git64e9980.el7_8.x86_64

原因:之前安装了旧版本的docker,需删除旧版本的包
解决:

  1. sudo yum erase docker-common-2:1.13.1-161.git64e9980.el7_8.x86_64

1.1.1.4第四步:启动

  1. $ sudo systemctl start docker
  2. 或者
  3. $ sudo service docker start
  4. service docker start #启动docker
  5. chkconfig docker on #加入开机启动

第五步:查看版本

  1. $ sudo docker version
  2. 执行结果:
  3. Client:
  4. Version: 17.03.1-ce
  5. API version: 1.27
  6. Go version: go1.7.5
  7. Git commit: c6d412e
  8. Built: Mon Mar 27 17:05:44 2017
  9. OS/Arch: linux/amd64

1.1.1.6第六步:校验

  1. $ sudo docker run hello-world

执行结果: (注意一下,结果没那么快出来)

  1. Unable to find image 'hello-world:latest' locally
  2. latest: Pulling from library/hello-world
  3. 78445dd45222: Pull complete
  4. Digest: sha256:c5515758d4c5e1e838e9cd307f6c6a0d620b5e07e6f927b07d05f6d12a1ac8d7
  5. Status: Downloaded newer image for hello-world:latest
  6. Hello from Docker!
  7. This message shows that your installation appears to be working correctly.
  8. To generate this message, Docker took the following steps:
  9. 1. The Docker client contacted the Docker daemon.
  10. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
  11. 3. The Docker daemon created a new container from that image which runs the
  12. executable that produces the output you are currently reading.
  13. 4. The Docker daemon streamed that output to the Docker client, which sent it
  14. to your terminal.
  15. To try something more ambitious, you can run an Ubuntu container with:
  16. $ docker run -it ubuntu bash
  17. Share images, automate workflows, and more with a free Docker ID:
  18. https://cloud.docker.com/
  19. For more examples and ideas, visit:
  20. https://docs.docker.com/engine/userguide/

1.1.2查看Docker版本
docker —help #帮助
docker –v #简单查看版本
docker version #查看版本

  1. Client version: 1.7.1
  2. Client API version: 1.19
  3. Go version (client): go1.4.2
  4. Git commit (client): 786b29d/1.7.1
  5. OS/Arch (client): linux/amd64
  6. Server version: 1.7.1
  7. Server API version: 1.19
  8. Go version (server): go1.4.2
  9. Git commit (server): 786b29d/1.7.1
  10. OS/Arch (server): linux/amd64

docker info #查看信息

  1. Containers: 0
  2. Images: 2
  3. Storage Driver: devicemapper
  4. Pool Name: docker-253:0-34097258-pool
  5. Pool Blocksize: 65.54 kB
  6. Backing Filesystem: xfs
  7. Data file: /dev/loop0
  8. Metadata file: /dev/loop1
  9. Data Space Used: 308.3 MB
  10. Data Space Total: 107.4 GB
  11. Data Space Available: 16.08 GB
  12. Metadata Space Used: 782.3 kB
  13. Metadata Space Total: 2.147 GB
  14. Metadata Space Available: 2.147 GB
  15. Udev Sync Supported: true
  16. Deferred Removal Enabled: false
  17. Data loop file: /var/lib/docker/devicemapper/devicemapper/data
  18. Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
  19. Library Version: 1.02.107-RHEL7 (2015-10-14)
  20. Execution Driver: native-0.2
  21. Logging Driver: json-file
  22. Kernel Version: 3.10.0-327.el7.x86_64
  23. Operating System: CentOS Linux 7 (Core)
  24. CPUs: 4
  25. Total Memory: 985.6 MiB
  26. Name: localhost.localdomain
  27. ID: B5ZW:FJXF:FNUD:OLH7:FCNI:56DJ:XEQY:I6J4:PPHQ:OKRW:CIJK:Y26P

1.1.3安装其它产品的两种方式
docker安装其它产品有两种方式
方式一:通过拉取事先做好的镜像,例如下面redis的安装方式
方式二:通过Dockerfile来构建新的镜像,例如下面tomcat的安装方式
1.1.4组成结构
docker安装教程 - 图1docker会自动给docker容器配置一个vip虚拟ip地址
bash-4.1#提示符就代表进入docker容器内部