Dockerize several solution project into one dockerfile - docker

There are several microservices in solution: api, auth, filtering. I'm wondering how to run them all in one dockerfile and docker-compose.
Here what i've done:
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["ShoppingCart.API/ShoppingCart.API.csproj", "ShoppingCart.API/"]
RUN dotnet restore "ShoppingCart.API/ShoppingCart.API.csproj"
COPY . .
WORKDIR "/src/ShoppingCart.API"
RUN dotnet build "ShoppingCart.API.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "ShoppingCart.API.csproj" -c Release -o /app/publish
WORKDIR /app
COPY --from=build /app/build .
ENTRYPOINT ["dotnet", "ShoppingCart.API.dll"]
Docker-compose:
version: '3.9'
networks:
localdev:
name: localdev
services:
api:
container_name: api
build: .
depends_on: [ mssql ]
ports:
- "8080:80"
networks:
- localdev
ordering:
container_name: ordering
mssql:
image: "mcr.microsoft.com/mssql/server"
container_name: mssql
hostname: mssql
environment:
SA_PASSWORD: "fA0bD7rO3iE1mU0w"
ACCEPT_EULA: "Y"
restart: unless-stopped
networks:
- localdev
ports:
- "1433:1433"
expose:
- 1433
rabbitmq:
image: "rabbitmq:3-management"
container_name: "rabbitmq"
ports:
- "5672:5672"
- "15672:15672"
So, API i could wrapped in docker. How to make it with others? For instance, in solution is ShoppingCart.Auth

Related

