Fail to resolve docker compose service name from front end - docker

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

Related

Unable to establish communication between API and WEB service in 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!

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...

Making an HTTP request within the same Docker network

I have a few services running in different docker containers, as per my docker-compose:
version: '3'
services:
rest:
build:
context: './service/'
image: persian_rest:latest
container_name: persian_rest
ports:
- 8080:8080
networks:
- persian_net
volumes:
- persian_volume:/data
scheduler:
build:
context: './scheduler/'
image: persian_scheduler:latest
container_name: persian_scheduler
networks:
- persian_net
ui:
build:
context: './ui/'
image: persian_ui:latest
container_name: persian_ui
ports:
- 5000:5000
networks:
- persian_net
database:
image: 'mongo:latest'
container_name: 'persian_database'
networks:
- persian_net
environment:
- MONGO_INITDB_ROOT_USERNAME=persian_admin
- MONGO_INITDB_ROOT_PASSWORD=123456
ports:
- 27017:27017
volumes:
- persian_volume:/data
volumes:
persian_volume:
networks:
persian_net:
driver: bridge
I have my UI persian_ui service making HTTP request to the REST service persian_rest. I thought that since they were in the same network, I would just make a request to http://persian_rest:8080/api
However, when I do make that request, it fails to find that resource:
Does anyone know why my containers joined by the same network are not able to perform requests?
Currently you are looking at a webpage at localhost:5000. You requested the webpage from the server localhost:5000 and it complied and sent you a webpage which is now sitting on your computer.
If you now want to access an API on the same server as the webpage, you can make another request to localhost but this time port 8080. localhost:8080/api.
The webpage in the browser is on the client-side, and the names you've given your containers are for reference inside the server. From outside the server, currently the reference is localhost.

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"

Docker hostnames are not resolved in a custom network

I have the following configuration in my docker-composer.yml file.
version: '3.3'
services:
service-1:
container_name: 'service-1'
build: './service-1'
depends_on:
- 'mongo'
- 'consul'
networks:
backend:
aliases:
- service-1
service-2:
build: './service-2'
ports:
- '8825:8825'
- '8835:8835'
networks:
frontend:
backend:
aliases:
- service-2
depends_on:
- 'mongo'
- 'consul'
consul:
image: 'consul:latest'
networks:
backend:
aliases:
- consul
mongo:
image: 'mongo:latest'
networks:
backend:
aliases:
- mongo
networks:
frontend:
backend:
internal: true
When my containers start they are not able to communicate between each other using host names.
Most of containers use the mongo db container, but they are not able even reach it and I am getting the following error.
Error connecting to mongo : no reachable servers
Please help me to solve the problem, I got stuck.
Thanks.
You've got a lot of unneeded settings in the compose file, here's a stripped down version that would work just as well:
version: '3.3'
services:
service-1:
build: './service-1'
networks:
- backend
service-2:
build: './service-2'
ports:
- '8825:8825'
- '8835:8835'
networks:
- frontend
- backend
consul:
image: 'consul:latest'
networks:
- backend
mongo:
image: 'mongo:latest'
networks:
- backend
networks:
frontend:
backend:
internal: true
You automatically get the alias of the service name for each container, no need to duplicate that. You also lose the ability to scale a service if you give it a container name. I'd also recommend moving the build step out of the compose file and use an image name for the apps you're building locally.
Now for the likely issue, you have a depends_on in your compose file. At best, this will not do what you're looking for. All it checks that the other container has been created and started, but not that the application inside is ready to serve traffic, and a DB may take time to become available. At worst, you'll get an error that it's unsupported if you try to move this into swarm mode.
Instead of depending on docker for this, update your application entrypoint to check for the external dependencies and wait a minute or two for them to become available before failing. A very simple example tool for this is wait-for-it that is written as a bash shell script.

Resources