docker使用指南

使用Docker搭建CTF Pwn做题环境

为了保证利用脚本能够正常打通,
我们通常需要在本地准备相同的运行环境
并且在远程利用之前先在本地进行测试

如果为每个不同的运行环境都单独准备一个Ubuntu虚拟机,
则太不优雅


Docker

操作系统层面上的虚拟化方案
能够非常便捷地搭建不同pwn题所对应的原始环境

[!note] 对比虚拟机
硬件层面的虚拟化方案

Docker使用

Docker入门教程(非常详细)从零基础入门到精通,看完这一篇就够了_docker教程-CSDN博客
Environment - CTF Wiki


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
54
❯ cat /etc/ssh/ssh_config

# This is the ssh client system-wide configuration file. See
# ssh_config(5) for more information. This file provides defaults for
# users, and the values can be changed in per-user configuration files
# or on the command line.

# Configuration data is parsed as follows:
# 1. command line options
# 2. user-specific file
# 3. system-wide file
# Any configuration value is only changed the first time it is set.
# Thus, host-specific definitions should be at the beginning of the
# configuration file, and defaults at the end.

# Site-wide defaults for some commonly used options. For a comprehensive
# list of available options, their meanings and defaults, please see the
# ssh_config(5) man page.

Include /etc/ssh/ssh_config.d/*.conf

Host *
# ForwardAgent no
# ForwardX11 no
# ForwardX11Trusted yes
# PasswordAuthentication yes
# HostbasedAuthentication no
# GSSAPIAuthentication no
# GSSAPIDelegateCredentials no
# GSSAPIKeyExchange no
# GSSAPITrustDNS no
# BatchMode no
# CheckHostIP yes
# AddressFamily any
# ConnectTimeout 0
# StrictHostKeyChecking ask
# IdentityFile ~/.ssh/id_rsa
# IdentityFile ~/.ssh/id_dsa
# IdentityFile ~/.ssh/id_ecdsa
# IdentityFile ~/.ssh/id_ed25519
# Port 22
# Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc
# MACs hmac-md5,hmac-sha1,umac-64@openssh.com
# EscapeChar ~
# Tunnel no
# TunnelDevice any:any
# PermitLocalCommand no
# VisualHostKey no
# ProxyCommand ssh -q -W %h:%p gateway.example.com
# RekeyLimit 1G 1h
# UserKnownHostsFile ~/.ssh/known_hosts.d/%k
SendEnv LANG LC_*
HashKnownHosts yes
GSSAPIAuthentication yes

网络问题

ping 命令不走配置代理的原因
通过配置代理是不能影响ping命令的。大家都知道ping命令是直接使用icmp协议来检测网址是否可达的。而我们配置的代理是直接配置了http代理。

Http是应用层协议,icmp是网络层协议。配置代理过程是配置了应用层协议,是不会影响网络层协议的,也就解释了设置http代理以后在终端还是ping不通Google

可以用curl


问题:

查看docker占用的存储空间

1
docker system df

清除build cache缓存

1
docker builder prune

重启docker服务

1
sudo systemctl restart docker

真神解决方案:
从Docker拉取镜像一直失败超时?这些解决方案帮你解决烦恼_docker拉取镜像超时-CSDN博客
我采用的方案二:
使用代理拉取镜像:
一、 创建配置文件

1
2
sudo mkdir -p /etc/systemd/system/docker.service.d 
sudo vim /etc/systemd/system/docker.service.d/http-proxy.conf

二、在配置文件中添加代理

1
Environment="HTTP_PROXY=http://127.0.0.1:7890" Environment="HTTPS_PROXY=http://127.0.0.1:7890"

三、重启docker

1
2
3
sudo systemctl restart docker
# 或(wsl)
sudo service restart docker

四、查看环境变量

1
sudo systemctl show --property=Environment docker

终于搞好了,泪目


swap file


查看已有镜像

1
2
3
4
docker images

# 包括中间层
docker images all

构建镜像

1
docker build -t pwnenv_ubuntu24 .

需指定tag(镜像名称)和构建上下文

创建并启动容器
守卫模式(后台运行)
映射端口
命名容器
挂载目录
镜像名

1
2
3
4
5
6
7
8
9
10
# 创建并启动容器
docker run \
-d \
-p 25000:22 \
--name=pwn14 \
-v ~/ctfDojo:/ctfDojo \
pwnenv_ubuntu14

# 仅创建,不启动容器
docker create

在已经运行的容器中执行命令 指定工作目录
设置环境变量TERM(显示相关)
指定容器用户
-i 交互模式 -t 分配伪终端(用于交互式会话) 并指定目标容器
启动bash

1
2
3
4
5
docker exec -w /ctfDojo \
-e TERM=xterm-256color \
-u ubuntu \
-it pwn24 \
bash

使用ssh连接入容器环境

1
ssh root@localhost -p 25000
1
ssh ubuntu@localhost -p 25000

docker常用命令

停止容器

1
docker stop <name>

启动容器

1
2
# 启动已停止或已创建的容器
docker start <容器ID或容器名>

查看容器

1
2
3
4
5
# 查看运行中的容器
docker ps

# 查看所有容器(包括停止的容器)
docker ps -a

删除容器

1
2
# 删除单个容器
docker rm <容器ID或容器名>

查看镜像列表

1
docker images

删除镜像

1
2
3
4
5
6
7
8
# 删除单个镜像
docker rmi <镜像ID或镜像名:标签>

# 删除所有镜像
docker rmi $(docker images -q)

# 删除所有未使用的镜像
docker images prune

docker pull可以成功但是docker build失败的奇怪问题

docker build 和 docker pull 的行为不同

  • **docker pull**:

    • 直接从 Docker Hub 拉取镜像到本地。
    • 如果网络连接正常,且镜像可以被成功解析和下载,docker pull 通常不会有问题。
  • **docker build**:

    • 在构建过程中,Docker 会尝试解析 Dockerfile 中的 FROM 指令,并从 Docker Hub 拉取基础镜像。
    • 如果 Docker 守护进程的网络配置(如代理、DNS)有问题,可能导致 docker build 无法解析或拉取镜像,即使 docker pull 可以成功。

sudo vim /etc/docker/daemon.json设置dns方法是不行的(8.8.8.8和8.8.4.4)仅5s就error 显示报错connection reset

但是上文方法二配置代理的方法还是有用的
配置代理后,docker pull可以成功(否则不能成功)
然后再docker build即可成功


补:

今天尝试在Dockerfile文件前面开头处添加配置代理即可直接build过程中拉取成
(看我放的位置,其实和它没关系)

再补:
并非,去掉之后也能拉取成功,看来直接build得看运气


pwndocker

skysider/pwndocker


关于pwn题的环境

目前我所已知的三种较为推荐的方式:将本地环境和远程环境相统一

  1. patchelf + glibc-all-in-one
    更改pwn题libc

  2. 在脚本中打开本地进程这么写

    1
    process(["ld路径“, "./题目"], env={"LD_PRELOAD":"libc路径"})
  3. docker pwndocker


如何优雅的给 Docker 配置网络代理 - CharyGao - 博客园

Docker网络模式

docker容器内的网络问题

我的层次结构是
windows -> ubuntu(wsl2) -> ubuntu(docker)
(docker用的是linux版docker)
windows上有代理服务

我实在没搞好怎么在docker桥接模式下走代理

于是我使用host模式

终于搞好:

docker网络问题总结

docker网络问题中的三个代理:
系统代理 docker拉取镜像时不会自动使用系统的代理设置,如果需要通过代理拉取镜像,需要手动配置 Docker 的代理设置。
docker拉取镜像时走的代理 /etc/systemd/system/docker.service.d/http-proxy.conf中设置(/etc/docker/daemon.json可能也可以)
docker容器走的代理 和docker网络模式有关

以上。


docker使用指南
http://example.com/2025/03/16/docker不完全指北/
作者
yvyvSunlight
发布于
2025年3月16日
许可协议