Dockerizing Angular 2应用程序失败

时间:2018-02-07 17:29:22

标签: angular docker docker-compose angular-cli

我正在尝试将现有的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时,表示无法访问网站。

enter image description here

这里出了什么问题?我的应用程序不称为客户端,它有不同的名称。我关注(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 

1 个答案:

答案 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