美文网首页
docker 镜像制作方法

docker 镜像制作方法

作者: 章工运维 | 来源:发表于2019-12-27 13:41 被阅读0次

    Docker Image的制作两种方法

    方法1 docker commit # 保存 container 的当前状态到 image 后,然后生成对应的 image

    方法2 docker build # 使用 Dockerfile 文件自动化制作 image

    方法 1 docker commit

    创建一个安装好apache 工具的容器镜像

    [root@test ~]# docker run -it docker.io/centos:latest /bin/bash

    [root@1d3563200047 /]# yum -y install httpd # 在 container 中安装 apache 软件包

    [root@1d3563200047 /]# exit

    查看

    images 列表

    [root@test ~]# docker images

    根据容器当前状态做一个image 镜像:创建一个安装了 apache 工具的 centos 镜像

    语法:

    docker commit <container 的 ID> 或 <image_

    例:查看容器ID

    [root@test ~]# docker ps -a

    [root@test ~]# docker commit bbd01c4b8567 docker.io/centos:apache

    sha256:e5917c01599c70d0680beeb35f6df98889dd22106399efd6907d956d8a943242

    [root@test ~]# docker images

    使用新创建的docker.io/centos apache 镜像,生成一台容器实例:

    镜像,生成一台容器实例:

    [root@test ~]# docker run it centos:apache /bin/bash

    [root@8b1afc920454 /]# rpm -qa httpd 查看,已经安装好 apache 命令httpd2.4.6 67.el7.centos.2.x86_64

    注:说明基于apache 镜像的容器创建成功。

    方法二:通过: docker build 创建一个基于 centos 的 httpd web 服务器镜像。

    使用docker build 创建镜像时,需要使用 Dockerfile 文件自动化制作 image 镜像

    注:Dockerfile 有点像源码编译时 ./configure 后产生的 Makefile

    1、创建工作目录

    [root@test ~]# mkdir /docker-build

    [root@test ~]# cd /docker-build

    [root@test docker-build]# touch Dockerfile

    注:make 自动化编译时需要 Makefile 文件,自动化创建 docker 镜像时,需要 Dockerfile

    2、编辑 Dockerfile

    Dockerfile

    用来创建一个自定义的 image, 包含了用户指定的软件依赖等。

    [root@test docker-build]# vim Dockerfile

    FROM docker.io/centos:latest

    MAINTAINER <mk xuegod.cn>

    RUN yum -y install httpd

    ADD start.sh /usr/local/bin/start.sh

    ADD index.html /var/www/html/index.html

    注释:

    FROM docker.io/centos:latest FROM 基于哪个镜像

    MAINTAINER < mk xuegod.cn> MAINTAINER 镜像创建者

    RUN yum -y install httpd RUN 安装软件用

    ADD start.sh /usr/local/bin/start.sh

    ADD index.html /var/www/html/index.html

    ADD 将文件 < 拷贝到 新产生的镜像 的文件系统对应的路径 < 。 所有拷贝到 新镜像 中的

    文件和文件夹权限为 0755,uid 和 gid 为 0

    CMD echo hello world

    container 启动时执行的命令 或启动服务 ,但是一个 Dockerfile 中只

    能有一条 CMD 命令,多条则只执行最后一条 CMD.

    如:dockefile1 中的内容如下:

    vim dockefile1

    FROM ubuntu

    MAINTAINER xxx

    RUN echo hello1 > test1.txt

    RUN echo hello2 > /test2.txt

    EXPOSE 80

    EXPOSE 81

    CMD ["/bin/bash"]

    创建 start.sh 脚本启动 httpd 服务和 apache 默认首页 index.html 文件

    [root@test docker-build]# echo "/usr/sbin/httpd DFOREGROUND" > start.sh

    注:/usr/sbin/httpd DFOREGROUND 相当于执行了 systemctl start httpd

    [root@test docker-build]# chmod a+x start.sh

    创建index.html

    [root@test docker-build]# echo "docker image build test" > index.html

    使用命令 build 来创建新的 image

    语法:

    docker build -t 父镜像名:镜像的 tag Dockerfile 文件所在路径

    -t : 表示 tage ,镜像名

    例:使用命令 docker build 来创建新的 image, 并命名为 docker.io/centos:httpd

    [root@test docker-build]# docker build t docker.io/centos:httpd ./

    注:./ 表示当前目录。另外你的 当前目录下 要 包含 Dockerfile

    Sending build context to Docker daemon 4.096 kB

    Step 1 : FROM docker.io/centos:latest

    ------> 0f0be3675e bb

    Step 2 : MAINTAINER userabc

    mk xuegod.cn

    ------> Using cache

    ------> 9d1cc5ad2a7b

    Step 3 : RUN yum -y install httpd

    。。。

    Complete!

    ------> bce6b3f0a700

    Removing intermediate container c9567092d67b

    Step 4 : ADD

    start.sh /usr/local/bin/start.sh

    ------> 521463f9bbeb

    Removing intermediate container 18b34849606d

    Step 5 : ADD index.html /var/www/html/index.html

    ------> 585eb8e1d7ad

    Removing intermediate container ecdbd06a3c1e

    Successfully built 585eb8e1d7ad

    查看

    images 列表

    [root@test ~]# docker images

    注:docker镜像=应用/程序+库

    Docker Image 的发布:

    方法1 Save Image To TarBall

    方法2 Push Image To Docker Hub

    方法1 Save Image To TarBall

    保存Image 到 tar 包

    语法:docker save -o 导出的镜像名 .tar 本地镜像名:镜像标签

    [root@test ~]# docker save -o docker.io-centos-httpd-docker-image.tar docker.io/centos:httpd

    [root@test ~]# ll -h

    -rw------- 1 root root 319M 12月 27 22:48 docker.io-centos-httpd-docker-image.tar

    例:

    使用导入本地镜像:[root@test ~]# docker rmi docker.io/centos:httpd

    删除镜像,这里写自己镜像的 ID或名字

    [root@test ~]# docker images

    [root@test docker-build]# docker load i centos httpd docker image.tar

    方法

    2 Push Image To Docker Hub 发布到外网

    1、 Signup on docker hub & create repo 注册一个帐号

    https://hub.docker.com/

    2、 Login to docker hub

    # docker login -u userabc p abc123 e userab@gmail.com

    3、 Push image to docker hub # 上传镜像

    #docker push centos:httpd

    4、 Pull image from docker hub # 下载镜像

    # docker pull userabc/centos:httpd #用户名 镜像名

    Container 容器端口映射

    实战: Container 端口 映射

    启动container

    [root@test ~]# docker run -d p 80:80 docker.io/centos:httpd /bin/bash c

    /usr/local/bin/start.sh

    87fadc0249a96736f588f16b7d3ad662ef3536a06d7a74115cd7c76546ed3a22

    注:-p 物理机的 80 端口 容器实例的 80 端口 ,把容器中的 80 端口映射到物理机上的 80 端口

    在物理机上查看容易状态:

    [root@test ~]# docker ps

    查看物理机上开启的

    80 代 理端口

    [root@test ~]# netstat -antup | grep 80

    tcp6 0 0 :::80 :::* LISTEN    50768/ docker proxy

    注:现在docker 实例运行的网络模式像 VMware 的 NAT 模式。 后期会讲给 docker 配置静态 IP就像 VMware 的桥接模式。

    访问正在运行的 container 容器实例

    语法docker exec -it <container id | name> /bin/bash

    [root@test ~]# docker ps

    [root@test ~]# docker exec -it 87fadc0249a9 /bin/bash 进入容器

    创建

    test.html 文件

    [root@87fadc0249a9 /]# echo xuegod > /var/www/html/test.html

    [root@87fadc0249a9 /]#

    测试:在物理机上查看新添加的test.html 文件

    [root@test ~]# curl http://192.168.1.6 /test.html

    相关文章

      网友评论

          本文标题:docker 镜像制作方法

          本文链接:https://www.haomeiwen.com/subject/cdtdoctx.html