docker php项目部署流程(Docker如何使用docker创建一个PHP)
我所用的Linux是Ubuntu,以下命令全是在Ubuntu下执行的。
第1步创建一个叫docker_php的目录并进入这个目录。下面的所有步骤都是在这个Docker_php目录里进行。
$ mkdir docker_php
$ cd docker_php
# 查看当前目录
decisionmaker@tdar-srv:~/examples/docker_php$ pwd
/home/decisionmaker/examples/docker_php
# 在当前目录下,创建一个叫init.sh的脚本,写入以下命令
decisionmaker@tdar-srv:~/examples/docker_php$ vi init.sh
mkdir -p code/learn_php
mkdir -p config/nginx/
touch config/nginx/learn_php.conf
touch code/learn_php/index.php
touch docker-compose.yml
touch Dockerfile_php81
# 在当前目录下,执行init.sh生成基本的目录和文件结构
decisionmaker@tdar-srv:~/examples/docker_php$ sh init.sh
# 最后,列出当前目录下生成的目录和文件
decisionmaker@tdar-srv:~/examples/docker_php$ tree
.
├── code
│ └── learn_php
│ └── index.php
├── config
│ └── nginx
│ └── learn_php.conf
├── docker-compose.yml
├── Dockerfile_php81
└── init.sh
4 directories, 5 files
当前目录下的目录和文件结构
第3步在当前目录下,执行vi Dockerfile_php81命令,添加以下内容:
FROM php:8.1-fpm
RUN cat /etc/apt/sources.list
RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/;s/security.debian.org/mirrors.aliyun.com/' /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y apt-utils
RUN apt-get install -y bzip2
RUN apt-get install -y libbz2-dev
RUN apt-get install -y zip
RUN apt-get install -y libzip-dev
RUN docker-php-ext-configure pdo_mysql
RUN docker-php-ext-configure mysqli
RUN docker-php-ext-configure pcntl
RUN docker-php-ext-configure bcmath
RUN docker-php-ext-configure bz2
RUN docker-php-ext-configure opcache
RUN docker-php-ext-install pdo_mysql mysqli pcntl bcmath bz2 opcache
RUN docker-php-ext-enable pdo_mysql mysqli pcntl bcmath bz2 opcache
RUN apt-get install -y libpq-dev \
&& docker-php-ext-configure pgsql \
&& docker-php-ext-install pdo pdo_pgsql pgsql
RUN docker-php-ext-configure exif \
&& docker-php-ext-install exif
RUN apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng-dev \
&& docker-php-ext-install -j$(nproc) iconv \
&& docker-php-ext-configure gd \
&& docker-php-ext-install -j$(nproc) gd
RUN docker-php-ext-configure gd --with-freetype
RUN docker-php-ext-install -j$(nproc) gd
RUN pecl install -o -f redis \
&& docker-php-ext-enable redis
RUN pecl install xdebug && docker-php-ext-enable xdebug
RUN pecl install zip && docker-php-ext-enable zip
RUN pecl install swoole && docker-php-ext-enable swoole
RUN pecl install msgpack && docker-php-ext-enable msgpack
RUN pecl install igbinary && docker-php-ext-enable igbinary
RUN rm -rf /tmp/pear
RUN cd /root \
&& php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
&& php composer-setup.php \
&& php -r "unlink('composer-setup.php');" \
&& mv composer.phar /usr/local/bin/composer
RUN composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
# Cancel
# composer config -g --unset repos.packagist
RUN apt-get install -y mariadb-client
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
在当前目录下,执行vi docker-compose.yml命令,添加以下内容:
version: '3'
networks:
network:
ipam:
driver: default
config:
- subnet: '192.168.87.0/24'
services:
nginx:
networks:
network:
ipv4_address: 192.168.87.11
image: nginx:latest
ports:
- "9080:9080"
volumes:
- ./code/learn_php:/var/www/learn_php
- ./config/nginx/learn_php.conf:/etc/nginx/conf.d/000_learn_php.conf
links:
- php81
php81:
networks:
network:
ipv4_address: 192.168.87.12
build:
context: ./
dockerfile: ./Dockerfile_php81
volumes:
- ./code/learn_php:/var/www/learn_php
links:
- mysql
- redis
mysql:
networks:
network:
ipv4_address: 192.168.87.13
image: mysql
ports:
- "13306:3306"
volumes:
- mysql_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: learn_php
MYSQL_USER: learn_php
MYSQL_PASSWORD: 123456
command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci', '--default-authentication-plugin=mysql_native_password', '--max_allowed_packet=32505856']
cap_add:
- SYS_NICE # solve mbind: Operation not permitted
redis:
networks:
network:
ipv4_address: 192.168.87.14
image: redis
# command: redis-server --requirepass 123456
ports:
- "16379:6379"
volumes:
- redis_data:/data
volumes:
mysql_data:
redis_data:
在当前目录下,执行vi config/nginx/learn_php.conf命令,添加以下内容:
server {
listen 9080;
server_name learn_php;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/learn_php;
index index.html index.php index.htm;
client_max_body_size 128m;
# pass the PHP scripts to FastCGI server
location ~ \.php$ {
fastcgi_pass 192.168.87.12:9000;
fastcgi_index index.php;
fastcgi_read_timeout 150;
fastcgi_param script_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
在当前目录下,执行vi code/learn_php/index.php,添加以下内容:
<?php
phpinfo();
decisionmaker@tdar-srv:~/examples/docker_php$ sudo docker-compose build
# 执行以下命令,运行已经构建好的镜像
decisionmaker@tdar-srv:~/examples/docker_php$ sudo docker-compose up
sudo docker-compose up
第9步打开浏览器,输入网址:http://127.0.0.1:9080/,查看结果:
访问http://127.0.0.1:9080/
第10步:在当前目录下,执行docker-compose down销毁容器
decisionmaker@tdar-srv:~/examples/docker_php$ sudo docker-compose down
Removing docker_php_nginx_1 ... done
Removing docker_php_php81_1 ... done
Removing docker_php_redis_1 ... done
Removing docker_php_mysql_1 ... done
Removing network docker_php_network
sudo docker-compose down
,免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com