一、Docker基础环境的安装
1. 环境说明
本机采用操作系统如下: CentOS-7.3-X86-64,内核3.10 x64位,docker 1.12.x版本。
建议采用CentOS7版本或ubuntu版本,本文采用CentOS7即CentOS-7-x86_64-DVD-1611.iso版本最小化安装。
Docker可以在虚拟机环境中运行进行使用测试。本文出自:http://koumm.blog.51cto.com/
2. 主机网络
1)关闭firewalld防火墙和SELinux
setenforce 0
sed -i ‘s#SELINUX=enforcing#SELINUX=disabled#g‘ /etc/sysconfig/selinux
systemctl disable firewalld
systemctl stop firewalld
docker是必须要防火墙功能模块的,个人目前用iptables
systemctl disable iptables.service
systemctl stop iptables.service
注意最小化需要补充安装的软件包:
yum install vim net-tools iptables-config
2)配置hosts文件(主机名称)
echo "192.168.1.90 docker1" >> /etc/hosts
3. 安装Docker软件包
1)可以直接安装包yum源
# yum install -y docker
2)配置docker开机自启动以及启动服务
# systemctl start docker.service
# systemctl enable docker.service
3)查看docker服务器,表示docker已安装成功
查看版本:docker version
帮助信息:docker --help
概要信息:docker info
镜像查看:docker images
进程查看:docker ps -a
搜索镜像:docker search centos #搜索可用docker镜像
示例: 表示docker已安装成功。
[[email protected] ~]# docker info
Containers: 4
Running: 0
Paused: 0
Stopped: 4
Images: 3
Server Version: 1.12.6
Storage Driver: devicemapper
Pool Name: docker-253:0-16798063-pool
Pool Blocksize: 65.54 kB
Base Device Size: 10.74 GB
Backing Filesystem: xfs
Data file: /dev/loop0
Metadata file: /dev/loop1
Data Space Used: 849.3 MB
Data Space Total: 107.4 GB
Data Space Available: 15.27 GB
Metadata Space Used: 1.937 MB
Metadata Space Total: 2.147 GB
Metadata Space Available: 2.146 GB
Thin Pool Minimum Free Space: 10.74 GB
Udev Sync Supported: true
Deferred Removal Enabled: false
Deferred Deletion Enabled: false
Deferred Deleted Device Count: 0
Data loop file: /var/lib/docker/devicemapper/devicemapper/data
WARNING: Usage of loopback devices is strongly discouraged for production use. Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
Library Version: 1.02.135-RHEL7 (2016-09-28)
Logging Driver: journald
Cgroup Driver: systemd
Plugins:
Volume: local
Network: bridge host null overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Security Options: seccomp
Kernel Version: 3.10.0-514.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 2
CPUs: 2
Total Memory: 984.8 MiB
Name: docker1
ID: YZZ7:NRHH:WJM7:PYUR:DGRH:4MLD:4GT4:UR2I:5PUZ:EAGS:P4Z2:JSL4
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Insecure Registries:
127.0.0.0/8
Registries: docker.io (secure)
二、Docker使用基础
1. 下载docker基础镜像
1)搜索centos基础镜像
[[email protected] ~]# docker search centos INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/centos The official build of CentOS. 3362 [OK] docker.io docker.io/jdeathe/centos-ssh CentOS-6 6.9 x86_64 / CentOS-7 7.3.1611 x8... 69 [OK] docker.io docker.io/nimmis/java-centos This is docker images of CentOS 7 with dif... 26 [OK] docker.io docker.io/gluster/gluster-centos Official GlusterFS Image [ CentOS-7 + Glu... 19 [OK] docker.io docker.io/million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 16 [OK] docker.io docker.io/kinogmt/centos-ssh CentOS with SSH 13 [OK] docker.io docker.io/egyptianbman/docker-centos-nginx-php A simple and highly configurable docker co... 9 [OK] docker.io docker.io/torusware/speedus-centos Always updated official CentOS docker imag... 8 [OK] docker.io docker.io/centos/mariadb55-centos7 5 [OK] docker.io docker.io/nathonfowlie/centos-jre Latest CentOS image with the JRE pre-insta... 5 [OK] docker.io docker.io/darksheer/centos Base Centos Image -- Updated hourly 3 [OK] docker.io docker.io/harisekhon/centos-java Java on CentOS (OpenJDK, tags jre/jdk7-8) 2 [OK] docker.io docker.io/harisekhon/centos-scala Scala + CentOS (OpenJDK tags 2.10-jre7 - 2... 2 [OK] docker.io docker.io/blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK] docker.io docker.io/freenas/centos Simple CentOS Linux interactive container 1 [OK] docker.io docker.io/sgfinans/docker-centos CentOS with a running sshd and Docker 1 [OK] docker.io docker.io/timhughes/centos Centos with systemd installed and running 1 [OK] docker.io docker.io/vorakl/centos CentOS7, EPEL, tools. Updated/Tested daily! 1 [OK] docker.io docker.io/grossws/centos CentOS 6 and 7 base images with gosu and l... 0 [OK] docker.io docker.io/januswel/centos yum update-ed CentOS image 0 [OK] docker.io docker.io/kz8s/centos Official CentOS plus epel-release 0 [OK] docker.io docker.io/labengine/centos Centos image base 0 [OK] docker.io docker.io/repositoryjp/centos Docker Image for CentOS. 0 [OK] docker.io docker.io/vcatechnology/centos A CentOS Image which is updated daily 0 [OK] docker.io docker.io/wenjianzhou/centos centos 0 [OK] [[email protected] ~]#
2)下载centos基础镜像
本文采用官方默认镜像仓库下载。
[[email protected] ~]# docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ...
latest: Pulling from docker.io/library/centos
343b09361036: Pull complete
Digest: sha256:bba1de7c9d900a898e3cadbae040dfe8a633c06bc104a0df76ae24483e03c077
下载其它示例:下载支持ssh服务器的centos镜像
[[email protected] ~]# docker pull jdeathe/centos-ssh
Using default tag: latest
Trying to pull repository docker.io/jdeathe/centos-ssh ...
latest: Pulling from docker.io/jdeathe/centos-ssh
45a2e645736c: Pull complete
f0d571ff94cf: Pull complete
ea228e12ac20: Pull complete
edd63eef0a83: Pull complete
27c3eb39991d: Pull complete
4bf26ef9f3dd: Pull complete
4e572750aff7: Pull complete
b94a26882ae9: Pull complete
91ee52de11b1: Pull complete
e89f8a480c42: Pull complete
bd26710715b3: Pull complete
75c421c47829: Pull complete
f6eabaf37913: Pull complete
Digest: sha256:c80d3b79c853ce41a993cfa76f1b5983dffeb08aceefaaf04d64c9dc2e177d8e
2. 通过导出、导入的方式部署基础镜像
#官方镜像的下载很慢,有时多次尝试才能下载。
方法一:
#一台已经下载的好的镜像的服务器上操作。
docker save centos > /opt/centos.tar #Docker导出镜像
docker load < /opt/centos.tar #Docker导入镜像
方法二:
docker export id > /opt/centos.tar #Docker导出镜像
cat /opt/centos.tar | docker import centos #Docker导入镜像
*注1:用户既可以使用 docker load 来导入镜像存储文件到本地镜像库,也可以使用 docker import 来导入一个容器快照到本地镜像库。
*注2:容器快照文件将丢弃所有的历史记录和元数据信息(即仅保存容器当时的快照状态),而镜像存储文件将保存完整记录,体积也要大。
此外,从容器快照文件导入时可以重新指定标签等元数据信息。
3. 查看下载的基础镜像
[[email protected] ~]# docker images
-----------------------------------------------------------------------------------------------------
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/jdeathe/centos-ssh latest 1d551d635e99 4 days ago 214 MB
docker.io/centos latest 8140d0c64310 2 weeks ago 192.5 MB
说明 :
REPOSITORY:来自于哪个仓库,比如 centos
TAG : 镜像的标记,一般修改版本号,latest为最后版本
IMAGE ID : 镜像的id号
CREATED : 创建镜像的时间
SIZE : 镜像的大小
4. 通过基础镜像创建一个容器
1)通过基础镜像centos创建一个容器名为centos_v1,并在后台执行
[[email protected] ~]# docker run -it -d --name centos_v1 centos /bin/bash
0c1841e5d1115d35b80177ed5ea4fc5782e8a3ffc5f5c801979fb90ac5f26423
常用参数说明:
-i: 开启交互式shell
-t: 为容器分配一个伪tty终端
centos: 指定镜像的名字
/bin/bash: 运行/bin/bash
2) 查看容器
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c1841e5d111 centos "/bin/bash" 7 seconds ago Up 4 seconds centos_v1
查看docker镜像的状态,-a表示列出所有的容器,STATUS如果为Exited为退出,UP为运行。
3)进入容器
[[email protected] ~]# docker exec -it centos_v1 /bin/bash
提示符变成了0c1841e5d111,也是该容器的ID, 也是该容器的主机名。
[[email protected] /]# ls
anaconda-post.log bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[[email protected] /]# hostname
0c1841e5d111
4)退出容器
[[email protected] /]# exit
exit
[[email protected] ~]#
5. 查看容器状态
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c1841e5d111 centos "/bin/bash" About an hour ago Up About an hour centos_v1
6. 关闭与启动容器
关闭容器
[[email protected] ~]# docker stop centos_v1
centos_v1
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c1841e5d111 centos "/bin/bash" About an hour ago Exited (137) 3 seconds ago centos_v1
说明:STATUS状态为EXIT表示容器是退出状态。
启动容器
[[email protected] ~]# docker start centos_v1
centos_v1
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0c1841e5d111 centos "/bin/bash" About an hour ago Up 3 seconds centos_v1
[[email protected] ~]#
[[email protected] ~]#
7. 删除容器
1)删除容器(通过镜像ID与镜像名称均可)
[[email protected] ~]# docker rm centos_v1 或 docker rm 0c1841e5d111
2)对于正在运行的容器,可以加-f参数强制进行删除。
[[email protected] ~]# docker rm -f centos_v1
8. 通过一个容器创建镜像
说明:只能对运行中的容器提交本地镜像,关闭状态的容器无法提交本地镜像。
1)通过容器提交镜像
[[email protected] ~]# docker commit centos_v1 centos:v1
sha256:66d2dc12144a87def3c16080553cca41d2a2965d9332f2a085dc23cc30366afa
[[email protected] ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos v1 66d2dc12144a 6 seconds ago 192.5 MB
docker.io/jdeathe/centos-ssh latest 1d551d635e99 4 days ago 214 MB
docker.io/centos latest 8140d0c64310 2 weeks ago 192.5 MB
2)删除镜像(通过镜像ID与镜像名称均可)
[[email protected] ~]# docker rmi centos:v1
或
[[email protected] ~]# docker rmi 66d2dc12144a
Untagged: centos:v1
Deleted: sha256:66d2dc12144a87def3c16080553cca41d2a2965d9332f2a085dc23cc30366afa
Deleted: sha256:9c6100b12798ef72aeb16c13d3d8da8bef0a060d3162f068ea8730025e3508ea
9. 示例:创建一个提供apache服务器的容器
1)通过最基础镜像创建一个容器,用于制做
[[email protected] ~]# docker run -it -d --name abc centos /bin/bash
[[email protected] ~]# docker exec -it httpd-80 /bin/bash
# yum install net-tools httpd -y
2) 通过httpd-80容器制一个http本地镜像
[[email protected] ~]# docker commit httpd-80 centos:apache
3) 通过centos:apache镜像可以创建很多个容器
参数说明:-p 80:80 -p 8022:22 映射本地端口到容器的对应端口上,可以配置多个端口。
[[email protected] ~]# docker run -it -d -p 8082:80 --name apache-8082 centos:apache /usr/sbin/apachectl -D FOREGROUND
1fb934170108b66fcdfa7c77f6673d33aca44fac63ca0b61b35f06519c3ef242
[[email protected] ~]# docker run -it -d -p 8081:80 --name apache-8081 centos:apache /usr/sbin/apachectl -D FOREGROUND
cc4e7689db7bd5674f131782c470cf640594d298dbc88b680fc3caeb77bce256
[[email protected] ~]# docker run -it -d -p 8080:80 --name apache-8080 centos:apache /usr/sbin/apachectl -D FOREGROUND
da494401dd34f843dac50e67a1a1c3077b44f9faeadff3403e50196fe4ce5318
[[email protected] ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da494401dd34 centos:apache "/usr/sbin/apachectl " 5 seconds ago Up 4 seconds 0.0.0.0:8080->80/tcp apache-8080
cc4e7689db7b centos:apache "/usr/sbin/apachectl " 17 seconds ago Up 15 seconds 0.0.0.0:8081->80/tcp apache-8081
1fb934170108 centos:apache "/usr/sbin/apachectl " 26 seconds ago Up 25 seconds 0.0.0.0:8082->80/tcp apache-8082
f4b6bffcca1b centos "/bin/bash" 8 minutes ago Up 8 minutes httpd-80
[[email protected] ~]#