Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
8
7

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 runでpipenvを使ったpythonプロジェクトを動かす

Last updated at Posted at 2018-04-15

ディレクトリ構成

project_name/
    Dockerfile
    src/
        Pipfile
        Pipfile.lock
        main.py

Dockerfile

FROM python:3.6-alpine
RUN pip install pipenv
RUN mkdir /app
COPY ./src/Pipfile /app
COPY ./src/Pipfile.lock /app
WORKDIR /app
RUN pipenv install
ENTRYPOINT ["pipenv", "run"]
CMD ["python", "main.py"]

build

docker build -t project_name .

実行

-vオプションを使うので,srcの変更後にビルドは必要ない.ただし,Pipfileが変わった場合は再ビルドが必要.

docker run --rm -v $(pwd)/src:/app project_name

main.pyの例

simple case

def main():
    print("Hello world from python")


if __name__ == '__main__':
    main()

argparse

CMDが上書きされるので,argparseを使ってpython scriptにコマンドライン引数を渡すこともできる.main.pyしか実行されない場合,ENTRYPOINTに["python", "main.py"]も含めても良いかもしれない.

import argparse


def main(args):
    print("Hello %s" % args.user_name)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--user-name', type=str, default="")
    args = parser.parse_args()
    main(args)

実行は以下のようにすれば良い.

docker run --rm -v $(pwd)/src:/app project_name python main.py --user-name hoge
8
7
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
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?