0

I been able to get the reverse proxy working for my angular 5 project. With below files. I am very new to Angular and nginx. Before I dockerized the client and nginx etc. I just installed everything under one path. So I just ran one npm install and I worked with npm start, ng build --prod and ng serve.

I am just a bit confused about Angular 2 version 5, I thought I was trying to separate the client from the server. Knowing that Angular 2 runs most things client side. However right now it looks like my app.js is still being called from within the same 'client' container. Am I supposed to separate and containerize the express server and what are the benefits of doing this?

I am also going to run mongo image from a container. Am I correct in linking the Client container to mongo?

nginx 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/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
   }
}

docker-compose.yml

   version: '2'

services:
  # Build the container using the client Dockerfile
   client:
   build: ./
  # This line maps the contents of the client folder into the container.
   volumes:
    - ./:/usr/src/app
    links:
     - mongo
    depends_on:
    - mongo
  mongo:      
    image: mongo
    container_name: "mongodb"
    environment:           
    - MONGO_DATA_DIR=/data/db
    - MONGO_LOG_DIR=/dev/null
   volumes:
    - ./data/db:/data/db
   ports:
    - 27017:27017
   command: mongod --smallfiles --logpath=/dev/null # --quiet
  # Build the container using the nginx Dockerfile
 nginx:
  build: ./nginx
   # Map Nginx port 80 to the local machine's port 80
  ports:
    - "80:80"
   # Link the client container so that Nginx will have access to it
  links:
    - client

Dockerfile

  #  Create a new image from the base nodejs 7 image.
 FROM node:8.1.4-alpine as builder
 # 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 3000
 # Start the application. This is the same as running ng serve.
 CMD ["npm", "start"]

1 Answer 1

1

Even though you are running your client (angular) and server (node) in the same container, they are still "separate". They are physically located & served on the same server, but run separately. Your api layer runs on node and your angular application runs on the client.

What you have is valid. I have pretty much the same setup. I have 2 containers. A node container that runs express to serve my api layer and my angular application. Then I have the mongo container as the db.

Sign up to request clarification or add additional context in comments.

11 Comments

Hi, I now have 4 containers they are all operating, so I have express separately now, however even though I can ping to the containers within the containers I have a feeling the port forwarding is wrong? when I do docker port client or docker port server nothing shows. Is this normal behavior? I am now trying to telnet to it, I have been struggling for days with this now. Just keep getting timeouts
@oudekaas this could be a number of things. Can you hit your servers from your machine?
yes that works fine, I can ping it. check my latest question on here, bit more details.
netstat isn't showing any of the ip's of the containers nor the port, should I be able to telnet from outside the containers or within to the ip/port of respective container? using standard network which get's created through compose up
@oudekaas are you running the docker toolbox or the Docker community edition
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.