背景
.devcontainerを使えば、vscodeでcontainerに入って作業がしやすい話を聞き、環境構築をした。
vscodeでcontainerに入って作業する方法は大きく2つ。
1.wslのシェル上で以下を実行
docker run -it -d <image ID>
vscodeで「F1→Dev Containers:Attach to running containers」を実行。
2..devcontainersでいい感じにローカルにmountしてくれたり、containerつくってくれる。
内容 .devcontainerの構成
今回はこれがメイン。私が現在使ってる環境は
p_env--.devcontainer--devcontainers.json
|-Dockerfile
|-requirement.txt
具体的にファイルの中身を見ていく。
{
"name": "pyenv",
"image": "mcr.microsoft.com/devcontainers/python:1-3.11-bullseye",
"build": {
"dockerfile": "Dockerfile"
},
"customizations": {
"vscode": {
"extensions": [
"ms-python.python",
"njpwerner.autodocstring",
"ms-toolsai.jupyter",
"ms-azuretools.vscode-docker"
]
}
}
}
上記ファイルの書く際に少し止まったところが2点。
・image
containerを立ち上げる際にpip installerがなかったので色々調べたところ"mcr.microsoft.com/devcontainers/python:1-3.11-bullseye"のようにmicrosoftが出してるイメージファイルはpipが入ってるらしく書き換えた。
・customizations
extensionIDでcopyして、customizationsで貼り付け。
これでdocker run したときにいちいちinstallしなくて済む。
FROM python:3.11
USER root
RUN apt-get update
RUN apt-get -y install locales && \
localedef -f UTF-8 -i ja_JP ja_JP.UTF-8
ENV LANG ja_JP.UTF-8
ENV LANGUAGE ja_JP:ja
ENV LC_ALL ja_JP.UTF-8
ENV TZ JST-9
ENV TERM xterm
COPY requirements.txt /root/
RUN apt-get install -y vim less
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
RUN pip install -r /root/requirements.txt
これに関して特に気を付けたことはなく、ネット上におちてるものをコピーしてきた。最後の行でrequirement.txtをインストールしてくるところが機能としてほしかったところ。
以下がその中身。
jupyter
psycopg2
numpy
pandas
plotly
これでdocker runの際にinstallされて立ち上がる。