The Docker container concatenates the Nginx instance sharing

  • 2020-05-17 06:56:36
  • OfStack

As an application of currently the most popular Docker does exist its unique feature, both program apes and operations should be heard Docker name, Docker has gone through a lot of pit, is currently the latest version is v1 version 11.0, it should be said that all can be used carrying development and operational monitoring, this tool can help us efficient packaging, publishing and running is bearing the weight of the container of application system. It also helps to collect the logs and help the rapid development of App.

Container and the virtual machine, often be out comparison of two products, in fact there is a fundamental difference, both the virtual machine is fully simulates the real computer 1, above running system may or may not know their running in a virtual environment, and bearing the virtual machine to convert the user instructions for the function of the privileged instructions, so the virtual machine is very complicated, but it is complete, and Docker is completely different. Docker USES the host's own Linux kernel, and then generates the disk directory and software from the mirror. All processes are running on the host. If you are interested, you can query 1 on ps aux and find the processes running on Docker.

Docker is actually used

In the early days of the development of Docker, due to the imperfection of lightweight images such as busybox, the reduced and slimmer images in major releases were more widely used. In particular, since Docker itself was developed in the environment of Ubuntu, Ubuntu and Debian were used as base images in many images to generate target images. However, as it is used in practice, its disadvantages are exposed, that is, it is too heavy quantification. For example, the log function of systemd and the log function of Docker itself are reused, and it is difficult to reduce the mirror image to less than 300M. And the recommended use of Docker is a single-process model, rather than multiple processes as a complete operating system 1. This image is the image recommended by the official image of Docker. In the future, the official image will be transferred to the image based on alpine. Therefore, we should get familiar with this image as soon as possible.

Build Dockerfile

This article describes the Nginx practice for Docker containers, but officials actually have an alpine mirror for Nginx. In the actual use, the author mostly USES Tengine, so according to the official Dockerfile reference, the author wrote Dockerfile mirror Tengine, I hope to be able to throw a brick to attract jade, you can criticize and correct.


FROM alpine:3.3
MAINTAINER ChasonTang <chasontang@gmail.com>

ENV TENGINE_VERSION 2.1.2
ENV CONFIG "\
    --prefix=/etc/nginx \
    --sbin-path=/usr/sbin/nginx \
    --conf-path=/etc/nginx/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=nginx \
    --group=nginx \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_auth_request_module \
    --with-mail \
    --with-mail_ssl_module \
    --with-file-aio \
    --with-http_spdy_module \
    --with-ipv6 \
    --with-jemalloc \
    "

ADD ngx_user.patch /
ADD repositories /etc/apk/repositories

RUN \
  addgroup -S nginx \
  && adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx nginx \
  && apk add --no-cache --virtual .build-deps \
    gcc \
    libc-dev \
    make \
    openssl-dev \
    pcre-dev \
    zlib-dev \
    linux-headers \
    curl \
    jemalloc-dev \
  && curl "http://tengine.taobao.org/download/tengine-$TENGINE_VERSION.tar.gz" -o tengine.tar.gz \
  && mkdir -p /usr/src \
  && tar -zxC /usr/src -f tengine.tar.gz \
  && rm tengine.tar.gz \
  && cd /usr/src/tengine-$TENGINE_VERSION/src/os/unix/ \
  && mv /ngx_user.patch ./ngx_user.patch \
  && patch ngx_user.c ngx_user.patch \
  && rm ngx_user.patch \
  && cd ../../../ \
#  && cd /usr/src/tengine-$TENGINE_VERSION \
  && ./configure $CONFIG --with-debug \
  && make \
  && mv objs/nginx objs/nginx-debug \
  && ./configure $CONFIG \
  && make \
  && make install \
  && rm -rf /etc/nginx/html/ \
  && mkdir /etc/nginx/conf.d/ \
  && mkdir -p /usr/share/nginx/html/ \
  && install -m644 html/index.html /usr/share/nginx/html/ \
  && install -m644 html/50x.html /usr/share/nginx/html/ \
  && install -m755 objs/nginx-debug /usr/sbin/nginx-debug \
  && strip /usr/sbin/nginx* \
  && runDeps="$( \
    scanelf --needed --nobanner /usr/sbin/nginx \
      | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
      | sort -u \
      | xargs -r apk info --installed \
      | sort -u \
  )" \
  && apk add --virtual .nginx-rundeps $runDeps \
  && apk del .build-deps \
  && rm -rf /usr/src/nginx-$NGINX_VERSION \
  && apk add --no-cache gettext \
  \
  # forward request and error logs to docker log collector
  && ln -sf /dev/stdout /var/log/nginx/access.log \
  && ln -sf /dev/stderr /var/log/nginx/error.log

COPY nginx.conf /etc/nginx/nginx.conf
COPY nginx.vh.default.conf /etc/nginx/conf.d/default.conf

EXPOSE 80 443

CMD ["nginx", "-g", "daemon off;"]

We know, Docker According to Dockerfile Build the mirror image. That's what I wrote Dockerfile . First, use the FROM directive to specify that the base image of this image is alpine:3.3, the second action is the Dockerfile maintainer declaration, and then use the two ENV directives to declare two environment variables, one specifying the version number that Tengine needs to obtain, and one the compile and install options. Here temporarily not explained, then a patch file and alpine image source configuration file is copied to the container, because actually tengine v2. 1.2 there is a legacy of glibc bug, can lead to a compile-time error, the latest code in upstream Nginx have repair, while the development of tengine branch has repair for the problem above, the author recently proposed issue patch this revised tengine v2. 1.3 branch code, but it's a pity, Version v2.1.3 has not yet been officially released, so this issue can only be fixed manually with a patch. As for the mirror source, the apk package management command was unable to successfully download the individual dependencies due to a domestic network problem, so it was designated as a domestic source and can be removed if it is used formally.

And then using RUN Command to execute the code, here you can see that the author used && and \ to compress all the instructions into 1 line, which is for two reasons:

The RUN directive does not save the working path of the previous instruction, and each RUN directive only specifies the working directory as/directory
Article 1. Dockerfile The command in the image will result in 1 mirror commit, in other words, a decrease Dockerfile The instruction in can improve the level of image reuse
The next step is to use the apk package management command to download and install dependencies, including compilers, and mark them as .build-deps Group so that it can be unloaded later. And then there's the very conventional way of thinking./ configure && make && make install , compiler options are very standard, the basic familiar with Nginx compiler friends can understand. But as you can see above, Nginx was compiled twice and turned on once -- the with-debug parameter was turned on once, but not once. This is because in many cases, we need Nginx to provide debug level monitoring logs, especially in development environments, so it was compiled twice for ease of use. The runtime dependencies of Tengine are then extracted using string analysis processing and marked as .nginx-rundeps Then unloading .build-deps Finally, two symbolic links link accessLog and errorLog to the standard I/o so that we can easily view the log using the docker logs command. Finally, copy the custom Nginx configuration file and use nginx-g daemon off; Have Nginx run in the former stage process mode.

conclusion

So far, we have finished the correct use of Docker in production and development. Docker is indeed a wonderful tool. I wish you can use Docker as soon as possible to improve your productivity.


Related articles: