简体   繁体   中英

Localhost not found even if my docker containers are up?

I am relatively new to dev in general, to the Docker universe and to Rails in particular, apologize in advance if it sounds like a silly question.

I am trying to run an application in a monorepo composed of 4 services (2 websites and 2 APIs) + Postgresql, with the help of Docker Compose. The final goal is to run it on a VPS with Traefik (once I get the current app to work locally).

Here are the different services:

  • Postgres (through the Postgres image available in Dockerhub)

  • a B2C website (NextJS)

  • an admin website (React with create Vite)

  • an API (Rails). It should be linked to the Postgres database

  • a Strapi API (for the content of the B2C website). Strapi has its own SQLite database. Only the B2C website requires the data coming from Strapi.

When I run the docker compose up -d command, it seems to be working (see pic below)

在此处输入图像描述

but when I go to one of the websites (except for the Strapi that seems to be correctly working) (https://localhost:3009, or 3008 or 3001), I get nothing (see below).

在此处输入图像描述

However, I don't see any error in the logs of any apps. For instance the Rails API logs below:

在此处输入图像描述

I assume that I have mistakes in my config, especially in the database.yml config of the Rails api and the docker-compose.yml file.

database.yml:

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: pg

development:
  <<: *default
  database: chana_api_v2_development

test:
  <<: *default
  database: chana_api_v2_test

production:
  <<: *default
  database: chana_api_v2_production
  username: chana
  password: <%= ENV["CHANA_DATABASE_PASSWORD"] %>

docker-compose.yml

version: '3'
services:
# ----------------POSTGRES -----------------
  pg:
    image: postgres:14.6
    container_name: pg
    networks:
      - chana_postgres_network
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: chana_development
      POSTGRES_USER: chana
      POSTGRES_PASSWORD: chana
    volumes:
      - ./data:/var/lib/postgresql/data

# ----------------- RAILS API -----------------
  api:
    build: ./api
    container_name: api
    networks:
      - chana_postgres_network
      - api_network
    volumes:
      - ./api:/chana_api
    ports:
      - "3001:3000"
    depends_on:
      - pg

# ----------------- STRAPI -----------------
  strapi:
    build:
      context: ./strapi
      args:
        BASE_VERSION: latest
        STRAPI_VERSION: 4.5.0
    container_name: chana-strapi
    restart: unless-stopped
    env_file: .env
    environment:
      NODE_ENV: ${NODE_ENV}
      HOST: ${HOST}
      PORT: ${PORT}
    volumes:
      - ./strapi:/srv/app
      - strapi_node_modules:/srv/app/node_modules
    ports:
      - "1337:1337"

  # ----------------- B2C website -----------------
  public-front: 
    build: ./public-front
    container_name: public-front
    restart: always
    command: yarn dev
    ports:
      - "3009:3000"
    networks: 
      - api_network
      - chana_postgres_network
    depends_on:
      - api
      - strapi
    volumes:
      - ./public-front:/app
      - /app/node_modules
      - /app/.next

  # ----------------- ADMIN website -----------------
  admin-front: 
    build: ./admin-front
    container_name: admin-front
    restart: always
    command: yarn dev
    ports:
      - "3008:3000"
    networks: 
      - api_network
      - chana_postgres_network
    depends_on:
      - api
    volumes:
      - ./admin-front:/app
      - /app/node_modules
      - /app/.next

volumes:
  strapi_node_modules:

networks:
  api_network:
  chana_postgres_network:

Do you have any idea why I cannot see anything on the website pages?

I tried to change the code of the different files that are relevant, especially database.yml, docker-compose.yml, and the dockerfiles of each app.

Also, I tried to look into the api container (Rails) with the command docker exec -it api /bin/sh to check the database through the Rails console, and I get this error message:

activeRecord::ConnectionNotEstablished could not connect to server: No such file or directory. Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

在此处输入图像描述

instead of localhost press ctrl and click on the url that it passes sometimes does not open on the localhost port of your website

looks like your host in docker is 127.0.0.1:3000 normally this is fine but in docker when you want to expose the app to your host machine you need to change the app to run on 0.0.0.0:3000 and then docker will be able to pass the app through to your host machine. Without specific Dockerfiles this is the best I can do. I have run into this issue with strapi and some other apps before so hopefully it helps. It will still be localhost:3000 on the host machine if i wasn't clear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM