The practical process of Docker in PHP

Recently, microservices are very popular, and many people are trying them. Our company is also trying to time microservices during this time, which involves Docker.
I have stepped on a lot of pits in the practice of docker, and I also have more understanding of Docker. Let me record it below.

When Docker is packaging the Spring boot project, because Spring boot integrates tomcat and provides a way to package it directly into a jar package, how to package Spring boot .

The Dockerfile file is as follows:

FROM java:8

COPY target/your-project-name.jar /app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]

Because there is still a previous PHP project in the company, it also needs to package PHP. After checking a lot of information, I finally found in practice that the PHP project needs an nginx to manage the network. If it is packaged with a Dockerfile file, there will be problems. .

FROM php:7.0-cli
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng12-dev \
    && docker-php-ext-install -j$(nproc) iconv mcrypt \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd
FROM php:7.1-fpm
RUN pecl install -o -f redis \
    &&  rm -rf /tmp/pear \
    &&  docker-php-ext-enable redis
RUN docker-php-ext-install  mysqli pdo pdo_mysql

EXPOSE 80

COPY . /php
WORKDIR /php

After searching for information, I found such an article:
http://geekyplatypus.com/dockerise-your-php-application-with-nginx-and-php7-fpm/ I
found a solution in it, just use docker-compose The docker-compose.yml file is as follows:

version: '2'

services:
    web:
        image: nginx:latest
        ports:
            - "8080:80"
        volumes:
            - ./:/php
            - ./site.conf:/etc/nginx/conf.d/default.conf
        networks:
            - code-network
    php:
        image: php:fpm
        volumes:
            - ./:/php
        networks:
            - code-network

networks:
    code-network:
        driver: bridge

The site.conf file mentioned above is as follows:

server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /php;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

Execute directly on the command line:

  $sudo docker-compose up

You can see the results of the operation, but because we are directly deploying to Alibaba Cloud Container Service, the docker-compose method has a flaw, because Alibaba Cloud Container Service does not support serverthis method, and it must be finally queried. And found:
https://hub.docker.com/r/richarvey/nginx-php-fpm/

Just use the following Dockerfile directly:

FROM richarvey/nginx-php-fpm:latest

MAINTAINER Ric Harvey <ric@ngd.io>

ENV PHPREDIS_VERSION 3.0.0
RUN mkdir -p /usr/src/php/ext/redis \
    && curl -L https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz | tar xvz -C /usr/src/php/ext/redis --strip 1 \
    && echo 'redis' >> /usr/src/php-available-exts \
    && docker-php-ext-install redis

EXPOSE 80

ADD ./ /var/www/html/

Since Docker was chosen at the beginning, we must find a way to understand this technology, otherwise it will be troublesome if something goes wrong in the production environment. The above practice process of Dockerization with PHP, staring at the line of log information to find the reason, because I am not very familiar with PHP, I also asked my colleagues at PHP a lot of questions.
Record the above and meet again in the future, although the above content does not introduce each Docker technical point, find time to record each technical point. Simply put, PHP must be used with nginx.

Guess you like

Origin blog.csdn.net/Ser_Bad/article/details/78019619