我已经使用以下设置将我的现有Angular CLI应用程序进行了Docker化:
根级别的Dockerfile:
# Create a new image from the base nodejs 7 image.
FROM node:7
# Create the target directory in the imahge
RUN mkdir -p /usr/src/app
# Set the created directory as the working directory
WORKDIR /usr/src/app
# Copy the package.json inside the working directory
COPY package.json /usr/src/app
# Install required dependencies
RUN npm install
# Copy the client application source files. You can use .dockerignore to exlcude files. Works just as .gitignore does.
COPY . /usr/src/app
# Open port 4200. This is the port that our development server uses
EXPOSE 4200
# Start the application. This is the same as running ng serve.
CMD ["npm", "start"]
docker-compose.yml:
version: '2'
services:
# Build the container using the client Dockerfile
client:
build: .
ports:
- "4200:4200"
# Build the container using the nginx Dockerfile
nginx:
build: ./nginx
# Map Nginx port 80 to the local machine's port 80
ports:
- "85:85"
# Link the client container so that Nginx will have access to it
links:
- client
我有另一个名为nginx的文件夹,其中包含dockerfile:
# Create a new image from the base nginx image.
FROM nginx
# Overwrite nginx's default configuration file with our own.
COPY default.conf /etc/nginx/conf.d/
并且是default.conf文件:
server {
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://client:4200/;
}
}
docker-compose build
和 docker-compose up
均已成功,我可以通过 localhost:4200 访问我的应用程序。但是热重载不起作用,例如每当我在.ts文件或HTML/CSS文件中更改内容时,不能立即反映出来。
我该如何解决这个问题?
更新:修改了 docker-compose 文件。
# Build the container using the client Dockerfile
client:
build: .
# This line maps the contents of the client folder into the container.
volumes:
- ./:/usr/src/app
ports:
- "4200:4200"
给我:
更新2 下面是docker-compose的内容:
version: '2'
services:
# Build the container using the client Dockerfile
client:
image: node:6
command: bash -c "cd /app && npm start"
volumes:
- D:/Development/personal_projects/library-owner-frontend :/app
ports:
- "4200:4200"
# Build the container using the nginx Dockerfile
nginx:
build: ./nginx
# Map Nginx port 80 to the local machine's port 80
ports:
- "85:85"
# Link the client container so that Nginx will have access to it
links:
- client
我看到的是:
这是一个包含图片链接的HTML代码。
link: client
部分。如果你根本没有使用这个nginx服务(你描述的用例并不需要),那么就更不需要了。 - jannis