2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Docker コンテナ内から他の Docker コンテナにdocker exec実行

Last updated at Posted at 2019-12-18

AWS Workspaces
Amazon Linux2
nginx 1.16.1

Dockerfile
FROM alpine:3.10.3

RUN apk --update add logrotate docker && rm -rf /var/cache/apk/*

# cronのdaemonを起動(log level=1, foreground)
CMD crond -l 1 -f
# Dockerイメージ作成
docker build -t base .

# Dockerコンテナ起動
docker run -d \
--name nginx \
-p 81:80 \
nginx:1.16.1

docker run -d \
--name base \
-v /var/run/docker.sock:/var/run/docker.sock \
base:latest

$ docker exec -it base sh
/ # docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
34f2c8645bd2        base:latest         "/bin/sh -c 'crond -…"   18 seconds ago      Up 17 seconds                            base
e48c7a09b233        nginx:1.16.1        "nginx -g 'daemon of…"   42 minutes ago      Up 42 minutes       0.0.0.0:81->80/tcp   nginx

/ # docker exec -it nginx bash
root@e48c7a09b233:/# ls
bin   dev  home  lib64	mnt  proc  run	 srv  tmp  var
boot  etc  lib	 media	opt  root  sbin  sys  usr

root@e48c7a09b233:/# nginx -v
nginx version: nginx/1.16.1

2019/12/20追記

コンテナ内でコンテナを操作するやり方は
「DinD(Docker in Docker)」
「DooD(Docker outside of Docker)」
があった。

alpineベースのDocker公式イメージ
ubuntuベースのteracy/ubuntu

DinD(Docker in Docker)

DinDではホストのDockerとは完全に別物のDocker環境なので、
docker ps、docker imagesを実行してもホスト側の情報は表示されない。

$ docker run --privileged --name dind -d docker:stable-dind
$ docker exec -it dind /bin/ash
/ # docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
/ # docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

DooD(Docker outside of Docker)

ホストのdocker.sock (/var/run/docker.sock)をマウント

$ docker run -d \
--name nginx \
-p 81:80 \
nginx:1.16.1

$ docker run -ti \
--rm \
-v /var/run/docker.sock:/var/run/docker.sock \
docker \
/bin/ash

/ # docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
a31010707f45        docker              "docker-entrypoint.s…"   33 seconds ago      Up 32 seconds                            infallible_antonelli
2780dae6110b        nginx:1.16.1        "nginx -g 'daemon of…"   51 seconds ago      Up 50 seconds       0.0.0.0:81->80/tcp   nginx

/ # docker exec -it nginx bash
root@2780dae6110b:/# nginx -v
nginx version: nginx/1.16.1
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?