1
0

Docker+jupyter+vscode(.devcontainer) 環境構築

Last updated at Posted at 2024-03-11

背景

.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

具体的にファイルの中身を見ていく。

devcontainer.json
{
    "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
image.png
extensionIDでcopyして、customizationsで貼り付け。
これでdocker run したときにいちいちinstallしなくて済む。

Dockerfile
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をインストールしてくるところが機能としてほしかったところ。
以下がその中身。

requirement.txt
jupyter
psycopg2
numpy
pandas
plotly

これでdocker runの際にinstallされて立ち上がる。

1
0
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
1
0