docker实战之centos6.5上安装、镜像、容器相关操作

学习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

时间: 2024-10-06 00:31:11

docker实战之centos6.5上安装、镜像、容器相关操作的相关文章

在CentOS6.1上安装puppet最新版本puppet-3.6.2版本问题

前述: 最近发现了一个问题,在CentOS6.4上安装puppet,直接安装一个puppet官方源即可,然后直接使用yum安装puppet. 但是现在在CentOS6.1上安装puppet-3.6.2,发现很多puppet所需要的依赖包的版本太高,而CentOS6.1提供的软件版本太低,无法满足puppet的安装,在安装puppet时会提示很多依赖包,需要安装,而这个安装无法通过yum安装,需要自己下载rpm包安装,特别麻烦. 后来想过安装低版本的puppet,比如安装3.2.4的,但是也可以,

LAMP+Centos6.5上安装zabbix

系统版本:centos6.5 内核版本:2.6.32-358.el6.x86_64 yum安装LAMP环境: yum install -y gcc gcc-c++httpd httpd-devel mysql msql-server mysql-devel php php-mysql php-common php-gdphp-mbstring php-mcrtpt php-devel php-xml php-bcmath libcurl-develnet-snmp-devel 2.  添加用户及

CentOS6.9上安装cobbler2.6实现自动化安装Linux系统

CentOS6.9上安装cobbler2.6实现自动化安装Linux系统 cobbler可以批量部署Linux系统,并实现无人值守安装. cobbler依赖的服务:dhcp,tftp,http,pxe,kickstart 服务对应的端口号为dhcp--67,68;tftp--69;http--80;cobbler--25151 1.环境准备:最小化安装centos6.9mini,配置固定IP和本地yum源,关闭防火墙和selinux iptables -F setenforce 0 chkcon

最小化安装的64位CentOS6.5上安装Oracle11g R2

安装环境: 操作系统:CentOS6.5 x86_64 安装步骤: 一.按照官方文档安装依赖包,主要包如下: binutils-2.20.51.0.2-5.11.el6 (x86_64) compat-libcap1-1.10-1 (x86_64) compat-libstdc++-33-3.2.3-69.el6 (x86_64) compat-libstdc++-33-3.2.3-69.el6.i686 gcc-4.4.4-13.el6 (x86_64) gcc-c++-4.4.4-13.el

centos6.5上安装guacamole

一.工作安排 在centos6.5上安装guacamole.安装guacamole前需要先安装jdk和tomcat. 二.具体步骤 一.安装jdk 1.下载jdk压缩文件 本次选择jdk1.8.0_65 2.新建/usr/java文件夹,将jdk压缩包解压到 /usr/java下,改名为jdk1.8 3.配置java环境变量 编辑 /etc/profile文件 # vim /etc/profile JAVA_HOME=/usr/java/jdk1.8 PATH=$JAVA_HOME/bin:$P

centos6.5上安装5.7版本的mysql

centos6.5上安装5.7版本的mysql https://www.cnblogs.com/lzj0218/p/5724446.html 设置root可以在本机以外的机器访问 mysql -uroot -ppassword > use mysql> select host , user from user;> update user set host = '%' where user = 'root';> select host , user from user; 搜权 myu

CentOS6.7上安装nginx1.8.0

主题: CentOS6.7上安装nginx1.8.0 环境准备: 1.gcc-c++ 示例:yum install gcc-c++ 安装:gcc-c++ gcc-c++编译工具 2.PCRE(Perl Compatible Regular Expressions) 示例:yum install -y pcre pcre-devel 安装:pcre和pcre-devel PCRE(Perl Compatible Regular Expressions)是一个 Perl 库,包括 perl 兼容的正

centos6.5 rpm安装mysql5.6.19操作及步骤

系统:centos 6.5  64位 数据库:mysql5.6.19 64位 采用RPM安装包安装,之后修改mysql默认路径. 首先要卸载系统自带mysql包,命令如下 yum remove mysql mysql-server mysql-libs 检查是否有残留 rpm -qa | grep mysql 若发现有则使用rpm -e 安装包名称 安装步骤: 1.官网下载tar压缩包,下载地址为http://cdn.mysql.com/Downloads/MySQL-5.6/MySQL-5.6

Docker学习笔记(99-1)Centos6.8上安装Docker

一.升级操作系统,准备工作 1.检查linux内核版本号和是否支持aufs [[email protected] ~]# uname -r2.6.32-642.el6.x86_64[[email protected] ~]# grep aufs /proc/filesystems 3.1以后才支持 2.升级linux内核,安装aufs yum安装带aufs模块的3.10内核(或到这里下载kernel手动安装:http://down.51cto.com/data/1903250)cd /etc/y