基于docker-compose.yml文件搭建lnmp

docker-compose.yml搭建lnmp

    • 安装docker-compose
    • 创建目录和文件
    • 编写php/Dockerfile
    • 编写docker-compose.yml
    • 查看容器是否启动
    • 配置php
    • 配置nginx
    • 测试mysql
    • 验证php

安装docker-compose

[root@node1 ~]# curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   633  100   633    0     0    499      0  0:00:01  0:00:01 --:--:--   499
100 12.1M  100 12.1M    0     0  1276k      0  0:00:09  0:00:09 --:--:-- 1891k
[root@node1 ~]# ll /usr/local/bin/docker-compose 
-rw-r--r--. 1 root root 12737304 89 04:40 /usr/local/bin/docker-compose
[root@node1 ~]# chmod +x /usr/local/bin/docker-compose 


[root@node1 ~]# docker-compose --version
docker-compose version 1.29.2, build 5becea4c

·

创建目录和文件

[root@node1 ~]# tree compose_lnmp/
compose_lnmp/
├── docker-compose.yml
└── php
    └── Dockerfile

1 directory, 2 files

编写php/Dockerfile

  • 由于官方的php-fpm镜像缺少一些扩展功能,所以要使用dockerfile构建新的镜像
  • 注意配置时区和mysql扩展

[root@node1 ~]# vim compose_lnmp/php/Dockerfile 
FROM php:7.0-fpm
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev \
        libmemcached-dev \
        zlib1g-dev \
        libcurl4-openssl-dev \
        libxml2-dev \
        --no-install-recommends && rm -rf /var/lib/apt/lists/* \
    && docker-php-ext-install -j$(nproc) \
        iconv mcrypt gettext curl mysqli pdo pdo_mysql zip \
        mbstring bcmath opcache xml simplexml sockets hash soap \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd

CMD ["php-fpm", "-F"]

编写docker-compose.yml

  • 官方推荐使用volume优于bind mount
  • volume默认存放在/var/lib/docker/volumes下

[root@node1 ~]# vim compose_lnmp/docker-compose.yml
version: "3"

services:
  mysql:
    hostname: mysql
    restart: always
    image: mysql:5.6
    container_name: mysql
    ports:
      - "3306:3306"
    volumes:
      - mysql-config:/etc/mysql
      - mysql-log:/var/log/mysql
      - mysql-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_USER: user
      MYSQL_PASSWORD: user123
  php:
    hostname: php
    restart: always
    container_name: php
    build:
      context: ./php
      dockerfile: Dockerfile
    ports:
      - "9000:9000"
    links:
      - mysql:mysql
    volumes:
      - nginx-html:/var/www/html
      - php-config:/usr/local/etc
  nginx:
    hostname: nginx
    restart: always
    container_name: nginx
    image: nginx:1.17.0
    ports:
      - "80:80"
      - "443:443"
    links:
      - "php:php"
    volumes:
      - nginx-config:/etc/nginx
      - nginx-log:/var/log/nginx
      - nginx-html:/usr/share/nginx/html

volumes:
  mysql-config:
  mysql-log:
  mysql-data:
  nginx-html:
  php-config:
  nginx-config:
  nginx-log:

启动服务

[root@node1 ~]# cd compose_lnmp/
[root@node1 compose_lnmp]# docker-compose up -d //启动
Creating network "compose_lnmp_default" with the default driver
Creating volume "compose_lnmp_mysql-config" with default driver
Creating volume "compose_lnmp_mysql-log" with default driver
Creating volume "compose_lnmp_mysql-data" with default driver

[root@node1 compose_lnmp]# docker-compose restart  //重启

查看容器是否启动

[root@localhost compose_lnmp]# docker ps
CONTAINER ID   IMAGE              COMMAND                  CREATED          STATUS          PORTS                                                                      NAMES
5283d0f88570   nginx:1.17.0       "nginx -g 'daemon of…"   36 seconds ago   Up 33 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   nginx
03579e9f302e   compose_lnmp_php   "docker-php-entrypoi…"   38 seconds ago   Up 36 seconds   0.0.0.0:9000->9000/tcp, :::9000->9000/tcp                                  php
7b23afc86436   mysql:5.6          "docker-entrypoint.s…"   40 seconds ago   Up 38 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp 

数据化持久目录

[root@localhost compose_lnmp]# docker volume ls
DRIVER    VOLUME NAME
local     78d81a1d5ef2bf7966317bbc7f46f0e5014c5e44d2b2617ddce23044bfe52cb2
local     167b316bf399c5ed20c6ebbfb4707d4bdfeaeb228a8ed34b5c1cf9b36473737a
local     c9aaa3511050f57ee02f2972ac1220774a73c1c317de2bb0370a056f6186c6aa
local     compose_lnmp_mysql-config
local     compose_lnmp_mysql-data
local     compose_lnmp_mysql-log
local     compose_lnmp_nginx-config
local     compose_lnmp_nginx-html
local     compose_lnmp_nginx-log
local     compose_lnmp_php-config
local     f10ca428e6a0e928904eaaeaac762b7549ddda5cba96211e8e7910520e23eca3

配置php

//默认的php.ini的文件是没有的,需要手动把模板配置文件复制为php.ini
[root@localhost compose_lnmp]# cd /var/lib/docker/volumes/compose_lnmp_php-config/_data/php
[root@localhost php]# ls
conf.d  php.ini-development  php.ini-production
[root@localhost php]# cp php.ini-production php.ini


[root@localhost php]# vim php.ini
date.timezone = Asia/shanghai
//取消注释,修改时区

配置nginx


server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;  //此处添加index.php
    }




    location ~ \.php$ {
        root           html;
        fastcgi_pass   php:9000;  //php为容器的名称
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   /var/www/html$fastcgi_script_name; //此处更改为存放网页的地方
        include        fastcgi_params;
    }

测试mysql

[root@localhost ~]# docker container exec -it mysql bash

root@mysql:/# mysql -uroot -p123456

Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.51 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

验证php

编写php页面

[root@localhost ~]# vim /var/lib/docker/volumes/compose_lnmp_nginx-html/_data/index.php
<?php
    phpinfo();
?>

重启所有容器

[root@localhost ~]# cd compose_lnmp/
[root@localhost compose_lnmp]# docker-compose restart
Restarting nginx ... done
Restarting php   ... done
Restarting mysql ... done


[root@localhost compose_lnmp]# ss -antl
State     Recv-Q    Send-Q         Local Address:Port         Peer Address:Port    Process    
LISTEN    0         128                  0.0.0.0:80                0.0.0.0:*                  
LISTEN    0         128                  0.0.0.0:22                0.0.0.0:*                  
LISTEN    0         128                  0.0.0.0:443               0.0.0.0:*                  
LISTEN    0         128                  0.0.0.0:9000              0.0.0.0:*                  
LISTEN    0         128                  0.0.0.0:3306              0.0.0.0:*                  
LISTEN    0         128                     [::]:80                   [::]:*                  
LISTEN    0         128                     [::]:22                   [::]:*                  
LISTEN    0         128                     [::]:443                  [::]:*                  
LISTEN    0         128                     [::]:9000                 [::]:*                  
LISTEN    0         128                     [::]:3306                 [::]:*       

基于docker-compose.yml文件搭建lnmp_第1张图片

你可能感兴趣的:(Docker)