I have two different containers (1. Frontend app, 2. Webserver ) 1st dependent on server.
Currently both containers are built using seperate docker files and running perfectly fine in localhost envionment. Application is built using nodejs and angular.
I am using docker desktop for windows server 2019.
Using user defined network for both containers to communicate with each other:
docker network create –driver bridge dev_network
I need to build a docker compose file for both of them but don’t have enough knowledge on how to build a working compose file. Will be glad if anyone could help me for the same.
Thanks for your time!
Frontend docker file:
FROM node:latest as build
WORKDIR /usr/local/app
COPY ./ /usr/local/app/
RUN npm install
FROM nginx:latest
COPY --from=build /usr/local/app/dist/ClientPortal /usr/share/nginx/html
EXPOSE 80
WebServer dockerfile:
FROM node:14-alpine
ENV NODE_ENV=production
WORKDIR /usr/src/app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm install --production --silent && mv node_modules ../
COPY . .
EXPOSE 3000 8600
CMD ["node", "server.js"]
I tried with following compose file
Defined volume to a windows drive as server image folders along with docker file is located into the directory.
Issue is i docker-compose build is building both the images but when i fire the up command the server container fails to load and exits with exit code 1
Error message: Error: Cannot find module ‘/usr/src/app/server.js’
Docker-Compose file:
version: '3.4'
services:
clientportal:
image: clientportal
container_name: cspfrontend
build:
context: .
dockerfile: ./Dockerfile
environment:
NODE_ENV: production
networks:
- dev_network
ports:
- 80:80
clientportalserver:
image: clientportalserver
container_name: cspserver
build:
context: .
dockerfile: E:WorkClientPortalServer/Dockerfile
volumes:
- E:WorkClientPortalServer
networks:
- dev_network
ports:
- 3000:3000
networks:
dev_network:
driver: bridge
2
Answers
You should start by reading the documentation on the website.
https://docs.docker.com/compose/
The first page shows a working example of a simple compose file with a
web
service andredis
service. If you managed to get the containers running, then the options found in the Compose file reference will be very familiar to you.You may find this link of particular interest in regards to using an existing Docker network:
https://docs.docker.com/compose/networking/#use-a-pre-existing-network
I would go with something like this in a docker-compose.yml in the root folder:
This requires you to have a frontend and a backend folder to your projects, and in those folder you have to have those Dockerfiles, that you showed. I don’t know what port are you using in your backend project, so I guessed with port 3000.