How to share enviroment variables in docker - 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.

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

docker-compose force services to create seperate containers even when images are the same

I have 2 services which use the same image:, what can i do, to force docker-compose to generate 2 seperate containers?
Thanks!
EDIT:
Full docker-compose:
version: "3.5"
services:
database:
container_name: proj-database
env_file: ../orm/.env.${PROJ_ENV}
image: postgres
restart: always
ports:
- 5432:5432
networks:
- proj
api:
image: golang:1.17
container_name: proj-api
env_file: ../cryptoModuleAPI/.env.${PROJ_ENV}
restart: always
build: ../cryptoModuleAPI/
links:
- database:database
ports:
- 8080:8080
volumes:
- ../cryptoModuleAPI:/proj/api
- ../orm:/proj/orm
networks:
- proj
admin:
image: golang:1.17
container_name: proj-admin
env_file: ../admin/.env.${PROJ_ENV}
restart: always
build: ../admin/
links:
- database:database
ports:
- 8081:8081
volumes:
- ../admin:/proj/admin
- ../orm:/proj/orm
networks:
- proj
networks:
proj:
external:
name: proj
I just run with docker-compose up
You misunderstand how the build and image directives work when used together.
Paraphrasing the docs,
https://docs.docker.com/compose/compose-file/compose-file-v3/#build
If you specify image as well as build, then Compose names the built image with the value of the image directive.
Compose is going to build two images, both named the same thing. Only one will survive. I'm surprised your app spins up at all!
Provide a different name for the image directive of each service, or leave it out entirely.

Communication between two .net core containers

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

Dockerfile - possible command line argument?

I have a value in a Dockerfile called ${APP_NAME}. What is it? If this were bash scripting, I would assume it to be some sort of variable but it hasn't been assigned a value anywhere. Is it a command line argument? If so, how would I pass it in when I wanted to call docker-compose with it?
For reference, the Docker file looks like this:
version: '2'
services:
nginx:
container_name: ${APP_NAME}_nginx
hostname: nginx
build:
context: ./containers/nginx
dockerfile: Dockerfile
ports:
- "80:80"
- "443:443"
volumes:
- .:/app
links:
- phpfpm
networks:
- backend
phpfpm:
container_name: ${APP_NAME}_phpfpm
hostname: phpfpm
expose:
- "9000"
build:
context: ./containers/php-fpm
dockerfile: Dockerfile
working_dir: /app
volumes:
- .:/app
links:
- mysql
networks:
- backend
mysql:
container_name: ${APP_NAME}_mysql
hostname: mysql
build:
context: ./containers/mysql
dockerfile: Dockerfile
volumes:
- ./storage/mysql:/var/lib/mysql
- ${MYSQL_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
environment:
- MYSQL_DATABASE=${DB_DATABASE}
- MYSQL_ROOT_PASSWORD=${DB_PASSWORD}
ports:
- "33061:3306"
expose:
- "3306"
networks:
- backend
networks:
backend:
driver: "bridge"
And actually, I'm probably going to have a lot of questions about docker because I've never really used it before so a reference to Dockerfile syntax would be helpful.
This means that there is probably somewhere in your project .env file which contains variables necessary for docker compose. You can find more about it at the official docker compose docs. It says that you can set default values for environment variables using a .env file, which Compose automatically looks for. Values set in the shell environment override those set in the .env file. Try to find more here: https://docs.docker.com/compose/compose-file/#variable-substitution

Resources