はじめに

docker composeで立ち上げたコンテナ群に対して、何か調査用にインストールをしたいけど、本番稼働しているコンテナに入れたくないというときに何かないかなと思ったので調べたメモ

環境

1
2
3
Windows 11 Professional
WSL2 Ubuntu24.04 LTS
Docker Desktop 4.33.1 (161083)

試す

docker compose run を使う

普段コンテナに入るときは、docker compose execを使っていると思うが、新しく作業用コンテナを立ち上げる際には docker compose runを使う。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
services:
  web:
    container_name: web
    image: nginx:1.27.0
    ports:
      - "8080:80"
    depends_on: 
      - db
  db:
    container_name: db
    image: mysql:8.4.2
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: secret
  monitor:
    container_name: monitor
    build:
      context: .
    stdin_open: true
    tty: true
1
2
3
4
5
6
FROM debian:bookworm-slim

RUN apt update && \
    apt install -y docker.io docker-compose

ENTRYPOINT ["sh"]

先日作成した DooDの記事のDockerfileを使う。

これを立ち上げる。

1
docker compose up -d
1
2
3
4
5
$ docker compose ps
NAME      IMAGE               COMMAND                  SERVICE   CREATED         STATUS         PORTS
db        mysql:8.4.2         "docker-entrypoint.s…"   db        4 seconds ago   Up 3 seconds   3306/tcp, 33060/tcp
monitor   dood-test-monitor   "sh"                     monitor   4 seconds ago   Up 3 seconds   
web       nginx:1.27.0        "/docker-entrypoint.…"   web       4 seconds ago   Up 3 seconds   0.0.0.0:8080->80/tcp

docker compose run で webコンテナを立ち上げる

では、docker compose runを使ってみる。

1
docker compose run web bash
1
2
3
4
docker compose run web bash
[+] Creating 1/0
 ✔ Container db  Running                                                                                                                                                                                            0.0s 
root@2b3a2ae943f2:/# 

psコマンドを入れる。

1
apt-get update && apt-get install procps
ログ
 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
root@2b3a2ae943f2:/# apt-get update && apt-get install procps
Get:1 http://deb.debian.org/debian bookworm InRelease [151 kB]
Get:2 http://deb.debian.org/debian bookworm-updates InRelease [55.4 kB]
Get:3 http://deb.debian.org/debian-security bookworm-security InRelease [48.0 kB]
Get:4 http://deb.debian.org/debian bookworm/main amd64 Packages [8788 kB]
Get:5 http://deb.debian.org/debian bookworm-updates/main amd64 Packages [13.8 kB]
Get:6 http://deb.debian.org/debian-security bookworm-security/main amd64 Packages [169 kB]
Fetched 9225 kB in 1s (9867 kB/s)                    
Reading package lists... Done
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libgpm2 libncursesw6 libproc2-0 psmisc
Suggested packages:
  gpm
The following NEW packages will be installed:
  libgpm2 libncursesw6 libproc2-0 procps psmisc
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 1178 kB of archives.
After this operation, 3778 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://deb.debian.org/debian bookworm/main amd64 libncursesw6 amd64 6.4-4 [134 kB]
Get:2 http://deb.debian.org/debian bookworm/main amd64 libproc2-0 amd64 2:4.0.2-3 [62.8 kB]
Get:3 http://deb.debian.org/debian bookworm/main amd64 procps amd64 2:4.0.2-3 [709 kB]
Get:4 http://deb.debian.org/debian bookworm/main amd64 libgpm2 amd64 1.20.7-10+b1 [14.2 kB]
Get:5 http://deb.debian.org/debian bookworm/main amd64 psmisc amd64 23.6-1 [259 kB]
Fetched 1178 kB in 0s (28.0 MB/s)
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libncursesw6:amd64.
(Reading database ... 7581 files and directories currently installed.)
Preparing to unpack .../libncursesw6_6.4-4_amd64.deb ...
Unpacking libncursesw6:amd64 (6.4-4) ...
Selecting previously unselected package libproc2-0:amd64.
Preparing to unpack .../libproc2-0_2%3a4.0.2-3_amd64.deb ...
Unpacking libproc2-0:amd64 (2:4.0.2-3) ...
Selecting previously unselected package procps.
Preparing to unpack .../procps_2%3a4.0.2-3_amd64.deb ...
Unpacking procps (2:4.0.2-3) ...
Selecting previously unselected package libgpm2:amd64.
Preparing to unpack .../libgpm2_1.20.7-10+b1_amd64.deb ...
Unpacking libgpm2:amd64 (1.20.7-10+b1) ...
Selecting previously unselected package psmisc.
Preparing to unpack .../psmisc_23.6-1_amd64.deb ...
Unpacking psmisc (23.6-1) ...
Setting up libgpm2:amd64 (1.20.7-10+b1) ...
Setting up psmisc (23.6-1) ...
Setting up libproc2-0:amd64 (2:4.0.2-3) ...
Setting up libncursesw6:amd64 (6.4-4) ...
Setting up procps (2:4.0.2-3) ...
Processing triggers for libc-bin (2.36-9+deb12u7) ...

