VsCode+Dockerが便利すぎた!

投稿日: 2021.06.12

MicrosoftがVsCodeでDockerを起動できるようにする拡張機能が出ていましたので紹介と実装方法をご紹介します。

Docker環境は、以下の通りです。

機能の簡単な説明

めちゃくちゃシンプルです。

VsCodeでDockerを立ち上げる!

Dockerの準備

「.devcontainer」のフォルダを作成し、その中にDockerの設定ファイルを置いていきます。

devcontainer.jsonに必要な情報を書き、VsCode実行時の設定を記述します。

{
	"name": "Your Definition Name Here (Community)",

	// Update the 'dockerComposeFile' list if you have more compose files or use different names.
	"dockerComposeFile": "container-project/docker-compose.yml",

	// The 'service' property is the name of the service for the container that VS Code should
	// use. Update this value and .devcontainer/docker-compose.yml to the real service name.
	"service": "php-fpm",

	// The optional 'workspaceFolder' property is the path VS Code should open by default when
	// connected. This is typically a volume mount in .devcontainer/docker-compose.yml
	"workspaceFolder": "/var/www/html",

	// Set *default* container specific settings.json values on container create.
	"settings": { 
		"terminal.integrated.shell.linux": null
	},

	// Add the IDs of extensions you want installed when the container is created.
	"extensions": [
		"felixfbecker.php-debug",
		"felixfbecker.php-intellisense",
		"onecentlin.laravel-blade",
		"jcbuisson.vue",
		"hollowtree.vue-snippets"
	],

	// Uncomment the next line if you want to keep your containers running after VS Code shuts down.
	// "shutdownAction": "none",

	// Uncomment the next line to use 'postCreateCommand' to run commands after the container is created.
	// "postCreateCommand": "uname -a",

	// Comment out connect as root instead. To add a non-root user, see: https://aka.ms/vscode-remote/containers/non-root.
	// "remoteUser": "vscode"
}

ファイル構成

├─.devcontainer
│  └─container-project
│      ├─config
│      │  ├─mysql
│      │  ├─nginx
│      │  └─php
│      ├─mysql
│      │  └─data
│      └─php
└─project
    └─public

projectフォルダにWordpressやLaravel、Symfonyを導入すればOKです。

gitで公開中

起動方法

手順は簡単です。

git連携すればもっと便利に!

上記のように設定するだけVsCode+Dockerで簡単にプロジェクトを起動することができます。

わざわざポート開放やDockerの切り替えをする手間が省けます。また、ファイルサイズもかなり小さいためgitで管理し、プロジェクト毎の環境設定を保持できます。

「git clone」→「vscodeで開く」だけですぐ開発できます。※便利すぎて感動しました。

まとめ

gitでプロジェクトと環境情報を持つ時代が来ましたね!

Dockerを本格導入すればwindowsで面倒なインストールが無くなります。おそらくgitのみになるのではないでしょうか。

Let's 開発ライフ!