Docker compose build Lnmp environment (with dockerfile download)

Posted by MercuryBlue on Thu, 16 Dec 2021 12:49:55 +0100

preface

Compose is a tool for defining and running multi container Docker applications. With compose, you can use the YML file to configure all the services required by your application. Then, with one command, you can create and start all services from the YML configuration file.

As a PHP developer, Lnmp environment is often where we work and study. This blog is about how to use docker compose to build a development environment.

At present, docker compose is pre installed in most docker desktop environments, so it does not need to be installed separately. If you are not sure, you can enter docker compose -- version in your terminal. If you are prompted that there is no such command, please install it yourself.

PHP, mariadb, nginx, redis, composer, swoole, etc. are installed in the example environment of this article.

text

Build directory tree

In fact, it is recommended that you unify the Lnmp environment into one directory, so that it will be more convenient for you to migrate to other devices in the future. The blogger's directory tree is roughly shown in the figure below. The specific directory meaning can be understood by combining the name.

Write dockerfile

php dockerfile

This article only uses 7.3 as an example. If you need other versions or multiple versions, please add them yourself. Some PHP extensions are also pre installed in this example. If you need others, please fill them in by yourself.

# Mirror version
ARG VERSION=7.3

# base image 
FROM php:${VERSION}-fpm

# Adding some metadata to the image can be omitted
LABEL maintainer="ucwords" version=${VERSION}

# The configuration file directly replaces the default file in the container, and the following CMD does not need to be specified.
COPY ../../config/php/php.ini   /usr/local/etc/php/php.ini
COPY ../../config/php/www.conf  /usr/local/etc/php-fpm.d/www.conf

# Pre installed expansion pack
COPY ../../src/composer.phar    /work/src/composer.phar
COPY ../../src/redis-5.1.0.tgz  /work/src/redis-5.1.0.tgz
COPY ../../src/swoole-4.6.0.tgz /work/src/swoole-4.6.0.tgz

# The update, installation and configuration environment can be added or deleted appropriately according to your needs.
# deb http: * * * these are to speed up the data and change the image. If your network accesses foreign images, OK. It can be removed.
RUN echo '' > /etc/apt/sources.list \
    && echo \
    deb http://mirrors.aliyun.com/debian/ buster main non-free contrib \
    deb http://mirrors.aliyun.com/debian-security buster/updates main \
    deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib \
    deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib \
    deb-src http://mirrors.aliyun.com/debian-security buster/updates main \
    deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib \
    deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib \
    deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib \
    > /etc/apt/sources.list \
    && apt-get update \
    && apt-get remove -y libssl1.1 \
    && apt-get install -y \
    wget zlib1g-dev libzip-dev htop vim less curl make gcc pkg-config inetutils-ping dnsutils git \
    openssl libssl-dev libmemcached-dev zlib1g-dev zlib1g ca-certificates \
    libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev libxpm-dev \
    && docker-php-ext-configure gd --with-gd --with-webp-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-xpm-dir --with-freetype-dir \
    && docker-php-ext-install zip pdo pdo_mysql mysqli opcache sockets bcmath gd \
# Update pecl
    && pecl channel-update pecl.php.net \
# redis
    && pecl install /work/src/redis-5.1.0.tgz \
    && docker-php-ext-enable redis \
    && rm /work/src/redis-5.1.0.tgz \
# swoole
    && mkdir swoole \
    && tar -xzf /work/src/swoole-4.6.0.tgz -C swoole --strip-components=1 \
    && rm /work/src/swoole-4.6.0.tgz \
    && ( \
        cd swoole \
        && phpize \
        && ./configure --enable-mysqlnd --enable-sockets --enable-openssl --enable-http2 \
        && make -j$(nproc) \
        && make install \
    ) \
    && docker-php-ext-enable swoole \
    && rm -r swoole \
# composer
    && chmod u+x /work/src/composer.phar \
    && mv /work/src/composer.phar /usr/local/bin/composer \
    && composer config -g repo.packagist composer https://mirrors.aliyun.com/composer \
# Closeout cleaning
    && apt-get clean \
    && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
    && ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone

# assign work directory
WORKDIR /work

# Claim port
EXPOSE 9000

# Start PHP FPM
CMD ["php-fpm", "-R"]

Mariadb dockerfile

mariadb is used here instead of mysql. Please pay attention to screening. It is best to plug in the configuration file and data.

