Docker networking, backend connection problem - node.js

I have three containers
Nginx container to forward requests to app container.
App container for React client
Proxy-server container serves as proxy server between react client and apis
I trying to hide backend from outer world with docker networking
I have the following docker-compose file
version: '3.3'
networks:
backend:
frontend:
services:
nginx:
build:
context: services/nginx
ports:
- '80:80'
depends_on:
- app
networks:
- frontend
app:
env_file: '.env'
build:
context: services/app/client
networks:
- backend
- frontend
volumes:
- '~/.bash_history:/.bash_history'
depends_on:
- proxy-server
proxy-server:
env_file: '.env'
build:
context: services/app/server
networks:
- backend
Dockerfile of proxy-server
# build environment
FROM node:12.2.0-alpine as build
WORKDIR /app
COPY package.json /app/package.json
RUN yarn
COPY . /app
CMD ["node", "./bin/www"]
Dokerfile of app
# build environment
FROM node:12.2.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json /app/package.json
RUN yarn
COPY . /app
RUN yarn build
# production environment
FROM nginx:1.16.0-alpine
COPY --from=build /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY config/nginx.conf /etc/nginx/conf.d
CMD ["nginx", "-g", "daemon off;"]
I try to access the proxy server from app with
http://proxy-server:4000
but Failed to load resource: net::ERR_NAME_NOT_RESOLVED

Related

How to run Nest+React app with docker-compose if both containers work fine separately

I try to run both React and Nest apps with docker-compose up, but it fails while running the server.
However, there is not problems when starting 2 containers separately.
The error is:
server | Unknown command: "start:dev"
server |
server | Did you mean one of these?
server | npm run start:dev # run the "start:dev" package script
docker-compose.yml:
version: "3"
services:
client:
container_name: client
build:
context: ./client
dockerfile: Dockerfile
image: client
ports:
- "3000:3000"
volumes:
- ./client:/app
server:
container_name: server
build:
context: ./server
dockerfile: Dockerfile
image: server
ports:
- "8080:8080"
volumes:
- ./server:/app
Server Dockerfile:
FROM node:16
WORKDIR ./app
COPY package.json ./
RUN npm install
COPY . .
ENV PORT=8080
EXPOSE 8080
CMD ["npm", "run", "start:dev"]
Client Dockerfile:
FROM node:16
WORKDIR ./app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
The issue is with docker-compose.yml, but I can't see if it's connected to nest or docker.
Thank you for the help in advance.

getting a local docker-composed node / express & react app deployed on GCP using a single Dockerfile

I have a nodejs / express / react app running locally that starts a node server at :3001 and a react app at :3000 which can make requests to the express API.
I then made a /client/Dockerfile and a /server/Dockerfile, and a /docker-compose.yml which is capable of running my app locally without issue.
I now want to deploy this to GCP's Cloud Run, but GCP does not allow multiple docker images / docker-compose (AFAIK - it only has a single field for "Dockerfile"), so I am trying to reconfigure things to work with a single Docker file.
Here's what I had for the working, local instance:
client/Dockerfile
FROM node:lts-slim
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
EXPOSE 3000
CMD [ "npm", "start" ]
server/Dockerfile
FROM node:lts-slim
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
EXPOSE 3001
# You can change this
CMD [ "npm", "run", "dev" ]
docker-compose.yml
version: "3"
services:
client:
container_name: gcp-cloudrun-client
build:
context: ./client
dockerfile: Dockerfile
image: mheavers/gcp-cloudrun-client
ports:
- "3000:3000"
volumes:
- ./client:/usr/src/app
server:
container_name: gcp-cloudrun-server
build:
context: ./server
dockerfile: Dockerfile
image: mheavers/gcp-cloudrun-server
ports:
- "3001:3001"
volumes:
- ./server:/usr/src/app
How do I combine all this into a single Dockerfile and do away with docker-compose?
this was my final docker file to replace docker-compose:
FROM node:lts-slim AS client
WORKDIR /usr/src/app
COPY client/ ./client/
RUN cd client && npm install && npm run build
FROM node:lts-slim AS server
WORKDIR /root/
COPY --from=client /usr/src/app/client/build ./client/build
COPY server/package*.json ./server/
RUN cd server && npm install
COPY server/index.js ./server/
EXPOSE 80
CMD ["node", "./server/index.js"]

Docker containers unable to reach each other. Getting CONNECTION_REFUSED

I am new to Docker but here's what I want to do. Have a create-react-app and node API server
Both are different folders "client" and "server".
I have Dockerfiles for both and docker-compose.yml at the root of project.
Now when I run docker-compose up and try to reach API server from React I get CONNECTION REFUSED error.
Can anyone help me on this ? React App is running on 3000 & API Server is running on 9040
Here are the DockerFiles and docker-compose files
Dockerfile of React
FROM node:13.12.0-alpine
WORKDIR /app
COPY package*.json ./
RUN yarn install
COPY . .
EXPOSE 8080
EXPOSE 3000
CMD ["yarn", "run", "frontend"]
Dockerfile of Node/Express API
FROM node:12
WORKDIR /app
COPY package*.json ./
RUN yarn install
COPY . .
EXPOSE 9040
CMD ["yarn", "run", "backend"]
Docker Compose file
version: "3"
services:
wt-client:
image: sriram2207/wt-client
stdin_open: true
ports:
- "3000:3000"
- "8080:8080"
networks:
- wt-app
wt-server:
image: sriram2207/wt-server
ports:
- "9040:9040"
networks:
- wt-app
networks:
wt-app:
driver: bridge
All I want to do is run both containers with one single command
Instead of connecting via localhost:9040, try connecting via wt-server:9040

