Dockerfile 构建镜像

1、Dockerfile相关参数

FROM            构建新镜像是基于哪个镜像
LABEL           标签
RUN            构建镜像时运行的Shell命令
COPY            拷贝文件或目录到镜像中
ENV             设置环境变量
USER            为RUN、CMD和ENTRYPOINT执行命令指定运行用户
EXPOSE           声明容器运行的服务端口
WORKDIR          为RUN、CMD、ENTRYPOINT、COPY和ADD设置工作目录
ENTRYPOINT       运行容器时执行,如果有多个ENTRYPOINT指令,最后一个生效
CMD              运行容器时执行,如果有多个CMD指令,最后一个生效

1.1构建Nginx镜像

[root@k8s-node1 ~]# mkdir nginx
[root@k8s-node1 ~]# cd nginx/
[root@k8s-node1 nginx]# cat Dockerfile #yum安装Nginx
# 名称:容器化的Nginx
# 用途:Web服务
# 创建时间:2018.06.11
FROM centos
LABEL MAINTAINER q
RUN yum install epel-release -y
RUN yum install -y nginx
EXPOSE 80
WORKDIR /usr/local
CMD ["nginx", "-g", "daemon off;"]

#daemon off; 是在前台运行
#docker 容器默认会把容器内部第一个进程,也就是pid=1的程序作为docker容器是否正在运行的依据,如果docker 容器pid挂了,那么docker容器便会直接退出。

 -t 指定名称  -f指定Dockerfile  . 在dockerfile 上下文,. 代表当前目录

[root@k8s-node1 nginx]# docker build -t  nginx:v1 -f Dockerfile .  

查看镜像

[root@k8s-node1 nginx]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
nginx                   v1                  4ab138f8031a        2 minutes ago       346MB

启动容器

[root@k8s-node1 nginx]# docker run -d -p 8887:80 nginx:v1
dc2e3f33171f49fb8ae9b9ddd21b1a0d1d46d33b37d31350b19e6cadef9e6be1

查看

[root@k8s-node1 nginx]# docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
dc2e3f33171f        nginx:v1            "nginx -g 'daemon of…"   19 seconds ago      Up 18 seconds       0.0.0.0:8887->80/tcp   sad_hopper

访问

源码编译安装

FROM centos:7
LABEL maintainer www.ctnrs.com
RUN yum install -y gcc gcc-c++ make \
    openssl-devel pcre-devel gd-devel \
    iproute net-tools telnet wget curl && \
    yum clean all && \
    rm -rf /var/cache/yum/*

COPY nginx-1.15.5.tar.gz /
RUN tar zxf nginx-1.15.5.tar.gz && \
    cd nginx-1.15.5 && \
    ./configure --prefix=/usr/local/nginx \
    --with-http_ssl_module \
    --with-http_stub_status_module && \
    make -j 4 && make install && \
    rm -rf /usr/local/nginx/html/* && \
    echo "ok" >> /usr/local/nginx/html/status.html && \
    cd / && rm -rf nginx* && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENV PATH $PATH:/usr/local/nginx/sbin
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
WORKDIR /usr/local/nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

2、构建PHP镜像

[root@k8s-node1 dockerfile]# cd php/
[root@k8s-node1 php]# ls
Dockerfile  php-5.6.36.tar.gz  php-fpm.conf  php.ini
[root@k8s-node1 php]# cat Dockerfile 
FROM centos:7
MAINTAINER www.ctnrs.com
RUN yum install epel-release -y && \
    yum install -y gcc gcc-c++ make gd-devel libxml2-devel \
    libcurl-devel libjpeg-devel libpng-devel openssl-devel \
    libmcrypt-devel libxslt-devel libtidy-devel autoconf \
    iproute net-tools telnet wget curl && \
    yum clean all && \
    rm -rf /var/cache/yum/*

COPY php-5.6.36.tar.gz /
RUN tar zxf php-5.6.36.tar.gz && \
    cd php-5.6.36 && \
    ./configure --prefix=/usr/local/php \
    --with-config-file-path=/usr/local/php/etc \
    --enable-fpm --enable-opcache \
    --with-mysql --with-mysqli --with-pdo-mysql \
    --with-openssl --with-zlib --with-curl --with-gd \
    --with-jpeg-dir --with-png-dir --with-freetype-dir \
    --enable-mbstring --with-mcrypt --enable-hash && \
    make -j 4 && make install && \
    cp php.ini-production /usr/local/php/etc/php.ini && \
    cp sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf && \
    sed -i "90a \daemonize = no" /usr/local/php/etc/php-fpm.conf && \
    mkdir /usr/local/php/log && \
    cd / && rm -rf php* && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENV PATH $PATH:/usr/local/php/sbin
COPY php.ini /usr/local/php/etc/
COPY php-fpm.conf /usr/local/php/etc/
WORKDIR /usr/local/php
EXPOSE 9000
CMD ["php-fpm"]

3、构建tomcat镜像

[root@k8s-node1 tomcat]# ls
apache-tomcat-8.5.43.tar.gz  Dockerfile
[root@k8s-node1 tomcat]# cat Dockerfile 
FROM centos:7
MAINTAINER www.ctnrs.com

ENV VERSION=8.5.43

RUN yum install java-1.8.0-openjdk wget curl unzip iproute net-tools -y && \
    yum clean all && \
    rm -rf /var/cache/yum/*

COPY apache-tomcat-${VERSION}.tar.gz /
RUN tar zxf apache-tomcat-${VERSION}.tar.gz && \
    mv apache-tomcat-${VERSION} /usr/local/tomcat && \
    rm -rf apache-tomcat-${VERSION}.tar.gz /usr/local/tomcat/webapps/* && \
    mkdir /usr/local/tomcat/webapps/test && \
    echo "ok" > /usr/local/tomcat/webapps/test/status.html && \
    sed -i '1a JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"' /usr/local/tomcat/bin/catalina.sh && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ENV PATH $PATH:/usr/local/tomcat/bin

WORKDIR /usr/local/tomcat

EXPOSE 8080
CMD ["catalina.sh", "run"]

 查看镜像

[root@k8s-node1 dockerfile]# docker images
REPOSITORY                       TAG                             IMAGE ID            CREATED             SIZE
192.168.146.130/library/tomcat   v1                              0df1bc3997ff        12 hours ago        443MB
tomcat                           v1                              0df1bc3997ff        12 hours ago        443MB
php                              v1                              ba1480e952c7        12 hours ago        512MB
192.168.146.130/library/nginx    v1                              4ab138f8031a        4 days ago          346MB

 

posted @ 2019-12-06 01:32  w787815  阅读(328)  评论(0编辑  收藏  举报