在vscode中的dev容器中运行angular app

时间:2019-11-11 15:28:44

标签: vscode-remote

我正在尝试使用.NET Core和Angular在dev容器内运行项目。

.NET Core项目在此容器中运行没有问题,但是Angular项目出现了一些问题。

当我在vscode中手动启动终端时,转到Angular项目目录,然后运行npm run ng serve-一切正常。

但是,我试图在task.json中定义任务,这使我可以运行相同的命令-存在错误。

这是我的Dockerfile specyfing开发容器:

#-------------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
#-------------------------------------------------------------------------------------------------------------
ARG DOTNETCORE_VERSION=2.1
FROM mcr.microsoft.com/dotnet/core/sdk:${DOTNETCORE_VERSION}

# This Dockerfile adds a non-root 'vscode' user with sudo access. However, for Linux,
# this user's GID/UID must match your local user UID/GID to avoid permission issues
# with bind mounts. Update USER_UID / USER_GID if yours is not 1000. See
# https://aka.ms/vscode-remote/containers/non-root-user for details.
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# [Optional] Version of Node.js to install.
ARG INSTALL_NODE="true"
ARG NODE_VERSION="lts/*"
ENV NVM_DIR=/home/vscode/.nvm

# [Optional] Install the Azure CLI
ARG INSTALL_AZURE_CLI="false"

# Avoid warnings by switching to noninteractive
ENV DEBIAN_FRONTEND=noninteractive

# Configure apt and install packages
RUN apt-get update \
    && apt-get -y install --no-install-recommends apt-utils dialog 2>&1 \
    #
    # Verify git, process tools, lsb-release (common in install instructions for CLIs) installed
    && apt-get -y install git iproute2 procps apt-transport-https gnupg2 curl lsb-release \
    #
    # Create a non-root user to use if preferred - see https://aka.ms/vscode-remote/containers/non-root-user.
    && groupadd --gid $USER_GID $USERNAME \
    && useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
    # [Optional] Add sudo support for the non-root user
    && apt-get install -y sudo \
    && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME\
    && chmod 0440 /etc/sudoers.d/$USERNAME \
    #
    # [Optional] Install Node.js for ASP.NET Core Web Applicationss
    && if [ "$INSTALL_NODE" = "true" ]; then \
    #
    # Install nvm and Node
    mkdir ${NVM_DIR} \
    && curl -so- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash 2>&1 \
    && chown -R vscode:vscode ${NVM_DIR} \
    && /bin/bash -c "source $NVM_DIR/nvm.sh \
    && nvm install ${NODE_VERSION} \
    && nvm alias default ${NODE_VERSION}" 2>&1 \
    && INIT_STRING='[ -s "$NVM_DIR/nvm.sh" ] && \\. "$NVM_DIR/nvm.sh"  && [ -s "$NVM_DIR/bash_completion" ] && \\. "$NVM_DIR/bash_completion"' \
    && echo $INIT_STRING >> /home/vscode/.bashrc \
    && echo $INIT_STRING >> /home/vscode/.zshrc \
    && echo $INIT_STRING >> /root/.zshrc; \
    #
    fi \
    #
    # [Optional] Install the Azure CLI
    && if [ "$INSTALL_AZURE_CLI" = "true" ]; then \
    echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $(lsb_release -cs) main" > /etc/apt/sources.list.d/azure-cli.list \
    && curl -sL https://packages.microsoft.com/keys/microsoft.asc | apt-key add - 2>/dev/null \
    && apt-get update \
    && apt-get install -y azure-cli; \
    fi \
    #
    # Clean up
    && apt-get autoremove -y \
    && apt-get clean -y \
    && rm -rf /var/lib/apt/lists/*

# Switch back to dialog for any ad-hoc use of apt-get
ENV DEBIAN_FRONTEND=

这是我的devcontainer.json

{
    "name": "Docker in Docker Compose",
    "dockerComposeFile": "docker-compose.yml",
    "appPort": [
        5000,
        5001,
        5005,
        4200
    ],
    "service": "docker-in-docker",
    "workspaceFolder": "/workspace",
    "settings": {
        "terminal.integrated.shell.linux": "/bin/bash",
        "remote.extensionKind": {
            "ms-azuretools.vscode-docker": "workspace"
        },
        "mssql.connections": [
            {
                "server": "sql.data",
                "database": "",
                "authenticationType": "SqlLogin",
                "user": "sa",
                "password": "Pass@word!",
                "emptyPasswordInput": false,
                "savePassword": true
            }
        ]
    },
    "extensions": [
        "ms-azuretools.vscode-docker",
        "ms-mssql.mssql",
        "ms-vscode.csharp",
        "cyrilletuzi.angular-schematics",
        "dbaeumer.vscode-eslint",
        "ms-vscode.vscode-typescript-tslint-plugin",
        "pmneo.tsimporter",
        "eg2.vscode-npm-script"
    ]
}

和launch.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/TaskManager/TaskManager.sln",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        },
        {
            "type": "npm",
            "label": "angular testing",
            "script": "start"        
        }
    ]
}

启动带有标签“ build”的任务成功。 标签为“角度测试”的启动任务会导致:

> Executing task: npm run start <
/bin/bash: npm: command not found
The terminal process terminated with exit code: 127

我也尝试过测试任务定义:

            "type": "shell",
            "label": "angular testing",
            "command": "which",
            "args": [
                "npm"
            ]
}

此结果与

> Executing task: which npm <
The terminal process terminated with exit code: 1

在vs代码中运行终端“ bash”并测试npm版本:

root@43f83c4b3ce5:/workspace# npm version
{
  npm: '6.12.0',
...
}

看起来不错。

如何定义在此dev容器内成功运行npm命令的任务?

0 个答案:

没有答案