一、背景
伴随着信息技术的快速发展,微服务/容器化思想流行,技术框架更新换代较快。架构方面最火的莫过于SpringCloud(虽然很多人也不知道为啥用SpringCloud,就是觉得好用),而部署方面则是Docker(虽然先进,但是用的不多,目前最多的还是tomcat部署和jar包部署)。自己有必要在这方面有所研究,但是呢,翻遍大小书籍和博客,大部分都是Ctrl+C、Ctrl+V,一点儿实战都没有,而自己又是内网环境,无法连接外网,而教程又是需要连接外网的,因此尝试了很久,终于找到了相关的博文并实战,记录如下。
二、环境(内网)
操作系统:CentOS7.3
docker版本:docker-ce-18.06.1.ce-3.el7.x86_64
docker安装包:docker-ce-18.06.1.ce-3.el7.x86_64.rpm
版本大于2.9的依赖包:container-selinux-2.9-4.el7.noarch.rpm
[root@docker-app01 ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
三、本地源配置
1、在/opt目录下新建docker文件夹。 备注:这个目录可以自定义
2、将rpm拷贝到/opt/docker目录内 备注:使用rz 命令
3、安装createrepo工具
[root@docker-app01 ~]# cd /opt/
[root@docker-app01 opt]# mkdir docker
[root@docker-app01 opt]# cd docker/
[root@docker-app01 docker]# ll
total 0
[root@docker-app01 docker]# rz
rz waiting to receive.
开始 zmodem 传输。 按 Ctrl+C 取消。
100% 25 KB 25 KB/s 00:00:01 0 Errors...
100% 41711 KB 182 KB/s 00:03:48 0 Errors..
[root@docker-app01 docker]# ls
container-selinux-2.9-4.el7.noarch.rpm docker-ce-18.06.1.ce-3.el7.x86_64.rpm
[root@docker-app01 docker]# yum -y install createrepo
4、在/opt/docker目录内执行createrepo
[root@docker-app01 ~]# cd /opt/docker/
[root@docker-app01 docker]# ls
container-selinux-2.9-4.el7.noarch.rpm docker-ce-18.06.1.ce-3.el7.x86_64.rpm
[root@docker-app01 docker]# ll
total 41740
-rw-r--r-- 1 root root 25976 Sep 26 2019 container-selinux-2.9-4.el7.noarch.rpm
-rw-r--r-- 1 root root 42712724 Sep 25 2019 docker-ce-18.06.1.ce-3.el7.x86_64.rpm
[root@docker-app01 docker]# createrepo ./
Spawning worker 0 with 1 pkgs
Spawning worker 1 with 1 pkgs
Spawning worker 2 with 0 pkgs
Spawning worker 3 with 0 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
5、在/etc/yum.repo.d目录内新建docker.repo,并录入内容。:wq! 保存并强制退出
[root@docker-app01 ~]# cd /etc/yum.repos.d/
[root@docker-app01 yum.repos.d]# vi docker.repo
[docker]
name=docker
baseurl=file:///opt/docker/
gpgcheck=0
enabled=1
"docker.repo" [New] 6L, 71C written
[root@docker-app01 yum.repos.d]# cat docker.repo
四、 安装docker
1、安装Docker
2、启动Docker服务
[root@docker-app01 yum.repos.d]# yum -y install docker-ce
Loaded plugins: fastestmirror, langpacks
docker | 2.9 kB 00:00:00
docker/primary_db | 3.7 kB 00:00:00
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package docker-ce.x86_64 0:18.06.1.ce-3.el7 will be installed
--> Processing Dependency: libseccomp >= 2.3 for package: docker-ce-18.06.1.ce-3.el7.x86_64
--> 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: libseccomp.so.2()(64bit) 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.9-4.el7 will be installed
--> Processing Dependency: policycoreutils-python for package: 2:container-selinux-2.9-4.el7.noarch
---> Package libcgroup.x86_64 0:0.41-11.el7 will be installed
---> Package libseccomp.x86_64 0:2.3.1-2.el7 will be installed
---> Package libtool-ltdl.x86_64 0:2.4.2-21.el7_2 will be installed
--> Running transaction check
---> Package policycoreutils-python.x86_64 0:2.5-8.el7 will be installed
--> Processing Dependency: setools-libs >= 3.3.8-1 for package: policycoreutils-python-2.5-8.el7.x86_64
--> Processing Dependency: libsemanage-python >= 2.5-4 for package: policycoreutils-python-2.5-8.el7.x86_64
--> Processing Dependency: audit-libs-python >= 2.1.3-4 for package: policycoreutils-python-2.5-8.el7.x86_64
--> Processing Dependency: python-IPy for package: policycoreutils-python-2.5-8.el7.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.4)(64bit) for package: policycoreutils-python-2.5-8.el7.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.2)(64bit) for package: policycoreutils-python-2.5-8.el7.x86_64
--> Processing Dependency: libapol.so.4(VERS_4.0)(64bit) for package: policycoreutils-python-2.5-8.el7.x86_64
--> Processing Dependency: checkpolicy for package: policycoreutils-python-2.5-8.el7.x86_64
--> Processing Dependency: libqpol.so.1()(64bit) for package: policycoreutils-python-2.5-8.el7.x86_64
--> Processing Dependency: libapol.so.4()(64bit) for package: policycoreutils-python-2.5-8.el7.x86_64
--> Running transaction check
---> Package audit-libs-python.x86_64 0:2.6.5-3.el7 will be installed
---> Package checkpolicy.x86_64 0:2.5-4.el7 will be installed
---> Package libsemanage-python.x86_64 0:2.5-4.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-1.1.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 41 M
Installing for dependencies:
audit-libs-python x86_64 2.6.5-3.el7 base 70 k
checkpolicy x86_64 2.5-4.el7 base 290 k
container-selinux noarch 2:2.9-4.el7 docker 25 k
libcgroup x86_64 0.41-11.el7 base 65 k
libseccomp x86_64 2.3.1-2.el7 base 56 k
libsemanage-python x86_64 2.5-4.el7 base 103 k
libtool-ltdl x86_64 2.4.2-21.el7_2 base 49 k
policycoreutils-python x86_64 2.5-8.el7 base 444 k
python-IPy noarch 0.75-6.el7 base 32 k
setools-libs x86_64 3.3.8-1.1.el7 base 612 k
Transaction Summary
===============================================================================================
Install 1 Package (+10 Dependent packages)
Total download size: 42 M
Installed size: 174 M
Downloading packages:
(1/9): audit-libs-python-2.6.5-3.el7.x86_64.rpm | 70 kB 00:00:00
(2/9): libcgroup-0.41-11.el7.x86_64.rpm | 65 kB 00:00:00
(3/9): libseccomp-2.3.1-2.el7.x86_64.rpm | 56 kB 00:00:00
(4/9): checkpolicy-2.5-4.el7.x86_64.rpm | 290 kB 00:00:00
(5/9): libsemanage-python-2.5-4.el7.x86_64.rpm | 103 kB 00:00:00
(6/9): libtool-ltdl-2.4.2-21.el7_2.x86_64.rpm | 49 kB 00:00:00
(7/9): python-IPy-0.75-6.el7.noarch.rpm | 32 kB 00:00:00
(8/9): setools-libs-3.3.8-1.1.el7.x86_64.rpm | 612 kB 00:00:00
(9/9): policycoreutils-python-2.5-8.el7.x86_64.rpm | 444 kB 00:00:00
-----------------------------------------------------------------------------------------------
Total 126 MB/s | 42 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libcgroup-0.41-11.el7.x86_64 1/11
Installing : setools-libs-3.3.8-1.1.el7.x86_64 2/11
Installing : libsemanage-python-2.5-4.el7.x86_64 3/11
Installing : checkpolicy-2.5-4.el7.x86_64 4/11
Installing : python-IPy-0.75-6.el7.noarch 5/11
Installing : audit-libs-python-2.6.5-3.el7.x86_64 6/11
Installing : policycoreutils-python-2.5-8.el7.x86_64 7/11
Installing : 2:container-selinux-2.9-4.el7.noarch 8/11
setsebool: SELinux is disabled.
Installing : libtool-ltdl-2.4.2-21.el7_2.x86_64 9/11
Installing : libseccomp-2.3.1-2.el7.x86_64 10/11
Installing : docker-ce-18.06.1.ce-3.el7.x86_64 11/11
Verifying : 2:container-selinux-2.9-4.el7.noarch 1/11
Verifying : libseccomp-2.3.1-2.el7.x86_64 2/11
Verifying : libtool-ltdl-2.4.2-21.el7_2.x86_64 3/11
Verifying : docker-ce-18.06.1.ce-3.el7.x86_64 4/11
Verifying : policycoreutils-python-2.5-8.el7.x86_64 5/11
Verifying : audit-libs-python-2.6.5-3.el7.x86_64 6/11
Verifying : python-IPy-0.75-6.el7.noarch 7/11
Verifying : libcgroup-0.41-11.el7.x86_64 8/11
Verifying : checkpolicy-2.5-4.el7.x86_64 9/11
Verifying : libsemanage-python-2.5-4.el7.x86_64 10/11
Verifying : setools-libs-3.3.8-1.1.el7.x86_64 11/11
Installed:
docker-ce.x86_64 0:18.06.1.ce-3.el7
Dependency Installed:
audit-libs-python.x86_64 0:2.6.5-3.el7 checkpolicy.x86_64 0:2.5-4.el7
container-selinux.noarch 2:2.9-4.el7 libcgroup.x86_64 0:0.41-11.el7
libseccomp.x86_64 0:2.3.1-2.el7 libsemanage-python.x86_64 0:2.5-4.el7
libtool-ltdl.x86_64 0:2.4.2-21.el7_2 policycoreutils-python.x86_64 0:2.5-8.el7
python-IPy.noarch 0:0.75-6.el7 setools-libs.x86_64 0:3.3.8-1.1.el7
Complete!
[root@docker-app01 yum.repos.d]# systemctl start docker
[root@docker-app01 yum.repos.d]# systemctl status docker
3、检查服务,并设置开机启动
使用docker version检查服务是否安装正常。
[root@docker-app01 yum.repos.d]# docker version
Client:
Version: 18.06.1-ce
API version: 1.38
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:23:03 2018
OS/Arch: linux/amd64
Experimental: false
Server:
Engine:
Version: 18.06.1-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: e68fc7a
Built: Tue Aug 21 17:25:29 2018
OS/Arch: linux/amd64
Experimental: false
[root@docker-app01 yum.repos.d]# systemctl enable docker
五、简单示例
[root@docker-app01 ~]# cd /opt/
[root@docker-app01 opt]# ls
docker rh
[root@docker-app01 opt]# mkdir images
[root@docker-app01 opt]# ls
docker images rh
[root@docker-app01 opt]# cd images/
[root@docker-app01 images]# rz
rz waiting to receive.
开始 zmodem 传输。 按 Ctrl+C 取消。
100% 12 KB 12 KB/s 00:00:01 0 Errors
[root@docker-app01 images]# ls
hello-world.tar
[root@docker-app01 images]# pwd
/opt/images
[root@docker-app01 images]# ll
total 16
-rw-r--r-- 1 root root 12800 Sep 26 2019 hello-world.tar
[root@docker-app01 images]# docker load < /opt/images/hello-world.tar
af0b15c8625b: Loading layer 3.584kB/3.584kB
Loaded image: hello-world:latest
[root@docker-app01 images]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest fce289e99eb9 15 months ago 1.84kB
[root@docker-app01 images]# 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/
[root@docker-app01 images]#
参考文献:
1、书籍:《Docker技术入门与实战 (第3版)》杨保华、戴玉剑著
2、博文:https://blog.csdn.net/zz_aiytag/article/details/101905750
3、官方网站: https://www.docker.com/
4、Docker源下载地址: http://www.rpmfind.net/linux/rpm2html/search.php?query=docker(x86-64)
5、安装包下载:https://download.csdn.net/download/zz_aiytag/11831396