LoginSignup
3
2

More than 3 years have passed since last update.

ローカルにあるdocker-composeプロジェクトをリモートのDockerで実行する

Posted at

docker context

Docker 19.03から導入されているdocker context
kubectlのcontextみたいなもんでしょうか。
VS codeとdocker contextを使う方法を昨日書いたのだが、もともとdocker contextの記事が少ないのが気になったので書いてみることにした。
ここが元々の記事
※How to deploy on remote Docker hosts with docker-compose
ポイントはdocker-composeに--contextで展開先を選べるところ。ローカルにあるプロジェクトをそのままリモートホストのDockerに展開出来ます。

環境

docker-composeが正式版ではcontextに対応していません。Pre-ReleaseのRC4を使います。
ローカル、リモート共に

[hoge@local test]$ lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.6.1810 (Core)
Release:        7.6.1810
Codename:       Core
[hoge@local test]$ cd
[hoge@local ~]$ lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description:    CentOS Linux release 7.6.1810 (Core)
Release:        7.6.1810
Codename:       Core
[hoge@local ~]$ docker -v
Docker version 19.03.8, build afacb8b
[hoge@local ~]$ docker-compose -v
docker-compose version 1.26.0-rc4, build d279b7a8

  • docker-compose プロジェクト
[hoge@local test]$ tree --charset=C
.
|-- app
|   |-- Dockerfile
|   `-- src
|       `-- test.py
`-- docker-compose.yml
docker-compose.yml
version: '3.7'
services:
  test:
    build:
      context: ./app
      dockerfile: Dockerfile
    container_name: test
    command: /bin/sh -c "while :; do sleep 10; done"
    volumes:
      - ./app/src:/src
Dockerfile
FROM python:slim
USER root
COPY ./src /src
WORKDIR /src
test.py
print('hello, world')

準備

  • docker-compose 1.26.0-rc4のインストール
sudo curl -L "https://github.com/docker/compose/releases/download/1.26.0-rc4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
  • パスワードレスでssh

ssh-keygenで鍵を作って、パスワードレスでsshを使えるようにしておく
※sshの設定(passwordなしでsshログイン(scp)する)

  • docker contextでリモートサイト登録

(注) --default-stack-orchestrator swarmをつけないとdocker-composeでcontextが使えません。
※Failed to execute script docker-compose when doing docker-compose up on SSH context
Docker Blogの元記事にはこれが抜けています(Docker-in-Dockerの方には正しく記載されている)

[hoge@local .ssh]$ docker context create remote --docker "host=ssh://hoge@172.16.0.24" --default-stack-orchestrator swarm
remote
Successfully created context "remote"
[hoge@localhost .ssh]$ docker context ls
NAME                DESCRIPTION                               DOCKER ENDPOINT               KUBERNETES ENDPOINT   ORCHESTRATOR
default *           Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                         swarm
remote                                                        ssh://hoge@172.16.0.24

default*がついているが、これは何も指定せずにdockerコマンドを実行するとローカルで実行されるという意味である。
リモートに切り替えたいときは`docker context use'を使う。

[hoge@local test]$ docker context ls
NAME                DESCRIPTION                               DOCKER ENDPOINT               KUBERNETES ENDPOINT   ORCHESTRATOR
default             Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                         swarm
remote *                                                      ssh://hoge@172.16.0.24

remote*がついたので、これ以降は全てdockerはリモートで実行される

さて、docker context use defaultdefualtに戻しておく。
今のローカルのdocker psはこうなっている。

[hoge@local test]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
312c02c07c52        test_test           "/bin/sh -c 'while :…"   10 minutes ago      Up 9 minutes                            test

これをリモート側のdockerで実行したい場合は2つ方法がある。
1.contextで毎回指定する

[hoge@local ~]$ docker --context remote ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

2.docker context useで切り替えてから使う

[hoge@local ~]$ docker context use remote
remote
Current context is now "remote"
[hoge@local ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[hoge@local ~]$ docker --context default ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
312c02c07c52        test_test           "/bin/sh -c 'while :…"   26 minutes ago      Up 26 minutes                           test

docker-composeをリモートで実行する

[hoge@local test]$ docker-compose --context remote up --build -d
Creating network "test_default" with the default driver
Building test
Step 1/4 : FROM python:slim
slim: Pulling from library/python
54fec2fa59d0: Pull complete
cd3f35d84cab: Pull complete
a0afc8e92ef0: Pull complete
9691f23efdb7: Pull complete
6512e60b314b: Pull complete
Digest: sha256:87095c142afaf0d5e361bdb00f3bf1f8887e82d1962f1f9ac0782539cf8d97eb
Status: Downloaded newer image for python:slim
 ---> e7d894e42148
Step 2/4 : USER root
 ---> Running in ee5b7c338c46
Removing intermediate container ee5b7c338c46
 ---> 9c112b0e2c4e
Step 3/4 : COPY ./src /src
 ---> 2a75df7e3c39
Step 4/4 : WORKDIR /src
 ---> Running in 446debff93fd
Removing intermediate container 446debff93fd
 ---> 4f4c9343ba81

Successfully built 4f4c9343ba81
Successfully tagged test_test:latest
Creating test ... done

[hoge@local test]$ docker --context remote ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
4a266955f394        test_test           "/bin/sh -c 'while :…"   16 seconds ago      Up 15 seconds                           test

リモートホストで見ると

[hoge@remote ~]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
4a266955f394        test_test           "/bin/sh -c 'while :…"   45 minutes ago      Up 45 minutes

(一部)出来てませんでした(´;ω;`)

当たり前ですが、DockerfileのCOPYが出来てないんですよね。
まぁ、そりゃそうですよね。
COPY/ADDが無ければ使えそうです。
何か方法あるんでしょうか???

3
2
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
3
2