首页 > 解决方案 > 如何将 vs code dev 容器与现有的 docker-compose 文件一起使用?

问题描述

我似乎找不到明确的答案,我找到了https://stackoverflow.com/a/68864132/17183293但它不是很清楚,也可能已经过时,因为“dockerComposeFile”不再是一个有效的选项。

我有一个带有现有 docker-compose.yml 文件的项目,它启动了一个 MariaDB 数据库,我为 Node 添加了一个生成的 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.202.3/containers/javascript-node
{
    "name": "Node.js",
    "runArgs": ["--init"],
    "build": {
        "dockerfile": "Dockerfile",
        // Update 'VARIANT' to pick a Node version: 16, 14, 12.
        // Append -bullseye or -buster to pin to an OS version.
        // Use -bullseye variants on local arm64/Apple Silicon.
        "args": { "VARIANT": "12" }
    },

    // 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": [
        "dbaeumer.vscode-eslint"
    ],

    // 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": "yarn install",

    // Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
    "remoteUser": "node"
}

它还生成了一个 Dockerfile

# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.202.3/containers/javascript-node/.devcontainer/base.Dockerfile

# [Choice] Node.js version (use -bullseye variants on local arm64/Apple Silicon): 16, 14, 12, 16-bullseye, 14-bullseye, 12-bullseye, 16-buster, 14-buster, 12-buster
ARG VARIANT="16-bullseye"
FROM mcr.microsoft.com/vscode/devcontainers/javascript-node:0-${VARIANT}

# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
#     && apt-get -y install --no-install-recommends <your-package-list-here>

# [Optional] Uncomment if you want to install an additional version of node using nvm
# ARG EXTRA_NODE_VERSION=10
# RUN su node -c "source /usr/local/share/nvm/nvm.sh && nvm install ${EXTRA_NODE_VERSION}"

# [Optional] Uncomment if you want to install more global node modules
# RUN su node -c "npm install -g <your-package-list-here>"

这些文件在我的 .devcontainer 文件夹中,现在在我项目的 docker-compose.yml 文件中

version: '3.8'

services:
  mariadb:
    image: mariadb:10.1
    env_file: .env
    environment:
    ports:
      - 3306:3306
    volumes:
      - ./docker/mariadb.conf.d/:/etc/mysql/conf.d:z
      - ./docker/mariadb-init/:/docker-entrypoint-initdb.d:z

我想要实现的是能够启动这个 mariadb 实例,以便我的开发容器中的应用程序可以访问它,理想情况下我还可以通过我的操作系统访问数据库,我想使用现有的docker-compose.yml 文件,以便没有开发容器扩展的人可以手动运行 docker-compose up,我该如何实现呢?

标签: dockervisual-studio-codedocker-composevscode-remotevscode-devcontainer

解决方案


推荐阅读