dicker 镜像(images)

                docker 镜像(images)

docker 架构体系图

dicker 镜像(images)

dockerfile:制作镜像

我们在容器运行一个镜像,如果本地没有会自动下载

最小镜像:hello-world   dockerfile

FROM scratch          //抓取,挠,从无到有创建的一个过程

COPY hello /           //镜像所要进行的操作

CMD ["/hello"]        //给了一个shell 环境

dockerfile 的缓存特性:如果在相同层中。需要用到之前缓存过的镜像,就无须重新下载,但如果此镜像层上层发生变化,即使是在相同层,也用不了缓存。

通过dockerfile制作的镜像,能够很明显的看到镜像每一层的操作,安全性高,并且可移植操作强。

查看ifconfig由那个软件包含有的命令

[[email protected] /]# yum provides ifconfig

下载支持ifconfig的软件包

[[email protected] /]# yum -y install net-tools

下载一个最小的镜像(hello-world)

[[email protected] /]# docker pull hello-world

[[email protected] /]# docker images

dicker 镜像(images)

编写dockerfile

1)编写docker镜像

[[email protected] /]# vim Dockerfile

编写以下文件

FROM centos:7                     //基于centos7镜像上面运行一个容器

RUN yum -y install vim

RUN yum -y install net-tools

RUN yum -y install tomcat

CMD ["/bin/bash"]                 //给了一个shell 环境

运行文件, . :是指在当前路径下查找文件

[[email protected] ~]]# docker build -t test:latest .

[[email protected] ~]# docker images

dicker 镜像(images)

dicker 镜像(images)

再次添加一个镜像

构建一个文件

[[email protected] ~]# mkdir test

[[email protected] ~]# cd test/

[[email protected] test]# vim Dockerfile

编写下面文件

FROM centos:7

RUN yum -y install vim

RUN yum -y install net-tools

RUN yum -y install httpd

CMD ["/bin/bash"]             

运行dockerfile

[[email protected] test]# docker build -t test1:latest .

[[email protected] test]# docker images

dicker 镜像(images)

dicker 镜像(images)

dockerfile 的缓存特性:如果在相同层中。需要用到之前缓存过的镜像,就无须重新下载,但如果此镜像层上层发生变化,即使是在相同层,也用不了缓存

再次创建一个镜像名称为test2:

要求:

下载vim

下载httpd

下载net-tools

就是和刚刚的顺序打乱一下看看是否会用到缓存

创建dockerfile

[[email protected] ~]# mkdir abc

[[email protected] ~]# cd abc

[[email protected] abc]# vim Dockerfile

添加以下文件

FROM centos:7

RUN yum -y install vim

RUN yum -y install httpd

RUN yum -y install net-tools

CMD ["/bin/bash"]

运行镜像

[[email protected] abc]# docker build -t test2:latest .

dicker 镜像(images)

再次创建一个镜像名称为test3:

要求:

下载vim

下载elinks

下载net-tools

就是和刚刚的顺序打乱一下看看是否会用到缓存

1)创建dockerfile

[[email protected] ~]# mkdir aa

[[email protected] ~]# cd aa/

[[email protected] aa]# vim Dockerfile

添加以下文件

FROM centos:7

RUN yum -y install vim

RUN yum -y install elinks

RUN yum -y install net-tools

CMD ["/bin/bash"]

2)运行镜像

[[email protected] aa]# docker build -t test3:latest .

dicker 镜像(images)

这里可以看出并没有用到缓存,而是重新下载

再次创建一个镜像名称为test4:

要求:

下载vim

下载elinks

下载net-tools

将宿主机上的test.txt文件,保存到镜像镜像

根据此镜像运行一个容器,查看是否有相应的文件

1)创建dockerfile

[[email protected] ~]# mkdir ab

[[email protected] ~]# cd ab

[[email protected] ab]# touch test.txt

[[email protected] ab]# echo 123456 > test.txt

[[email protected] ab]# vim Dockerfile

添加以下文件

FROM centos:7

RUN yum -y install vim

RUN yum -y install elinks

RUN yum -y install net-tools

COPY test.txt /

CMD ["/bin/bash"]

2)运行镜像

[[email protected] ab]# docker build -t test4:latest .

dicker 镜像(images)

基于test4的镜像运行一个容器看看是否有test.txt的文件

[[email protected] ~]# docker run -it --name test4 test4

[[email protected] /]# cat test.txt

dicker 镜像(images)

查看docker镜像的历史命令,可以看出这个镜像做了什么

[[email protected] ~]# docker history test4:latest

dicker 镜像(images)

切换目录,在dockerfile文件中添加WORKDIR,就可以切换到其他目录

比如:

dicker 镜像(images)

写一个dockerfie,然后通过此文件构建镜像,要求如下:

  1. 基于centos镜像,部署安装nginx服务。
  2. 基于以上镜像,运行一个容器,并将此容器提供的web服务的默认访问界面内容更改为:This is a testweb in container.
  3. 在dockerhost上访问此容器,验证页面是否更改成功。

实验如下:

上传安装包至docker系统上

dicker 镜像(images)

  1. 编写dockerfile文件

添加以下内容

FROM centos

COPY nginx-1.17.3.tar.gz /

RUN yum -y install gcc

RUN yum -y install openssl-devel

RUN yum -y install pcre-devel

RUN yum -y install zlib-devel

RUN yum -y install make

RUN useradd -M -s /sbin/nologin nginx

RUN tar zxf /nginx-1.17.3.tar.gz

WORKDIR /nginx-1.17.3/

RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install

RUN ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

RUN nginx

CMD ["/bin/bash"]

运行镜像

[[email protected] ~]# docker build -t zhouchuan:latest .

dicker 镜像(images)

查看镜像是否成功

[[email protected] ~]# docker images

dicker 镜像(images)

基于以上镜像,运行一个容器,并将此容器提供的web服务的默认访问界面内容更改为:This is a testweb in container.

[[email protected] ~]# docker run -it --name zhouchuan zhouchuan

[[email protected] nginx-1.17.3]# echo This is a testweb in container > /usr/local/nginx/html/index.html

[[email protected] nginx-1.17.3]# curl 127.0.0.1

This is a testweb in container

dicker 镜像(images)

在dockerhost上访问此容器,验证页面是否更改成功。

查看容器内的ip地址

dicker 镜像(images)

在docker上面访问

dicker 镜像(images)

 

用history 命令查看一下镜像的过程

[[email protected] ~]# docker history zhouchuan:latest

dicker 镜像(images)