I am trying to make a fetch request from a nodejs server ( that is dockerized --thus a container ) to a strapi server (that is also dockerized – thus a container ). I am running my containers using [docker compose](Docker vs Docker Compose | A Comparision On Both), meaning they already share the same network. But i am getting the following error upon making a fetch request :
ERROR [ExceptionsHandler] request to http://strapi:1377/api/login-page failed, reason: connect ECONNREFUSED 172.19.0.7:1377
As you can see in the code below, I am using the service name of the container to make a request, but i can’t seem to figure out what the error is:
Here’s my code to fetch data :
export class GetLabelsService {
async getRouteLabels(labelPath: string) {
const request = `http://strapi:1377${labelPath}`;
console.log(request)
const res = await fetch(request);
const labels_data = await res.json();
return labels_data
}
}
Here’s my docker-yaml:
version: "3.8"
services:
api:
build:
context: .
dockerfile: Dockerfile-api
environment:
DB_TYPE: ${DB_TYPE}
DB_HOST: ${DB_HOST}
DB_PORT: ${DB_PORT}
DB_USERNAME: ${DB_USERNAME}
DB_PASSWORD: ${DB_PASSWORD}
DB_DATABASE: ${DB_DATABASE}
APP_HOST: ${APP_HOST}
JWT_SECRET: ${JWT_SECRET}
JWT_EXIPRY_TIME: ${JWT_EXIPRY_TIME}
STRIPE_PUBLISH_KEY: ${STRIPE_PUBLISH_KEY}
STRIPE_SECRET_KEY: ${STRIPE_SECRET_KEY}
currency: ${currency}
FCM_DATABASE_URL: ${FCM_DATABASE_URL}
SMTP_HOST: ${SMTP_HOST}
SMTP_USERNAME: ${SMTP_USERNAME}
SMTP_PASSWORD: ${SMTP_PASSWORD}
ports:
- 4000:4000
networks:
- app-network
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin_container
restart: always
environment:
PMA_HOST: ${DB_HOST}
PMA_USER: ${DB_USERNAME}
PMA_PASSWORD: ${DB_PASSWORD}
UPLOAD_LIMIT: 64M
ports:
- "8080:80"
networks:
- app-network
strapi:
build:
context: ./cms
dockerfile: Dockerfile-cms
environment:
DATABASE_CLIENT: ${DATABASE_CLIENT}
DATABASE_HOST: ${DATABASE_HOST}
DATABASE_PORT: ${DATABASE_PORT}
DATABASE_NAME: ${DATABASE_NAME}
DATABASE_USERNAME: ${DATABASE_USERNAME}
DATABASE_PASSWORD: ${DATABASE_PASSWORD}
APP_KEYS: ${APP_KEYS}
API_TOKEN_SALT: ${API_TOKEN_SALT}
ADMIN_JWT_SECRET: ${ADMIN_JWT_SECRET}
JWT_SECRET: ${JWT_SECRET}
NODE_ENV: ${NODE_ENV}
MASTER_KEY: ${MASTER_KEY}
MEILI_HOST: http://meilisearch:7700
CLOUDINARY_KEY: ${CLOUDINARY_KEY}
CLOUDINARY_NAME: ${CLOUDINARY_NAME}
CLOUDINARY_SECRET: ${CLOUDINARY_SECRET}
volumes:
- ./cms/config:/srv/app/config
- ./cms/src:/srv/app/src
- ./cms/package.json:/srv/package.json
- ./cms/yarn.lock:/srv/yarn.lock
- ./cms/datadump:/srv/app/datadump
- ./cms/public:/srv/app/public
ports:
- 1337:1337
depends_on:
- postgres
networks:
- app-network
command: yarn develop
meilisearch:
image: getmeili/meilisearch:latest
environment:
MEILI_MASTER_KEY: ${MASTER_KEY}
ports:
- 7700:7700
networks:
- app-network
volumes:
- ./data:/srv/app/data
pgadmin:
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD}
ports:
- 8088:80
depends_on:
- postgres
networks:
- app-network
# Back-end services
postgres:
image: postgres:12
environment:
POSTGRES_USER: ${DATABASE_USERNAME}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_DB: ${DATABASE_NAME}
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- 5432:5432
networks:
- app-network
volumes:
db_data:
pgdata:
networks:
app-network:
driver: bridge
Dockerfile-api
# Dockerfile
FROM node:16-alpine
# create destination directory
RUN mkdir -p /usr/src/api
WORKDIR /usr/src/api
# update and install dependency
RUN apk update && apk upgrade
# copy the app, note .dockerignore
COPY . /usr/src/api/
RUN yarn
RUN yarn build
EXPOSE 3000
CMD [ "yarn", "start:dev" ]
My thoughts are maybe i am setting the wrong host name, but i can’t seem to figure out the probblem. Any idea of what the cause of the error is?