简体   繁体   English

即使我的 docker 容器已启动,也找不到本地主机?

[英]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.总的来说,我对开发人员,尤其是 Docker 领域,尤其是 Rails 还比较陌生,如果这听起来像是一个愚蠢的问题,请提前致歉。

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.在 Docker Compose 的帮助下,我试图在由 4 个服务(2 个网站和 2 个 API)+ Postgresql 组成的 monorepo 中运行一个应用程序。 The final goal is to run it on a VPS with Traefik (once I get the current app to work locally).最终目标是使用 Traefik 在 VPS 上运行它(一旦我让当前应用程序在本地运行)。

Here are the different services:以下是不同的服务:

  • Postgres (through the Postgres image available in Dockerhub) Postgres(通过 Dockerhub 中可用的 Postgres 图像)

  • a B2C website (NextJS) B2C 网站 (NextJS)

  • an admin website (React with create Vite)一个管理网站(React with create Vite)

  • an API (Rails). API(Rails)。 It should be linked to the Postgres database它应该链接到 Postgres 数据库

  • a Strapi API (for the content of the B2C website).一个 Strapi API(用于 B2C 网站的内容)。 Strapi has its own SQLite database. Strapi 有自己的 SQLite 数据库。 Only the B2C website requires the data coming from Strapi.只有 B2C 网站需要来自 Strapi 的数据。

When I run the docker compose up -d command, it seems to be working (see pic below)当我运行docker compose up -d命令时,它似乎在工作(见下图)

在此处输入图像描述

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).但是当我访问其中一个网站时(除了似乎正常工作的 Strapi)(https://localhost:3009,或 3008 或 3001),我什么也得不到(见下文)。

在此处输入图像描述

However, I don't see any error in the logs of any apps.但是,我没有在任何应用程序的日志中看到任何错误。 For instance the Rails API logs below:例如下面的 Rails API 日志:

在此处输入图像描述

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.我假设我的配置有错误,尤其是在 Rails api 的 database.yml 配置和 docker-compose.yml 文件中。

database.yml:数据库.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 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.我尝试更改相关的不同文件的代码,尤其是 database.yml、docker-compose.yml 和每个应用程序的 dockerfiles。

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:此外,我尝试使用命令docker exec -it api /bin/sh查看 api 容器 (Rails),以通过 Rails 控制台检查数据库,但我收到以下错误消息:

activeRecord::ConnectionNotEstablished could not connect to server: No such file or directory. activeRecord::ConnectionNotEstablished 无法连接到服务器:没有这样的文件或目录。 Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?服务器是否在本地运行并接受 Unix 域套接字“/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而不是 localhost 按 ctrl 并单击它传递的 url 有时不会在您网站的 localhost 端口上打开

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.看起来你在 docker 中的主机通常是127.0.0.1:3000这很好但是在 docker 中当你想将应用程序暴露给你的主机时你需要将应用程序更改为在0.0.0.0:3000上运行然后 docker 将能够将应用程序传递到您的主机。 Without specific Dockerfiles this is the best I can do.如果没有特定的 Dockerfile,这是我能做的最好的。 I have run into this issue with strapi and some other apps before so hopefully it helps.我之前在 strapi 和其他一些应用程序中遇到过这个问题,希望它能有所帮助。 It will still be localhost:3000 on the host machine if i wasn't clear.如果我不清楚,它仍然是主机上的localhost:3000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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