psを確認。
本番稼働のコンテナとは異なることを確認できる。

ログ
1
2
3
4
5
root@2b3a2ae943f2:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0   4188  3500 pts/0    Ss   17:53   0:00 bash
root       199  0.0  0.0   8112  4008 pts/0    R+   17:54   0:00 ps aux
root@2b3a2ae943f2:/# 

volumesを追加して作業用コンテナのほうに反映されるかの確認

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
services:
  web:
    container_name: web
    image: nginx:1.27.0
    ports:
      - "8080:80"
    depends_on: 
      - db
+   volumes:
+     - .:/src
  db:
    container_name: db
    image: mysql:8.4.2
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: secret
  monitor:
    container_name: monitor
    build:
      context: .
    stdin_open: true
    tty: true
1
2
3
4
5
6
7
$ docker compose run web bash
[+] Creating 1/0
 ✔ Container db  Running     

root@038caf04d897:/# ls /src
Dockerfile  compose.yml
root@038caf04d897:/# 

というわけで、作業用コンテナのほうでもバインドマウントはされていることを確認できる。

runのプロセス確認

1
docker compose run web bash

のコマンドで立ち上げたコンテナを抜けた際にどうなっているかを確認する。

1
2
3
4
5
6
$ docker ps -a
CONTAINER ID   IMAGE                                                                                         COMMAND                  CREATED         STATUS                      PORTS                  NAMES
038caf04d897   nginx:1.27.0                                                                                  "/docker-entrypoint.…"   4 minutes ago   Exited (0) 28 seconds ago                          dood-test-web-run-8301340140dc
95b6bd7cbb3e   nginx:1.27.0                                                                                  "/docker-entrypoint.…"   4 minutes ago   Up 4 minutes                0.0.0.0:8080->80/tcp   web
5e6fb12546bd   mysql:8.4.2                                                                                   "docker-entrypoint.s…"   4 minutes ago   Up 4 minutes                3306/tcp, 33060/tcp    db
e82de3ec42af   dood-test-monitor                                                                             "sh"                     4 minutes ago   Up 4 minutes                                       monitor

Exited (0) 28 seconds と出ているので、コンテナとしては残っていることになる。

※ちなみにこのコンテナは、docker compose downするときに、docker compose down --remove-orphansとすることでもコンテナを削除できる。

docker compose run –rmを使う

docker compose runのみだと、コマンド実行後にコンテナが残ってしまうので --rmオプションを付ける。

1
docker compose run --rm web bash

一連の流れでテストをする。

1
2
3
4
5
6
$ docker compose up -d
[+] Running 4/4
 ✔ Network dood-test_default  Created                                                                                                                                                                               0.0s 
 ✔ Container monitor          Started                                                                                                                                                                               0.6s 
 ✔ Container db               Started                                                                                                                                                                               0.6s 
 ✔ Container web              Started                                                                                                                                                                               0.8s 

docker compose run --rmで確認する。

1
2
3
4
5
$ docker compose run --rm web bash
[+] Creating 1/0
 ✔ Container db  Running                                                                                                                                                                                            0.0s 
root@12288db82779:/# exit
exit
1
2
3
4
5
$ docker ps -a
CONTAINER ID   IMAGE                                                                                         COMMAND                  CREATED          STATUS          PORTS                  NAMES
0a4a773fa2cb   nginx:1.27.0                                                                                  "/docker-entrypoint.…"   10 seconds ago   Up 9 seconds    0.0.0.0:8080->80/tcp   web
b6708963ea4d   mysql:8.4.2                                                                                   "docker-entrypoint.s…"   10 seconds ago   Up 9 seconds    3306/tcp, 33060/tcp    db
b30918052556   dood-test-monitor                                                                             "sh"                     10 seconds ago   Up 9 seconds                           monitor

docker composeで構成しているコンテナ以外のコンテナがないことを確認できた。

参考

おわりに

今後、何か入れて調査するときにも使えそうではあるので覚えておきたい。