Deploy php in docker

Clone project

git clone https://github.com/BurningBright/minishowcase
https://github.com/BurningBright/minishowcase

Pull images

https://hub.docker.com/_/php?tab=tags&page=1&name=5.6
pick version you like in docker
docker pull php:5.6.40-fpm-alpine

alpine release is not popular as the ‘apk’ command install software is not as usual as debian fedora.
so it’s better to pull other version php image like
docker pull php:5.6.40-fpm-jessie

pull stable version nginx version image.
docker pull nginx:stable

Prepare

Copy nginx configuration

start nginx container first
docker run --name nginx-tmp -d nginx:stable
copy default.conf
docker cp nginx-tmp:/etc/nginx/confi.d nginx-tmp/
delete tmp container
docker rm nginx-tmp

Start php container

1
2
3
docker run --name php-mini \
-p 9000:9000 -v /Users/root/minishowcase:/var/www \
-d php:5.6.40-fpm-alpine

run image mapping container port 9000 to host port 9000, mapping host project file to container.

check php inner ip

1
docker inspect --format='{{.NetworkSettings.IPAddress}}' php-mini

https://www.runoob.com/docker/docker-install-php.html

Start nginx container

modify conf.d/default.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
user  nginx;
worker_processes 1;
worker_rlimit_nofile 65535;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 65535;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;

sendfile on;
keepalive_timeout 65;
server_tokens off;
gzip on;
gzip_disable "msie6";

server {
listen 80;
server_name localhost;

root /var/www;
index index.html index.htm index.php;

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
fastcgi_pass 172.17.0.2:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}

error_page 500 502 503 504 /50x.html;
location ~ /\.ht {
deny all;
}
}
}

https://www.jianshu.com/p/5edfbb2af389

fastcgi_pass 172.17.0.2:9000; this line is php’s inner address.

start nginx

1
2
3
4
docker run --name nginx-mini \
-p 80:80 -v /Users/root/minishowcase:/var/www \
-v /Users/root/nginx-mini/conf.d:/etc/nginx/conf.d \
-d nginx:stable

check php status

add info.php check php status http://127.0.0.1/info.php

1
2
3
<?php
echo phpinfo();
?>

Install php extenstion

minishowcase needs php ‘gd’ extenstion, it be used to make thumbnail picture.

cause i use apline version php image, it wasted alot of time to insatll.

firt apt yum is not command to install dependency in apline, apk is the key.

https://blog.csdn.net/flame1980/article/details/98851803

get into php container
docker exec -it php-mini bash

install gd and it’s dependencies

1
2
3
4
5
6
7
8
9
10
apk upgrade --update && apk add \
coreutils \
freetype-dev \
libjpeg-turbo-dev \
libltdl \
libmcrypt-dev \
libpng-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

it’s better to write a Dockerfile to build a integrated image

1
2
3
4
5
6
7
8
9
10
11
12
FROM php:5.6.40-fpm-alpine
RUN docker-php-ext-install mysqli
RUN apk upgrade --update && apk add \
coreutils \
freetype-dev \
libjpeg-turbo-dev \
libltdl \
libmcrypt-dev \
libpng-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

check gd status
add gd.php to checkout gd extenstion.

1
2
3
4
5
6
7
8
<?php
if(extension_loaded('gd')) {
echo 'gd ready';
foreach(gd_info() as $cate=>$value)
echo "$cate: $value";
} else
echo 'no gd';
?>

https://blog.csdn.net/wt1286331074/article/details/91425518
https://www.jb51.cc/php/139142.html