dockerfile creates lnmp image

Posted by anthonyw17 on Wed, 08 May 2019 11:35:02 +0200

Catalog

I. Creating the mirror image of lnmp

1.1. dockerfile creates php7.2.16 image

1. 2. dockerfile creates nginx-1.15.7 image

1.3. The php image is pull ed directly on the docker warehouse

2. Running lnmp environment through dockerpose-compose

2.1. Install docker-compose

2.2. Run the lnmp environment with docker-compose and mount the docker container data directory to the local directory

2.3. Running and managing the lnmp environment with docker-compose

Dokerfile's lnmp environment is successful!

In virtualization technology, docker is one of the hottest technologies. In the production environment, more and more business needs use docker to deploy the environment. This article is about how to use dockerfile to create the mirror image of each component of LNMP and realize the environment of lnmp.

I. Creating the mirror image of lnmp

  • 1.1. dockerfile creates php7.2.16 image

FROM centos:latest
MAINTAINER https://blog.csdn.net/lituxiu
ENV TIME_ZOME Asia/Shanghai
ARG WJ="php-7.2.16"
# wget https://www.php.net/distributions/php-7.2.16.tar.gz

ADD $WJ.tar.gz /tmp
RUN yum -y install gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel bison \
    && mkdir -p /usr/php/etc \
    && cd /tmp/$WJ \
    && ./configure --prefix=/usr/php \
        --with-config-file-path=/usr/php/etc \
        --with-gd --with-mysqli \
        --with-openssl --with-zlib --with-curl \
        --with-jpeg-dir --with-png-dir --with-iconv \
        --enable-fpm --enable-zip --enable-mbstring \
    && make -j 4 \   
    && make install \
    && cp /usr/php/etc/php-fpm.conf.default /usr/php/etc/php-fpm.conf \
    && cp /usr/php/etc/php-fpm.d/www.conf.default /usr/php/etc/php-fpm.d/www.conf \
    && sed -i '/;daemonize/a\daemonize = no' /usr/php/etc/php-fpm.conf \
    && sed -i 's/127.0.0.1/0.0.0.0/g' /usr/php/etc/php-fpm.d/www.conf \
    && echo "${TIME_ZOME}" > /etc/timezone \
    && ln -sf /usr/share/zoneinfo/${TIME_ZOME} /etc/localtime \
    && rm -rf /tmp/php* \
    && yum clean all \
    && yum -y remove gcc gcc-c++ make

WORKDIR /usr/php/
EXPOSE 9000
CMD ["sbin/php-fpm","-c","etc/php-fpm.conf"]

File structure:

[root@dingzhi ~]# tree php7.2.16/
php7.2.16/
├── dockerfile
└── php-7.2.16.tar.gz

Create a php mirror php 7.2.16: latest

[root@dingzhi ~]# cd php7.2.16/
[root@dingzhi php7.2.16]# docker build -t php7.2.16:latest ./

(Note: When docker build s an image, pay attention to the context of the file path, which is usually created in the. / Current directory of the docker file.)

Explain:

# cp/tmp/php-7.2.16/php.ini-production/usr/php/etc/php.ini\\\ php.ini when installing common source code is to copy php.ini-production renaming. Here only PHP modules are installed, php-fpm configuration files are modified, and php-fpm services are started.

# Make-j 4 specifies the number of tasks that run in parallel, that is, four compiled task programs run at the same time to improve speed (note the cpu bottleneck, if multiple-j is provided, the last one is valid).

# echo "${TIME_ZOME}">/etc/timezone and ln-sf/usr/share/zoneinfo/${TIME_ZOME}/etc/localtime are modified to shanghai.

# daemonize = no is the php-fpm service started in the foreground, because to start the container in the background, the container service must start in the foreground, otherwise it will automatically exit after starting the container.

 

  • 1. 2. dockerfile creates nginx-1.15.7 image

FROM centos:latest
MAINTAINER https://blog.csdn.net/lituxiu
ENV TIME_ZOME Asia/Shanghai
ARG WJ="nginx-1.15.7"

#COPY nginx.conf /usr/local/nginx/
ADD $WJ.tar.gz /tmp
RUN yum -y install gcc gcc-c++ make openssl-devel pcre-devel zlib-devel \
        && mkdir -p /usr/local/nginx \
        && cd /tmp/$WJ \
        && ./configure --prefix=/usr/local/nginx  --with-stream --with-http_ssl_module \
        && make -j 4 \
        && make install \
        && echo "${TIME_ZOME}" > /etc/timezone \
        && ln -sf /usr/share/zoneinfo/${TIME_ZOME} /etc/localtime \
        && rm -rf /tmp/nginx* \
        && yum clean all \
        && yum -y remove gcc gcc-c++ make \
        && ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

WORKDIR /usr/local/nginx/
EXPOSE 80
CMD ["nginx","-g","daemon off;"]

File structure:

[root@dingzhi ~]# tree nginx1.15.7/
nginx1.15.7/
├── dockerfile
└── nginx-1.15.7.tar.gz

Create nginx mirrors nginx 1.15, 7: latest

[root@dingzhi ~]# cd nginx1.15.7/
[root@dingzhi nginx1.15.7]# docker build -t nginx1.15.7:latest ./

Explain:

# daemon off denotes nginx service front-end startup

# COPY nginx.conf/usr/local/nginx/This line is commented out in the dockerfile of nginx, using the default. If you want to use your own modified nginx.conf, just annotate the modified nginx.conf and dockerfile in the same directory path, COPY line.