Docker subdomen (Error: P1001: Can't reach database server at `db-subdomen`:`5436` )

I have a main domen and 3 subdomains on server are running on it, but I decided to add 3 subdomains and for some reason I get an error (Error: P1001: Can't reach database server at db-subdomen:5436 or 5432 ) . Project Next.js + prisma +docker.
As I understand it, the problem is either inside the scope in the container. Or in the .env file
In the other 3 subdomains , I have the same cat in Dockerfile and env . In docker-compose, which is the same for all projects, everything is also the same, but there is still an error. There may be a typo , I don't know anymore, tk did everything as always
My docker-compose (for all projects , this example for main and one subdomen):
#subdomen
app-subdomen:
container_name: app-subdomen
image: subdomen-image
build:
context: subdomen
dockerfile: Dockerfile
restart: always
environment:
NODE_ENV: production
networks:
- subdomen-net
env_file: subdomen/.env
ports:
- 7000:3000
depends_on:
- "db-subdomen"
command: sh -c "sleep 13 && npx prisma migrate deploy && npm start"
db-subdomen:
container_name: db-subdomen
env_file:
- subdomen/.env
image: postgres:latest
restart: always
volumes:
- db-subdomen-data:/var/lib/postgresql/data
networks:
- subdomen-net
#main domen
app-main:
image: main-image
build:
context: main
dockerfile: Dockerfile
restart: always
environment:
NODE_ENV: production
env_file: main/.env
ports:
- 3000:3000
depends_on:
- "db-main"
command: sh -c "sleep 3 && npx prisma migrate deploy && npm start"
networks:
- main-net
db-main:
env_file:
- main/.env
image: postgres:latest
restart: always
volumes:
- db-main-data:/var/lib/postgresql/data
networks:
- main-net
volumes:
db-main-data: {}
db-subdomen-data: {}
networks:
main-net:
name: main-net
subdomen-net:
name: subdomen-net
.env subdomen :
POSTGRES_USER=subdomenUser
POSTGRES_PASSWORD=subdomen
POSTGRES_DB=subdomen-db
SECRET=88xU_X8yfsfdsfsdfsdfsdfsdfdsdc
HOST=https://subdomen.domen.ru
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}#db-arkont:5436/${POSTGRES_DB}?schema=public
Submodem Dockerfile (the other 3 projects and subdomains have the same problem and there is no problem:
FROM node:lts-alpine AS builder
# Create app directory
WORKDIR /app
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./
COPY prisma ./prisma/
# Install app dependencies
RUN npm install
RUN npx prisma generate
COPY . .
RUN npm run build
FROM node:lts-alpine
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public
COPY --from=builder /app/prisma ./prisma
ENV NODE_ENV=production
EXPOSE 3000

Docker-compose with multi stage dockerfile is not creating images

I want to use single Dockerfile for my multiple services
I have docker file like below
FROM node:alpine as ui
WORKDIR /app/ui
COPY ./ui/package.json .
RUN npm install
COPY ./ui/ .
RUN npm run start
FROM node:alpine as server
WORKDIR /app/server
COPY ./server/package.json .
RUN npm install
COPY ./server/ .
RUN npm run start
FROM nginx as engine
EXPOSE 3000 5000 80
CMD ["nginx", "-g", "daemon off;"]
And yaml file looks like
version: "3.4"
services:
api:
build:
context: ./
target: server
volumes:
- /app/node_modules
- ./server:/app/server
environment:
- PGUSER=postgres
- PGHOST=postgres
- PGDATABASE=postgres
- PGPASSWORD=postgres_password
- PGPORT=5432
ui:
stdin_open: true
environment:
- CHOKIDAR_USEPOLLING=true
build:
context: ./
target: ui
volumes:
- /app/node_modules
- ./ui:/app/ui
postgres:
image: "postgres:latest"
environment:
- POSTGRES_PASSWORD=postgres_password
nginx:
depends_on:
- api
- ui
restart: always
volumes:
- ./nginx/dev.conf:/etc/nginx/conf.d/default.conf
build:
context: ./
target: engine
ports:
- "80:80"
Here , When I run docker-compose -f dev.yml up , nothing happens its not creating all the images, when i use seperate dockerfile for ui and api , its working fine , but for single dockerfile like i mentioned , its not creating any images.Can some one explain correct way to use multistage with dockercompose.
I trie to put nginx at top and it got struck at Building 0.0s (0/6)

Cant Reach Docker Container

I am trying to build my microservice project with docker. I can reach vue app but i cant reach my identity service. I tried some small projects with the same dockerfile and compose file it worked. Netstat gives same url i checked that. I tried nearly everthing i see but cant find any solution.
Dockercompose
version: "3.8"
services:
category:
container_name: category
build:
context: .
dockerfile: sln/CodeChat-Backend/Category/Dockerfile
ports:
- "7000:7000"
depends_on:
- redis
networks:
- es-network
gateway:
container_name: gateway
build:
context: .
dockerfile: sln/CodeChat-Backend/Gateway/Dockerfile
ports:
- "7007:7007"
depends_on:
- identity
- category
- message
networks:
- es-network
identity:
container_name: identity
restart: always
build:
context: .
dockerfile: sln/CodeChat-Backend/Identity/Dockerfile
ports:
- "7001:7001"
depends_on:
- mssql
networks:
- es-network
message:
container_name: message
build:
context: .
dockerfile: sln/CodeChat-Backend/Message/Dockerfile
ports:
- "7002:7002"
depends_on:
- elasticsearch
- rabbitmq
- redis
networks:
- es-network
codechat-web:
container_name: vue
build:
context: .
dockerfile: /Dockerfile
environment:
- CHOKIDAR_USEPOLLING=true
ports:
- "8081:8080"
networks:
- es-network
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.0.0
container_name: elasticsearch
restart: always
environment:
- cluster.name=docker-cluster
- xpack.security.enabled=false
- discovery.type=single-node
ulimits:
memlock:
soft: -1
hard: -1
ports:
- "9200:9200"
networks:
- es-network
rabbitmq:
image: rabbitmq:3-management-alpine
container_name: "rabbitmq"
ports:
- "5672:5672"
- "15672:15672"
networks:
- es-network
redis:
image: redis
environment:
- ALLOW_EMPTY_PASSWORD=yes
ports:
- "6379:6379"
networks:
- es-network
mssql:
image: mcr.microsoft.com/mssql/server:2019-latest
environment:
SA_PASSWORD: "1576Orhan_"
ACCEPT_EULA: "Y"
ports:
- "1433:1433"
networks:
- es-network
networks:
es-network:
driver: bridge
Vue dockerfile
FROM node:lts-alpine
RUN npm install -g http-server
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 80
CMD [ "http-server", "dist" ]
Category Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /app
EXPOSE 80
COPY sln/CodeChat-Backend/Category/*.csproj sln/CodeChat-Backend/Category/
COPY sln/CodeChat-Backend/Core/*.csproj sln/CodeChat-Backend/Core/
RUN dotnet restore sln/CodeChat-Backend/Category/*.csproj
COPY . .
RUN dotnet publish sln/CodeChat-Backend/Category/*.csproj -c Release -o out
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS runtime
WORKDIR /app
COPY --from=build /app/out .
ENV ASPNETCORE_URLS http://*:7000
ENV ASPNETCORE_ENVIRONMENT docker
EXPOSE 7000
ENTRYPOINT ["dotnet","Category.dll"]
Gateway dockerfile
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /app
EXPOSE 80
COPY sln/CodeChat-Backend/Gateway/*.csproj sln/CodeChat-Backend/Gateway/
RUN dotnet restore sln/CodeChat-Backend/Gateway/*.csproj
COPY . .
RUN dotnet publish sln/CodeChat-Backend/Gateway/*.csproj -c Release -o out
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS runtime
WORKDIR /app
COPY --from=build /app/out .
ENV ASPNETCORE_URLS http://*:7007
ENV ASPNETCORE_ENVIRONMENT docker
EXPOSE 7007
ENTRYPOINT ["dotnet","Gateway.dll"]
Identity dockerfile
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
WORKDIR /app
EXPOSE 80
COPY sln/CodeChat-Backend/Identity/*.csproj sln/CodeChat-Backend/Identity/
COPY sln/CodeChat-Backend/Core/*.csproj sln/CodeChat-Backend/Core/
RUN dotnet restore sln/CodeChat-Backend/Identity/*.csproj
COPY . .
RUN dotnet publish sln/CodeChat-Backend/Identity/*.csproj -c Release -o out
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS runtime
WORKDIR /app
COPY --from=build /app/out .
ENV ASPNETCORE_URLS http://*:7001
ENV ASPNETCORE_ENVIRONMENT docker
EXPOSE 7001
ENTRYPOINT ["dotnet","Identity.dll"]
Message dockerfile
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /app
EXPOSE 80
COPY sln/CodeChat-Backend/Message/*.csproj sln/CodeChat-Backend/Message/
COPY sln/CodeChat-Backend/Core/*.csproj sln/CodeChat-Backend/Core/
RUN dotnet restore sln/CodeChat-Backend/Message/*.csproj
COPY . .
RUN dotnet publish sln/CodeChat-Backend/Message/*.csproj -c Release -o out
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS runtime
WORKDIR /app
COPY --from=build /app/out .
ENV ASPNETCORE_URLS http://*:7002
ENV ASPNETCORE_ENVIRONMENT docker
EXPOSE 7002
ENTRYPOINT ["dotnet","Message.dll"]
Gateway ocelot
{
"Routes": [
{
"DownstreamPathTemplate": "/api/category/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 7000
}
],
"UpstreamPathTemplate": "/category/{everything}",
"UpstreamHttpMethod": [ "GET","POST","PUT","DELETE" ]
},
{
"DownstreamPathTemplate": "/api/user/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 7001
}
],
"UpstreamPathTemplate": "/user/{everything}",
"UpstreamHttpMethod": [ "GET","POST","PUT","DELETE" ]
},
{
"DownstreamPathTemplate": "/api/message/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 7002
}
],
"UpstreamPathTemplate": "/message/{everything}",
"UpstreamHttpMethod": [ "GET","POST","PUT","DELETE" ]
}
],
"GlobalConfiguration": {
"BaseUrl": "http://localhost:7007"
}
}

Two Api connection via docker-compose and docker registry

We have one API project with DockerFile
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 43001
EXPOSE 43002
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY Api.csproj Api/
RUN dotnet restore Api/Api.csproj
COPY . .
WORKDIR /src/Api
RUN dotnet build Api.csproj --no-restore -c Release -o /app
FROM build AS publish
WORKDIR /src/Api
RUN dotnet publish Api.csproj --no-restore --no-self-contained -c Release -o /app
FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "Api.dll"]
And with docker-compose
version: '3.5'
services:
Api:
image: Api
restart: always
environment:
- ASPNETCORE_URLS=https://+:443;http://+:80
- ASPNETCORE_ENVIRONMENT=Production
build:
context: .
dockerfile: Api/Dockerfile
ports:
- "43001:80"
- "43002:443"
This project pushed to private docker registry.
And we have second Prod project that should use and connect to Api project.
docker-compose Prod project:
version: '3.5'
services:
Prod:
image: Prod
restart: always
environment:
- ASPNETCORE_ENVIRONMENT=Production
- ASPNETCORE_URLS=http://+:80
build:
context: .
dockerfile: Prod/Dockerfile
depends_on:
- Api
networks:
- proxy-net
expose:
- "80"
Api:
image: {RegistryAddress}/Api
restart: always
networks:
- proxy-net
ports:
- "43002:443"
networks:
proxy-net:
external:
name: proxy-net
Issue - When I am connecting from Prod to Api I am getting Connection refused. Perhaps something with ports settings, but I tried a lot of changes and it didn`t help. I thought Api ports settings should be enough, but looks like we need to set something in Prod also.

How to run docker-compose in production

I have built a MEAN stack application with nginx front end.
I have 2 docker files - one for front end and one for back end
And I have a docker-compose file that pulls them together along with the database
This works great on my development machine
I then push the images to my dockerhub site
On my production ubuntu machine I pull the images that I want from my dockerhub repository
But how should I run them?
I transfer my docker-compose file to the server and try to run it:
docker-compose -f docker-compose.prod.yml up
but it complains that the folder structure isnt what I have on my dev machine:
ERROR: build path /home/demo/api either does not exist, is not accessible, or is not a valid URL.
I dont want to put all the code on the server and rebuild it.. surely that defeats the purpose of using dockerhub images?
I also need the docker compose file to pull in the .prod.env file for database credentials etc.
I know Im missing something here.
How do I run my images without rebuilding them from scratch?
Do I need another service for this?
Thanks in advance
docker-compose.prod.yml:
version: '3'
services:
# Database
database:
env_file:
- .prod.env
image: mongo
restart: always
environment:
# MONGO_INITDB_ROOT_USERNAME: root
# MONGO_INITDB_ROOT_PASSWORD: $DB_ADMIN_PASSWORD
# Create a new database. Please note, the
# /docker-entrypoint-initdb.d/init.js has to be executed
# in order for the database to be created
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: $MONGO_INITDB_ROOT_PASSWORD
DB_NAME: $DB_NAME
DB_USER: $DB_USER
DB_PASSWORD: $DB_PASSWORD
MONGO_INITDB_DATABASE: $DB_NAME
volumes:
# Add the db-init.js file to the Mongo DB container
- ./mongo-init.sh:/docker-entrypoint-initdb.d/mongo-init.sh:ro
- /data/db
ports:
- '27017-27019:27017-27019'
networks:
- backend-net
# Database management
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: $MONGO_INITDB_ROOT_PASSWORD
ME_CONFIG_MONGODB_SERVER: database
depends_on:
- database
networks:
- backend-net
# Nodejs API
backend:
depends_on:
- database
env_file:
- .prod.env
build:
context: ./api
dockerfile: Dockerfile-PROD-API
# Note: put this container name into proxy.conf.json for local angular CLI development instead of localhost
container_name: node-api-prod
networks:
- backend-net
# Nginx and compiled angular app
frontend:
build:
context: ./ui
dockerfile: Dockerfile-PROD-UI
ports:
- "8180:80"
container_name: nginx-ui-prod
networks:
- backend-net
networks:
backend-net:
driver: bridge
DOCKERFILE-PROD-API:
#SERVER ========================================
FROM node:10-alpine as server
WORKDIR /app
# Install dependencies
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . .
#RUN ls -lha
EXPOSE 3000
CMD ["npm", "run", "start"]
DOCKERFILE-PROD-UI:
#APP ========================================
FROM node:10-alpine as build
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install #angular/cli && npm install
COPY . .
RUN npm run build
#RUN ls -lha
#FINAL ========================================
FROM nginx:1.18.0-alpine
COPY --from=build /usr/src/app/dist /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
Using full image names including dockerhub path resolved the issue for me.
Working solution shown below:
Dockerfile-PROD-UI
#GET ANGULAR ========================================
FROM node:10-alpine as base
WORKDIR /usr/src/app
COPY ui/package*.json ./
RUN npm install #angular/cli && npm install
COPY ui/. .
#BUILD ANGULAR ========================================
FROM base as build
RUN npm run build
#RUN ls -lha
#NGINX ========================================
FROM nginx:1.18.0-alpine
COPY --from=build /usr/src/app/dist /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
Dockerfile-PROD-API
#SERVER ========================================
FROM node:10-alpine as server
WORKDIR /app
# Install dependencies
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . .
#RUN ls -lha
EXPOSE 3000
CMD ["npm", "run", "start"]
docker-compose.yml
version: '3.5'
services:
# Database
database:
image: mongo
restart: always
env_file:
- .prod.env
environment:
# MONGO_INITDB_ROOT_USERNAME: root
# MONGO_INITDB_ROOT_PASSWORD: $DB_ADMIN_PASSWORD
# Create a new database. Please note, the
# /docker-entrypoint-initdb.d/init.js has to be executed
# in order for the database to be created
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: $MONGO_INITDB_ROOT_PASSWORD
DB_NAME: $DB_NAME
DB_USER: $DB_USER
DB_PASSWORD: $DB_PASSWORD
MONGO_INITDB_DATABASE: $DB_NAME
volumes:
# Add the db-init.js file to the Mongo DB container
- ./mongo-init.sh:/docker-entrypoint-initdb.d/mongo-init.sh:ro
- db-data:/data/db
ports:
- '27017-27019:27017-27019'
networks:
- backend-net
# Nodejs API
backend:
image: DOCKERHUBHUSER/DOCKERHUB_REPO:prod-backend
restart: always
depends_on:
- database
env_file:
- .prod.env
build:
context: ./api
dockerfile: Dockerfile-PROD-API
container_name: backend
networks:
- backend-net
# Nginx and compiled angular app
frontend:
image: DOCKERHUBHUSER/DOCKERHUB_REPO:prod-frontend
restart: always
depends_on:
- backend
build:
context: .
dockerfile: Dockerfile-PROD-UI
ports:
- "8180:80"
container_name: frontend
networks:
- backend-net
networks:
backend-net:
driver: bridge
volumes:
db-data:
name: db-data
external: true

Resources