一.Docker的简单介绍
1.什么是Docker
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口;
由Docker客户端、Docker Daemon守护进程、Docker Image镜像、DockerContainer容器组成。
2.Docker的应用场景
web应用的自动化打包和发布;
自动化测试和持续集成、发布;
在服务型环境中部署和调整数据库或其他的后台应用;
从头编译或者扩展现有的OpenShift或Cloud Foundry平台来搭建自己的PaaS环境
二.Docker环境的部署
1.yum源的配置
因为使用的7.3虚拟机,则需要挂载7.5的镜像用于解决docker依赖性
[root@server1 ~]# cd /etc/yum.repos.d
[root@server1 yum.repos.d]# vim westos.repo
[rhel7.3]
name=rhel7.3
baseurl=http://172.25.4.250/westos
gpgcheck=0
[rhel7.5]
name=rhel7.5
baseurl=http://172.25.4.250/redhat7.5
gpgcheck=0
[root@server1 yum.repos.d]# yum clean all
[root@server1 yum.repos.d]# yum repolis
repo id repo name status
rhel7.3 rhel7.3 4,751
rhel7.5 rhel7.5 5,099
repolist: 9,850
2.docker的安装
[root@server1 ~]# ls ##解决依赖性的包
containerd.io-1.2.5-3.1.el7.x86_64.rpm docker-ce-18.09.6-3.el7.x86_64.rpm
container-selinux-2.21-1.el7.noarch.rpm docker-ce-cli-18.09.6-3.el7.x86_64.rpm
[root@server1 ~]# yum install *.rpm -y ##安装解决依赖性的包
[root@server1 ~]# systemctl start docker ##开启docker
[root@server1 ~]# systemctl enable docker ##设置为开机自启
[root@server1 ~]# yum install -y bash-* ##安装自动补齐包
3.查看docker的信息
(1)docker images 查看docker镜像
[root@server1 ~]# docker images ##此时镜像为空
REPOSITORY TAG IMAGE ID CREATED SIZE
(2)docker info 查看docker宿主机信息
[root@server1 ~]# docker info ##查看docker宿主机信息
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 18.09.6
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc ##为runc则正常
Default Runtime: runc
Init Binary: docker-init
containerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84
runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30
init version: fec3683
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-514.el7.x86_64
Operating System: Red Hat Enterprise Linux Server 7.3 (Maipo)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 992.7MiB
Name: server1
ID: RJEX:A3L4:SXQK:KQFW:NHES:SRGR:NXGZ:LFU4:MVRG:LEXS:ES4A:YF67
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
Product License: Community Engine
(3)sysctl -a |grep iptab 查看系统中iptables的相关信息
[root@server1 ~]# sysctl -a |grep iptab ##查看系统中iptables的相关信息,
net.bridge.bridge-nf-call-iptables = 1
(4)ip addr show 查看docker自动分配一条ip
[root@server1 ~]# ip addr show ##此时docker自动分配一条ip
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
link/ether 02:42:2c:d5:6c:3c 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
(5)docker version 查看docker版本信息
[root@server1 ~]# docker version
Client:
Version: 18.09.6
API version: 1.39
Go version: go1.10.8
Git commit: 481bc77156
Built: Sat May 4 02:34:58 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.6
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: 481bc77
Built: Sat May 4 02:02:43 2019
OS/Arch: linux/amd64
Experimental: false
三.docker简单容器的搭建
以game2048为例
1.准备game2048镜像
[root@server1 ~]# ls
containerd.io-1.2.5-3.1.el7.x86_64.rpm docker-ce-cli-18.09.6-3.el7.x86_64.rpm
container-selinux-2.21-1.el7.noarch.rpm game2048.tar
docker-ce-18.09.6-3.el7.x86_64.rpm
[root@server1 ~]# docker load -i game2048.tar ##将镜像导入
011b303988d2: Loading layer 5.05MB/5.05MB
36e9226e74f8: Loading layer 51.46MB/51.46MB
192e9fad2abc: Loading layer 3.584kB/3.584kB
6d7504772167: Loading layer 4.608kB/4.608kB
88fca8ae768a: Loading layer 629.8kB/629.8kB
Loaded image: game2048:latest
[root@server1 ~]# docker images ##查看镜像,此时导入成功
REPOSITORY TAG IMAGE ID CREATED SIZE
game2048 latest 19299002fdbe 2 years ago 55.5MB
[root@server1 ~]# docker run -d --name vm1 -p 80:80 game2048 ##创建容器
-d:后台
--name:给该容器取的名字
-p:端口映射,客户端访问80端口
77df6f1c7c9af8ab5f18de58656135a6d4f3196da11ade8321c698c40d03e8cc
[root@server1 ~]# docker ps ##查看容器状态
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
77df6f1c7c9a game2048 "/bin/sh -c 'sed -i …" 52 seconds ago Up 50 seconds 0.0.0.0:80->80/tcp, 443/tcp vm1
[root@server1 ~]# docker inspect vm1 ##查看创建的容器状态
2.浏览器访问:172.25.4.111:80