と思って、dockerでやってみることにしました。
まずは、コンテナの準備をDockerfileで進めていきます。
今回は、pythonのイメージを使いたいので、ドキュメント見ながら書いて行きます。
$ vim Dockerfile
FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "./script.py" ]
FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "./script.py" ]
このDockerファイルを実行するとコンテナにコピーした「script.py」というスクリプトが実行されます。
次は、実行するスクリプトを作ります
$ vim script.py
if __name__ == '__main__':
print('hello')
if __name__ == '__main__':
print('hello')
実行すると「hello」が表示されるスクリプトです。 Dockerfileでビルドしてみます。
$ docker build . -t script --no-cache
Sending build context to Docker daemon 63.49kB
Step 1/5 : FROM python:3
...
Step 2/5 : WORKDIR /usr/src/app
...
Step 3/5 : COPY requirements.txt ./
...
Step 4/5 : RUN pip install --no-cache-dir -r requirements.txt
...
Step 5/5 : CMD [ "python", "./script.py" ]
...
Successfully built 36d309824e5f
Successfully tagged script:latest
Sending build context to Docker daemon 63.49kB
Step 1/5 : FROM python:3
...
Step 2/5 : WORKDIR /usr/src/app
...
Step 3/5 : COPY requirements.txt ./
...
Step 4/5 : RUN pip install --no-cache-dir -r requirements.txt
...
Step 5/5 : CMD [ "python", "./script.py" ]
...
Successfully built 36d309824e5f
Successfully tagged script:latest
docker buildの-tオプションでscriptを指定したので、実行するときはこのタグを指定して実行します。
$ docker run --rm -it script
hello
hello
無事に「hello」が表示されたので、実行できたことを確認できました。 思ったより簡単だったけど、docker buildでファイルをコピーしているので、毎回docker buildしないといけないのはめんどくさいなと思ったりしました。