LoginSignup
5
4

More than 3 years have passed since last update.

【Rails】rails6 + docker + nuxt ssr で connect ECONNREFUSED ERROR socket hang up

Last updated at Posted at 2020-03-01

rails + docker + nuxtでなにか作ろうとしていた際にハマった部分
時間があるときに詳細を書きたい。

docker-compose.yml
version: '3'
services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: password
    ports:
      - '3306:3306'
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - mysql-data:/var/lib/mysql
  backend:
    build: ./backend/
    command: bash -c "rm -f tmp/pids/server.pid && bundle install && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    depends_on:
      - db
    stdin_open: true
    tty: true
    command: bundle exec rails server -b 0.0.0.0
  frontend:
    build: ./frontend/
    command: npm run dev
    volumes:
      - .:/app
    ports:
      - 8080:3000
volumes:
  mysql-data:
    driver: local

frontend/pages/users/_id.vue
<template>
  <h1>Hello, {{ id }}</h1>
</template>

<script>
export default {
  asyncData ({ $axios, params }) {
    return $axios.$get(`http://localhost:3000/users/${params.id}`).then((res) => {
      return { id: res.id }
    })
  }
}
</script>

ブラウザからapi(http://localhost:3000/users/1) を叩くと

{
id: 1,
email: "test@example.com",
created_at: "2020-02-29T13:39:03.638Z",
updated_at: "2020-02-29T13:39:12.055Z",
url: "http://localhost:3000/users/1"
}

vueのfront(http://localhost:8080/users/1) からアクセスすると

frontend_1  |  ERROR  socket hang up
frontend_1  | 
frontend_1  |   at connResetException (internal/errors.js:604:14)
frontend_1  |   at Socket.socketOnEnd (_http_client.js:460:23)
frontend_1  |   at Socket.emit (events.js:323:22)
frontend_1  |   at Socket.EventEmitter.emit (domain.js:482:12)
frontend_1  |   at endReadableNT (_stream_readable.js:1204:12)
frontend_1  |   at processTicksAndRejections (internal/process/task_queues.js:84:21)

以下に書き換える

frontend/pages/users/_id.vue
<template>
  <h1>Hello, {{ id }}</h1>
</template>

<script>
export default {
  asyncData ({ $axios, params }) {
    return $axios.$get(`http://backend:3000/users/${params.id}`).then((res) => {
      return { id: res.id }
    })
  }
}
</script>

Request failed with status code 403
スクリーンショット 2020-03-01 16.21.47.png

railsを以下を追加

backend/config/environments/development.rb
  config.hosts << "backend"

解決した。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4