在 docker 容器中使用 nggenerate 创建的文件没有写权限

问题描述 投票:0回答:1

我正在开发一个在 docker 容器内运行的 Angular CLI 项目。

当我在 bash 进入容器后使用

ng g c name
创建新组件时,任何修改的新文件都会提示我输入用户帐户的密码以保存更改。

在新创建的目录上

ls -ld
的输出是-

drwxr-xr-x 2 root root 4096 Dec  2 13:41 test2

有什么办法可以避免这种情况吗?

编辑:以下是我的docker堆栈文件

     version: 3.4  

       services:                            

       package-installer:  
               image: dev-env  
       volumes:  
               - ./:/home/app  
       entrypoint: yarn install  

       client-side-dev-env:  
               image: dev-env  
       volumes:  
           - ./:/home/app  
       ports:  
           - "7000:4200" 

我使用

docker stack deploy
来运行 docker。

Dockerfile-

    FROM node:latest

    RUN curl -o- -L https://yarnpkg.com/install.sh | bash

    RUN yarn global add @angular/cli

    RUN ng set --global packageManager=yarn

    ENV CONTAINER_PATH /home/app

    WORKDIR $CONTAINER_PATH

    VOLUME $CONTAINER_PATH

    EXPOSE 4200

    ENTRYPOINT ["ng", "serve", "--host=0.0.0.0"]

docker 运行时安装在容器中的本地目录上的

ls -ld
给出
drwxrwxr-x 6 shiv_saz shiv_saz 4096
shiv_saz 是我系统上的用户帐户。

docker angular-cli
1个回答
0
投票

我有同样的“问题”,我做了这个解决方案。

在您的

Dockerfile
中,使用
node
用户非常重要,以确保您在创建项目后有权修改项目。

###################################
## Angular CLI
###################################
FROM node:18.18.0-alpine3.18

ARG ANGULAR_CLI_VERSION=

RUN npm install -g @angular/cli@${ANGULAR_CLI_VERSION}

ARG WORKING_DIRECTORY=/app

USER node

WORKDIR ${WORKING_DIRECTORY}

docker-compose.yml
需要以下配置。

version: '3.8'
services:
  ##########################################################
  ## Angular CLI - Utiltiy Container
  ##########################################################
  angular-cli:
    image: angular-cli:18.18.0-alpine3.18
    build:
      context: ./
      dockerfile: ./docker/angular-cli.dockerfile
      args:
        - ANGULAR_CLI_VERSION=15.2.9
        - WORKING_DIRECTORY=/app
    volumes:
      - ./frontend:/app:rw

您可以添加一个

.dockerignore
文件来防止复制某些目录和文件。

node_modules/
.vscode/
.gitignore
README.md

有了这一切,你就可以像下面这样使用

commands

# Create a new Angular project:
$ docker compose run --rm -it --build angular-cli ng new app --directory ./

# Use ng commands:
$ docker compose run --rm -it --build angular-cli ng generate component header --skip-tests --style none

# Use npm commands:
$ docker compose run --rm -it --build angular-cli npm install @angular/fire
$ docker compose run --rm -it --build angular-cli npm remove @angular/fire

# Install project dependencies:
docker compose run --rm -it --build angular-cli npm install

我真的希望这个解决方案对您有所帮助。

再见!!!

© www.soinside.com 2019 - 2024. All rights reserved.