使用Dockerfile和dockercompose构建LAMP官方镜像

启动mariadb将数据库初始化

docker run --rm --name some-mariadb -v /root/tierama/mariadb/data:/var/lib/mysql -e MARIADB_ROOT_PASSWORD=mysecret -d mariadb:10.6
docker stop some-mariadb

自定义httpd.conf文件

docker run --rm httpd:2.4 cat /usr/local/apache2/conf/httpd.conf > my-httpd.conf
vim httpd.conf

加上

ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1

修改User,Group和PHP-FPM一致

编写docker-compose.yml和Dockerfile文件

[root@iZj6c5u0jb8ritgz2h3tjfZ tierama]# pwd
/root/tierama

[root@iZj6c5u0jb8ritgz2h3tjfZ tierama]# ls
docker-compose.yml  httpd  mariadb  php  webroot

[root@iZj6c5u0jb8ritgz2h3tjfZ tierama]# cat docker-compose.yml 
version: "3.9"
services:
    db:
        image: "mariadb:10.6"
        restart: always
        volumes:
            - "/root/tierama/mariadb/data:/var/lib/mysql"
        networks:
            - backend

    php:
        build: php
        volumes:
            - "/root/tierama/webroot:/var/www/html"
            - "/root/tierama/php/php.ini:/usr/local/etc/php/php.ini"
        networks:
            - frontend
            - backend

    httpd:
        build: httpd
        ports:
            - "80:80"
            - "443:443"
        volumes:
            - "/root/tierama/webroot:/usr/local/apache2/htdocs"
            - "/etc/letsencrypt/live/www.tierama.com/fullchain.pem:/usr/local/apache2/conf/server.crt"
            - "/etc/letsencrypt/live/www.tierama.com/privkey.pem:/usr/local/apache2/conf/server.key"
        networks:
            - frontend

networks:
    frontend:
    backend:

[root@iZj6c5u0jb8ritgz2h3tjfZ tierama]# cat httpd/
conf/       Dockerfile  

[root@iZj6c5u0jb8ritgz2h3tjfZ tierama]# cat httpd/Dockerfile 
FROM httpd:2.4.48
COPY ./conf/httpd.conf /usr/local/apache2/conf/httpd.conf
COPY ./conf/extra/httpd-ssl.conf /usr/local/apache2/conf/extra/httpd-ssl.conf

[root@iZj6c5u0jb8ritgz2h3tjfZ tierama]# cat php/Dockerfile 
FROM php:7.4.22-fpm                                                              
RUN apt-get update && apt-get install -y \                                       
        libfreetype6-dev \                                                       
        libjpeg62-turbo-dev \                                                    
        libpng-dev \                                                             
    && docker-php-ext-configure gd --with-freetype --with-jpeg \                 
    && docker-php-ext-install -j$(nproc) gd \                                    
    && docker-php-ext-configure mysqli \                                         
    && docker-php-ext-install mysqli \                                           
    && sed -i 's/127.0.0.1:9000/0.0.0.0:9000/1' /usr/local/etc/php-fpm.d/www.conf

注意修改项

  • webroot的权限修改
docker exec -it tierama_httpd_1 /bin/bash
chown -R www-data:www-data htdocs/*
exit
  • 将php.ini,httpd-ssl.conf等配置文件自定义配置
docker cp tierama_httpd_1:/usr/local/apache2/conf/extra/httpd-ssl.conf httpd/conf/extra/httpd-ssl.conf
docker cp tierama_php_1:/usr/local/etc/php/php.ini-production php.ini
vim php.ini
vim httpd-ssl.conf

启动docker-compose

[root@iZj6c5u0jb8ritgz2h3tjfZ tierama]# docker-compose up -d
Creating network "tierama_backend" with the default driver
Creating network "tierama_frontend" with the default driver
Creating tierama_httpd_1 ... done
Creating tierama_php_1   ... done
Creating tierama_db_1    ... done

[root@iZj6c5u0jb8ritgz2h3tjfZ tierama]# docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED             STATUS             PORTS                                                                      NAMES
6e464a37bbb3   tierama_httpd   "httpd-foreground"       About an hour ago   Up About an hour   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp   tierama_httpd_1
1c725011fe43   tierama_php     "docker-php-entrypoi…"   About an hour ago   Up About an hour   9000/tcp                                                                   tierama_php_1
5a086f147fdd   mariadb:10.6    "docker-entrypoint.s…"   About an hour ago   Up About an hour   3306/tcp

Leave a reply:

Your email address will not be published.

Site Footer