Nuxt3 Strapi4 in one Docker container | Error: connect ECONNREFUSED 127.0.0.1:1337

I’m running in a docker container called nuxtsrapi with two apps as two different images. one is a nuxt3 app in a folder named frontend on port 3000 the other is a strapi4 app in a backend on port 1337.
my strapi app is set up for sqlite as db and I’m using yarn.

My questions are:

  1. How can I be able to connect them so that when I run the container I won’t get the error: nuxtstrapi-frontend-1 | Error: connect ECONNREFUSED 127.0.0.1:1337
  2. How should set up my files do they will run a develop env on my local machine and production on a server

Here are some content of my docker files

Dockerfile in frontend

FROM node:14.19.1
WORKDIR /app/frontend
COPY package*.json ./
RUN yarn install
COPY . .
EXPOSE 3000
CMD ["yarn", "dev"]

Dockerfile in backend

FROM node:14.19.1
WORKDIR /app/backend
COPY package*.json ./
RUN yarn install
COPY . .
EXPOSE 1337
CMD ["yarn", "start"]

docker-compose.yml in root

version: '3'
services:
  frontend:
    build:
      context: ./frontend
    ports:
      - 3000:3000
  backend:
    build:
      context: ./backend
    ports:
      - 1337:1337
    environment:
      DATABASE_CLIENT: sqlite
      DATABASE_NAME: strapi
      DATABASE_HOST: backend
      DATABASE_PORT: 1337
      DATABASE_USERNAME: strapi
      DATABASE_PASSWORD: strapi

Hello @tarikkavaz ,

that means it’s trying to access the 1337 port within the same frontend container.

you should be accessing the backend from the front end using proper container hostname. below is the

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

hope this will help

1 Like

In addition to @alishah730’s good advice, you have probably realized it by now, but I have noticed the DATABASE environment variables are set in the backend container not the frontend. I guess that was a mistake and the variables should have been declared in the frontend container so it would know about the hostname that you actually set. On the other hand, I don’t understand those variables because as far as I know, sqlite doesn’t use any port and the title mentions “Nuxt3 Strapi4 in one Docker container” but we are talking about two containers.

So my guess is that you haven’t got any answer until now, because the issue was confusing and it is hard to tell what the problem is without knowing what is behind the compose files.