docker容器部署及使用(一)
2016-08-11 TsengYia#126.com
http://tsengyia.blog.chinaunix.net/
系统环境:
CentOS 7.2 [3.10.0-327.el7.x86_64]
软件环境:
docker-engine-1.12.0-1.el7.centos.x86_64.rpm
docker-engine-selinux-1.12.0-1.el7.centos.noarch.rpm
仓库:用来提供/存放镜像,有官方仓库、私有仓库
镜像:针对某个虚拟机或某个应用封装的独立环境,作为容器的模板
容器:基于镜像启动的实例
###########################################
一、安装docker平台
1. 在线安装方法
[root@ct ~]# vim /etc/yum.repos.d/docker.repo
[dockerrepo]
name=Docker Repository
baseurl=
enabled=1
gpgcheck=1
gpgkey=
[root@ct ~]# yum install docker-engine
参考:
2. 离线安装方法
通过YUM缓存(keepcache=1)或手动下载获取docker相关的rpm安装包,然后手动安装
[root@ct ~]# yum localinstall docker-engine*.rpm docker-engine-selinux*.rpm
参考下载地址:
/Packages/docker-engine-1.12.0-1.el7.centos.x86_64.rpm
/Packages/docker-engine-selinux-1.12.0-1.el7.centos.noarch.rpm
3. 启用docker服务
[root@ct ~]# systemctl restart docker.service
[root@ct ~]# systemctl enable docker.service
[root@ct ~]# docker -v //查看版本
Docker version 1.12.0, build 8eab29e
[root@ct ~]# docker version //查看详细版本
Client:
Version: 1.12.0
.. ..
OS/Arch: linux/amd64
Server:
Version: 1.12.0
.. ..
OS/Arch: linux/amd64
二、管理镜像
1. 搜索镜像资源
可浏览器访问 了解官方镜像,或者
[root@ct ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 3754 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker c... 750 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable ... 240 [OK]
million12/nginx-php Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS... 76 [OK]
.. ..
[root@ct ~]# docker search centos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
centos The official build of CentOS. 2526 [OK]
jdeathe/centos-ssh CentOS-6 6.8 x86_64 / CentOS-7 7.2.1511 x8... 27 [OK]
nimmis/java-centos This is docker images of CentOS 7 with dif... 13 [OK]
million12/centos-supervisor Base CentOS-7 with supervisord launcher, h... 12 [OK]
.. ..
2. 下载指定镜像资源
[root@ct ~]# docker pull nginx
.. ..
[root@ct ~]# docker pull centos
.. ..
[root@ct ~]# docker pull hello-world
.. ..
3. 查看本地镜像列表
[root@ct ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 970633036444 12 days ago 196.7 MB
hello-world latest c54a2cc56cbb 5 weeks ago 1.848 kB
nginx latest 0d409d33b27e 10 weeks ago 182.7 MB
4. 修改镜像名称(根据ID或名称定位)
[root@ct ~]# docker tag c54a hello-world:1.0
[root@ct ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 970633036444 12 days ago 196.7 MB
hello-world 1.0 c54a2cc56cbb 5 weeks ago 1.848 kB
hello-world latest c54a2cc56cbb 5 weeks ago 1.848 kB
nginx latest 0d409d33b27e 10 weeks ago 182.7 MB
5. 删除镜像(根据ID或名称定位)
[root@ct ~]# docker rmi hello-world:1.0
Untagged: hello-world:1.0
[root@ct ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 970633036444 12 days ago 196.7 MB
hello-world latest c54a2cc56cbb 5 weeks ago 1.848 kB
nginx latest 0d409d33b27e 10 weeks ago 182.7 MB
6. 从备份文件导入镜像
从文件 /opt/nginx.tar.gz 导入镜像,并命名为nginx-new:latest
[root@ct opt]# docker import /opt/nginx.tar.gz nginx-new:latest
sha256:cf3ca2039174d88a14abf65ac0da0bdd1a0e27e958d21022b2f15495788df217
[root@ct opt]# docker images nginx-new
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx-new latest cf3ca2039174 35 seconds ago 181.2 MB
三、管理容器
1. 启动容器
1)启动nginx容器,执行容器内的一条命令(nginx -v、grep root .. ..)后自动退出
[root@ct ~]# docker run nginx nginx -v
nginx version:
[root@ct ~]# docker run nginx grep root /etc/nginx/conf.d/default.conf
root /usr/share/nginx/html;
.. ..
2)启动nginx容器,并进入容器内的/bin/bash命令行环境
-i,允许交互
-t,开启终端
[root@ct ~]# docker run -it nginx /bin/bash
root@851ee377ebdf:/# ip add list //执行容器内的ip命令
.. ..
93: eth0@if94: mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff
inet 172.17.0.2/16 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::42:acff:fe11:4/64 scope link
valid_lft forever preferred_lft forever
root@851ee377ebdf:/# exit //退出/bin/bash终端
exit
[root@ct ~]#
3)在后台启动一个nginx容器,将docker主机的8000端口映射到此nginx容器的80端口
-d,在后台运行
-p 本地端口:容器端口
[root@ct ~]# docker run -d -p 8000:80 nginx
3afe6f63d40e9f77869a8871ae7daf20f1dda58d9ddb4f0f9c1d594853235977
[root@ct ~]# docker ps //列出活动中的容器(ps -a列出所有)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3afe6f63d40e nginx "nginx -g 'daemon off" 11 seconds ago Up 10 seconds 443/tcp, 0.0.0.0:8000->80/tcp grave_thompson
4)在后台启动一个nginx容器,将docker主机的8001端口映射到此nginx容器的80端口,将docker主机的/opt/webroot映射为此nginx容器的web目录
-v 本地目录:容器内目录[:ro]
[root@ct ~]# docker run -d -p 8001:80 -v /opt/webroot:/usr/share/nginx/html:ro nginx
bd75f0f274611949cb3ea70d5295241fe79c959814a2861bc243f801baf93e2c
[root@ct ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd75f0f27461 nginx "nginx -g 'daemon off" 8 minutes ago Up 8 minutes 443/tcp, 0.0.0.0:8001->80/tcp mad_shannon
3afe6f63d40e nginx "nginx -g 'daemon off" 14 minutes ago Up 14 minutes 443/tcp, 0.0.0.0:8000->80/tcp grave_thompson
2. 访问容器
1)连接到id值3afe代表的容器的命令行
[root@ct ~]# docker exec -it 3afe /bin/bash
root@3afe6f63d40e:/# service nginx status //确认nginx服务状态
nginx is running.
root@bd75f0f27461:/# ss -anpt | grep nginx //检查此nginx容器的监听端口
LISTEN 0 128 *:80 *:* users:(("nginx",pid=1,fd=6))
root@3afe6f63d40e:/# exit
exit
[root@ct ~]#
[root@ct ~]# ss -anpt | grep :800 //检查docker主机的监听端口
LISTEN 0 128 :::8000 :::* users:(("docker-proxy",pid=93856,fd=4))
LISTEN 0 128 :::8001 :::* users:(("docker-proxy",pid=94171,fd=4))
2)与id值3afe代表的容器传输文件
[root@ct ~]# echo AAAA > /opt/a.html
[root@ct ~]# docker cp /opt/a.html 3afe:/usr/share/nginx/html/a.html
[root@ct ~]# elinks -dump
AAAA
[root@ct ~]#
3. 关闭、结束、删除容器
1)关闭id值3afe代表的容器
[root@ct ~]# docker stop 3afe
3afe
[root@ct ~]# docker ps -a | grep 3afe //确认被stop的容器状态
3afe6f63d40e nginx "nginx -g 'daemon off" 29 minutes ago Exited (0) About a minute ago grave_thompson
2)杀死id值3afe代表的容器
[root@ct ~]# docker kill bd75
bd75
[root@ct ~]# docker ps -a | grep bd75 //确认被stop的容器状态
bd75f0f27461 nginx "nginx -g 'daemon off" 25 minutes ago Exited (137) 6 seconds ago mad_shannon
3)删除容器
[root@ct ~]# docker rm 3afe //删除已关闭的容器
3afe
[root@ct ~]# docker start bd75 //启用已关闭的容器
[root@ct ~]# docker rm -f bd75 //删除正在运行的容器(需要 -f)
bd75
[root@ct ~]# docker ps -a | grep -E '3afe|bd75' //确认删除结果
[root@ct ~]#
3. 将更新的容器存储为镜像
1)启用一个新的容器
[root@ct ~]# docker run -d -p 80:80 nginx
b42ec0b3976fba596937a365111ce04e738b246f2a90bc72a45f3e0cbaf10e2b
2)向容器内添加文件
[root@ct ~]# echo "New page." > /opt/b.html
[root@ct ~]# docker cp /opt/b.html b42e:/usr/share/nginx/html/b.html
[root@ct ~]# docker exec b42e cat /usr/share/nginx/html/b.html
New page.
3)将id值b42e代表的容器存储为镜像 nginx:1.2
[root@ct ~]# docker commit -m "nginx update test" b42e nginx:1.2
sha256:f72e1aad1a94bb6538255ec21597837ad7eca8b92afb1324e9c945aa523e343c
[root@ct ~]# docker images nginx:1.2
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.2 f72e1aad1a94 About a minute ago 182.7 MB
4. 将容器导出为包文件
1)将id值b42e代表的容器导出
[root@ct ~]# docker export b42e -o /opt/nginx1.2.tar
[root@ct ~]# file /opt/nginx1.2.tar //确认结果
/opt/nginx1.2.tar: POSIX tar archive
[root@ct ~]# du -sh /opt/nginx1.2.tar
181M /opt/nginx1.2.tar
[root@ct images]# gzip nginx.tar
[root@ct images]# du -sh nginx.tar.gz
68M nginx.tar.gz
[root@ct images]# file nginx.tar.gz
nginx.tar.gz: gzip compressed data, was "nginx.tar", from Unix, last modified: Mon Aug 8 13:16:45 2016
四、docker相关指令帮助
1)指令清单
[root@ct opt]# docker
Usage: docker [OPTIONS] COMMAND [arg...]
docker [ --help | -v | --version ]
.. ..
Commands:
attach Attach to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
.. ..
2)获取具体指令的帮助
[root@ct ~]# man docker-images
[root@ct ~]# man docker-run
[root@ct ~]# man docker-cp
[root@ct ~]# man docker-ps
[root@ct ~]# man docker-start
[root@ct ~]# man docker-stop
[root@ct ~]# man docker-kill
[root@ct ~]# man docker-export
[root@ct ~]# man docker-import
[root@ct ~]# man docker-exec
.. ..
###########################################
阅读(5242) | 评论(0) | 转发(0) |