5
3

More than 3 years have passed since last update.

privateなGitHubリポジトリを参照するPoetry on Docker の作り方

Last updated at Posted at 2021-03-08

はじめに

docker buildkit sshを試してみました。

ファイル

aaa/main.py
# pri_pjはprivateのライブラリ
from pri_pj import hello

print("start")
print(hello("aa"))

mainファイルではプライベートなGitHubリポジトリにあるライブラリ pri_pjhello 関数を使うだけの簡単なpythonファイルです。

pyproject.toml
[tool.poetry]
name = "aaa"
version = "0.1.0"
description = ""
authors = ["va034600"]

[tool.poetry.dependencies]
python = "3.6.*"
# プライベートなGitHubリポジトリ
pri_pj = { git = "ssh://git@github.com/va034600/pri_pj.git" }

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

poetryを使ってプライベートなGitHubリポジトリにあるライブラリを使う設定をしています。

Dockerfile
FROM python:3.6.8

WORKDIR /usr/src/app

ENV POETRY_VERSION=1.0.10 \
    PATH="/root/.poetry/bin:$PATH"

RUN curl -sSL https://raw.githubusercontent.com/sdispater/poetry/${POETRY_VERSION}/get-poetry.py | python && \
    poetry config virtualenvs.create false

COPY ./pyproject.toml /usr/src/app/pyproject.toml

RUN mkdir -m 700 $HOME/.ssh
RUN ssh-keyscan github.com > $HOME/.ssh/known_hosts
RUN --mount=type=ssh poetry install

CMD tail -f /dev/null

Dockerfile では poetry install をしています。
RUN --mount=type=ssh poetry install
poetry install する際に --mount=type=ssh をつけることでprivateなリポジトリを参照することができます。

docker buildを実行

準備は整ったのでこれでbuildできるかと思います。

$ DOCKER_BUILDKIT=1 docker build -f docker/Dockerfile -t aaa --ssh default .

docker-composeを実行

docker-composeを直接 buildkitを使ってsshアクセスするやり方が見当たらなかったので、
仕方なく、docker build で作成したイメージをdocker-composeで使うことにしました。

docker-compose.yml
version: '3.5'
services:
  abc:
    image: aaa
    working_dir: /usr/src/app/aaa
    command: python main.py
    volumes:
      - ../aaa:/usr/src/app/aaa
$ docker-compose up

終わりに

以前、Dockerfileからprivateなリポジトリにアクセスするイメージを作った際には、ssh keyをDockerイメージに埋めましたが、これはimageの中にssh keyが残るアンチパターンだった様です。
docker-compose上でbuildkit sshがまだ実現出来なかったのが気になりますが、これで開発する上でdockerイメージにprivateなライブラリを埋め込むことが出来そうです。

参考

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