21
26

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-composeでコンテナ間通信

Last updated at Posted at 2018-12-10

はじめに

test1とtest2というコンテナを作成して2つのコンテナの通信ができる環境の作成

事前環境・GitHub

docker:18.06.1-ce
docker-compose:1.23.1
OS:Mac High Sierra 10.13.6

GitHub
https://github.com/Dhikigame/docker-compose-network

コンテナ間通信に必要なローカルネットワークの作成

$ docker network create test-network

docker-compose.ymlの作成

docker-compose.yml
version: '3'
services:
  test1:
    build: ./test1
    image: test1
    container_name: test1
    privileged: true
    networks:
      - test-network
    command: /sbin/init

  test2:
    build: ./test2
    image: test2
    container_name: test2
    privileged: true
    networks:
      - test-network
    command: /sbin/init

networks:
    test-network:
        external: true

networksで使用するネットワークを選択
privilegedで使うイメージのCentOS上でsystemctlコマンドの使用を有効

Dockerfileの作成

test1,test2両方に同じDockerfileを作成

FROM centos:7
ENV LC_ALL=C
# text editer install
RUN yum install -y vim
# ssh install
RUN yum -y install openssh-server
RUN yum -y install openssh-clients
# sudo install
RUN yum -y install sudo
RUN sed -i -e 's/# %wheel\tALL=(ALL)\tNOPASSWD: ALL/%wheel\tALL=(ALL)\tNOPASSWD: ALL/' /etc/sudoers
RUN sed -i -e 's/%wheel\tALL=(ALL)\tALL/# %wheel\tALL=(ALL)\tALL/' /etc/sudoers
RUN visudo -c
RUN echo "root:password" | chpasswd
# FOREGROUND change access
CMD ["/usr/bin/ssh","-D", "FOREGROUND"]

sshとscpで通信できるように必要なソフトのインストールをし、rootのパスワードを設定する(本来は公開鍵を使用してください)
CMD ["/usr/bin/ssh","-D", "FOREGROUND"]でsshをバックグラウンドで動作させる

ディレクトリの構造

$ tree
.
├── docker-compose.yml
├── test1
│   └── Dockerfile
└── test2
    └── Dockerfile

docker-composeのbuildとup

$ docker-compose build
$ docker-compose up -d

ネットワークの情報確認

$ docker network inspect test-network
[
    {
        "Name": "test-network",
        "Id": "309cef5e5dc0f2b54c433e5fa6c32edfef6e7bf2a0cdd93ecde5c923477e9292",
        "Created": "2018-11-24T19:50:38.13185134Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "586d0f87ad7d13899723334a9849d49202d44eb9fedc3cdfa2a04c13e7de3a85": {
                "Name": "test2",
                "EndpointID": "fbdc91b23717134f934c34a61730aa1a620f0736de2ca3706c01b173fdb37886",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            },
            "8713e688f6301028b9bfd7e21814dc42b4d95c2d3d6833bea2879fc7ab7afa8c": {
                "Name": "test1",
                "EndpointID": "a16b7a17a58958fd66501c7b2d2b6e3284b681a2d3715ab06439289f1574ccf9",
                "MacAddress": "02:42:ac:13:00:03",
                "IPv4Address": "172.19.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

Containersでtest1コンテナは172.19.0.3、test2コンテナは172.19.0.2のIPアドレスが割り当てられていることを確認

test1コンテナにログイン

$ docker exec -it test1 bash
[root@8713e688f630 /]#

適当なファイルを作り、test2コンテナに送る

[root@8713e688f630 /]# echo "Hello! My name is test1." > /root/hello.txt
[root@8713e688f630 ~]# scp /root/hello.txt 172.19.0.2:/root/
root@172.19.0.2's password: 
hello.txt                                                                                                                            100%   25    29.1KB/s   00:00 

scpでtest2のコンテナにファイルを送ろうとするとパスワードが聞かれるのでDockerfileで設定したパスワードを入力

test2コンテナにsshでアクセスし、ファイルを確認

[root@8713e688f630 ~]# ssh 172.19.0.2
root@172.19.0.2's password: 
[root@586d0f87ad7d ~]# cat /root/hello.txt 
Hello! My name is test1.

test2のコンテナにsshでログインに成功し、送ったファイルがあることを確認

参考

https://knowledge.sakura.ad.jp/15253/
https://knowledge.sakura.ad.jp/16082/
https://knowledge.sakura.ad.jp/16862/
マンガでわかるDocker ① 〜概念・基本コマンド編〜
マンガでわかるDocker ② 〜開発環境を作ろう編〜

21
26
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
21
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?