Communication between two .net core containers - docker

net core application. I have webapp and api application.
Below is my docker-compose file.
version: '3.4'
services:
enrichment.webapi:
container_name: enrichment.webapi
ports:
- 8000:80
- 8001:443
environment:
- "ASPNETCORE_URLS=https://+;http://+"
- ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx
- ASPNETCORE_Kestrel__Certificates__Default__Password=mypassword
volumes:
- ./conf.d/https/:/https/
build:
context: .
dockerfile: Enrichment.WebApi/Dockerfile
enrichment.webapp:
image: enrichment.web
ports:
- 7000:80
- 7001:443
environment:
- "ASPNETCORE_URLS=https://+;http://+"
- ASPNETCORE_Kestrel__Certificates__Default__Path=/https/aspnetapp.pfx
- ASPNETCORE_Kestrel__Certificates__Default__Password=mypassword
volumes:
- ./conf.d/https/:/https/
build:
context: .
dockerfile: Enrichment.Web/Dockerfile
depends_on:
- enrichment.webapi
When I do docker-compose up I see below when I do docker ps
0.0.0.0:8000->80/tcp, 0.0.0.0:32835->80/tcp, 0.0.0.0:8001->443/tcp, 0.0.0.0:32834->443/tcp
for the api app. When I do https://localhost:32834/index.html it works fine. But this port 32834 dynamically assigned. I want to set some static port so that I can handle my CORS in my web app. Each time this port is keep on getting changed when I rebuild docker-compose file. So Is there any way to handle this. Any help would be appreciated. Thanks

Related

HTTPS for docker containers

I am developing a workflow service as a training project. Abstracting from the details, everything you need to know for this question is in the image. For deployment, I rented a server and ran docker-compose on it. Everything works well, but what I'm worried about is that ports 8000 and 5432 are open.
The first question is, is it worth worrying? And if so, how to get rid of it?
Docker-compose file content below
version: "3"
services:
db:
container_name: 'emkk-db'
image: postgres
volumes:
- ./backend/data:/var/lib/postgresql/data
env_file:
- ./backend/db.env
ports:
- "5432:5432"
backend:
container_name: 'emkk-backend'
image: emkk_backend
build: ./backend
volumes:
- ./backend:/emkk/backend
env_file:
- ./backend/.env
ports:
- "8000:8000"
depends_on:
- db
frontend:
container_name: 'emkk-frontend'
image: emkk_frontend
build: ./frontend
command: npm run start
env_file:
- ./frontend/.env
volumes:
- /emkk/frontend/node_modules
- ./frontend:/emkk/frontend
ports:
- "80:80"
depends_on:
- backend
I also want to configure HTTPS protocol. I tried installing nginx and putting a certificate on it using a certbot, and then proxying requests to containers. I sat with this for several hours and I still did not manage to achieve anything better than a HTTPS for the nginx start page.
Maybe I'm doing completely wrong things, but I'm new to this, I haven't had to deal with deployments before. I would be grateful for your answers, which will contain an idea or an example of how you can do this.
If you don't have a connection to 8000 (probably WAS) or 5432 (database) from an external server, you can change docker-compose.yml to:
you have to expose only necessary ports for external clients.
when you connect to backend from web, you should use service name like backend:8000
when you connect to db from backend, you should use service name like db:5432
version: "3"
services:
db:
container_name: 'emkk-db'
image: postgres
volumes:
- ./backend/data:/var/lib/postgresql/data
env_file:
- ./backend/db.env
backend:
container_name: 'emkk-backend'
image: emkk_backend
build: ./backend
volumes:
- ./backend:/emkk/backend
env_file:
- ./backend/.env
depends_on:
- db
frontend:
container_name: 'emkk-frontend'
image: emkk_frontend
build: ./frontend
command: npm run start
env_file:
- ./frontend/.env
volumes:
- /emkk/frontend/node_modules
- ./frontend:/emkk/frontend
ports:
- "80:80"
depends_on:
- backend
And, you can use nginx proxy manager to service with HTTPS and a certificate from the certbot.

Docker Compose Error: services Additional property ​* is not allowed

