zoukankan      html  css  js  c++  java
  • Docker

    实际工作中,我们可能需要自己去创建一个docker镜像,下面给大家介绍如何创建docker镜像

    1. 创建一个最简单的镜像

    准备Dockerfile文件
    [root@dockhost ~]# mkdir d1  # 创建一个空的目录,避免被别的文件打扰
    [root@dockhost ~]# cd d1  # 切换到该目录
    [root@dockhost d1]# vi Dockerfile  # 创建一个dockerfile文件,名称必须是Dockerfile

    FROM alpine:latest
    MAINTAINER yb
    CMD echo 'hello, this is my first image'


    命令解释
    FROM alpine:latest 
    # alpine可以看成是一个基础镜像,它是一个极简版的linux系统。我们自定义的镜像一般都要通过FROM关键字指定一个基础镜像
    MAINTAINER yb 
    # 这个指定镜像的所有者,yb是我的名字
    CMD echo 'hello, this is my first image' 
    # CMD是command的简写,后面跟实际执行的命令

    创建镜像
    [root@dockhost d1]# docker build -t hello_docker . #-t后面跟镜像的名字,.表示当前目录下的所有文件

    Sending build context to Docker daemon 2.048 kB
    Step 1/3 : FROM alpine:latest
    Trying to pull repository docker.io/library/alpine ... 
    latest: Pulling from docker.io/library/alpine
    e7c96db7181b: Pull complete 
    Digest: sha256:769fddc7cc2f0a1c35abb2f91432e8beecf83916c421420e6a6da9f8975464b6
    Status: Downloaded newer image for docker.io/alpine:latest
     ---> 055936d39205
    Step 2/3 : MAINTAINER yb
     ---> Running in af8fa3cb5325
     ---> f728cbd10fa7
    Removing intermediate container af8fa3cb5325
    Step 3/3 : CMD echo 'hello, this is my first image'
     ---> Running in 7ac6db88f533
     ---> dd6668981af6
    Removing intermediate container 7ac6db88f533
    Successfully built dd6668981af6


    运行镜像
    [root@dockhost d1]# docker run hello_docker 
    hello, this is my first image

    查看镜像
    [root@dockhost d1]# docker images 

    REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
    hello_docker        latest              dd6668981af6        About a minute ago   5.53 MB
    docker.io/alpine    latest              055936d39205        2 weeks ago          5.53 MB


    从docker build的输出和docker images的显示都可以看到在创建镜像hello_docker的同时还下载了基础镜像alpine

    2. 将基础镜像换成centos,其余不变。来看有啥变化

    [root@dockhost d1]# cd ..  # 切到上一层目录
    [root@dockhost ~]# mkdir d2  # 创建一个空的目录,避免被别的文件打扰
    [root@dockhost ~]# cd d2  # 切换到该目录
    [root@dockhost d2]# vi Dockerfile  # 创建一个dockerfile文件,名称必须是Dockerfile

    FROM centos
    MAINTAINER yb
    CMD echo 'hello, this is my first image'


    [root@dockhost d2]# docker build -t hello_docker2 .  # 创建镜像,名字改成hello_docker2
    省略输出...

    [root@dockhost d2]# docker run hello_docker2  # 运行镜像
    hello, this is my first image

    [root@dockhost d2]# docker images  # 查看镜像

    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    hello_docker2       latest              1787753b5550        2 minutes ago       202 MB
    hello_docker        latest              dd6668981af6        10 minutes ago      5.53 MB
    docker.io/alpine    latest              055936d39205        2 weeks ago         5.53 MB
    docker.io/centos    latest              9f38484d220f        2 months ago        202 MB


    可以看到因为基础镜像的不同,hello_docker与hello_docker2文件尺寸差别好大。具体该选择什么基础镜像呢。
    如果是一个单功能的容器,而且自身对alpine比较熟悉,那就用alpine作为基础镜像,否则就使用centos或者ubuntu,根据自己所运行的操作系统来。

    3. 接下来创建一个复杂的docker镜像

    使用nginx来做测试,从官网下载nginx源码包 http://nginx.org/en/download.html
    [root@dockhost ~]# mdkir mydocker
    [root@dockhost ~]# cd mydocker
    上传安装包到mydocker目录下

    准备Dockerfile
    [root@docker mydocker]# vi Dockerfile

    # base image
    FROM centos
    
    # MAINTAINER
    MAINTAINER yb
    
    # put nginx-1.16.0.tar.gz into /usr/local/src and unpack nginx
    ADD nginx-1.16.0.tar.gz /usr/local/src
    
    # running required command
    RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel 
    RUN yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel
    RUN useradd -M -s /sbin/nologin nginx
    
    # mount a dir to container
    VOLUME ["/data"]
    
    # change dir to /usr/local/src/nginx-1.16.0
    WORKDIR /usr/local/src/nginx-1.16.0
    
    # execute command to compile nginx
    RUN ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module && make && make install
    
    # add path 
    ENV PATH /usr/local/nginx/sbin:$PATH
    
    # expose port
    EXPOSE 80
    
    # add cmd
    ENTRYPOINT ["nginx"]
    
    CMD ["-g","daemon off;"]

    创建镜像
    docker build -t centos_nginx:v1 .

    运行镜像
    docker run -d -p80:80 centos_nginx:v1

    打开网页:输入http://10.40.16.61:80/ #其中10.40.16.61我docker运行的主机

    命令解释
    FROM 选择一个基础镜像
    MAINTAINER 镜像作者
    ADD 添加文件或目录,类似的命令还有COPY,不过ADD功能更加强大,该命令还能拷贝远程的文件到容器中,比如ftp上的文件,而且能自动解压
    RUN 执行命令
    VOLUME 给docker添加一个卷组,卷组的含义类似于给操作系统挂载一个磁盘
    WORKDIR 切换Docker的目录
    ENV 设置环境变量
    EXPOSE 暴露端口
    ENTRYPOINT 容器入口
    CMD 参数,上面例子中ENTRYPOINT+CMD = nginx -g "daemon off;",运行该镜像最后就会执行此条命令

    文章参考:https://www.cnblogs.com/jsonhc/p/7767669.html
    建议大家跟着上面的文章走一遍,可以理解很多命令到底是做什么的。我这里贴出一些我看该篇文章的个人疑惑点,以及好的解答。
    1.daemon on | off是干什么的?
    参考:https://www.cnblogs.com/weifeng1463/p/10277178.html
    意思文章讲的很清楚了,如果想让nginx运行,你必须设置成daemon off

    2.如何进入到container中?
    docker exec -it e09423b0c114 /bin/bash  # e09423b0c114是容器id
    退出就直接使用exit

    3.如何查找docker的VOLUME对应主机的目录?
    docker inspect e09423b0c114  # e09423b0c114是容器id
    返回是一个json字符串,找"Mounts"键
    "Mounts": [
         {
             "Type": "volume",
             "Name": "4d0ff619cee2a21a75af2bea3fe7aa30d4f9fc0bf4612c48c8dced96234cec3e",
             "Source": "/var/lib/docker/volumes/4d0ff619cee2a21a75af2bea3fe7aa30d4f9fc0bf4612c48c8dced96234cec3e/_data",
             "Destination": "/data",
             "Driver": "local",
             "Mode": "",
             "RW": true,
             "Propagation": ""
         }

    4.在文章的v7中,应该是先建一个image再运行。但是我运行了之后发现打不开nginx,目前尚不清楚dockerfile里面有onbuild,nginx就不运行,这个先标注,等将来弄明白了再改这段。大家可以忽略这段。

    4. Dockerfile语法总结

    FROM 指定基础镜像
    RUN 执行命令
    COPY 添加文件
    ADD 添加文件
    EXPOSE 暴露端口
    WORKDIR 指定路径
    MAINTAINER 维护者
    ENV 设定环境变量
    ENTRYPOINT 容器入口
    CMD 执行参数
    USER 指定用户
    VOLUMN 添加卷组

  • 相关阅读:
    hdu4578线段树维护平方和,立方和(加,乘,赋值)或者珂朵莉树
    珂朵莉树(ODT老司机树)
    Codeforces Round #524 (Div. 2)D
    HDU1402 FFT高精度乘法模板题
    中国剩余定理poj1006
    POJ
    Install and Config MySQL 8 on Ubuntu
    Protobuf Examples
    Learning Thrift
    Flask Quickstart
  • 原文地址:https://www.cnblogs.com/ddzj01/p/10955874.html
Copyright © 2011-2022 走看看