devcontainer.jsonについては別ページ参照。
プロジェクトディレクトリ構成
proj/my-proj ...<プロジェクトルートからマウントし、コンテナ内での作業ディレクトリ>
├── .devcontainer/
│ ├── build/ ...<ビルドに使用する環境のコンテナ>
│ │ ├── Dockerfile
│ │ └── library-scripts/
│ │ ├── common-debian.sh
│ │ └── docker-debian.sh
│ ├── devcontainer.json ...<今回注目する設定>
│ └── docker-compose.override.yml ...<VSCode時に上書きする設定>
├── deploy/
│ ├── docker-compose.yml ...<通常の開発環境構築用>
│ └── web/ or db/ or etc/ ...<各種コンテナ>
│ └── Dockerfile
├── doc/ ...<ドキュメント配置>
└── src/ ...<ソース配置>
devcontainer.json
は.devcontainer/
ディレクトリに置く。docker-compose.yml
を使用し、コンテナの設定はできるだけcomposeに任せる。- VSCode時に上書きする設定を
.devcontainer/docker-compose.override.yml
に書く。 - 相対パス基準はオリジナルの
deploy/docker-compose.yml
のディレクトリとなるので注意。
- VSCode時に上書きする設定を
doc/
,src/
などはプロジェクトルートからマウントされるため、コンテナ内からもアクセス可能。
devcontainer.json
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.177.0/containers/docker-from-docker-compose
{
"name": "my-proj",
"dockerComposeFile": [
"../deploy/docker-compose.yml",
"docker-compose.override.yml"
],
"service": "build",
"workspaceFolder": "/workspace",
// Use this environment variable if you need to bind mount your local source code into a new container.
"remoteEnv": {
"LOCAL_WORKSPACE_FOLDER": "${localWorkspaceFolder}"
},
// Set *default* container specific settings.json values on container create.
"settings": {
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "docker --version",
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
docker-compose.override.yaml
#: Remote Container拡張で使うComposeファイル
version: '3'
# Base path expects ../deploy/
services:
build:
build:
context: ../.devcontainer/build
dockerfile: Dockerfile
args:
# On Linux, you may need to update USER_UID and USER_GID below if not your local UID is not 1000.
USER_UID: 1000
USER_GID: 1000
init: true
volumes:
# Forwards the local Docker socket to the container.
- /var/run/docker.sock:/var/run/docker-host.sock
# Update this to wherever you want VS Code to mount the folder of your project
- ..:/workspace:cached
# Overrides default command so things don't shut down after the process ends.
command: /bin/sh -c "while sleep 1000; do :; done"
# Uncomment the next four lines if you will use a ptrace-based debuggers like C++, Go, and Rust.
cap_add:
- SYS_PTRACE
security_opt:
- seccomp:unconfined
# Uncomment the next line to use a non-root user for all processes.
# user: vscode
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)
build/Dockerfile
本当は1から自分で書けるようになりたいけど、なんかうまくいかないから 公式のリスト のものを使用しています。 C言語はよく使うので、基本的にcppをベースに使います。
下記はこのサイトのビルドとFTPアップロードで使っているものです。
# Note: You can use any Debian/Ubuntu based image you want.
ARG VARIANT=debian-11
FROM mcr.microsoft.com/vscode/devcontainers/cpp:0-${VARIANT}
# Setting the ENTRYPOINT to docker-init.sh will configure non-root access
# to the Docker socket. The script will also execute CMD as needed.
# ENTRYPOINT [ "/usr/local/share/docker-init.sh" ]
# CMD [ "sleep", "infinity" ]
# [Optional] Uncomment this section to install additional OS packages.
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends ncftp
ENV HUGO_VERSION=0.88.1
RUN curl -L https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_${HUGO_VERSION}_Linux-64bit.tar.gz | tar -zx -C /usr/local/bin/