【问题标题】:Unable to connect to docker container development machine无法连接到docker容器开发机
【发布时间】:2022-11-23 02:34:51
【问题描述】:

我正在尝试将 Rails 应用程序配置为用户 docker-compose 以进行本地开发。尝试在浏览器中访问它时我没有得到响应,我收到DNS_PROBE_FINISHED_NXDOMAIN

docker-compose.yml

version: '3.8'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    ports:
      - "3000:3000"
    volumes:
      - .:/app
      - gem_cache:/usr/local/bundle/gems
      - node_modules:/app/node_modules
      - web_logs:/app/log
    env_file: .env
    environment:
      - RAILS_ENV=development
    depends_on:
      - database
      - redis
  database:
    image: postgres:14-bullseye
    restart: always
    ports:
      - 5432:5432
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: integral_development
  redis:
    image: redis:6.2
volumes:
  postgres_data:
  gem_cache:
  node_modules:
  web_logs:

文件

FROM ruby:3.1.2-slim-buster

# Install dependencies
RUN apt-get update && apt-get install -y \
  build-essential \
  libpq-dev \
  nodejs \
  postgresql-client \
  yarn

# Bundle install
WORKDIR /app

COPY Gemfile Gemfile.lock ./

RUN bundle config build.nokogiri --use-system-libraries

RUN bundle check || bundle install 

# Yarn install
# COPY package.json yarn.lock ./
# RUN yarn install

# Copy the main application.
COPY . ./

ENTRYPOINT ["./entrypoints/docker-entrypoint.sh"]

docker-entrypoint.sh

#!/bin/sh

set -e

if [ -f tmp/pids/server.pid ]; then
  rm tmp/pids/server.pid
fi

bundle exec foreman start -f Procfile

简介

web: bundle exec puma -C config/puma.rb -p 3000
worker: bundle exec rake jobs:work

【问题讨论】:

  • 尝试连接到localhost:3000127.0.0.1:3000

标签: ruby-on-rails docker macos docker-compose


【解决方案1】:

如果你 docker exec 进入容器,你可以做一个 curl http://localhost:3000 并从应用程序获取 html 吗?

如果可行,则很可能您的应用只接受来自本地主机的连接。请记住,容器内的本地主机与容器外的本地主机不同。

您需要告诉应用程序它应该绑定到 0.0.0.0 而不是这样它将允许来自任何地方的连接。我从未部署过 ruby​​ 应用程序或使用过 puma,但基于 github readme,我认为将你的 bundle exec 命令更新为 bundle exec puma -C config/puma.rb -b tcp://0.0.0.0:3000 应该可以。

【讨论】:

    猜你喜欢
    • 2018-01-03
    • 2019-06-15
    • 2015-05-01
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2017-10-05
    相关资源
    最近更新 更多