简体   繁体   English

如何使用docker-compose运行Angular?

[英]How can I run Angular using docker-compose?

I'm trying to create my own MEAN solution from scratch and I want to use docker-compose to run it. 我正在尝试从头开始创建自己的MEAN解决方案,并且我想使用docker-compose来运行它。 I've succeeded in making the Node server work, but when I try to run the Angular client using docker, that doesn't work. 我已经成功地使Node服务器正常工作,但是当我尝试使用docker运行Angular客户端时,那是行不通的。

Just using docker I run the following commands: 仅使用docker,我运行以下命令:

docker build -t ng-client . docker build -t ng-client

docker run -p 4200:4200 ng-client

And from the output, it appears to be working, but when I go to http://localhost:4200/ I get the message that localhost isn't working. 从输出看,它似乎正在运行,但是当我转到http:// localhost:4200 /时,我收到一条消息,表明localhost无法正常工作。

I have the same issue when I use docker-compose by using this command: 通过以下命令使用docker-compose时,我遇到相同的问题:

docker-compose up

My Dockerfile looks like this: 我的Dockerfile看起来像这样:

# based on https://mherman.org/blog/dockerizing-an-angular-app/
# base image
FROM node:8.9.3

# install chrome for protractor tests
# RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
# RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
# RUN apt-get update && apt-get install -yq google-chrome-stable

# set working directory
RUN mkdir /usr/src/app
WORKDIR /usr/src/app

# add `/usr/src/app/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH

# install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install

# add app
COPY . /usr/src/app

EXPOSE 4200

# start app
CMD npm start

And docker-compose.yml looks like this: docker-compose.yml看起来像这样:

version: '3'
services:
  server:
    build: ./server
    ports:
    - '3000:3000'
  # db:
  #   build: mongo:4.1
  #   ports:
  #   - '27017:27017'
  client:
    build: ./client
    ports:
    - '4200:4200'

The full code is at https://github.com/FrisoPrograms/MEANpress/tree/feature/initial-docker 完整代码在https://github.com/FrisoPrograms/MEANpress/tree/feature/initial-docker

Try: 尝试:

ng serve --host 0.0.0.0

Have the angular serve process listen to requests on 0.0.0.0 instead of localhost. 让角度服务进程监听0.0.0.0而不是localhost上的请求。 The reason to use --host 0.0.0.0 is the default of localhost will not work as the server by default binds to localhost and in this case, server needs to listen to all the requests, not just ones coming to localhost. 使用--host 0.0.0.0是默认值localhost的原因将不起作用,因为服务器默认情况下绑定到localhost,在这种情况下,服务器需要侦听所有请求,而不仅仅是侦听到localhost的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM