Business mirrored write articles ---- Dockerfile

Base image, people in contact with docker's basically know this concept, to run the business, running the basis of the environment, for example: the need to use the base image java java business environment, the general will choose when to use the mirror:

java: 8 or apline-java: 8 or openjdk: 8 and the like, if business requirements under the circumstances is not particularly high, usually written using official mirror, after all, than the official optimize our own and write their own much better optimization .

And here, I want to make is a special case, such as a start code to run on the set of directories, a good decision to start reading order code directory, and so on, which mirror some of the path and directory there are special requirements, if only use the official mirror, in many places to follow the principle of writing the official mirror, change your code at larger or more code modules of the situation than the code, it will add significant workload . At this time you need to write your own business base image, integrated customize their own mirror image, of course, you can make changes or commit based on the official mirrors. In many cases, easy to manage and easy to modify hard demand, mirroring the way business base so generated by Dockerfile way than to commit a lot of good. The following is an example of what I wrote, based centos7 written in php + nginx base image (Tips, write Dockerfile best familiar with various linux installation):

The FROM centos: # 7 centos here is based on the series of base image packages do, if just write centos likely to pull that centos8
 MAINTAINER adif0028 <longliu2long @ 163 .com> # authors stated, the phrase dispensable
 COPY ustc.repo /etc/yum.repos.d/ustc.repo # following copy is to download a good thing and has been configured into just under the directory path and needs. Here there will be a public directory to put something into it and copy download something.
COPY the run.sh /run.sh
 COPY the php.ini /usr/local/php/etc/php.ini
 COPY PHP-fpm.conf /usr/local/php/etc/php-fpm.conf
 COPY nginx.conf / usr /local/nginx/conf/nginx.conf
 COPY index.php /usr/local/nginx/html/index.php
 COPY the WWW-php.conf /usr/local/php/etc/php-fpm.d/www-php .conf
 COPY rsyslog.conf /etc/rsyslog.conf
 COPY7.0.31.tar.gz /data/php-7.0.31-php .tar.gz # specified here is they need php version, you can download online, I was too slow to download their own local copy into
 RUN yum repolist && -Y && wget the install CD yum / Data -xf PHP && the tar-7.0.31 .tar.gz \
    && mv /etc/yum.repos.d/CentOS-* /data && yum clean all && yum repolist && yum install nc -y && yum install rsyslog -y && yum install ImageMagick-devel -y \
    && yum install libssh2-devel -y && yum install gcc-c++ -y && yum install cyrus-sasl-devel -y \
    && wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo && yum repolist && yum install autoconf -y \
    && wget http://nginx.org/download/nginx-1.16.1.tar.gz && wget https://pecl.php.net/get/msgpack-2.0.2.tgz && wget https://pecl.php.net/get/yar-2.0.4.tgz && wget https://pecl.php.net/get/yaconf-1.0.7.tgz \
    && wget https://pecl.php.net/get/redis-4.1.0.tgz && wget https://pecl.php.net/get/imagick-3.4.3.tgz && wget https://pecl.php.net/get/swoole-4.0.2.tgz \
    && wget https://pecl.php.net/get/ssh2-1.1.2.tgz && wget https://pecl.php.net/get/mongodb-1.5.3.tgz && wget https://pecl.php.net/get/grpc-1.19.0.tgz \
    && wget https://pecl.php.net/get/protobuf-3.7.1.tgz && wget https://pecl.php.net/get/SeasClick-0.1.0.tgz \
    && yum install gcc -y && yum install libxml2-devel -y && yum install openssl-devel -y && yum install curl-devel -y && yum install libjpeg-devel -y \
    && yum install libpng-devel -y && yum install libXpm-devel -y && yum install freetype-devel -y && yum install libmcrypt-devel -y && yum install make -y \ # yum upper replacement source, required for installation php and operational requirements dependent module php
    Cd && / the Data / PHP- 7.0.31 \ # this is a public directory of php.
    CONFIG = && "\ #Dockerfile is to support the definition of variables, here is the definition of variables php installation path.
         --Prefix = / usr / local / php \
        --with-config-file-path=/usr/local/php/etc \
        --with-config-file-scan-dir=/usr/local/php/etc/php.d \
        --disable-ipv6 \
        --enable-bcmath \
        --enable-calendar \
        --enable-exif \
        --enable-fpm \
        --with-fpm-user=www \
        --with-fpm-group=www \
        --enable-ftp \
        --enable-gd-jis-conv \
        --enable-gd-native-ttf \
        --enable-inline-optimization \
        --enable-mbregex \
        --enable-mbstring \
        --enable-mysqlnd \
        --enable-opcache \
        --enable-pcntl \
        --enable-shmop \
        --enable-soap \
        --enable-sockets \
        --enable-static \
        --enable-sysvsem \
        --enable-wddx \
        --enable-xml \
        --with-curl \
        --with-gd \
        --with-jpeg-dir \
        --with-freetype-dir \
        --with-xpm-dir \
        --with-png-dir \
        --with-gettext \
        --with-iconv \
        --with-libxml-dir \
        --with-mcrypt \
        --with-mhash \
        --with-mysqli \
        --with-pdo-mysql \
        --with-pear \
        --with-openssl \
        --with-xmlrpc \
        --with-zlib \
        --disable-debug \
        --disable-phpdbg \
         " \
    && CONFIGU = "\ # here is the definition of the relevant variables nginx installation path, etc.
        --prefix=/usr/local/nginx \
        --sbin-path=/usr/sbin/nginx \
        --modules-path=/usr/lib/nginx/modules \
        --conf-path=/usr/local/nginx/conf/nginx.conf \
        --error-log-path=/var/log/nginx/error.log \
        --http-log-path=/var/log/nginx/access.log \
        --pid-path=/var/run/nginx.pid \
        --lock-path=/var/run/nginx.lock \
        --http-client-body-temp-path=/var/cache/nginx/client_temp \
        --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
        --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
        --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
        --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
        --user=www \
        --group=www \
        --with-http_stub_status_module \
        --with-http_v2_module \
        --with-ipv6 \
        --with-http_gzip_static_module \
        --with-http_realip_module \
        --with-http_flv_module \
        --with-http_ssl_module \
          "\
     && ./configure $ CONFIG \ # pay attention to the context of the corresponding variable, were compiled config. Remember to be in the source directory
    && make && make install \ # compiler installation.
    && # / usr / local / php / bin / install PEAR Channel: //pear.php.net/PHP_Archive-0.12.0 \ # This step is actually a troubleshooting for php, but due to network causes frequent card out here mirror, I removed because they do not affect the use.
    && cp /data/php-7.0.31/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm && chmod a+x /etc/init.d/php-fpm && cp /usr/local/php/bin/php /usr/sbin/ \
    && groupadd -r www && useradd -r -g www -s /bin/false -M www && cd /data/php-7.0.31/ext/zip && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install \
    && cd /data/php-7.0.31/ext/opcache && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install \
    && cd /data && tar -xf msgpack-2.0.2.tgz && cd /data/msgpack-2.0.2 && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install \
    && cd /data && tar -xf yar-2.0.4.tgz && cd /data/yar-2.0.4 && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install \
    && cd /data && tar -xf yaconf-1.0.7.tgz && cd /data/yaconf-1.0.7 && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install \
    && cd /data && tar -xf redis-4.1.0.tgz && cd /data/redis-4.1.0 && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install \
    && cd /data && tar -xf imagick-3.4.3.tgz && cd /data/imagick-3.4.3 && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install \
    && cd /data && tar -xf swoole-4.0.2.tgz && cd /data/swoole-4.0.2 && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install \
    && cd /data && tar -xf ssh2-1.1.2.tgz && cd /data/ssh2-1.1.2 &&  /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install \
    && cd /data && tar -xf mongodb-1.5.3.tgz && cd /data/mongodb-1.5.3 && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install \
    && cd /data && tar -xf protobuf-3.7.1.tgz && cd /data/protobuf-3.7.1 && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install \
    && cd /data && tar -xf grpc-1.19.0.tgz && cd /data/grpc-1.19.0 && /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make && make install \
    CD && / Data && the tar -xf SeasClick- 0.1.0.tgz CD /data/SeasClick-0.1.0 && && / usr / local / PHP / bin / the phpize && ./configure --with-PHP-config = / usr / local / php / bin / php-config && && the make the make install \ 
