一、环境准备
VMware Workstation Pro 14请在https://my.vmware.com/en/web/vmware/info/slug/desktop_end_user_computing/vmware_workstation_pro/14_0下载。下载后请自行按照“VMware Workstation Pro 14 永久激活”搜索激活序列号。
CentOS 7.5请在http://mirrors.huaweicloud.com/repository/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1804.iso下载。
安装完成后请修改IP地址和hostname,方法请参见https://blog.csdn.net/twingao/article/details/80217938。
# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
# hostname
docker3
二、Docker 1.13安装
更新一下yum。
# yum update
安装Docker
# yum install docker
启动Docker和配置自启动。
# systemctl start docker
# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
查看Docker版本。
# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64
Go version: go1.9.4
Git commit: 94f4240/1.13.1
Built: Fri May 18 15:44:33 2018
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64
Go version: go1.9.4
Git commit: 94f4240/1.13.1
Built: Fri May 18 15:44:33 2018
OS/Arch: linux/amd64
Experimental: false
三、Docker CE安装
安装所需安装包。
# yum install -y yum-utils device-mapper-persistent-data lvm2
安装yum源仓库。
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
安装Docker CE。
# yum install docker-ce
启动Docker,并设置自启动。
# systemctl start docker
# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
查看Docker版本号,安装成功。
# docker version
Client:
Version: 18.06.0-ce
API version: 1.38
Go version: go1.10.3
Git commit: 0ffa825
Built: Wed Jul 18 19:08:18 2018
OS/Arch: linux/amd64
Experimental: false
Server:
Engine:
Version: 18.06.0-ce
API version: 1.38 (minimum version 1.12)
Go version: go1.10.3
Git commit: 0ffa825
Built: Wed Jul 18 19:10:42 2018
OS/Arch: linux/amd64
Experimental: false
四、Docker使用
从Docker Hub上拉取镜像。
# docker pull 镜像
如拉取hello-world镜像。
# docker pull hello-world
Using default tag: latest
Trying to pull repository docker.io/library/hello-world ...
latest: Pulling from docker.io/library/hello-world
9db2ca6ccae0: Pull complete
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc
Status: Downloaded newer image for docker.io/hello-world:latest
查看本地镜像列表
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest 2cb0d9787c4d 3 days ago 1.85 kB
搜索Docker Hub上镜像,如Redis。
# docker search redis
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/redis Redis is an open source key-value store th... 5428 [OK]
docker.io docker.io/bitnami/redis Bitnami Redis Docker Image 80 [OK]
docker.io docker.io/sameersbn/redis 67 [OK]
docker.io docker.io/tenstartups/redis-commander 32 [OK]
docker.io docker.io/hypriot/rpi-redis Raspberry Pi compatible redis image 31
docker.io docker.io/kubeguide/redis-master redis-master with "Hello World!" 24
docker.io docker.io/kubeguide/guestbook-redis-slave Guestbook redis slave 19
docker.io docker.io/redislabs/redis Clustered in-memory database engine compat... 15
docker.io docker.io/tutum/redis Base docker image to run a Redis server 10
docker.io docker.io/webhippie/redis Docker images for redis 9 [OK]
docker.io docker.io/arm32v7/redis Redis is an open source key-value store th... 7
docker.io docker.io/rediscommander/redis-commander Alpine image for redis-commander - Redis m... 6 [OK]
docker.io docker.io/oliver006/redis_exporter Prometheus Exporter for Redis Metrics. Su... 5
docker.io docker.io/rtoma/logspout-redis-logstash Logspout including Redis adapter for sendi... 5
docker.io docker.io/centos/redis-32-centos7 Redis in-memory data structure store, used... 3
docker.io docker.io/dynomitedb/redis Redis backend for DynomiteDB. 2 [OK]
docker.io docker.io/frodenas/redis A Docker Image for Redis 2 [OK]
docker.io docker.io/tomesar/redis-arm Redis for ARM! 2 [OK]
docker.io docker.io/google/guestbook-python-redis A simple guestbook example written in Pyth... 1
docker.io docker.io/kilsoo75/redis-master This image is for the redis master of SK C... 1
docker.io docker.io/tiredofit/redis Redis Server w/ Zabbix monitoring and S6 O... 1 [OK]
docker.io docker.io/anchorfree/redis redis cache server for logging 0
docker.io docker.io/brendangibat/docker-logspout-redis Docker Logspout container with Logspout-Re... 0 [OK]
docker.io docker.io/circleci/redis CircleCI images for Redis 0 [OK]
docker.io docker.io/iadvize/redis 0
运行tomcat容器。
# docker run -d -p 8080:8080 --name myweb tomcat
Unable to find image 'tomcat:latest' locally
Trying to pull repository docker.io/library/tomcat ...
latest: Pulling from docker.io/library/tomcat
0bd44ff9c2cf: Pull complete
047670ddbd2a: Pull complete
ea7d5dc89438: Pull complete
4a05570971bb: Pull complete
66f679cd5859: Pull complete
89362eaac850: Pull complete
d76c23323cb4: Pull complete
f7a113d2d566: Pull complete
f7ffd00be2be: Pull complete
dd678d267c76: Pull complete
70443378c06d: Pull complete
a23e4449e436: Pull complete
Digest: sha256:6210133c6a0c677aba36326dc152d1eb7e95fc7f77a628db507824505e626f0b
Status: Downloaded newer image for docker.io/tomcat:latest
4460365a8d7faf2f43be476224370b638e2906b4e77ea63b235f5955397e4a51
打开浏览器,访问http://192.168.232.12:8080/,发现tomcat已经运行。
查看正在运行的容器。
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4460365a8d7f tomcat "catalina.sh run" 58 seconds ago Up 56 seconds 0.0.0.0:8080->8080/tcp myweb
停止容器,然后刷新http://192.168.232.12:8080/,发现已经无法访问。
# docker stop myweb
myweb
启动容器,然后刷新http://192.168.232.12:8080/,发现又可以重新访问。
# docker start myweb
myweb
删除正在运行的容器。
# docker rm -f myweb
myweb
查看容器列表
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
删除镜像。
# docker rmi tomcat
Untagged: tomcat:latest
Untagged: docker.io/tomcat@sha256:6210133c6a0c677aba36326dc152d1eb7e95fc7f77a628db507824505e626f0b
Deleted: sha256:2d084b11164d1d05bfe0a28afc966153e43efa6760dd2b4707b5764395def4f7
Deleted: sha256:051aa88fac6b19e2065a57e75656d246ee6e1877b69e3d1d2bc6d9a4b7d6b3ea
Deleted: sha256:fd3038453d0a86035a2a0cd3e27d279ebf5c13876a7dac057cbbb9b7eab55b51
Deleted: sha256:7a5783120283e76664302f7490f841f1cf350375a63c5ff1ef5e4a81c8d9c410
Deleted: sha256:5515301a37e9bbd84b6292e616c831d0205b326e5b2c602a0797d454181bb147
Deleted: sha256:722f68739f33d93cf104cef65d99f8976acd5548e5270f9cd6e0c1f20e1f3eb7
Deleted: sha256:67722f8db877938181c612fcf98771d8c4a1bfabcc7e5b75e15f89e2278bfc23
Deleted: sha256:6fc7e192e1abc8312638195b503db0c16db55a9657b83630271de2404bd5898d
Deleted: sha256:0fede3ada53233aecee0edf1b279d1f8caf95b07b7e8183beda83e86400c43a7
Deleted: sha256:cd373840cbe37d3e3b667f9358a14812ffe266d964a1889d3291da6020641d72
Deleted: sha256:0b9961a11f6c91674ce49c45d04111c2623f94ab5b6123cc9ddede62fb853cf7
Deleted: sha256:31da0268e97cfd9637a003e208218c8e55886838c8d6ed533b96ac842c4d0980
Deleted: sha256:a2e66f6c6f5f248f2f8dcf31a3b569626ef61c6371e157fb0db857152983fc1d
查看镜像列表
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest 2cb0d9787c4d 3 days ago 1.85 kB
五、国内Docker镜像配置
由于官方Docker Hub镜像仓库访问速度很慢,国内一些云供应商搭建了Docker Registry Mirror服务,速度快乐很多,其中DaoCloud比较常用。可以访问https://www.daocloud.io/进行注册,注册完成后点击下图的加速器图标。
拷贝以下红色框中字符串。
修改如下配置。
# vi /etc/docker/daemon.json
{
"registry-mirrors": [
"http://68cdf6cf.m.daocloud.io"
]
}
重启Docker。
# systemctl restart docker
重新拉取tomcat,发现快了很多。
# docker pull tomcat
Using default tag: latest
Trying to pull repository docker.io/library/tomcat ...
latest: Pulling from docker.io/library/tomcat
0bd44ff9c2cf: Pull complete
047670ddbd2a: Pull complete
ea7d5dc89438: Pull complete
4a05570971bb: Pull complete
66f679cd5859: Pull complete
89362eaac850: Pull complete
d76c23323cb4: Pull complete
f7a113d2d566: Pull complete
f7ffd00be2be: Pull complete
dd678d267c76: Pull complete
70443378c06d: Pull complete
a23e4449e436: Pull complete
Digest: sha256:6fff39f0eb3605d7d5a0c1f9059528b4e18197a01385bd788d23f8d78666df2a
Status: Downloaded newer image for docker.io/tomcat:latest