0

OK so I am trying to deploy a Rails app to a docker container (host machine is a mac). I was thinking to deploy in development first to check everything is working.

I have setup a phpmyadmin service and I can connect to the server by typing in server name moviedb_mariamovie_1 with user root and corresponding PW. But whatever I put into my database.yml for Rails doesn't work: I tried localhost, I tried 127.0.0.1, I tried "mariamovie" and I tried "moviedb_mariamovie_1", and it always says "host not found" when I tried rails db:create (or anything actually that involves the DB). I am totally confused by this. I read the database section of the docker manuals and I seem to be too stupid for that.

(I have other problems with this but one after the other :)

docker-compose.yml:

version: "3.7"
services:
  moviedb:
    image: tkhobbes/moviedb
    restart: unless-stopped
    ports:
      - 3001:3000
    depends_on:
      - mariamovie
    environment:
      MYSQL_ROOT_PASSWORD: redacted
      RAILS_ENV: development
    volumes:
      - /Users/thomas/Documents/Production/moviedb/storage:/opt/activestorage
  mariamovie:
    image: mariadb
    restart: unless-stopped
    ports:
      - 3333:3306
    environment:
      MYSQL_ROOT_PASSWORD: redacted
  phpmymaria:
    image: phpmyadmin
    restart: unless-stopped
    ports:
      - 8021:80
    depends_on:
      - mariamovie
    environment:
      PMA_PORT: 3333
      PMA_ARBITRARY: 1
    image: nginx:1.21-alpine
    volumes:
      - /Users/thomas/Documents/Production/moviedb/vendor/nginx:/etc/nginx/user.conf.d:ro
    ports:
      - 8020:8020
    depends_on:
      - moviedb
    restart: unless-stopped

database.yml:

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: 127.0.0.1
  port: 3333
  username: redacted
  password: redacted

development:
  <<: *default
  database: newmovie_development
...
tkhobbes
  • 368
  • 3
  • 16

1 Answers1

1

You're inside your docker "network". Your database should be accessible from your Rails app (which is inside too) via mariamovie:3306.

brcebn
  • 1,571
  • 1
  • 23
  • 46
  • thanks - don't know how I got that confused, this seemed to work! – tkhobbes Feb 06 '22 at 11:15
  • I did the same years ago. It's not obvious the first time. – brcebn Feb 07 '22 at 09:06
  • So essentially: I ONLY need to map/expose ports if I want to access stuff from OUTSIDE the docker network. WITHIN the network, everything works on standard paths and by "name". – tkhobbes Feb 07 '22 at 13:06
  • Actually there is difference between `expose` and `bind` ports. There is a cool article about that [what-is-the-difference-between-docker-compose-ports-vs-expose](https://stackoverflow.com/questions/40801772/what-is-the-difference-between-docker-compose-ports-vs-expose) – brcebn Feb 08 '22 at 16:34
  • a nice read that clarifies a lot. Thank you! Docker is a really nice and powerful technology, but some aspects of it are kind of hard to grasp :) – tkhobbes Feb 09 '22 at 08:18