学习docker前,理解以下几个概念有助于更好的使用docker。
镜像,容器,仓库。
镜像,就是一个操作系统环境,里面只有你需要的几个应用程序,如apache,mysql,php之类,只读模板。
容器,从镜像创建的运行实例。可视为一个简易环境中和其中运行的应用。
仓库,存放镜像的地方。学过git的同学可能更容易理解。
一、安装docker时,增加第三方源epel
如果是centos7,下载并安装这个软件包
#wget http://mirror.hust.edu.cn/epel/beta/7/x86_64/epel-release-7-0.2.noarch.rpm
#rpm -ivh epel-release-7-0.2.noarch.rpm
如果是centos6,下载并安装这个软件包
#wget http://mirrors.hustunique.com/epel/6/i386/epel-release-6-8.noarch.rpm 32位
#rpm -Uvh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm 64位
#rpm -ivh epel-release-6-8.noarch.rpm
二、安装docker
#yum install docker-io
如果之前的系统中存在docker这个软件,最好先删除掉这个包,一个老旧的包
#service docker start
#chkconfig docker on
[[email protected] master]# docker -v
Docker version 1.5.0, build a8a31ef/1.5.0
三、镜像相关操作
3.1列出本地所有的镜像,显示出的依次是:镜像所属仓库 标签名 镜像id 创建日期 所占空间大小
[[email protected] master]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos 7 fd44297e2ddb 6 weeks ago 215.7 MB
centos centos7 fd44297e2ddb 6 weeks ago 215.7 MB
centos latest fd44297e2ddb 6 weeks ago 215.7 MB
四、容器相关操作
4.1查看容器
[[email protected] master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4304fbe22a8 centos:7 "/bin/bash" 18 minutes ago Up 17 minutes goofy_euclid
4.2 新建、查看容器
//新建,使用docker create命令新建一个docker容器,该命令新建的容器处于停止的状态
[[email protected] master]# docker create -i -t centos:centos7 /bin/bash
72096e2a30802ce5b5e504cbc904e241da3fdb7ee11b8eef989e362b8953db2c
//查看刚创建的容器72096e2a3080
[[email protected] master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72096e2a3080 centos:7 "/bin/bash" 5 seconds ago elated_franklin
d4304fbe22a8 centos:7 "/bin/bash" 19 minutes ago Up 18 minutes goofy_euclid
4.3 启动新建的容器
//启动
[[email protected] master]# docker start 72096e2a3080
72096e2a3080
//查看容器的状态
[[email protected] master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72096e2a3080 centos:7 "/bin/bash" 4 minutes ago Up 2 seconds elated_franklin
d4304fbe22a8 centos:7 "/bin/bash" 23 minutes ago Up 23 minutes goofy_euclid
4.4 进入新建的容器
[[email protected] master]# docker exec -ti d4304fbe22a8 /bin/bash
[[email protected] /]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
4.5 退出容器,使用命令exit或ctrl+d,用exit后不关闭容器 而是在后台继续运行
[[email protected] /]# exit
exit
[[email protected] master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72096e2a3080 centos:7 "/bin/bash" 4 minutes ago Up 2 seconds elated_franklin
d4304fbe22a8 centos:7 "/bin/bash" 23 minutes ago Up 23 minutes goofy_euclid
4.6 停止新建的容器
//停止
[[email protected] master]# docker stop 72096e2a3080
72096e2a3080
//重新启动
[[email protected] master]# docker restart 72096e2a3080
//查看
[[email protected] master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72096e2a3080 centos:7 "/bin/bash" 35 minutes ago Exited (137) 43 seconds ago elated_franklin
d4304fbe22a8 centos:7 "/bin/bash" 55 minutes ago Exited (0) 7 minutes ago goofy_euclid
[[email protected] master]#
4.7创建守护式容器
守护式容器:没有交互式会话,非常适合运行应用程序和服务。
//更多的时候,需要让docker容器在后台以守护态形式运行。用户可以通过添加-d参数来添加,
[[email protected] master]# docker run --name daemon -d centos:centos7 /bin/bash -c "while true;do echo hello world;sleep 1; done"
07369b21b6c0130b12af0ca2c689e5cd08181422475457d3e17519565159c818
[[email protected] master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
07369b21b6c0 centos:7 "/bin/bash -c ‘while 7 seconds ago Up 5 seconds daemon
//此时用docker run 只会返回一个容器id,必须用docker attach才能附着到新的shell会话,附着到容器会话,显示他一直在打印hello world
[[email protected] master]# docker attach 07369b21b6c0
hello world
hello world
hello world
4.8停止守护式容器
[[email protected] master]# docker stop 07369b21b6c0
4.9在容器内运行进程
我们可以通过docker exec命令在容器内部额外启动新进程。可以在容器内创建的进程有两种类型:后台任务和交互式任务
//后台任务在容器内运行,且没有交互需求,交互式任务则保持在前台运行。
[[email protected] master]#docker exec -d daemon touch /tmp/test
-d: 表明需要运行一个后台进程。
//验证
[[email protected] master]#docker exec -t -i daemon /bin/bash
[email protected]:/# cd /tmp/
[email protected]:/tmp# ls
test
4.10删除停止状态的容器
[[email protected] master]# docker rm d4304fbe22a8
d4304fbe22a8
[[email protected] master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
719f071a730d centos:7 "/bin/bash -c ‘while 46 minutes ago Exited (0) 2 minutes ago daemon
c43565344cb7 centos:7 "/bin/echo hello" 53 minutes ago Exited (0) 53 minutes ago prickly_galileo
07369b21b6c0 centos:7 "/bin/bash -c ‘while About an hour ago Exited (137) 47 minutes ago clever_torvalds
3cd2fa08c942 centos:7 "/bin/bash" About an hour ago Exited (0) 26 minutes ago ecstatic_pare
b8db23a1c3e6 centos:7 "/bin/bash" About an hour ago Exited (0) About an hour ago happy_jones
72096e2a3080 centos:7 "/bin/bash" 2 hours ago Exited (137) About an hour ago elated_franklin
4.11删除运行中的容器
[[email protected] master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
719f071a730d centos:7 "/bin/bash -c ‘while 55 minutes ago Exited (0) 12 minutes ago daemon
c43565344cb7 centos:7 "/bin/echo hello" About an hour ago Exited (0) 8 minutes ago prickly_galileo
07369b21b6c0 centos:7 "/bin/bash -c ‘while About an hour ago Exited (137) 56 minutes ago clever_torvalds
3cd2fa08c942 centos:7 "/bin/bash" About an hour ago Exited (0) 36 minutes ago ecstatic_pare
b8db23a1c3e6 centos:7 "/bin/bash" About an hour ago Exited (0) About an hour ago happy_jones
72096e2a3080 centos:7 "/bin/bash" 2 hours ago Up 12 seconds elated_franklin
[[email protected] master]# docker rm -f 72096e2a3080
72096e2a3080
[[email protected] master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
719f071a730d centos:7 "/bin/bash -c ‘while About an hour ago Exited (0) 16 minutes ago daemon
c43565344cb7 centos:7 "/bin/echo hello" About an hour ago Exited (0) 13 minutes ago prickly_galileo
07369b21b6c0 centos:7 "/bin/bash -c ‘while About an hour ago Exited (137) About an hour ago clever_torvalds
3cd2fa08c942 centos:7 "/bin/bash" About an hour ago Exited (0) 40 minutes ago ecstatic_pare
b8db23a1c3e6 centos:7 "/bin/bash" About an hour ago Exited (0) About an hour ago happy_jones
4.12导出和导入容器
//docker export命令导出
[[email protected] master]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
719f071a730d centos:7 "/bin/bash -c ‘while About an hour ago Exited (0) 16 minutes ago daemon
c43565344cb7 centos:7 "/bin/echo hello" About an hour ago Exited (0) 13 minutes ago prickly_galileo
07369b21b6c0 centos:7 "/bin/bash -c ‘while About an hour ago Exited (137) About an hour ago clever_torvalds
3cd2fa08c942 centos:7 "/bin/bash" About an hour ago Exited (0) 40 minutes ago ecstatic_pare
b8db23a1c3e6 centos:7 "/bin/bash" About an hour ago Exited (0) About an hour ago happy_jones
[[email protected] master]# docker export b8db23a1c3e6 > test.tar
[[email protected] master]# ls
rc.tomcat.tar.gz software test.tar ttp_admin-150525.tar.gz www.ttpai.cn-150525.tar.gz
//docker import命令导入,成为镜像
[[email protected] master]# cat test.tar | docker import - test/centos:v1
fc9df930147ad57f2ea516748375864a612dc6fe645f0e32f5992ac19088adf6
[[email protected] master]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
test/centos v1 fc9df930147a 15 seconds ago 215.7 MB
centos 7 fd44297e2ddb 6 weeks ago 215.7 MB
centos centos7 fd44297e2ddb 6 weeks ago 215.7 MB
centos latest fd44297e2ddb 6 weeks ago 215.7 MB