# Note: The location PHP $configuration of nginx.conf needs to be modified.

 location ~ \.php$ {
            root           html;
            fastcgi_pass   php:9000;   #php container name and port number
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  #Use the path specified by root
            include        fastcgi_params;

 

  • 1.3. The php image is pull ed directly on the docker warehouse

The order is:

docker pull mysql:5.7
[root@dingzhi ~]# docker pull mysql:5.7
Trying to pull repository docker.io/library/mysql ... 
5.7: Pulling from docker.io/library/mysql
27833a3ba0a5: Pull complete 
864c283b3c4b: Pull complete 
cea281b2278b: Pull complete 
8f856c14f5af: Pull complete 
9c4f38c23b6f: Pull complete 
1b810e1751b3: Pull complete 
5479aaef3d30: Pull complete 
1d924ec3d520: Pull complete 
1ab7ae63ac60: Pull complete 
08aa5f3680e9: Pull complete 
a832d0a0972a: Pull complete 
Digest: sha256:dba5fed182e64064b688ccd22b2f9cad4ee88608c82f8cff21e17bab8da72b81
Status: Downloaded newer image for docker.io/mysql:5.7

It is recommended that the official mysql image of docker be used directly, and the mysql image be compiled and installed through the source code of dockerfile. The final image size is over 1G, while the official mysql 5.7 image of docker is less than 400M.

2. Running lnmp environment through dockerpose-compose

  • 2.1. Install docker-compose

arrive earlier than Docker-compose official website download

Download the latest version of docker-compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

[root@dingzhi ~]# chmod +x /usr/local/bin/docker-compose
[root@dingzhi ~]# docker-compose -v
docker-compose version 1.24.0, build 0aa59064

 

Explain:

# After adding x execution permissions, docker-compose execution fails, generally the path of environment variables. Execute the following command ln-s/usr/local/bin/docker-compose/usr/bin/docker-compose

# If you want to delete docker-compose, go directly to rm-rf/usr/local/bin/docker-compose

# Docker-compose-v# View the docker-compose version

# Docker-compose operation container command is based on the docker-compose.yml file stored in the directory output, otherwise, the following error!!!

  • 2.2. Run the lnmp environment with docker-compose and mount the docker container data directory to the local directory

View php, nginx, and mysql container id

[root@dingzhi ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx1.15.7         latest              4b05c87dcbec        3 hours ago         302 MB
php7.2.16           latest              3428fd528787        4 hours ago         627 MB
docker.io/mysql     5.7                 98455b9624a9        2 weeks ago         372 MB
docker.io/centos    latest              9f38484d220f        4 weeks ago         202 MB

Edit docker-compose.yml

version: '2'
services:
  nginx:
    image: 4b05c87dcbec
    container_name: 'nginx'
    restart: 'always'
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - "/data/nginx/html:/usr/local/nginx/html"
      - "/data/nginx/logs/:/usr/local/nginx/logs/"
      - "/data/nginx/conf/:/usr/local/nginx/conf/"
    networks:
      dangjian:
        ipv4_address: 172.19.0.2

  php:
    image: 3428fd528787 
    container_name: 'php'
    restart: 'always'
    ports:
      - "9000:9000"
    volumes:
      - "/data/nginx/html:/usr/local/nginx/html"
    networks:
      dangjian:
        ipv4_address: 172.19.0.3


  mysql:
    image: 98455b9624a9
    container_name: 'mysql'
    restart: 'always'
    ports:
      - "3306:3306"
    volumes:
      - "/data/mysql/data/:/var/lib/mysql/"
      - "/data/mysql/conf/:/etc/mysql/conf.d/"

    environment:
      MYSQL_ROOT_PASSWORD: 123456
    networks:
      dangjian:
        ipv4_address: 172.19.0.4

networks:
   dangjian:
      ipam:
         config:
         - subnet: 172.19.0.0/16
           gateway: 172.19.0.1

Create locally mounted directories

nginx directory

mkdir -p /data/nginx/{conf,html,logs}

mysql directory

mkdir -p /data/mysql/{conf,data}

Because the configuration in / etc/mysql/my.cnf of the mysql container is as follows: (Note exclamation marks)

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

# Note! Incudedir / etc/mysql/conf.d / Contains the *. cnf configuration in / etc/mysql/conf.d / directory

# Implementation validation as # cat/data/mysql/conf/my.cnf 
[mysqld]
skip-grant-tables
 Starting mysql container can realize login to database directly with mysql command in mysql container

# Note! Incudedir / etc/mysql/conf.d / Contains the *. cnf configuration in / etc/mysql/conf.d / directory

# Note that there is a nginx.conf file in the local / data/nginx/conf / or the nginx container will report an error if it is not started

  • 2.3. Running and managing the lnmp environment with docker-compose

The docker-compose command must be entered in the existing docker-compose.yml directory

Start lnmp
~]# docker-compose up -d

Common docker-compose commands

docker-compose up -d           #Background docker-compose.yml all containers
docker-compose down            #Close all docker-compose.yml containers
docker-compose ps              #View the status of all containers
docker-compose restart Container name   #Restart a container

Verification

[root@dingzhi ~]# vi /data/nginx/html/test.php
<?php
    echo "Hello lituxiu<br/>";
    echo "Hello PHP<br/>";
    $conn = mysqli_connect("mysql","test","123456");
    if(!$conn){
        echo "Failure to connect database";
    }else{
        echo "Successful connection to database";
    }
    phpinfo();
?>

Browser access: http://192.168.21.140/test.php

Dokerfile's lnmp environment is successful!

Topics: Docker PHP Nginx MySQL