zoukankan      html  css  js  c++  java
  • docker(二)

    手动制作docker镜像

    手动制作openssh的docker镜像

    • 启动一个容器, 安装服务
    $  docker run -it -p 1022:22 centos:6.9
    $ yum -y install openssh-server
    $ /etc/init.d/sshd start
    $ echo 123456|passwd --stdin root  # 此时输入ssh root@ip 1022 可以远程容器
    

    • 把刚才安装好服务的容器, 提交为镜像

      docker commit <镜像名>

    $ docker commit 05b05cf43d2c centos6.9_ssh:v1
    $ docker images centos6.9_ssh
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    centos6.9_ssh       v1                  85cb44e9a390        2 minutes ago       323MB
    

    • 测试

      /usr/sbin/sshd -D 前台启动sshd服务, 会夯住

    $ docker run -d -p 1322:22 centos6.9_ssh:v1 /usr/sbin/sshd -D
    

    输入 ssh root@192.168.221.10 1222, 成功登陆


    手动制作http和ssh的docker镜像, 并且自定制密码

    • 启动一个容器, 安装服务
    $ docker run -it -p 1422:22 -p 8888:80 --name http_server centos6.9_ssh:v1
    $ yum -y install openssh-server
    $ /etc/init.d/sshd start
    $ yum -y install httpd
    $ /etc/init.d/httpd start
    

    • 使用linux进行测试ssh
    $ ssh root@192.168.221.10 -p 1422
    

    • 使用linux测试http服务
    $ curl -I 192.168.221.10:8888
    HTTP/1.1 403 Forbidden
    Date: Thu, 28 Nov 2019 12:27:28 GMT
    Server: Apache/2.2.15 (CentOS)
    Accept-Ranges: bytes
    Content-Length: 4961
    Connection: close
    Content-Type: text/html; charset=UTF-8
    

    • 把刚才安装好服务的容器, 提交为镜像

      docker commit <镜像名>

    $ docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                        NAMES
    e84010fa4dbb        centos6.9_ssh:v1    "/bin/bash"         43 hours ago        Up 19 minutes       0.0.0.0:1422->22/tcp, 0.0.0.0:8888->80/tcp   http_server
    
    $ docker commit e84010fa4dbb http_server_images
    sha256:9fce8392068370a0c16c70f389b18340778bb3721bf3469daf922c2982d9cace
    
    $ docker images http_server_images
    REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
    http_server_images   latest              9fce83920683        46 seconds ago      413MB
    

    • 编写脚本
    $ vim init.sh
    #!/bin/bash
    echo $SSH_PWD|passwd --stdin root
    /etc/init.d/httpd start
    /usr/sbin/sshd -D
    
    $ chmod +x init.sh
    

    • 提交为镜像
    $ docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                        NAMES
    e84010fa4dbb        centos6.9_ssh:v1    "/bin/bash"         44 hours ago        Up About an hour    0.0.0.0:1422->22/tcp, 0.0.0.0:8888->80/tcp   http_server
    $ docker commit e84010fa4dbb http_server_images:v5
    

    • 启动并新建容器
    $ docker run -d  --env "SSH_PWD=123" -p 4022:22 -p 90:80  --name http_server_v5 http_server_images:v5   /init.sh
    

    dockerfile自动构建docker


    dockerfile主要组成部分

    • 基础镜像信息

      FROM: centos:6.9


    • 制作镜像操作指令

      RUN yum install openssh-server -y


    • 容器启动时执行命令

      CMD ["/bin/bash"]


    dockerfile常用指令

    • FROM

      指定基础镜像


    • MAINTAINER

      指定维护者信息, 可以没有


    • LABEL

      写一些镜像的提示信息


    • RUN

      在命令前加上RUN


    • ADD

      COPY文件, 会自动解压


    • COPY

      复制文件


    • ENV

      环境变量


    • ENTRYPOINT

      容器启动后执行的命令(无法被替换, 启动容器的时候指定的命令, 会被当成参数)


    • WORKDIR

      设置当前工作目录


    • VOLUME

      设置卷, 挂载主机目录


    • EXPOSE

      指定对外的端口(-P 随机端口)


    • CMD

      指定容器启动后要干的事情


    创建一个简单的dockerfile

    • 创建文件
    $ mkdir -p /opt/dockerfile/centos6.9_ssh
    $ vim /opt/dockerfile/centos6.9_ssh/dockerfile
    FROM centos:6.9
    RUN yum -y install openssh-server 
    RUN /etc/init.d/sshd start && echo 123456 |passwd --stdin root
    CMD ["/usr/sbin/sshd", "-D"]
    
    

    • 构建镜像
    $ docker image build -t centos6.9_ssh:v2 /opt/dockerfile/centos6.9_ssh/
    

    • 查看镜像
    $ docker images
    REPOSITORY                   TAG                 IMAGE ID            CREATED              SIZE
    centos6.9_ssh                v2                  523057254626        About a minute ago   323MB
    centos6.9_ssh                v1                  85cb44e9a390        45 hours ago         323MB
    

    • 查看构建镜像的流程
    $ docker image history centos6.9_ssh:v2
    IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
    523057254626        2 minutes ago       /bin/sh -c #(nop)  CMD ["/usr/sbin/sshd" "-D…   0B                  
    297dfb187996        2 minutes ago       /bin/sh -c /etc/init.d/sshd start && echo 12…   5.45kB              
    76120e8ba1c7        11 minutes ago      /bin/sh -c yum -y install openssh-server        128MB               
    2199b8eb8390        8 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
    <missing>           8 months ago        /bin/sh -c #(nop)  LABEL name=CentOS Base Im…   0B                  
    <missing>           8 months ago        /bin/sh -c #(nop) ADD file:0e6d175401c5b4260…   195MB      
    

    • 新建并启动容器
    $ docker run -d -p 1022:22 centos6.9_ssh:v2
    

    • 测试
    $ ssh root@192.168.221.10 -p 1022
    

    创建一个支持sshd和httpd双服务的镜像

    • 创建文件和脚本
    $ vim /opt/dockerfile/centos6.9_ssh_httpd/dockerfile 
    FROM centos:6.9
    RUN yum -y install openssh-server 
    RUN /etc/init.d/sshd start
    RUN echo 123456 |passwd --stdin root
    RUN yum -y install httpd
    EXPOSE 80 22 
    ADD init.sh /init.sh
    CMD ["/bin/bash", "/init.sh"]
    
    $ vim /opt/dockerfile/centos6.9_ssh_httpd/init.sh 
    #!/bin/bash
    /etc/init.d/httpd start
    /usr/sbin/sshd -D
    

    • 构建镜像
    $ docker build -t centos_ssh_httpd:v1 /opt/dockerfile/centos6.9_ssh_httpd/
    

    • 基于镜像创建并启动容器
    $ docker run -d -p 1122:22 -p 88:80 --name ssh_http_server centos_ssh_httpd:v1
    
    $ docker ps -l
    CONTAINER ID        IMAGE                 COMMAND                CREATED             STATUS              PORTS                                      NAMES
    bbca28761e60        centos_ssh_httpd:v1   "/bin/bash /init.sh"   2 minutes ago       Up 2 minutes        0.0.0.0:1122->22/tcp, 0.0.0.0:88->80/tcp   ssh_http_server
    

    • 容器内测试
    $ ssh root@docker01 -p 1122
    $ /etc/init.d/httpd status
    httpd (pid  14) is running...
    $ /etc/init.d/sshd status
    openssh-daemon (pid  19) is running...
    

    • 宿主机测试
    $ curl -I docker01:88
    HTTP/1.1 403 Forbidden
    Date: Thu, 28 Nov 2019 18:10:48 GMT
    Server: Apache/2.2.15 (CentOS)
    Accept-Ranges: bytes
    Content-Length: 4961
    Connection: close
    Content-Type: text/html; charset=UTF-8
    

    写一个http网页, 使用dockerfile部署镜像

    • 清空容器
    $ docker rm -f `docker ps -a -q`
    

    • 创建文件和脚本
    $ mkdir /opt/dockerfile/php
    $ cd /opt/dockerfile/php/
    $ vim dockerfile 
    FROM centos:6.9
    RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo && 
    yum -y install php php-cli openssh-server
    WORKDIR /var/www/html
    RUN echo '<?php phpinfo(); ?>' > index.php
    ADD init.sh /init.sh
    EXPOSE 80 22
    CMD ["/bin/bash", "/init.sh"]
     
    $ vim init.sh 
    #!/bin/bash
    service httpd start
    /usr/sbin/sshd -D
    

    • 构建镜像
    $ docker build -t php:v3 /opt/dockerfile/php/
    docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    php                 v3                  23ad7e7c60ef        2 minutes ago       339MB
    php                 v2                  5d91294e82d0        7 minutes ago       339MB
    php                 v1                  9bc9b381e2e0        5 hours ago         339MB     
    

    • 基于镜像创建并启动容器
    $ docker run -d -P --name php_v3 php:v3
    
    $ docker ps
    CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                          NAMES
    18f48fb9863c        php:v3              "/bin/bash /init.sh"   3 minutes ago       Up 3 minutes        0.0.0.0:32775->22/tcp, 0.0.0.0:32774->80/tcp   php_v3
    

    • 测试


    启动容器时候手动执行的命令不生效

    $ vim dockerfile 
    FROM centos:6.9
    RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo && 
    yum -y install php php-cli openssh-server
    WORKDIR /var/www/html
    RUN echo '<?php phpinfo(); ?>' > index.php
    ADD init.sh /init.sh
    EXPOSE 80 22
    # CMD ["/bin/bash", "/init.sh"]
    ENTRYPOINT ["/bin/bash", "/init.sh"]
    
    $ vim init.sh 
    #!/bin/bash
    service httpd start
    /usr/sbin/sshd -D
    
    $ docker build -t php:v4 /opt/dockerfile/php/
    
    $ docker run -d -P php:v4 /bin/bash
    
    $ docker ps -l --no-trunc
    CONTAINER ID                                                       IMAGE               COMMAND                          CREATED             STATUS              PORTS                                          NAMES
    060e96e713c5c0c9a236e1b57c5cba6776bb8a64096d00b5c3196107af35b3be   php:v4              "/bin/bash /init.sh /bin/bash"   27 seconds ago      Up 26 seconds       0.0.0.0:32777->22/tcp, 0.0.0.0:32776->80/tcp   vigorous_blackwell
    

    • 如果使用CMD来执行命令, 则启动容器时候指定命令, CMD里面的命令会被替换
    $ docker run -d -P --name php_by_CMD php:v3 /bin/bash
    $ docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                          PORTS                                          NAMES
    136a891f58dc        php:v3              "/bin/bash"              33 seconds ago       Exited (0) 32 seconds ago                                                      php_by_CMD
    

    启动容器指定密码, 如果不指定用默认密码登录

    $ vim dockerfile 
    FROM centos:6.9
    RUN curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo && 
    yum -y install php php-cli openssh-server
    WORKDIR /var/www/html
    RUN /etc/init.d/sshd start
    ENV SSH_PWD=123
    RUN echo '<?php phpinfo(); ?>' > index.php
    ADD init.sh /init.sh
    EXPOSE 80 22
    # CMD ["/bin/bash", "/init.sh"]
    ENTRYPOINT ["/bin/bash", "/init.sh"]
    
    $ vim init.sh 
    #!/bin/bash
    echo $SSH_PWD |passwd --stdin root
    service httpd start
    /usr/sbin/sshd -D
    

    • 构建镜像
    $ docker build -t php:v6 /opt/dockerfile/php/
    

    • 新建并启动容器
    $ docker run -d -P --name php_default3 --env "SSH_PWD=weixinyu" php:v6
    $ docker run -d -P  --env "SSH_PWD=weixinyu" --name php_by_def php:v6
    

    • 测试
    $ docker ps
    CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                                          NAMES
    988ac431195c        php:v6              "/bin/bash /init.sh"   3 minutes ago       Up 3 minutes        0.0.0.0:32777->22/tcp, 0.0.0.0:32776->80/tcp   php_by_def
    da9d59e81afa        php:v6              "/bin/bash /init.sh"   7 minutes ago       Up 7 minutes        0.0.0.0:32775->22/tcp, 0.0.0.0:32774->80/tcp   php_default3
    
    $ hostname -i
    192.168.221.10
    
    $ ssh root@192.168.221.10 -p 32776
    $ ssh root@192.168.221.10 -p 32774
    

    docker镜像的分层



    docker组件之间的关系

  • 相关阅读:
    hdu 3068 最长回文
    Educational Codeforces Round 1 C. Nearest vectors
    Educational Codeforces Round 6 C. Pearls in a Row
    poj 3304 Segments
    Toy Storage
    poj 2318 TOYS
    CFA二级中文精讲(第2版)
    探秘大香格里拉
    巴西:热辣里约
    巴西:性感圣保罗
  • 原文地址:https://www.cnblogs.com/cjwnb/p/11939452.html
Copyright © 2011-2022 走看看