2017-07-06 24 views
1

我想dockerize角4前端與後端的Django和PostgreSQL數據庫。此時我的文件如下所示。我很確定這是否正確完成?當我嘗試docker-compose up時,我得到了有關使用Angular 4的前端和使用Django的後端成功啓動的信息。不幸的是,當我打開http://localhost:4200它不工作(本地主機:8001似乎工作):Dockerized角4和Django的成功編譯但本地主機:4200不工作

Safari can't open the page because the server unexpectedly dropped the connection 

我的日誌:從前端

├── Backend 
│ ├── AI 
│ │ ├── __init__.py 
│ │ ├── __pycache__ 
│ │ │ ├── __init__.cpython-36.pyc 
│ │ │ ├── settings.cpython-36.pyc 
│ │ │ ├── urls.cpython-36.pyc 
│ │ │ └── wsgi.cpython-36.pyc 
│ │ ├── settings.py 
│ │ ├── urls.py 
│ │ └── wsgi.py 
│ └── manage.py 
├── Dockerfile 
├── Frontend 
│ └── angularProject 
     ├── Dockerfile 
│  └── all files in my angular project 
├── docker-compose.yml 
└── requirements.txt 

Dockerfile:我的文件

​​

結構:

# Create image based on the official Node 6 image from dockerhub 
FROM node:6 

# 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 
COPY . /usr/src/app 

# Expose the port the app runs in 
EXPOSE 4200 

# Serve the app 
CMD ["npm", "start"] 

Dockerfile從主目錄:

FROM python:3.6.1 
ENV PYTHONUNBUFFERED 1 
RUN mkdir /code 
WORKDIR /code 
ADD requirements.txt /code/ 
RUN pip3 install -r requirements.txt 
ADD . /code/ 

docker-compose.yml

version: '3' 

services: 
    db: 
    image: postgres 
    environment: 
     POSTGRES_USER: aso 
     POSTGRES_PASSWORD: somepass 
    django: 
    build: . 
    command: python3 MainDirectory/backend/myProject/manage.py runserver 0.0.0.0:8001 
    volumes: 
     - .:/code 
    ports: 
     - "8001:8001" 
    depends_on: 
     - db 
    angular: 
    build: MainDirectory/frontend 
    ports: 
     - "4200:4200" 
    depends_on: 
     - django 

聽力:

CONTAINER ID  IMAGE    COMMAND     CREATED    STATUS    PORTS     NAMES 
e214d1966286  dockerproj_angular "npm start"    2 hours ago   Up 36 seconds  0.0.0.0:4200->4200/tcp dockerpri_angular_1 
b9fa457bd119  dockerproj_django "python3 PROJ/backe..." 2 hours ago   Up 37 seconds  0.0.0.0:8000->8000/tcp dockerpri_django_1 
e36635448b6e  postgres   "docker-entrypoint..." 2 hours ago   Up 37 seconds  5432/tcp     dockerpri_db_1 
docker exec e214d1966286 ss -ltn 
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port 
LISTEN  0  128    127.0.0.11:42403     *:*  
LISTEN  0  128    127.0.0.1:4200      *:* 

SOLUTION:package.json"start": "ng serve"改爲"start": "ng serve --host 0.0.0.0"

+0

你有沒有考慮過使用nginx代替節點?它不依賴於任何npm的東西。您只需將構建的文件複製到您的nginx容器中的一個目錄中即可獲得服務。 Nginx默認爲80端口,所以請記住。我從來沒有與其他配置混淆,但你可以包含一個配置文件nginx設置像它監聽的端口的東西。這是最基本的。 http://queirozf.com/entries/angular-2-app-running-on-nginx-on-docker-a-simple-example – SaxyPandaBear

+0

什麼是應用服務角?它在聽:4200嗎? – Robert

+0

@SaxyPandaBear我也試着用nginx。不幸的是我有一些問題,所以我決定嘗試這種方式。我將在Heroku上部署它。我注意確定哪種方法更優化? – wahtdbogh

回答

0

這個問題似乎涉及到你的節點的應用程序被監聽接口。

當搬運工映射的端口,但它確實它主機接口和所述容器的eth0接口之間的,而不是本地主機接口。

所以,你說,改變這種:

"start": "ng serve" 

到:

"start": "ng serve --host 0.0.0.0". 

將解決這個問題。

+0

這很有趣,因爲我已經使用了Docker Compose和Angular CLI,並且我沒有使用localhost 4200的問題。 – SaxyPandaBear

相關問題