Docker Compose File for Node.Js app that wraps a static frontend

I am new to Docker and I am trying to figure out how to setup a docker-compose file to build my node.js image and my frontend image
my Node.js App wraps my Vue.js Frontend and serves the static files.
Folder Structure
In my docker-compose.yml
root of project
version: '3'
services:
backend:
env_file:
"./app/frontend/.env"
build:
context: ./app
dockerfile: ./Dockerfile
image: "myusername/backend"
ports:
- "8000:8000"
frontend:
build:
context: ./app/frontend
dockerfile: ./Dockerfile
image: "myusername/frontend"
ports:
- "8080:8080"
links:
- "backend"
Backend DockerFile
/app
FROM node:12.18.3
LABEL version="1.0"
LABEL description="This is the base docker image for Backend"
LABEL maintainer = ["test#testuser.com"]
WORKDIR /app
COPY ["package.json", "package-lock.json", "./"]
RUN ls
RUN npm install --production
COPY . .
EXPOSE 8000
CMD ["node", "app.js"]
DockerFile for Frontend
/app/fronted
FROM node:12.18.3
LABEL version="1.0"
LABEL description="This is the base docker image for Frontend"
LABEL maintainer = ["test#testuser.com"]
WORKDIR /app/frontend
COPY ["package.json", "package-lock.json", "./"]
RUN npm install --production
COPY . .
EXPOSE 8080
CMD ["npm", "start"]
I am not sure if this is correc, I can successfully build the images, but can't navigate to any of the ports I exposed
docker-compose config
does return the contents of my docker-compose.yml file
Note
at the root of /app and app/frontend
there is a .dockerignore file that just contains
node_modules

Docker Compose with Docker Toolbox: Node, Mongo, React. React app not showing in the said adress

I am trying to run Express server and React app trough docker containers.
The Express server runs correctly at the given address (the one on Kitematic GUI).
However I am unable to open the React application trough the given address, giving me site cannot be reached.
Running Windows 10 Home with Docker Toolbox.
React app dockerfile:
FROM node:10
# Set the working directory to /client
WORKDIR /frontend
# copy package.json into the container at /client
COPY package*.json ./
# install dependencies
RUN npm install
# Copy the current directory contents into the container at /client
COPY . .
# Make port 3001 available to the world outside this container
EXPOSE 3001
# Run the app when the container launches
CMD ["npm", "run", "start"]
Node/Express dockerfile:
# Use a lighter version of Node as a parent image
FROM node:10
# Set the working directory to /api
WORKDIR /backend
# copy package.json into the container at /api
COPY package*.json ./
# install dependencies
RUN npm install
# Copy the current directory contents into the container at /api
COPY . .
# Make port 3000 available to the world outside this container
EXPOSE 3000
# Run the app when the container launches
CMD ["npm", "start"]
Docker compose file:
version: '3'
services:
client:
container_name: hydrahr-client
build: .\frontend
restart: always
environment:
- REACT_APP_BASEURL=${REACT_APP_BASEURL}
expose:
- ${REACT_PORT}
ports:
- "3001:3001"
links:
- api
api:
container_name: hydrahr-api
build: ./backend
restart: always
expose:
- ${SERVER_PORT}
environment: [
'API_HOST=${API_HOST}',
'MONGO_DB=${MONGO_DB}',
'JWT_KEY=${JWT_KEY}',
'JWT_HOURS_DURATION=${JWT_HOURS_DURATION}',
'IMAP_EMAIL_LISTENER=${IMAP_EMAIL_LISTENER}',
'IMAP_USER=${IMAP_USER}',
'IMAP_PASSWORD=${IMAP_PASSWORD}',
'IMAP_HOST=${IMAP_HOST}',
'IMAP_PORT=${IMAP_PORT}',
'IMAP_TLS=${IMAP_TLS}',
'SMTP_EMAIL=${SMTP_EMAIL}',
'SMTP_PASSWORD=${SMTP_PASSWORD}',
'SMTP_HOST=${SMTP_HOST}',
'SMTP_PORT=${SMTP_PORT}',
'SMTP_TLS=${SMTP_TLS}',
'DEFAULT_SYSTEM_PASSWORD=${DEFAULT_SYSTEM_PASSWORD}',
'DEFAULT_SYSTEM_EMAIL=${DEFAULT_SYSTEM_EMAIL}',
'DEFAULT_SYSTEM_NAME=${DEFAULT_SYSTEM_NAME}',
'SERVER_PORT=${SERVER_PORT}'
]
ports:
- "3000:3000"
depends_on:
- mongo
mongo:
image: mongo
restart: always
container_name: mongo
ports:
- "27017:27017"
Running with docker-compose up -d
UPDATE 1:
I am able to run the react application using docker run -p 3000:3000 hydra-client-test after building that image.
Since running the container with -p 3000:3000 works, the client is actually probably listening on port 3000. Try setting:
ports:
- 3001:3000

Resources