Unable to establish communication between API and WEB service in Docker - docker

I am trying my hands-on on Docker and am a newbie to this technology. Here's an issue I am stuck since long:
I have an application composed of DJango REST Framework, Angular and MySQL as database. I am trying to dockerize each of these component and execute using docker-compose
Here is my docker-compose.yml file:
version: '3'
services:
db:
image: mysql:5.7.33
container_name: db
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: ******
MYSQL_DATABASE: database
volumes:
- mysql_db:/var/lib/mysql
api:
build:
context: ./api
dockerfile: Dockerfile
container_name: api
volumes:
- /api36:/home/api/api36
ports:
- "8010:8010"
depends_on:
- db
ui:
build:
context: ./ui
dockerfile: Dockerfile
volumes:
- /ui:/ui
container_name: ui
ports:
- "4201:4201"
depends_on:
- db
volumes:
mysql_db:
I updated settings.py file to point to db as HOST in DATABASES, so I am able to have a communication between my DB and API calls.
However, when I load the UI I face a failure saying: Failed to load resource: net::ERR_NAME_NOT_RESOLVED. I had added serviceUrl as http://api:8010/ in my environment.ts file. As api is name of my API service, I expected that docker-compose will establish communication internally, but looks like I am missing something. Also, I am able to get the expected behavior when I add ip of the machine in environment.ts file.
Can someone please help me out on this?
The way I bring up and execute all the containers is docker-compose up.
Thanks in advance!

Related

Error with linking containers in docker-compose

I am trying to build an image and a container of a application with docker compose. However when i try to link the containers it keeps giving the error "Service api has a link to service 'containername' which is undefined". Does someone know what i am doing wrong?
This is my code:
version: "3.9"
services:
db:
image: mysql:5.6
ports:
- 23306:3306
volumes:
- .:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: quint
MYSQL_DATABASE: cddb_quin
MYSQL_USER: cddb_quint
MYSQL_PASSWORD: quint
container_name: cddb_mysql
api:
build: ./backend
ports:
- 28080:8080
image: javaimage:latest
links:
- cddb_mysql:mysql
container_name: cddb_backend
web:
build: ./frontend
ports:
- 20080:80
image: angularimage:latest
links:
- cddb_backend
container_name: cddb_frontend
Compose links: are an obsolete feature. In even vaguely modern Compose – any Compose file with a top-level services: block – Compose will automatically create a Docker network for you and attach all of the services to it. You can almost always safely delete links: without losing any functionality.
The one thing that it's possible to do with links: but not Docker networking is to make another container visible under a different name. The links: [cddb_mysql:mysql] syntax you have does this. There's not particularly any benefit to doing this, though.
So the easiest way to fix the error you're getting is to just delete all of the links: blocks. You don't show how you're configuring your database connection, but you need to configure it to point to the Compose service name of the database db. Networking in Compose in the Docker documentation describes this setup further.
(You can also delete all of the container_name: settings, and on the images you build:, you can delete their manual image: names if you're not planning to push them to a registry.)
version: '3.8'
services:
api:
build: ./backend
ports:
- 28080:8080
environment:
- MYSQL_HOST=db
db: { ... }
backend: { ... }

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

Portainer - how to add local path to a volume in docker compose

I'm trying to do a basic configuration with Portainer but I can't get my volumes to connect properly. I'm trying a relatively simple configuration as you can see but I get an error when I try this:
version: '3'
networks:
frontend:
backend:
services:
app:
image: webdevops/apache:alpine
container_name: app
volumes:
- "/my/host/absolute/path/data: /var/www"
networks:
- frontend
php:
image: php:fpm-alpine
container_name: php
networks:
- backend
db:
image: mariadb
container_name: db
volumes:
- "/my/host/absolute/path/storage: /var/lib/mysql"
networks:
- backend
Could you give me a hand to make this configuration work which would make me a good little starting point to learn to setup the rest correctly.
All I can find in the documentation is how to link named volumes but I don't see how to link them to a folder on my local computer so I'm not really advanced with this information...

Fail to resolve docker compose service name from front end

Hi I'm new to using docker for development. I'm trying to communicate from frontend (react) to the backend (express.js) here.
I have enabled cors as well, I'm getting an error saying net::ERR_NAME_NOT_RESOLVED when trying to fetch from the back end using the url http://backend:4001,
but it's working when I use the docker internal IPAddress, like: http://172.18.0.3:4001.
Following is my docker-compose.yml file.
Please advise on getting this working, thanks.
version: "3"
services:
backend:
build: ./api
volumes:
- ./api:/usr/src/api
ports:
- 6002:4001
depends_on:
- database
database:
image: mongo:4.0.15-xenial
ports:
- 27018:27017
frontend:
build: ./app
volumes:
- ./app:/usr/src/app
ports:
- 6001:3000
links:
- backend
depends_on:
- backend
It will not work, because your browser(internet client) is not part of docker stack network, you have to configure you frontend service to connect to http://localhost:6002 instead of http://backend:4001

How do i make API service wait until Liquibase service applies all the changes to DB

I have 3 services. DB, API, Liquibase. API is dependant on DDB and Liquibase. Below is my compose.yml. The issue I am facing is, even though I have wait-for.sh script under API service which waits for DB port to be available for connections, it comes up as soon as the port is ready for connections. But liquibase which creates Db schema and applies DB related changes is still under process. So the API service is not able to connect to DB as the schema is still not created by the liquibase service. how do I make API service to wait until liquibase applies all the changes on DB.
version: '3'
services:
db:
build: drs-db
user: "1000:50"
volumes:
- /data/mysql:/var/lib/mysql
container_name: drs-db
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
ports:
- "3307:3306"
drs-api:
container_name: drs-api
hostname: drs-api
domainname: XX.com
build:
context: ./drs-api
args:
DRS_API_URL: XX/${DRS_API_VERSION}
DRS_API_WAR: drs-web-${DRS_API_VERSION}.war
DRS_CLIENT_URL: CC/drs-client/${DRS_CLIENT_VERSION}
DRS_CLIENT_WAR: drs-client-${DRS_CLIENT_VERSION}.war
ports:
- "8096:8443"
depends_on:
- db
- drs-liquibase
command: [/usr/local/bin/wait-for-it.sh]
links:
- "drs-liquibase"
drs-liquibase:
container_name: drs-liquibase
build:
context: ./drs-liquibase
environment:
DRS_CLIENT_VERSION : ${DRS_LIQUIBASE_VERSION}
depends_on:
- db
links:
- "db"
command: ["/usr/local/bin/wait_for_liquibase.sh"]
You could use some kind of temporary file via shared volume.
Adjust wait-for to check for the presence of a file created when migrations are ready or wait until file is deleted by migration service.

Resources