I'm using nginx to serve angular(index.html) and that works fine. The issue is that I keep getting error when trying to communicate with my backend.
My NGINX Angular Dockerfile
FROM node:12-alpine as builder
WORKDIR /usr/src/app
COPY . .
RUN npm install --silent
RUN npm run ng build --prod
FROM nginx:latest
COPY --from=builder /usr/src/app/dist/FrontEnd /var/www
COPY ./default.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
My Golang Dockerfile
FROM golang:latest
ENV GO111MODULE=on
RUN mkdir /app
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go build main.go
EXPOSE 8000
CMD [ "./main" ]
My Docker-compose
version: '3.7'
services:
postgres:
image: "postgres:latest"
restart: always
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
- data:/var/lib/postgresql/data
ports:
- "5432:5432"
environment:
- POSTGRES_DB=Project
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=password
networks:
- mynet
#Back-end
golang:
build:
context: .
dockerfile: Dockerfile
container_name: golang
links:
- postgres
ports:
- "8000:8000"
networks:
- mynet
#Front-end Angular Application
angular:
links:
- golang
build:
context: ./FrontEnd
dockerfile: Dockerfile
environment:
- host=golang
ports:
- "4200:80"
networks:
- mynet
volumes:
data:
networks:
mynet:
driver: bridge
My nginx default.conf
worker_processes 4;
events {
worker_connections 1024;
}
http {
server {
listen 80;
root /var/www;
index index.html index.htm;
include /etc/nginx/mime.types;
location / {
try_files $uri $uri/ /index.html;
}
location /users {
proxy_pass http://golang:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
}
}
This is the result of when I try to send api call to golang [1]: https://i.stack.imgur.com/XQuhf.png
I've been stuck on this issue for several days now and is running low on time. Please help! Thanks in advance.
CodePudding user response:
golang:8000
is only reachable in docker network. You could check your golang API by visiting http://35.238.xx.xx:4200/users/xxx
in browser, and use relative url like "/users/xxx"
in your angular code.