Docker

Docker(更新中)

先说说为什么要写这篇文章,大概只是记录一下两天啥也没搞出来的痛苦,总不能啥也没学会吧

php7.3以上 + MariaDB

这个版本下面的MariaDB,不会自启动,需要进行初始化处理,同时,安装插件的过程比较抽象,某些部分也是需要看chatgpt以及文档慢慢调试

Dockerfile

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
FROM php:7.4-apache


RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

# Update apt and install GD extension dependencies
RUN apt-get update && \
apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev && \
# Install GD extension
docker-php-ext-configure gd --with-freetype --with-jpeg && \
docker-php-ext-install -j$(nproc) gd

# Install PDO MySQL extension
RUN docker-php-ext-install pdo_mysql

# Install MySQLi extension
RUN docker-php-ext-install mysqli


# Install MariaDB 10.5
RUN apt-get install -y mariadb-server-core-10.5 mariadb-server-10.5 supervisor

RUN /etc/init.d/mariadb start && \
mysql -e "CREATE USER 'test'@'%' IDENTIFIED BY 'test';" && \
mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'test'@'%' WITH GRANT OPTION;" && \
mysql -e "" && \
mysql -e "FLUSH PRIVILEGES;"

COPY . /var/www/html
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
WORKDIR /var/www/html

# Expose ports
EXPOSE 80
EXPOSE 3306

# Set the container's startup command to apache2-foreground and start MariaDB service
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]


然后可以进行多线程操作

supervisord.conf

1
2
3
4
5
6
7
8
9
[supervisord]
nodaemon=true

[program:apache2]
command=apache2-foreground

[program:mariadb]
command=/etc/init.d/mariadb start

php7.2以下 + MariaDB

在较低版本下面的MariaDB,会直接自启动,所以并不需要多线程操作

较为粗糙来写的话,用下面的这个即可

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
FROM php:7.2-apache

# 将php.ini-production重命名为php.ini
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

# 更新apt并安装GD扩展的依赖项
RUN apt-get update && \
apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev && \
# 配置GD扩展
docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && \
# 安装GD扩展
docker-php-ext-install -j$(nproc) gd

# 安装PDO MySQL扩展
RUN docker-php-ext-install pdo_mysql

# 安装MySQLi扩展
RUN docker-php-ext-install mysqli

# 安装MariaDB 10.3
RUN apt-get install -y mariadb-server-10.3

# 设置MySQL root 用户密码和创建新用户
RUN service mysql start && \
mysql -e "UPDATE mysql.user SET Password = PASSWORD('your_root_password') WHERE User = 'root';" && \
mysql -e "CREATE USER 'your_user'@'%' IDENTIFIED BY 'your_password';" && \
mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'your_user'@'%' WITH GRANT OPTION;" && \
mysql -e "FLUSH PRIVILEGES;"

# Expose ports
EXPOSE 80
EXPOSE 3306

# 设置容器的启动命令为apache2-foreground和启动MariaDB服务
CMD ["bash", "-c", "apache2-foreground & service mysql start && tail -f /dev/null"]


php + mysql

对于不是在线进行安装的,不需要过多进行操作的,可以直接写docker-compose.yml + Dockerfile 进行处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
version: '3.0'
services:
webserver:
build: .
ports:
- "8081:80"
links:
- db
volumes:
- ./:/var/www/html
db:
image: mysql:5.6
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_DATABASE: aaa
MYSQL_USER: aaa
MYSQL_PASSWORD: aaa
MYSQL_ROOT_PASSWORD: rootroot
volumes:
- ./mysql-init-files:/docker-entrypoint-initdb.d # 放置sql文件
networks:
- default

设置好文件以及sql文件就可以了

多容器手法

使用links或者depends_on,就可以将多个镜像和容器进行连接,然后进行处理(超过2个可以考虑depends_on,2个只要links)

类似于这样,如此就不太需要怎么样进行配置,至于前面的Dockerfile写得如此复杂,是因为在本地调试的时候放在一起更加方便一些(有时间我会写成yml)

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
version: '3.1'

services:
nginx:
image: nginx:1.25.2-alpine
restart: unless-stopped
volumes:
- ./front:/front
- ./conf/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
mem_limit: 256m
pids_limit: 1024
cpus: 1
ports:
- "5000:80"
waf:
build:
dockerfile: waf.Dockerfile
context: .
mem_limit: 256m
pids_limit: 1024
cpus: 1
environment:
- PROXY_HOST=http://api:5000
depends_on:
- api
restart: unless-stopped
api:
build:
dockerfile: app.Dockerfile
context: .
depends_on:
- db
restart: unless-stopped
environment:
MYSQL_PASSWORD: password
mem_limit: 256m
pids_limit: 1024
cpus: 1

db:
image: mariadb:latest
restart: unless-stopped
volumes:
- ./conf/db_init:/docker-entrypoint-initdb.d
environment:
MYSQL_USER: app
MYSQL_DATABASE: app
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: root_password
cpus: 1
pids_limit: 256
mem_limit: 512m