我正在“dockersizing”(我希望这是正确的术语)一款现有的Angular 2应用程序,运行在angular-cli
(1.0.0-beta.31)上。
我正在努力寻找一种方法,使得ng serve
可以在我更新工作目录中的文件时进行捕捉,因此可以像往常一样刷新我的应用程序。否则,每当我更改文件时,我都需要运行docker-compose up --build
...
编辑:我正在探索添加一个卷的想法。
以下是我的Dockerfile
:
# Dockerizing Angular 2 Client App
# @link: https://scotch.io/tutorials/create-a-mean-app-with-angular-2-and-docker-compose
# Create image based on the official Node 7 image from dockerhub
FROM node:7
# Create a directory where our app will be placed
RUN mkdir -p /usr/src/app
# Change directory so that our commands run inside this new directory
WORKDIR /usr/src/app
# Copy dependency definitions
COPY package.json /usr/src/app
# Install dependecies
RUN npm install
# Get all the code needed to run the app
# **EDIT**: comment out this, so I can set-up a volume
# COPY . /usr/src/app
# Expose the port the app runs in
EXPOSE 4200
# Serve the app
CMD ["npm", "start"]
这是我的 docker-compose.yml
文件:
# specify docker-compose version
version: '2'
# Define the services/containers to be run
services:
angular2app: # name of the service
build: ./ # specify the directory of the Dockerfile
ports:
- "4200:4200" # specify port forewarding
# **EDIT**: Setting-up the volume here
volumes:
- .:/usr/src/app
编辑:没有音量配置时,应用程序可以成功构建和运行。设置音量后,会输出错误信息:
Building angular2app
Step 1/7 : FROM node:7
---> b3a95b94bd6c
Step 2/7 : RUN mkdir -p /usr/src/app
---> Using cache
---> 2afb01ffe055
Step 3/7 : WORKDIR /usr/src/app
---> Using cache
---> 44d08fdb4a19
Step 4/7 : COPY package.json /usr/src/app
---> Using cache
---> 87bb4f71c13c
Step 5/7 : RUN npm install
---> Using cache
---> ba88a0e1e480
Step 6/7 : EXPOSE 4200
---> Using cache
---> 4fddacae8486
Step 7/7 : CMD npm start
---> Using cache
---> c5ac29bf85fc
Successfully built c5ac29bf85fc
Creating minterface_angular2app_1
ERROR: for angular2app Cannot start service angular2app: Mounts denied: closed
ERROR: Encountered errors while bringing up the project.
如何配置ng serve
以便捕捉当前工作目录中的更改并重新构建我的Angular 2应用程序?
PS:我使用的是Docker for Mac,版本为1.11.1(build 7c5d5e4),在Mac(Sierra 10.12.3)上运行docker-compose。
/Users/<my-user>/Sites/<my-project>
是我的当前工作目录。 - Kaloyan Kosevdocker run --rm -ti -v $(pwd):/usr/src/app debian ls -lah
并检查输出吗?它应该像本地调用ls -lah
一样查看访问权限。如果你在Docker上有一般问题,它应该会显示一个类似的错误信息。 - Alexander Blockdocker: Error response from daemon: Mounts denied: closed.
/ERRO[0013] error getting events from daemon: net/http: request canceled
. 或许这是一个线索? - Kaloyan Kosevdocker run --rm -ti -v /:/host
debian ls -lah /host/Users,然后再尝试
docker run --rm -ti -v /:/hostdebian ls -lah /host/Users/<my-user>/Sites/<my-project>
。如果两者都失败了,我建议重新安装Docker for Mac。 - Alexander Block