zoukankan      html  css  js  c++  java
  • dockerfile编写

    微服务之——dockerfile 编写

     

    一、自定义镜像有两种方法:

    • 1、docker commit

    启动一个容器,增删改查,安装软件,修改配置文件等 ; 另存为一个新镜像

    docker run -it docker.io/centos 启动一个容器

    yum install -y vim net-tools

    ctrl+p+q 退出容器

    docker ps 查看容器ID

    docker commit 容器id 镜像名:标签名

    • 2、编写dockerfile 文件

    Dockerfile 语法格式:

    FROM : 基础镜像

    MAINTAINER: 镜像创建者信息

    EXPOSE: 开放端口

    ENV: 设置变量 (有些服务软件安装需要环境变量)

    ADD: 复制文件到镜像,也可以使用wget功能

    COPY: 类似于add,只不过他不支持wget

    RUN: 制作镜像时执行的命令,可以有多个

    WORKDIR: 定义容器默认工作目录

    CMD: 容器启动执行时的命令,仅可以有一条CMD,如果多条,只有最后一条有效。CMD在docker run时运行。为启动的容器指定默认要运行的程序,程序运行结束,容器也就结束。CMD指令指定的程序可被 docker run 命令行参数中指定要运行的程序所覆盖。

    ENTRYPOINT : ENTRYPOINT的目的和CMD一样,都是在指定容器启动程序及参数,cmd命令可以被docker run覆盖,而他不会被覆盖,而且要比CMD或者docker run指定的命令要靠前执行,需要通过docker run的参数 --entrypoint来指定才行。当指定了ENTRYPOINT后,CMD的含义就发生了改变,不再是直接的运行其命令,而是将CMD的内容作为参数传给ENTRYPOINT指令,换句话说实际执行时,将变为:
    <ENTRYPOINT>"<CMD>"
    上面的意思就是CMD命令会被ENTRYPOINT覆盖掉;但有个特殊的情况,如果CMD的格式是如下格式
    CMD ["<param1>","<param2>",...]
    就是把CMD的参数,当作参数传给ENTRYPOINT命令

    微服务之——dockerfile 编写

     

    -----------------Dockerfile(1)--USER--WORKDIR指令----------------

    [root@localhost docker]# mkdir /data/docker/dockerfile

    [root@localhost docker]# cd /data/docker/dockerfile

    [root@localhost docker]# vim dockerfile

    FROM feixiangkeji974907/nginx:v1.12.2

    USER nginx

    WORKDIR /usr/share/nginx/html

    --------------------构建镜像--------------------------

    docker build -t 镜像名:标签 Dockerfile所在路径

    eg : [root@localhost dockerfile]#docker build -t feixiangkeji974907/nginx:v1.12.2_with_user_workdir /data/docker/dockerfile/dockerfile

    查看镜像:

    [root@localhost dockerfile]# docker images

    微服务之——dockerfile 编写

     

    启动容器

    [root@localhost dockerfile]# docker run --rm -it --name nginx_with_nginx_workdir feixiangkeji974907/nginx:v1.12.2_with_user_workdir /bin/bash

    nginx@
    ab5a49eef0cd:/usr/share/nginx/html$ whoami ---USER 指令,查看当前登录的用户是谁

    nginx

    nginx@
    ab5a49eef0cd:/usr/share/nginx/html$ pwd ---WORKDIR指令,定义容器默认工作目录

    /usr/share/nginx/html

    微服务之——dockerfile 编写

     

    ------------------Dockerfile(2)---ADD-EXPOSE指令-------------------------

    [root@localhost dockerfile]# cp /root/html/index.html /data/docker/dockerfile

    [root@localhost dockerfile]# ls

    dockerfile index.html

    [root@localhost dockerfile]# vim dockerfile

    FROM feixiangkeji974907/nginx:v1.12.2

    ADD /root/html/index.html
    /usr/share/nginx/html/index.html ----复制文件到镜像

    EXPOSE 80 ----指定容器内使用端口

    构建镜像:

    [root@localhost dockerfile]# docker build -t feixiangkeji974907/nginx:v1.12.2_with_add_expose /data/docker/dockerfile

    微服务之——dockerfile 编写

     

    [root@localhost dockerfile]# docker images

    微服务之——dockerfile 编写

     

    启动容器:

    [root@localhost dockerfile]# docker run -d --name nginx_expose -P feixiangkeji974907/nginx:v1.12.2_with_add_expose

    ####说明####

    -P 大写 (Docker 会随机映射一个随机的端口到内部容器开放的网络端口)

    -p 小写 (指定要映射的IP和端口,但是在一个指定端口上只可以绑定一个容器。支持的格式有 hostPort:containerPort

    ip:hostPort:containerPort、 ip::containerPort )

    微服务之——dockerfile 编写

     

    [root@localhost dockerfile]# curl 127.0.0.1:32773

    微服务之——dockerfile 编写

     

    ------------------Dockerfile(3)---RUN-ENV指令-------------------------

    先用 yum list bind --show-duplicates 查看bind 的版本

    [root@localhost dockerfile]# yum list bind --show-duplicates

    Loaded plugins: fastestmirror

    Loading mirror speeds from cached hostfile

    * base: mirrors.aliyun.com

    * epel: mirrors.tuna.tsinghua.edu.cn

    * extras: mirrors.aliyun.com

    * updates: mirrors.aliyun.com

    Available Packages

    bind.x86_64 32:9.11.4-9.P2.el7 base

    当前版本是 9.11.4

    编写 /data/dock/dockerfile

    FROM centos

    ENV VER 9.11.4 #设置环境变量

    RUN yum install bind-$VER -y # 制作镜像时执行的命令,可以有多个

    构建镜像:

    [root@localhost dockerfile]# docker build -t feixiangkeji974907/bind:v9.11.4_with_env_run .

    [root@localhost dockerfile]# docker images

    微服务之——dockerfile 编写

     

    启动容器:

    [root@localhost dockerfile]# docker run -it --name centos_bind feixiangkeji974907/bind:v9.11.4_with_env_run /bin/bash

    ####可以看到容器里的环境变为也存在VER=9.11.4 ;bind软件也已经安装好了

    微服务之——dockerfile 编写

     

    -----------------Dockerfile(4)---CMD指令--------------------

    编写 /data/dock/dockerfile

    FROM centos:7

    RUN yum install httpd -y

    CMD ["httpd", "-D", "FOREGROUND"]

    ### CMD 指令支持 shell 和 exec 两种格式 ,推荐使用exec格式

    shell 格式的话如下:

    CMD echo "hello"

    exec 格式的话如下:

    CMD ["echo","hello"]

    其中 exec 格式必须用双引号,中间用逗号隔开,执行命令 和命令后参数都是用双引号引起来,每个都要用逗号隔开。

    shell 格式和 exec 格式主要区别在于:

    shell 格式的话,实际的命令会被包装为 sh -c 的参数的形式进行执行。比如:

    CMD echo $HOME

    在实际执行中,会将其变更为:

    CMD [ "sh", "-c", "echo $HOME" ]

    构建镜像

    [root@localhost dockerfile]# docker build . -t feixiangkeji974907/centos:7_httpd

    [root@localhost dockerfile]# docker images

    微服务之——dockerfile 编写

     

    启动容器:

    [root@localhost dockerfile]# docker run -d --name myhttpd -p 8888:80 feixiangkeji974907/centos:7_httpd

    微服务之——dockerfile 编写

     

    访问测试:

    微服务之——dockerfile 编写

     

    -----------------Dockerfile(5)---ENTRYPOINT指令--------------------

    编写 /data/dock/dockerfile

    FROM centos:7

    RUN yum install epel-release -q -y && yum install nginx -y

    ENTRYPOINT
    /usr/local/nginx/sbin/nginx && tail -f /etc/passwd #命令不会被docker-run 命令覆盖,ENTRYPOINT/CMD中最后的一个命令必须要是无限执行的命令

    构建镜像

    [root@localhost dockerfile]# docker build -t feixiangkeji/centos:nginx .

    启动容器

    [root@localhost dockerfile]# docker run -d --name centos8_nginx -p 88:80 feixiangkeji/centos:nginx

    微服务之——dockerfile 编写

     

    访问测试:

    微服务之——dockerfile 编写

     

    -----------------Dockerfile 精选案例之基于centos安装httpd--------------------

    FROM docker.io/myos:latest

    MAINTAINER hjk

    RUN yum install httpd

    ENV EnvironmentFile=/etc/sysconfig/httpd

    ENV EnvironmentFile='[ oot@h w] $' #不想显示全路径,只显示当前目录

    WORKDIR /var/www/html/

    ADD index.html index.html

    EXPOSE 80

    EXPOSE 443

    CMD ["httpd", “-DFOREGROUND”]

    上面的环境变量,工作目录,

    启动命令 通过查看软件的service 文件

    find / -name service$ 或者 rpm -ql httpd

    /usr/lib/systemd/system/httpd.service

    启动容器:

    [root@localhost docker]# docker run -itd -p 80:80 -v /www:/var/www/html

    -----------------Dockerfile 精选案例之基于centos安装sshd--------------------

    FROM myos:latest

    RUN yum install -y openssh-server initscripts

    RUN sshd-keygen

    RUN echo "a" | passwd --stdin root

    ENV EnvironmentFile=/etc/sysconfig/sshd

    ENV EnvironmentFile='[ oot@h w] $'

    EXPOSE 22

    CMD ["/usr/sbin/sshd", "-D"]

    构建镜像:

    [root@izivh68d9cis1zz dockerfile]# docker build -t centos:sshd .

    启动容器:

    [root@izivh68d9cis1zz dockerfile]# docker run -d --name centos_sshd -p 2222:22 centos:sshd

    -----------------Dockerfile 精选案例之基于centos源码安装nginx---------------

    [root@izivh68d9cis1zz dockerfile]# mkdir dokcerfile-nginx

    [root@izivh68d9cis1zz dockerfile]# cat demo.od.com.conf

    server{

    listen 80;

    server_name localhost;

    root /usr/local/nginx/html;

    }

    FROM centos:centos7

    ADD nginx-1.14.0.tar.gz . ##从本地导入,也可以从 网络下载nginx源码包(
    http://nginx.org/download/nginx-1.14.0.tar.gz .);会自动解压缩

    RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel

    RUN useradd -M -s /sbin/nologin nginx

    RUN mkdir -p /usr/local/nginx

    RUN cd nginx-1.14.0 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install

    ADD 404.tar.gz /usr/local/nginx/html

    ADD demo.od.com.conf /usr/local/nginx/conf

    EXPOSE 80

    ENTRYPOINT
    /usr/local/nginx/sbin/nginx && tail -f /etc/passwd #命令不会被docker-run 命令覆盖

    构建镜像: docker build -t nginx:latest .

    启动容器: docker run -itd -p 80:80 nginx:latest

    转自:https://m.toutiaocdn.com/group/6825262814672716296/?app=news_article&timestamp=1589193801&use_new_style=1&req_id=202005111843210100180851412A53F925&group_id=6825262814672716296&tt_from=mobile_qq&utm_source=mobile_qq&utm_medium=toutiao_android&utm_campaign=client_share

  • 相关阅读:
    资源网站
    远程服务SSH
    Samba服务配置
    练习raid5
    raid(0、1)
    NFS服务器配置(windows访问)
    LVM磁盘阵列
    ISCSI服务器配置
    Zibbix监控
    MySQL Replication配置
  • 原文地址:https://www.cnblogs.com/nizuimeiabc1/p/13055565.html
Copyright © 2011-2022 走看看