Docker使用

docker

0x00 背景

给NewStar2024出题 , 正好学了一下怎么使用docker 以及使用环境设置动态flag

感谢Pazuris师傅, Baozongwi 师傅的帮助

0x01 准备

环境

win11 > wsl2 > Ub22

docker 安装

应用商店下载 exe (docker desktop)
登录

在desktop里

设置 resource 把 Ub22 勾选了

1
2
❯ docker -v
Docker version 27.2.0, build 3ab4256

注册 docker 仓库

1
2
3
4
5
ch0ico/chocolate

❯ docker login -u ch0ico
Password:
Login Succeeded

YML 启动

在 docker_res 目录 > wd > 新建一个 yml (docker-compose.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
version: '3.1'

services:
wordpress:
image: wordpress
restart: always
ports:
- 8080:80
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: exampleuser
WORDPRESS_DB_PASSWORD: examplepass
WORDPRESS_DB_NAME: exampledb
volumes:
- wordpress:/var/www/html

db:
image: mysql:8.0
restart: always
environment:
MYSQL_DATABASE: exampledb
MYSQL_USER: exampleuser
MYSQL_PASSWORD: examplepass
MYSQL_RANDOM_ROOT_PASSWORD: '1'
volumes:
- db:/var/lib/mysql

volumes:
wordpress:
db:
1
2
3
4
5
6
7
8
9
10
11
12
❯ docker-compose up -d
WARN[0000] /root/docker_Res/wordpress/docker-compose.yml: the attribute `version` is obsolete, it will be ignored, please remove it to avoid potential confusion
[+] Running 34/23
✔ db Pulled 102.1s
✔ wordpress Pulled 66.4s


[+] Running 5/5
✔ Network wordpress_default Created 0.1s
✔ Volume "wordpress_db" Created 0.0s ✔ Volume "wordpress_wordpress" Created 0.0s
✔ Container wordpress-db-1 Started 0.9s
✔ Container wordpress-wordpress-1 Started
1
http://127.0.0.1:8080/

已经可以访问了 这是用 yml 启动

0x02 构建镜像

编写 dockerfile
基于这些信息创建一个 Dockerfile 来部署你的 PHP 应用。

假设你的 Chocolate 目录中包含你的 PHP 代码,我们将使用 php:7.3.4-fpm 镜像,因为它包括了 PHP-FPM,可以更好地与 Nginx 配合使用。

以下是一个完整的 Dockerfile 示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 使用官方的PHP 7.3.4 FPM镜像
FROM php:7.3.4-fpm

# 安装Nginx
RUN apt-get update && apt-get install -y nginx

# 复制PHP代码到容器中
COPY Chocolate/ /var/www/html/

# 复制Nginx配置文件
COPY nginx.conf /etc/nginx/nginx.conf

# 启动Nginx
CMD service nginx start && php-fpm7.3 -F && tail -f /var/log/nginx/error.log

# 暴露80端口
EXPOSE 80

步骤解析

  1. 基础镜像:使用 php:7.3.4-fpm 镜像,它包括了 PHP 7.3.4 和 FPM。
  2. 安装Nginx:使用 apt-get 安装 Nginx。
  3. 复制PHP代码:将 Chocolate 目录中的 PHP 代码复制到容器的 /var/www/html/ 目录。
  4. 复制Nginx配置文件:将你的Nginx配置文件复制到容器的 /etc/nginx/nginx.conf
  5. 启动服务:启动 Nginx 和 PHP-FPM 服务,并监控 Nginx 的错误日志。
  6. 暴露端口:暴露 80 端口,以便外部可以访问 Nginx 服务。

我的 dockerfile 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
FROM docker.sysumsc.cn/php:fpm-alpine
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories &&\
apk add --update --no-cache nginx bash

COPY ./service/docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh

COPY ./config/nginx.conf /etc/nginx/nginx.conf

COPY src /var/www/html

RUN chown -R www-data:www-data /var/www/html

WORKDIR /var/www/html

EXPOSE 80

VOLUME ["/var/log/nginx"]

ENTRYPOINT [ "/docker-entrypoint.sh" ]

这里我一直拉不到官方仓库的alpine 所以直接从学校的镜像源拉了

0x03 完整环境

我的文件结构

1
2
3
4
5
6
7
8
9
10
11
choco
├── config
│ └── nginx.conf
├── service
│ └── docker-entrypoint.sh
├── src
│ ├── index.php
│ ├── choco.gif
│ └── nginx.htaccess
├── docker-compose.yml
└── Dockerfile

3.1 config文件

这是 Nginx 的配置文件,主要负责配置 Nginx 服务器的工作进程、事件模型和 HTTP 服务

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
# daemon off;

worker_processes auto;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php index.html index.htm;

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

location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

}
}

3.2 docker-entrypoint.sh

这是 Docker 容器的入口点脚本,用于在容器启动时执行一些初始化操作

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
#!/bin/sh

rm -f /docker-entrypoint.sh

# Get the user
user=$(ls /home)

# Check the environment variables for the flag and assign to INSERT_FLAG
if [ "$ICQ_FLAG" ]; then
INSERT_FLAG="$ICQ_FLAG"
export ICQ_FLAG=no_FLAG
ICQ_FLAG=no_FLAG
elif [ "$FLAG" ]; then
INSERT_FLAG="$FLAG"
export FLAG=no_FLAG
FLAG=no_FLAG
elif [ "$GZCTF_FLAG" ]; then
INSERT_FLAG="$GZCTF_FLAG"
export GZCTF_FLAG=no_FLAG
GZCTF_FLAG=no_FLAGdocker
else
INSERT_FLAG="flag{TEST_Dynamic_FLAG}"
fi

# 将FLAG写入文件
echo $INSERT_FLAG | tee /flag

chmod 744 /flag

php-fpm & nginx &

echo "Running..."

tail -F /var/log/nginx/access.log /var/log/nginx/error.log

3.3 docker-compose.yml

这是 Docker Compose 的配置文件,用于定义和运行多容器 Docker 应用

1
2
3
4
5
6
7
8
9
10
version: '3.3'
services:
web:
build: .
image: eci_2024newstar_week4_chocolate:latest
container_name: week4_chocolate
environment:
- ICQ_FLAG=flag{test_real_flag}
ports:
- '0.0.0.0:32892:80'

0x04 启动容器

构建和运行 Docker 镜像

  1. 构建 Docker 镜像

    1
    docker build -t eci_2024newstar_week4_chocolate .
  2. 运行 Docker 容器

    1
    docker run -d -p eci_2024newstar_week4_chocolate

这将构建一个名为 eci_2024newstar_week4_chocolate 的 Docker 镜像,并运行一个容器,将主机的 80 端口映射到容器的 80 端口

除了的dockerfile启动外

也可以使用

  1. docker-compose 快速启动
1
2
3
docker build . -t eci_2024newstar_week4_chocolate:latest

docker-compose up -d

注意事项

  • 确保你的 Chocolate 目录中的 PHP 代码是正确的。
  • 确保你的 Nginx 配置文件是正确的,并且路径和设置符合你的需求。
  • 如果你的 PHP 代码需要额外的 PHP 扩展,可以在 Dockerfile 中添加相应的安装命令。

这样,就可以通过 Docker 部署你的 Nginx 和 PHP 应用了。