1
2

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.

VSCodeでDocker管理

1
Last updated at Posted at 2020-09-26

Dockerで開発環境を管理してVSCodeでコーディングしたい
その為に調べた一連の流れとVSCode上でDockerの操作を記述しました。

環境:

Windows 10 Home: 2004
VSCode: 1.49.2
Docker: 19.03.12

Remote Developmentインストール

VSCodeを立ち上げて拡張機能でRemote Developmentを検索してインストール

実行環境の作成

Remote Developmentを用いた開発でのファイル構造はこんな感じです。

プロジェクト
├ .devcontainer
 ├ devcontainer.json
 └ Dockerfile
└ 内容

プロジェクト直下にある**.devcontainerフォルダ内にある
devcontainer.jsonと
Dockerfile**にコンテナに関する設定を記述する感じです。
例ではPythonの環境をここを参考に書いておきます。

DockerPython
├.devcontainer
  ├ devcontainer.json
  └ Dockerfile
├ requirements.txt
└ test.py
Dockerfile
# -------------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
# -------------------------------------------------------------------------------------------------------------

FROM python:3
devcontainer.json
{
    "name": "Python 3",
    "context": "..",
    "dockerFile": "Dockerfile",

    "settings": { 
        "terminal.integrated.shell.linux": "/bin/bash",
        "python.pythonPath": "/usr/local/bin/python",
        "python.linting.enabled": true,
        "python.linting.pylintEnabled": true,
        "python.linting.pylintPath": "/usr/local/bin/pylint"
    },

    "appPort": [ 9000 ],

    "postCreateCommand": "pip install -r requirements.txt",

    "extensions": ["ms-python.python"]
}
equirements.txt
scikit-learn
test.py
print("haloworld")

コンテナの作成・起動

左下にある**[><]をクリック、その後Remote-Containers: Open Folder in Container…を選択、そして
先ほど説明したプロジェクト(例ではDockerPython)を選択でコンテナが立ち上がります。(少し時間がかかります)
無事立ち上がりプロジェクトが表示されるされたら、試しにターミナルでpython test.pyと
pip freeze**を実行してみましょう。

/workspaces/DockerPython# python test.py
haloworld
/workspaces/DockerPython# pip freeze
joblib==0.16.0
numpy==1.19.2
scikit-learn==0.23.2
scipy==1.5.2
threadpoolctl==2.1.0

以上の通りになればちゃんと出来ています。

コンテナから抜ける

再び左下にある**[><]をクリック、その後リモート接続を終了する**を選択
するとコンテナから抜けることが出来ます。またコンテナも停止する。

コンテナに再び入る

Remote Developmentをインストール際に出来たバーのリモートエクスプローラーをクリック
その後出てきたリスト内から先ほどのプロジェクトをクリックその後出てきたファイル端のOpen Contanerを
クリック、そうすればコンテナが再起動して再び入ることが出来ます。

コンテナの削除

コンテナから抜けた後リモートエクスプローラーをクリック
消したいプロジェクトを右クリックで出てくるメニューでRemoveContanerをクリック
するとコンテナは削除される

Docker拡張機能

VSCodeの拡張機能としてDockerがあります。
実行環境を作る上ではRemote Developmentで十分ですが
Dockerはレスポンスが良くイメージも表示してくれるのでコンテナの管理の為に
一緒に入れとくのも良いと思います。

感想

Remote Developmentを使えばVSCodeでDocker環境での開発ができる上
コマンドを打たずにDockerの操作出来ていい感じでした。

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?