当前位置 博文首页 > 小小之食人魔:用dockerfile创建docker镜像 2019-05-22

    小小之食人魔:用dockerfile创建docker镜像 2019-05-22

    作者:[db:作者] 时间:2021-07-25 12:28

    dockerfile是一个文本文档,通过docker build来读取其中的命令来构建镜像

    目标:

    编辑dockerfile来定义并制作docker镜像。

    准备:

    -docker
    -centos7

    过程

    编写dockerfile文件:
    -制作一个http镜像
    -必须要有一个基础镜像

    [root@server ~]# mkdir dockerfile (工作目录)
    [root@server ~]# cd dockerfile/
    [root@server dockerfile]# ll
    total 8
    -rw-r--r-- 1 root root  78 May 23 11:49 centos.repo
    -rw-r--r-- 1 root root 215 May 23 12:30 dockerfile
    [root@server dockerfile]# cat dockerfile 
    FROM 192.168.200.104:5000/centos:latest(基础镜像)
    MAINTAINER centos7 (作者)
    RUN rm -rf /etc/yum.repos.d/* (构建镜像时执行的命令)
    ADD centos.repo /etc/yum.repos.d/ (添加当前目录的centos.repo到/etc/yum.repos.d/)
    RUN yum -y install httpd
    RUN echo "this centos/http" > /var/www/html/index.html 
    EXPOSE 80 (准备开放的端口号)
    

    制作镜像到仓库:
    -t:设置镜像标签

    [root@server dockerfile]# docker build -t 192.168.200.104:5000/centos/http:latest .
    [root@server dockerfile]# docker images centos/http
    REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
    192.168.200.104:5000/centos/http   latest              42335e60849e        13 seconds ago      276.9 MB
    

    检验:

    运行容器:

    [root@server dockerfile]# docker run -dit --name http -P 192.168.200.104:5000/centos/http:latest 
    dod2f6489cda06d1505f6e772448fcf57c5145793654e2f8430830dc4cf0375b3a
    CONTAINER ID        IMAGE                                        COMMAND                  CREATED             STATUS              PORTS                                            NAMES
    d2f6489cda06        192.168.200.104:5000/centos/http:latest      "/bin/bash"              3 minutes ago       Up 3 minutes        0.0.0.0:32768->80/tcp   
    

    进入容器启动服务:

    [root@server dockerfile]# docker exec -it http bash        
    [root@d2f6489cda06 /]# /sbin/httpd (运行http服务)
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.5. Set the 'ServerName' directive globally to suppress this message
    [root@d2f6489cda06 /]# exit
    

    访问服务:

    [root@server dockerfile]# curl http://192.168.200.104:32768
    this centos/http
    

    成功

    cs
    下一篇:没有了