Question
Is the following output an error?
Target
I want to run frontend, backend and a database container through Docker.
I want to hot reload my docker-compose builds on code changes.
Context
If I run this on PowerShell: docker-compose build; docker-compose up -d, I ran into this:
services Additional property ​mongodb is not allowed
services Additional property ​mongodb is not allowed
docker-compose.yml:
version: '3.8'
services:
api:
build: ./api
container_name: api
ports:
- 4080:4080
networks:
- network-backend
- network-frontend
depends_on:
- '​mongodb'
volumes:
- .:/code
​mongodb:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
ports:
- 27017:27017
networks:
- network-backend
volumes:
- db-data:/mongo-data
volumes:
db-data:
networks:
network-backend:
network-frontend:
I thought this is regarded to this issue.
OK found the answer. There are a weird chars in the config file. VS Code and Notebook don't showed me the chars. After testing a couple online YAML validators, I detected the issue.
Youtube Video of the Error

How do you set two docker containers from the same docker-compose to communicate with one another using HTTP-Get?

Okay so we have this C# .net core app going on, which has 3 parts. Each part communicates through HTTP requests. The docker-compose starts all 3 parts
Using postman, we're able to use dbconn directly and successfully connect to the db.
However, we can't go from the app to the dbconn. If we make a GET request from deployUS to connDB, it throws an error saying :
---> System.Net.Http.HttpRequestException: Connection refused (dbconn:5002)
This is our docker-compose.yml:
version: '3.9'
services:
job:
container_name: "job"
build:
context: .
dockerfile: Job/Dockerfile
ports:
- "5003:80"
dbconn:
container_name: "dbconn"
build:
context: .
dockerfile: ConnDB/Dockerfile
ports:
- "5002:80"
depends_on:
- database
deploy:
container_name: "deploy"
build:
context: .
dockerfile: Deploy/Dockerfile
# command: docker run -p 5002:5002
ports:
- "5001:80"
depends_on:
- dbconn
database:
container_name: "database"
image: postgres:latest
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=deploy
- POSTGRES_DB=deploy_DB
volumes:
- ./DB/init-script.sql:/docker-entrypoint-initdb.d/init-script.sql
- deploy-databse:/var/lib/postgresql/data/
ports:
- "5432:5432"
volumes:
deploy-database:
Note: The dbconn container is called with http://dbconn:5002
Is there a way that I can tell my containers to talk to one another? Thanks a lot:)

Request from one docker container to another fails

I've been trying to connect two docker containers. My flask backend and my react frontend, when I use localhost in the request the request goes through, but when i use the docker container name ie http://backend-service:5000/endpoint , the name can't be resolved. The documentation states that the containers connect to the same networking automatically and that accessing services from one should be as simple as that. I've tried adding links to the docker compose file as well with no luck.
Here is my docker-compose file:
version: '3'
services:
backend-service:
build: ./api
expose:
- 5000
ports:
- "5000:5000"
volumes:
- ./api:/usr/src/app
environment:
- FLASK_ENV=development
- FLASK_APP=app.py
- FLASK_DEBUG=1
client-service:
build: ./clientside
expose:
- 3000
ports:
- "3000:3000"
volumes:
- ./clientside/src:/usr/src/app/src
- ./clientside/public:/usr/src/app/public
links:
- "backend-service:backend"

How to share enviroment variables in docker

I´m new to docker and I´m having lots of troubles to make it start. I´m making an asp.net core 1.0.1 application user docker container tools for visual studio 2017. I have the following env.file in the same root as the compose file with this values:
REDIS_PORT=6379
and this docker compose yml:
version: '2'
services:
haproxy:
image: eeacms/haproxy
links:
- webapplication3
ports:
- "80:80"
webapplication3:
image: webapplication3
enviroment:
- REDIS_PORT=${REDIS_PORT}
build:
context: .
dockerfile: Dockerfile
links:
- redis
ports:
- "80"
redis:
image: redis
ports:
- ${REDIS_PORT}
I want to know the redis port I have to connect to from my Asp.net core app. As far as I know, the only way to do it is using env variables, and since I don´t want to copy paste the port everywhere I´d like to use the .env file style. Anyway this is not working saying:
Unsupported config option for services.webapplication3:'enviroment'
Any ideas what the problem could be?
You missed a letter "n" in a word environment.
You need to pass the env_file option:
version: '2'
services:
haproxy:
image: eeacms/haproxy
links:
- webapplication3
ports:
- "80:80"
webapplication3:
image: webapplication3
env_file:
- env.file
environment:
- REDIS_PORT=${REDIS_PORT}
build:
context: .
dockerfile: Dockerfile
links:
- redis
ports:
- "80"
redis:
image: redis
env_file:
- env.file
ports:
- ${REDIS_PORT}
Take a look at https://docs.docker.com/compose/environment-variables/ for more info.

Resources