Docker学习(一) 镜像与容器

安装Docker

$ curl -sSL https://get.docker.com | sh

$ sudo usermod -aG docker $USER

重启

什么是镜像什么是容器?

先看看什么是容器?

容器是一个进程,这个进程可以模拟基础操作系统环境。

什么是镜像?

镜像是生成容器的模板。镜像和容器的关系就像 类与对象 的关系。可以说容器是镜像的实例。

创建新镜像

创建新镜像有两种方式:

1、从已有的容器中提取

2、使用Dockerfile创建

这里我们使用方式1

实验目的

创建一个支持RoR环境的CentOS镜像,类似于《CentOS 7 快速安装RoR环境》所述。

实验过程

1、查看本地已有镜像

$ sudo docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

发现啥都没有

2、下载最新CentOS镜像到本地

先查询一下Docker Hub上现有的镜像

$ sudo docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   4308                [OK]                
ansible/centos7-ansible            Ansible on Centos7                              110                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x86_…   96                                      [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   54                                      [OK]
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              40                                      [OK]
tutum/centos                       Simple CentOS docker image with SSH access      39                                      
gluster/gluster-centos             Official GlusterFS Image [ CentOS-7 +  Glust…   30                                      [OK]
centos/mysql-57-centos7            MySQL 5.7 SQL database server                   27                                      
openshift/base-centos7             A Centos7 derived base image for Source-To-I…   24                                      
centos/python-35-centos7           Platform for building and running Python 3.5…   22                                      
kinogmt/centos-ssh                 CentOS with SSH                                 19                                      [OK]
centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational …   18                                      
openshift/jenkins-2-centos7        A Centos7 based Jenkins v2.x image for use w…   13                                      
centos/httpd-24-centos7            Platform for running Apache httpd 2.4 or bui…   13                                      
openshift/mysql-55-centos7         DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
openshift/jenkins-1-centos7        DEPRECATED: A Centos7 based Jenkins v1.x ima…   4                                       
openshift/wildfly-101-centos7      A Centos7 based WildFly v10.1 image for use …   3                                       
darksheer/centos                   Base Centos Image -- Updated hourly             3                                       [OK]
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   3                                       
pivotaldata/centos                 Base centos, freshened up a little with a Do…   2                                       
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                                       [OK]
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   1                                       
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   0                                       
jameseckersall/sonarr-centos       Sonarr on CentOS 7                              0                                       [OK]

smartentry/centos                  centos with smartentry                          0                                       [OK]

然后选择一个下载

$ sudo docker pull centos
Using default tag: latest
latest: Pulling from library/centos
469cfcc7a4b3: Pull complete 
Digest: sha256:989b936d56b1ace20ddf855a301741e52abca38286382cba7f44443210e96d16

Status: Downloaded newer image for centos:latest

再次查询

$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

centos              latest              e934aafc2206        6 weeks ago         199MB

3、以这个镜像运行一个容器

sudo docker run -t -i centos /bin/bash

[root@379adebe4b02 /]# 

这句命令的含义

run:以某个镜像(这里指明了是centos镜像)运行一个容器

-t:在容器内指定一个伪终端或终端

-i:允许与容器内的标准输入(stdin)进行交互

/bin/bash:在容器里面执行的要运行的命令

这样,我们就可以在容器内安装RoR环境了。

4、在容器内安装RoR环境(具体做法省略)

这里要说明一点:虽然容器可以保存状态,但是最佳实践是:

将重要的环境提取到镜像中保存,这样的话这个镜像实例化出来的容器都会支持这个环境。软件数据保存到宿主环境中,这样即使容器崩溃了数据也不会丢失。


例如,假设which命令不能用,于是你在容器里面安装了一个(yum install which)。当你退出容器(准确地讲是退出容器进程,exit)之后再次通过同一个镜像运行容器,这个容器已经不是前面的那个容器了,而是一个全新的实例,which命令依旧不能用。在这种情况下,你需要从安装了which命令的那个容器中提取出新镜像,这样容器的当前状态就以新镜像的形式保存了下来,下次用这个新镜像启动容器,发现which命令可用。

再例如,nginx产生的日志文件,如果保存在容器内,那么退出容器时这些日志文件将全部丢失。而显然这些日志文件是不合适提取到镜像中保存的(虽然这样做理论上没有错误),在这种情况下就需要将宿主环境(运行docker的操作系统)里面的目录挂载到容器中,容器对文件的读写就转变成了对宿主环境中文件的读写,这样数据就得到了保存。

5、提取镜像

退出容器,执行

$ sudo docker commit -m="update with RoR" -a="thinking" f44cf0ebec18 ror-centos-20180524001

sha256:8e494a7f92ca8122527879fd6e2ab808e631b7859ef45d7423bc18c7363b5f1d

其中

-m:提交描述信息

-a:指明作者

f44cf0ebec18:指出容器ID

ror-centos-20180524001:命名新容器

再查询一下

$ sudo docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
ror-centos-20180524001   latest              8e494a7f92ca        6 minutes ago       586MB

centos                   latest              e934aafc2206        6 weeks ago         199MB

有了,用ror-centos-20180524001镜像运行一个容器试一下

$ sudo docker run -t -i ror-centos-20180524001 /bin/bash
[root@6b643e4e7c2d /]# rails -v
Rails 5.2.0

[root@6b643e4e7c2d /]# 

完成!

猜你喜欢

转载自blog.csdn.net/yongyu_it/article/details/80434416
今日推荐