Linux运维-陈雷雷


基于centos7制作docker镜像_构建nginx镜像,构建php镜像,构建tomcat镜像,构建java镜像

admin 2020-03-18 618浏览 4条评论
首页/正文
分享到: / / / /

基于centos7制作docker镜像

docker镜像是企业非常常用的一种应用打包,应用交付的方式. docker天生优势,一处构建处处运行,在任何机器构建的服务均可以在任意一台安装有docker的主机上运行

1. docker构建nginx镜像

1. 首先安装centos7平台环境
docker run -itd --name centos7 cenots:7
docker run -it --name centos7 centos:7
2. 进入cenots7
docker  exec -it centos7 bash

3. 安装yum源

curl  -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install epel-release
yum clean all

3. 安装nginx依赖包
yum install -y pcre pcre-devel openssl openssl-devel \
    gd-devel  zlib-devel gcc net-tools iproute telnet wget curl && \
rm -rf /var/cache/yum/*

4. 安装nginx
useradd  www -u 1200 -M -s /sbin/nologin
wget https://www.chenleilei.net/soft/nginx-1.16.1.tar.gz
tar xf nginx-1.16.1.tar.gz && \
cd  nginx-1.16.1 && \
    ./configure --prefix=/usr/local/nginx --with-http_image_filter_module --user=www --group=www \
    --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module  && \
make -j 4 && make install && \
rm -rf /usr/local/nginx/html/*  && \
echo "leilei hello" >/usr/local/nginx/html/index.html  && \
cd / && rm -rf nginx* && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

--------------------------------------------------
5. 书写nginx  docker file
vim dockerfile-nginx
---------------------------------------------------------------
FROM centos:7
LABEL maintainer www.chenleilei.net
RUN useradd  www -u 1200 -M -s /sbin/nologin
RUN mkdir -p /var/log/nginx
RUN yum install -y cmake pcre pcre-devel openssl openssl-devel gd-devel \
    zlib-devel gcc gcc-c++ net-tools iproute telnet wget curl &&\
    yum clean all && \
    rm -rf /var/cache/yum/*
RUN wget https://www.chenleilei.net/soft/nginx-1.16.1.tar.gz
RUN tar xf nginx-1.16.1.tar.gz
WORKDIR nginx-1.16.1
RUN ./configure --prefix=/usr/local/nginx --with-http_image_filter_module --user=www --group=www \
    --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module \
    --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log \
    --pid-path=/var/run/nginx/nginx.pid
RUN make -j 4 && make install && \
    rm -rf /usr/local/nginx/html/*  && \
    echo "leilei hello" >/usr/local/nginx/html/index.html  && \
    rm -rf nginx* && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\
    ln -sf /dev/stdout /var/log/nginx/access.log && \
    ln -sf /dev/stderr /var/log/nginx/error.log
RUN chown -R www.www /var/log/nginx
ENV LOG_DIR /var/log/nginx
ENV PATH $PATH:/usr/local/nginx/sbin
COPY nginx.conf /usr/local/nginx/conf/nginx.conf
EXPOSE 80
WORKDIR /usr/local/nginx
CMD ["nginx","-g","daemon off;"]
---------------------------------------------------------------
##注意点: COPY nginx.conf /usr/local/nginx/conf/nginx.conf 这一句的nginx必须在当前执行的目录中,他是将nginx.conf文件拷贝到容器中替代容器的nginx.conf文件.

日志查看办法:
docker logs a-nginx

6. docker 构建:
  docker build -t nginx-test-v001 -f dockerfile-nginx .

7. 使用这个镜像
docker run --name aa-nginx-v10 -d -p 80:80 -v /www:/usr/local/nginx/html -v /var/log/nginx:/var/log/nginx --restart=always nginx:v1

#指定日志和网站根目录:
 -v /www:/usr/local/nginx/html -v /var/log/nginx:/var/log/nginx

8. docker运行测试:
docker start nginx

[root@master1 ~]# docker ps -a
CONTAINER ID  IMAGE    OMMAND                  CREATED       STATUS   PORTS                NAMES
2b79fdc30a2d  nginx:v2 "nginx -g 'daemon of…"  5 seconds ago Up 4     0.0.0.0:80->80/tcp   a-nginx


8. 日志查看:
[root@master1 ~]# docker logs a-nginx
10.0.0.1 - - [12/Mar/2020:12:26:00 +0800] "GET /index.html HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"
10.0.0.1 - - [12/Mar/2020:12:26:00 +0800] "GET /index.html HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/

持久化本地的日志查看:
[root@master1 ~]# tailf  /var/log/nginx/access.log 
10.0.0.1 - - [12/Mar/2020:12:26:00 +0800] "GET /index.html HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36"
10.0.0.1 - - [12/Mar/2020:12:26:00 +0800] "GET /index.html HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/

8.  访问测试:

2. docker构建PHP镜像

先启动一个centos镜像 : docker run -it --name centos7 centos:7 bash 
进入centos镜像: docker exec -it centos7 bash
构建步骤在centos7上跑通,整理步骤为dockerfile
FROM centos:7
LABEL maintainer www.chenleilei.net
RUN yum install -y curl
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && \
yum clean all
RUN yum remove oniguruma-devel -y 
RUN yum remove oniguruma -y
RUN rpm -ivh https://www.chenleilei.net/soft/docker/oniguruma-6.7.0-1.el7.x86_64.rpm
RUN rpm -ivh https://www.chenleilei.net/soft/docker/oniguruma-devel-6.7.0-1.el7.x86_64.rpm
RUN yum install -y cmake pcre pcre-devel openssl openssl-devel gd-devel \
zlib-devel gcc gcc-c++ net-tools iproute telnet wget curl \
wget vim libxml2 libxml2-devel openssl openssl-devel libjpeg-turbo \
libjpeg-turbo-devel libpng-devel libpng freetype-devel freetype icu \
libicu-devel libicu libmcrypt libmcrypt-devel libxslt libxslt-devel  php-mysql libsqlite3x-devel && \
rm -rf /var/cache/yum/*
WORKDIR /
RUN useradd -s /sbin/nologin -M www
RUN wget https://www.chenleilei.net/soft/php-7.4.3.tar.gz
RUN tar xf php-7.4.3.tar.gz
WORKDIR php-7.4.3
RUN ./configure --prefix=/usr/local/php-7.4.3 \
--with-config-file-path=/usr/local/php-7.4.3/etc \
--with-config-file-scan-dir=/usr/local/php-7.4.3/conf.d \
--enable-fpm --with-fpm-user=www \
--with-fpm-group=www \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--enable-intl \
--with-xsl
RUN make -j 4 && make install
RUN cp ./sapi/fpm/php-fpm.conf /usr/local/php-7.4.3/etc/php-fpm.conf && \
cp /php-7.4.3/php.ini-production /usr/local/php-7.4.3/etc/php.ini && \
cp /usr/local/php-7.4.3/etc/php-fpm.d/www.conf.default /usr/local/php-7.4.3/etc/php-fpm.d/www.conf && \
sed  -i  "s#127.0.0.1:9000#0.0.0.0:9000#g" /usr/local/php-7.4.3/etc/php-fpm.d/www.conf && \
sed -in '99idaemonize = yes' /usr/local/php-7.4.3/etc/php-fpm.conf && \
cp /php-7.4.3/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && \
chmod a+x /etc/init.d/php-fpm
WORKDIR /
RUN rm -rf /php-7.4.3.tar.gz
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
CMD /usr/local/php-7.4.3/sbin/php-fpm -F
EXPOSE 9000
#构建镜像
docker build -t a-php:v600 -f dockerfile .

#检查镜像
[root@master1 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
a-php               v600                0857b0aadba1        46 minutes ago      1.25GB
nginx               v1                  c28c58556ba8        16 hours ago        393MB
centos              7                   5e35e350aded        4 months ago        203MB


#使用镜像:
docker run --name php-600 -d -p 9000:9000 --restart=always a-php:v600

[root@master1 ~]# docker run --name php-200 -d -p 9000:9000 --restart=always a-php:v600
67672f2e802cb76248632f052527afdca629fabcec5809fb14245092250e7d8c

#检查状态:
[root@master1 ~]# docker ps -a

3. docker构建PHP+NGINX镜像

dockerfile  php与nginx交互:

#第一步: 安装php  (dockerfile)

[root@master1 ~]# 
[root@master1 ~]# 
[root@master1 ~]# vi dockerfile-test-php 
[root@master1 ~]# vim dockerfile-test-php 

FROM centos:7
LABEL maintainer www.chenleilei.net
RUN yum install -y curl
RUN curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && \
yum clean all
RUN yum remove oniguruma-devel -y
RUN yum remove oniguruma -y
RUN rpm -ivh https://www.chenleilei.net/soft/docker/oniguruma-6.7.0-1.el7.x86_64.rpm
RUN rpm -ivh https://www.chenleilei.net/soft/docker/oniguruma-devel-6.7.0-1.el7.x86_64.rpm
RUN yum install -y cmake pcre pcre-devel openssl openssl-devel gd-devel \
zlib-devel gcc gcc-c++ net-tools iproute telnet wget curl \
wget vim libxml2 libxml2-devel openssl openssl-devel libjpeg-turbo \
libjpeg-turbo-devel libpng-devel libpng freetype-devel freetype icu \
libicu-devel libicu libmcrypt libmcrypt-devel libxslt libxslt-devel  php-mysql libsqlite3x-devel && \
rm -rf /var/cache/yum/*
WORKDIR /
RUN useradd www -u 1200 -M -s /sbin/nologin
RUN wget https://www.chenleilei.net/soft/php-7.4.3.tar.gz
RUN tar xf php-7.4.3.tar.gz
WORKDIR php-7.4.3
RUN ./configure --prefix=/usr/local/php-7.4.3 \
--with-config-file-path=/usr/local/php-7.4.3/etc \
--with-config-file-scan-dir=/usr/local/php-7.4.3/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--enable-intl \
--with-xsl \
--without-pear \
--disable-phar
RUN make -j 4 && make install
RUN cp ./sapi/fpm/php-fpm.conf /usr/local/php-7.4.3/etc/php-fpm.conf && \
cp /php-7.4.3/php.ini-production /usr/local/php-7.4.3/etc/php.ini && \
cp /usr/local/php-7.4.3/etc/php-fpm.d/www.conf.default /usr/local/php-7.4.3/etc/php-fpm.d/www.conf && \
sed  -i  "s#127.0.0.1:9000#0.0.0.0:9000#g" /usr/local/php-7.4.3/etc/php-fpm.d/www.conf && \
sed -in '99idaemonize = yes' /usr/local/php-7.4.3/etc/php-fpm.conf && \
cp /php-7.4.3/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && \
chmod a+x /etc/init.d/php-fpm
WORKDIR /
RUN rm -rf /php-7.4.3.tar.gz
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN mkdir -p /usr/local/nginx/html
CMD /usr/local/php-7.4.3/sbin/php-fpm -F
EXPOSE 9000

#保存为dockerfile-test-php  
#如果构建dockerfile:
docker build -t php-test-v001 -f dockerfile-test-php .

#注意启动的时候需要执行挂载:  将 /usr/local/nginx/html 挂载到本地 /www 后面用于和nginx进行交互. 
# -v www:/usr/local/nginx/html

启动PHP镜像:
docker run --name php-001 -d -p 9000:9000 -v /www:/usr/local/nginx/html php-test-v001

------------------------------------------------------------------------------

#第二步: 安装nginx  (dockerfile)

FROM centos:7
LABEL maintainer www.chenleilei.net
RUN yum install -y cmake pcre pcre-devel openssl openssl-devel gd-devel \
    zlib-devel gcc gcc-c++ net-tools iproute telnet wget curl &&\
    yum clean all && \
    rm -rf /var/cache/yum/*
RUN wget https://www.chenleilei.net/soft/nginx-1.16.1.tar.gz
RUN tar xf nginx-1.16.1.tar.gz
WORKDIR nginx-1.16.1
RUN ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_image_filter_module \
    --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module  && \
    make -j 4 && make install && \
    rm -rf /usr/local/nginx/html/*  && \
    echo "leilei hello" >/usr/local/nginx/html/index.html  && \
    rm -rf nginx* && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    ln -sf /dev/stdout /var/log/nginx/access.log && \
    ln -sf /dev/stderr /var/log/nginx/error.log
ENV PATH $PATH:/usr/local/nginx/sbin
# COPY nginx.conf /usr/local/nginx/conf/nginx.conf
EXPOSE 80
WORKDIR /usr/local/nginx
CMD ["nginx","-g","daemon off;"]

------------------------------------------------------------------------------

第三步 : 配置nginx解析

通过: docker inspect php-001 |grep "IP" 来获取PHP的IP地址 配置在 fastcgi_pass中
docker inspect php-001 |grep "IPAddress"|awk -F '[";]' 'NR==2{print $4}'
[root@master1 ~]# docker inspect php-001 |grep "IPAddress"|awk -F '[";]' 'NR==2{print $4}'
172.17.0.4

通过挂载的方式将配置文件 : /usr/local/nginx/conf/nginx.conf 挂载到本地

# 配置php+nignx解析:
# 这里需要注意两点, 一个是 fastcgi_pass 这个地址是php服务器的地址 , 第二个是 /usr/local/nginx/html
# 这个 /usr/local/nginx/html 目录需要在两个docker容器中都存在, php负责解析 php内容,html由nginx来解析.
# 这个目录可以配置为NFS,用于共享两个目录.以便于管理php解析的文件和html文件

 location ~ \.php$ {
     root           html;
     fastcgi_pass   172.17.0.2:9000;
     fastcgi_index  index.php;
     fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
     include        fastcgi_params;
 }
 
 
#第四步 : 测试:
-------------------------------
<?php
        phpinfo();
?>
-------------------------------
将环境测试代码放在:
/usr/local/nginx/html 目录中.

通过启动镜像时添加挂载参数 -v 将php中的 /usr/local/nginx/html 目录以及nginx中的目录/usr/local/nginx/html
挂载到本地 www 目录
-v /www:/usr/local/nginx/html
-v /conf/nginx.conf:/usr/local/nginx/conf/nginx.conf
执行命令:
docker run --name nginx-001 -d -p 80:80 -v /www:/usr/local/nginx/html -v /conf/nginx.conf:/usr/local/nginx/conf/nginx.conf nginx:v10
这样可以访问的原因是 nginx容器和php容器都存在 /usr/local/nginx/html 目录.而 这个index.php文件是在php服务器
所以可以解析,如果它在nginx容器中 是无法解析的.
这里配置目录共享是必须的. 现在主流的文件服务器 可以使用 NFS  fastdfs 来实现
同时他们要指定 UID :
如:
useradd  www -u 1200 -M -s /sbin/nologin

4. docker构建tomcat镜像:

FROM centos:7
MAINTAINER www.chenleilei.net
ENV VERSION=8.5.43
RUN yum install -y java-1.8.0-openjdk wget curl unzip iproute net-tools && \
    yum clean all && \
    rm -fr /var/cache/yum/*
WORKDIR /usr/local/
RUN wget https://www.chenleilei.net/soft/apache-tomcat-9.0.17.tar.gz && \
tar xf apache-tomcat-9.0.17.tar.gz && ln -s apache-tomcat-9.0.17 tomcat && \
rm -f apache-tomcat-9.0.17.tar.gz && mkdir /usr/local/tomcat/webapps/test -p && \
echo "tomcat is 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"]

第二种:
FROM java:8-jdk-alpine
ENV JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8 -Duser.timezone=GMT+08"
RUN apk add -U tzdata && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY ./target/eureka-service.jar ./
EXPOSE 8888
CMD java -jar $JAVA_OPTS /eureka-service.jar
最后修改:2020-03-18 13:57:32 © 著作权归作者所有
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付

上一篇

发表评论

评论列表

匿名用户 2020-09-02 20:16:29
[url=https://lexapro2020.top/]lexapro2020.top[/url]
回复
匿名用户 2020-07-31 17:30:05
博主 这样构建出来的镜像很大 你怎么处理呢
回复
匿名用户 2020-05-16 14:35:16
Purchase 60 Mg Orlistat [url=https://cheapcialisir.com/]buy online cialis[/url] Female Cialis Online <a href=https://cheapcialisir.com/#>canadian cialis</a> Celexa Online Pharmacy
回复
匿名用户 2020-05-15 03:49:44
Que Es El Propecia [url=https://viacialisns.com/]buy liquid cialis online[/url] Cialis 20 Mg Tabletas <a href=https://viacialisns.com/#>cheap cialis online pharmacy</a> Cialis Spedizione 24 Ore
回复

博客信息

  • 文章数目 123
  • 标签数目 333
  • 运行天数
  • 最后活动

广告



文章目录