我正在尝试将现有的Angular 2应用程序停靠。
我在根级别的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"]
我有一个名为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/;
}
}
在root级别再次使用docker-compose.yml:
version: '2'
services:
# Build the container using the client Dockerfile
client:
build: .
# Build the container using the nginx Dockerfile
nginx:
build: ./nginx
# Map Nginx port 80 to the local machine's port 80
ports:
- "85:80"
# Link the client container so that Nginx will have access to it
links:
- client
Docker-compose build
成功,甚至docker-compose up
成功,但是,当我点击localhost:4200时,表示无法访问网站。
这里出了什么问题?我的应用程序不称为客户端,它有不同的名称。我关注(https://dpopescu.me/2017/03/13/running-angular-applications-inside-a-docker-container-part-1/)本教程
更新:After running docker-compose ps
我看到以下输出:
D:\Development\Personal projects\library-owner-frontend>docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------
libraryownerfrontend_client_1 npm start Up 4200/tcp
libraryownerfrontend_nginx_1 nginx -g daemon off; Up 0.0.0.0:4200->4200/tcp, 80/tcp
答案 0 :(得分:2)
我非常确定您需要添加ports
,因为EXPOSE
只会公开端口,因此链接服务可以访问它们。尝试将docker-compose.yml
文件更改为:
version: '2'
services:
# Build the container using the client Dockerfile
client:
build: .
ports: "4200:4200"
...
如果您现在运行docker-compose ps
命令,您应该看到类似的内容:
<name> <command> Up 0.0.0.0:4200->4200/tcp
而不是:
<name> <command> Up 0.0.0.0:<RANDOM>->4200/tcp