I have the following Docker Compose file where I declare 3 services: db, api, sdk-py-test
The service sdk-py-test creates a container craft-sdk-py-test which sends a bunch of http requests to the GraphQL API container craft-api created by the service named api.
The requests succeed when craft-sdk-py-test sends them on the external URL: http://172.30.0.3:5433/graphql
But when I try to send them via the Docker network internal URL: http://api:5433/graphql I get immediately an error message:
gql.transport.exceptions.TransportServerError: 502 Server Error: notresolvable for url: http://api:5433/graphql
How can I use the internal service name api to route the requests instead of the IP address?
Docker Compose File
version: "3.5"
services:
db:
container_name: craft-db
restart: always
image: craft-db
env_file:
- ./.env
ports:
- 5432:5432
api:
container_name: craft-api
restart: always
image: craft-api
env_file:
- ./.env
depends_on:
- db
ports:
- 5433:5433
sdk-py-test:
container_name: craft-sdk-py-test
image: craft-sdk-py-test
build:
context: ./
dockerfile: Dockerfile.test
env_file:
- ./.env
depends_on:
- api
volumes:
- ./tests:/tests/tests
- ./craft:/tests/craft
command: ["nose2", "-v"]
Related
I am beginner in Docker and can not get response from my project that running in docker. I have a Go project with 4 services. When It Run as local machine in my pc, everything is good and not have problem. But when it run in docker and send request by postman, could not get response and socket hang up was present.
I have 4 service for this:
1- Rest API service that dockerfile is :
FROM golang:latest as GolangBase
...
...
EXPOSE 8082
CMD ["/go/bin/ecg", "server"]
2- Page service that dockerfile is :
FROM golang:latest as GolangBase
...
...
EXPOSE 8080
CMD ["/go/bin/ecg", "page"]
2- Redis
3- Postgres
docker-compose in root:
version: "2.3"
services:
server:
build:
context: .
dockerfile: docker/app/Dockerfile
container_name: ecg-go
ports:
- "127.0.0.1:8082:8082"
depends_on:
- postgres
- redis
networks:
- ecg-service_default
restart: always
page:
build:
context: .
dockerfile: docker/page/Dockerfile
container_name: ecg-page
ports:
- "127.0.0.1:8080:8080"
depends_on:
- postgres
networks:
- ecg-service_default
restart: always
redis:
image: redis:6
container_name: ecg-redis
volumes:
- redis_data:/data
networks:
- ecg-service_default
postgres:
image: postgres:alpine
container_name: ecg-postgres
environment:
POSTGRES_PASSWORD: docker
POSTGRES_DB: ecg
POSTGRES_USER: ecg
volumes:
- pg_data:/var/lib/postgresql/data
networks:
- ecg-service_default
volumes:
pg_data:
redis_data:
networks:
ecg-service_default:
I build images and run containers by docker-compose up -d command and all services is created and running.
But when sending Request to http://localhost:8082/.. it return Could not get response, socket hang up.
What's the problem ??
How to take place name resolution. net::ERR_NAME_NOT_RESOLVED error is occurred, when using docker compose and docker network
Hello.
When I use docker compose and docker network to create frontend server and backend server and db server, net::ERR_NAME_NOT_RESOLVED is occurred in google chrome console.
Below file is my docker-compose.yml.
I assured that the communication of backend-container to frontend-container is no problem, because I attached to frontend-container ( using command of docker exec -it frontend-container /bin/bash) and confirmed communication ( using command of curl backend-container:4000).
However, when I attempted to communicate backend-container from web browser (frontend application), net::ERR_NAME_NOT_RESOLVED is occurred in chrome developer console.
I think that name resolution is not taking place from web browser.
However I have no idea how to name resolution in this case.
Please help me and teach me how to resolve name in this case.
Thank you.
version: "3.5"
services:
frontend:
build:
context: "./project01_frontend"
dockerfile: "Dockerfile.prod"
container_name: frontend-container
restart: always
tty: true
ports:
- "80:80"
env_file:
- "./project01_frontend/.env"
depends_on:
- "db"
- "backend"
backend:
build:
context: "./project01_backend"
dockerfile: "Dockerfile.dev"
container_name: backend-container
restart: always
tty: true
expose:
- "4000"
env_file:
- "./project01_backend/.env"
depends_on:
- "db"
command: ["./wait-for-db-container.sh", "yarn", "start"]
db:
image: mysql:5.7.34
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
container_name: db-container
volumes:
- ./project01_backend/initdb.d:/docker-entrypoint-initdb.d
environment:
MYSQL_DATABASE: "typeorm_db"
MYSQL_ROOT_PASSWORD: "password"
TZ: "Asia/Tokyo"
restart: "always"
expose:
- "3306"
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:)
I have a static Gatsby app that needs the uri from another container for a Hasura GraphQL connection.
The Problem
The Gatsby container finishes the docker build before Hasura's, so the URI in Gatsby is set to undefined.
How can I make it so the uri is dynamic and changes to Hasura's actual container IP address when it's done building?
What I tried
Add a depends_on in docker-compose.yml to force Gatsby to wait until the Hasura container is ready, so it'll have the IP by the time Gatsby container starts the build. But according to [0] it doesn't guarantee that Gatsby will wait until Hasura finishes to start itself.
It suggests adding a custom bash script to force the Gatsby container to wait. If I were to use wait-for-it.sh, what should the subcommand be (the command after the wait finishes)?
[0] https://docs.docker.com/compose/startup-order/
docker-compose.yml
version: '3.6'
services:
database:
image: postgres:12.2
container_name: 'postgres-db'
env_file:
- database.env
volumes:
- ./schema.sql:/docker-entrypoint-initdb.d/1-schema.sql
- ./seed.sql:/docker-entrypoint-initdb.d/2-seed.sql
hasura:
image: hasura/graphql-engine:v1.2.1
restart: on-failure
container_name: 'hasura'
depends_on:
- database
ports:
- '8180:8080'
env_file:
- hasura.env
web:
build: '.'
image: 'webserver'
container_name: 'nginx-webserver'
restart: on-failure
depends_on:
- hasura
ports:
- '8080:80'
volumes:
- /app/node_modules
- .:/app
env_file:
- webserver.env
webserver.env file
NODE_ENV=production
GATSBY_WEBPACK_PUBLICPATH=/
HASURA_ENDPOINT=http://hasura:8080/v1/graphql
GraphQL Apollo client that needs the Hasura URI:
export const client = new ApolloClient({
uri: process.env.HASURA_ENDPOINT,
fetch,
});
Found the solution.
I was thinking about the container network relationship incorrectly.
The client looks at the HOST'S ip address when connecting, not the container's.
Explanation
The Hasura container exposes itself to everyone via localhost:8180. If you look at the docker-compose file, port 8180:8080 means "Make Hasura's port 8080 accessible to localhost's port 8180".
The gatsby app (nginx-webserver) should point to localhost:8180, not hasura:8080.
My final docker-compose.yml:
version: '3.6'
services:
database:
image: postgres:12.2
container_name: 'postgres-db'
env_file:
- database.env
volumes:
- ./schema.sql:/docker-entrypoint-initdb.d/1-schema.sql
- ./seed.sql:/docker-entrypoint-initdb.d/2-seed.sql
hasura:
image: hasura/graphql-engine:v1.2.1
restart: on-failure
container_name: 'hasura'
depends_on:
- database
ports:
- '8180:8080'
env_file:
- hasura.env
web:
build: '.'
image: 'nginx-webserver'
container_name: 'web'
restart: on-failure
ports:
- '8080:80'
volumes:
- .:/app
- app/node_modules
env_file:
- webserver.env
ApolloClient setup:
import ApolloClient from 'apollo-boost';
import fetch from 'isomorphic-fetch';
export const HASURA_ENDPOINT_URI =
process.env.NODE_ENV === 'development'
? 'http://localhost:8090/v1/graphql'
: 'http://localhost:8180/v1/graphql';
export const client = new ApolloClient({
uri: HASURA_ENDPOINT_URI,
fetch
});
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.