【问题标题】:VSCode Remote - Add to Existing - Breaks ContainerVSCode Remote - 添加到现有 - 破坏容器
【发布时间】:2020-10-04 04:10:29
【问题描述】:

我有一个使用 docker-compose 部署运行的 LAMP 应用。

docker-compose.yml:

app_web:
    depends_on:
      - app_db
    build: ./web
    ports: 
      - '3000:80'
    volumes:
      - ./web/www:/var/www/html
    environment:
      - MYSQL_DATABASE=appdb
      - MYSQL_USER=appuser
      - MYSQL_PASSWORD=password

Dockerfile:

  FROM php:7.4-apache

  WORKDIR /var/www

  RUN apt-get update \
    && apt-get install -y wget \
    && rm -rf /var/lib/apt/lists/*

  RUN apt-get update \
      && apt-get install -y git
  RUN docker-php-ext-install pdo pdo_mysql mysqli

  RUN a2enmod rewrite

  COPY ./www /var/www/html

这很好用,localhost:3000 为我提供了一个工作网络应用程序。

我想使用 VSCode 远程容器来改进我的调试。第 1 步是将其添加到我的 docker 实例中。

我点击了Remote Containers: Add Development Container Configuration 并选择了“来自 docker-compose.yaml”,然后选择了app_web。然后我可以在容器中重新打开。

这启动了 Docker Desktop 中的一些东西,看起来就像以前一样,但是,Apache 没有运行,当我用 service apache2 start 启动它时,它无法访问我从 PHP 设置的 ENV 变量,当我最初启动它时它做得很好。

.devcontainer\devcontainer.json:

// For format details, see https://aka.ms/vscode-remote/devcontainer.json or this file's README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.122.1/containers/docker-existing-docker-compose
// If you want to run as a non-root user in the container, see .devcontainer/docker-compose.yml.
{
    "name": "Existing Docker Compose (Extend)",

    // Update the 'dockerComposeFile' list if you have more compose files or use different names.
    // The .devcontainer/docker-compose.yml file contains any overrides you need/want to make.
    "dockerComposeFile": [
        "..\\docker-compose.yaml",
        "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": "app_web",

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

    // 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": []

    // Use 'forwardPorts' to make a list of ports inside the container available locally.
    // "forwardPorts": [],

    // Uncomment the next line if you want start specific services in your Docker Compose config.
    // "runServices": [],

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

    // Uncomment the next line to run commands after the container is created - for example installing curl.
    // "postCreateCommand": "apt-get update && apt-get install -y curl",

    // Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
    // "remoteUser": "vscode"
}

以及.devcontainer中已经生成的docker-compose.yml:

.devcontainer\docker-compose.yml

#-------------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
#-------------------------------------------------------------------------------------------------------------

version: '3'
services:
  # Update this to the name of the service you want to work with in your docker-compose.yml file
  app_web:
    # If you want add a non-root user to your Dockerfile, you can use the "remoteUser"
    # property in devcontainer.json to cause VS Code its sub-processes (terminals, tasks, 
    # debugging) to execute as the user. Uncomment the next line if you want the entire 
    # container to run as this user instead. Note that, on Linux, you may need to 
    # ensure the UID and GID of the container user you create matches your local user. 
    # See https://aka.ms/vscode-remote/containers/non-root for details.
    #
    # user: vscode

    # Uncomment if you want to override the service's Dockerfile to one in the .devcontainer 
    # folder. Note that the path of the Dockerfile and context is relative to the *primary* 
    # docker-compose.yml file (the first in the devcontainer.json "dockerComposeFile"
    # array). The sample below assumes your primary file is in the root of your project.
    #
    # build:
    #   context: .
    #   dockerfile: .devcontainer/Dockerfile

    volumes:
      # Update this to wherever you want VS Code to mount the folder of your project
      - .:/workspace:cached

      # Uncomment the next line to use Docker from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker-compose for details.
      # - /var/run/docker.sock:/var/run/docker.sock 

    # Uncomment the next four lines if you will use a ptrace-based debugger like C++, Go, and Rust.
    # cap_add:
    #   - SYS_PTRACE
    # security_opt:
    #   - seccomp:unconfined

    # Overrides default command so things don't shut down after the process ends.
    command: /bin/sh -c "while sleep 1000; do :; done"

为什么在 Visual Studio 中自动生成远程处理配置会破坏核心 VM?我在这里有什么误解和错误?

干杯,

【问题讨论】:

    标签: php apache docker visual-studio-code docker-compose


    【解决方案1】:

    啊,所以,稍后想一想,.devcontainer\docker-compose.yml 以:

     # Overrides default command so things don't shut down after the process ends.
        command: /bin/sh -c "while sleep 1000; do :; done"
    

    默认命令应该是 apache2-foreground,它在 php 映像中启动 Apache2。所以删除它,并依靠主 dockerfile 末尾的命令来处理 php 图像解决了这个问题。

    .devcontainer\docker-compose.yml

    #-------------------------------------------------------------------------------------------------------------
    # Copyright (c) Microsoft Corporation. All rights reserved.
    # Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
    #-------------------------------------------------------------------------------------------------------------
    
    version: '3'
    services:
      # Update this to the name of the service you want to work with in your docker-compose.yml file
      app_web:
        # If you want add a non-root user to your Dockerfile, you can use the "remoteUser"
        # property in devcontainer.json to cause VS Code its sub-processes (terminals, tasks, 
        # debugging) to execute as the user. Uncomment the next line if you want the entire 
        # container to run as this user instead. Note that, on Linux, you may need to 
        # ensure the UID and GID of the container user you create matches your local user. 
        # See https://aka.ms/vscode-remote/containers/non-root for details.
        #
        # user: vscode
    
        # Uncomment if you want to override the service's Dockerfile to one in the .devcontainer 
        # folder. Note that the path of the Dockerfile and context is relative to the *primary* 
        # docker-compose.yml file (the first in the devcontainer.json "dockerComposeFile"
        # array). The sample below assumes your primary file is in the root of your project.
        #
        # build:
        #   context: .
        #   dockerfile: .devcontainer/Dockerfile
    
        volumes:
          # Update this to wherever you want VS Code to mount the folder of your project
          - .:/workspace:cached
    
          # Uncomment the next line to use Docker from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker-compose for details.
          # - /var/run/docker.sock:/var/run/docker.sock 
    
        # Uncomment the next four lines if you will use a ptrace-based debugger like C++, Go, and Rust.
        # cap_add:
        #   - SYS_PTRACE
        # security_opt:
        #   - seccomp:unconfined
    
        # LEAVE THE DEFAULT COMMAND ALONE!
    

    【讨论】:

      猜你喜欢
      • 2019-10-19
      • 2017-07-31
      • 2011-11-11
      • 2020-02-24
      • 1970-01-01
      • 1970-01-01
      • 2013-08-24
      • 1970-01-01
      • 2017-05-28
      相关资源
      最近更新 更多