# up here is to create a user www, then compiled php modules installed && mkdir -p / home / wwwlogs / phplog / && chown -R www: www / home / wwwlogs / phplog / && mkdir -p / usr / local / nginx / html && chmod a + x /run.sh && mkdir -p / var / cache / nginx \ # here is run.sh I'll explain later released to do && chown www:www -R /var/cache/nginx && chown www:www -R /usr/local/nginx \ && cd /data && tar -xf nginx-
1.16.1.tar.gz && cd /data/nginx-1.16.1 \ && ./configure $CONFIGU \ The make the make install && && \
# here is nginx installed && yum remove perl -y && yum remove wget -y && yum remove gcc -y && yum remove make -y \ Remove autoconf yum -Y && && Remove GCC yum -Y-C ++ headers && yum Remove Kernel -Y-CPP -Y && && Remove yum yum Clean All && RM -rf / Data
/ * # deleted without affecting the number of functions and data package, streamlined image EXPOSE 80 # exposed port ENTRYPOINT [ "/run.sh"] # execution run.sh CMD [ "/ bin / sh", "- c"] # specify the run bash

I made a comment about the above steps, and write Dockerfile optimization techniques is to reduce the volume:

1, RUN to minimize the number of layers, one layer will increase the volume mirroring increases a lot, I've just written a RUN. But this layer RUN getting bigger.

2, because it is CentOS, after extracting the base image 200 m, it is possible to reduce dependence of the installation. To reduce the volume of Dockerfile.

3, except for the necessary configuration files, be sure to remove unnecessary files in it, COPY or downloaded into the package, the installation is complete source packets deleted, and then removed the cache.

4, delete unnecessary components, such as the cases of the last, I will not be deleted or used loopholes or deleted without affecting the packet mirroring use. Like the inside of the gcc and the like.

5, writing skills, as much as possible and use && \ segmented by area, I feel writing is very clear, of course, a large cattle please correct me.

Written above the mirror, I downsized to around final uncompressed image 650M, apply to the company's business on the running speed by a mile. (Since the previous base image uncompressed when more than 1G)

Something about run.sh, the script is as follows:

#!/bin/sh
nginx="/usr/sbin/nginx"
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
chown -R www:www /dev/shm/
echo "start nginx..."
$nginx -c $NGINX_CONF_FILE

echo "start php-fpm..."
/etc/init.d/php-fpm start --daemonize
/usr/sbin/rsyslogd

tail -f /dev/null                

There is the definition of how to start the program starts, the docker which is not supported by systemd, of course, can be enabled to obtain the specified parameter when a container starts, the official website say I do not say, in his own writing business on the inside Do not think of another way systemd quick start, and with a script to run the program and specify the configuration, enable it, the focus is the following tail -f / dev / null sentence, because docker does not support background launcher (successfully enabled the docker container is not), you need to program in the foreground process hold live when docker start, the front desk needs to hold live output and tail -f / dev / null commands.

I wrote above that mirrored the company's business experience, hope to help you, Daniel, correct me also welcome to learn together.

Guess you like

Origin www.cnblogs.com/adif0028/p/12157888.html