# Specify version
ARG VERSION=10.5

# image
FROM mariadb:${VERSION}

# Adding some metadata to the image can be omitted
LABEL maintainer="ucwords" version="1.19.0"

# root default password
ENV MARIADB_ROOT_PASSWORD Your password

# Claim port
EXPOSE 3306

# run
CMD ["mysqld"]

Nginx dockerfile

# Specify version
ARG VERSION=1.19.0

# image
FROM nginx:${VERSION}

# Adding some metadata to the image can be omitted
LABEL maintainer="ucwords" version="1.19.0"

# configuration file
COPY ../../config/nginx/nginx.conf /etc/nginx/nginx.conf

# Claim port
EXPOSE 80 443

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

redis dockerfile

It's best to plug in the configuration file and data. If you want to delve into the source code of redis, you can see this series of articles< Redis series article navigation>.

# Mirror version
ARG VERSION=6.2.0

# base image 
FROM redis:${VERSION}

# Adding some metadata to the image can be omitted
LABEL maintainer="ucwords" version=${VERSION}

# configuration file
COPY  ../../config/redis/redis.conf /etc/redis/redis.conf

# Claim port
EXPOSE 6379

# When docker run s, specify the default program to run for the started container. When the program runs, the container ends.
CMD ["/usr/local/bin/redis-server", "/etc/redis/redis.conf"]

Write docker compose YML

Put all the above containers into a bridged network environment (specified in the networks section), so that you can communicate directly by using the container name.

version: '3'
services:
  nginx:
    build:
      context: .
      dockerfile: dockerfile/nginx/Dockerfile
    image: nginx:1.19.0
    restart: always
    container_name: lnmp_nginx
    volumes:
      - ./config/nginx/vhosts:/etc/nginx/conf.d:rw
      - ./work:/work:rw
    networks:
      - lnmp
    depends_on:
      - fpm_73
      - mariadb
    ports:
      - "80:80"
      - "443:443"
    ulimits:
      nproc: 65535
      nofile:
        soft: 20000
        hard: 40000
    tty: true
    #mem_limit: 512m #Memory limit
  fpm_73:
    build:
      context: .
      dockerfile: dockerfile/php/Dockerfile_7.3
    image: php:7.3-fpm
    restart: always
    container_name: lnmp_fpm_73
    volumes:
      - ./work:/work:rw
    networks:
      - lnmp
    ports:
      - "9500:9500"
      - "9501:9501"
    tty: true
    #mem_limit: 512m #Memory limit
  redis:
    build:
      context: .
      dockerfile: dockerfile/redis/Dockerfile
    image: redis:6.2.0
    restart: always
    container_name: lnmp_redis
    ports:
      - "6379:6379"
    volumes:
      - ./data/redis:/data:rw
    environment:
      # Set the environment variable time zone code UTF-8
      TZ: Asia/Shanghai
      LANG: en_US.UTF-8
    networks:
      - lnmp
    tty: true
    #mem_limit: 512m #Memory limit
  mariadb:
    build:
      context: .
      dockerfile: dockerfile/mysql/Dockerfile
    image: mariadb:10.5
    restart: always
    container_name: lnmp_mariadb
    volumes:
      - ./data/mysql:/var/lib/mysql:rw
    networks:
      - lnmp
    ports:
      - "3306:3306"
    ulimits:
      nproc: 65535
      nofile:
        soft: 20000
        hard: 40000
    tty: true
    #mem_limit: 512m #Memory limit
networks:
  lnmp:
    driver: bridge

start-up

Please first write according to the directory tree, docker file and YML file of the article, or you can directly download the resources in the example of this article( Point me).

And then in and docker compose Execute docker compose up - D in the directory at the same level of YML, wait for the installation to complete, and you can have an out of the box environment.

summary

This paper explains the one click building tutorial of Lnmp environment from the perspective of graphics and code. This environment has great expansibility. Notice several points in this example.

  1. docker-compose. The context of YML is dependent on the file directory tree in this example. If you don't want to use this directory tree, you need to make some changes.
  2. In order to facilitate the migration or reconstruction of containers, the data and configuration of redis and mysql are plug-in to the physical host.
  3. Please note whether the working directory is consistent with the directory in the configuration file. For example, PHP is under / work, and the corresponding configuration in www.conf, such as slowlog, needs to correspond to the same working directory.

Topics: PHP Docker lnmp docker compose