0

I was following a tutorial on how to Dockerize a brand new Rails 7 app. I made the Docker container 'distant-mirror' Setting up the Docker container took about two hours, but now I entered in my Ubuntu terminal docker-compose up and I'm seeing this:

Starting demo-postgres-14.2 ... done
Starting distant_mirror_distant-mirror_1 ... done
Attaching to demo-postgres-14.2, distant_mirror_distant-mirror_1
demo-postgres-14.2 |
demo-postgres-14.2 | PostgreSQL Database directory appears to contain a database; Skipping initialization
demo-postgres-14.2 |
demo-postgres-14.2 | 2024-07-10 04:38:13.141 UTC [1] LOG:  starting PostgreSQL 14.2 on x86_64-pc-linux-musl, compiled by gcc (Alpine 10.3.1_git20211027) 10.3.1 20211027, 64-bit
demo-postgres-14.2 | 2024-07-10 04:38:13.141 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
demo-postgres-14.2 | 2024-07-10 04:38:13.141 UTC [1] LOG:  listening on IPv6 address "::", port 5432
demo-postgres-14.2 | 2024-07-10 04:38:13.237 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
demo-postgres-14.2 | 2024-07-10 04:38:13.304 UTC [21] LOG:  database system was shut down at 2024-07-10 04:34:48 UTC
demo-postgres-14.2 | 2024-07-10 04:38:13.326 UTC [1] LOG:  database system is ready to accept connections
demo-postgres-14.2 | 2024-07-10 04:38:14.830 UTC [28] ERROR:  database "distantmirror_production" already exists
demo-postgres-14.2 | 2024-07-10 04:38:14.830 UTC [28] STATEMENT:  CREATE DATABASE "distantmirror_production" ENCODING = 'unicode'
distant-mirror_1  | Database 'distantmirror_production' already exists
distant-mirror_1  | => Booting Puma
distant-mirror_1  | => Rails 8.0.0.alpha application starting in production
distant-mirror_1  | => Run `bin/rails server --help` for more startup options
distant-mirror_1  | [1] Puma starting in cluster mode...
distant-mirror_1  | [1] * Puma version: 6.4.2 (ruby 3.3.4-p94) ("The Eagle of Durango")
distant-mirror_1  | [1] *  Min threads: 3
distant-mirror_1  | [1] *  Max threads: 3
distant-mirror_1  | [1] *  Environment: production
distant-mirror_1  | [1] *   Master PID: 1
distant-mirror_1  | [1] *      Workers: 8
distant-mirror_1  | [1] *     Restarts: (✔) hot (✖) phased
distant-mirror_1  | [1] * Preloading application
distant-mirror_1  | [1] * Listening on http://0.0.0.0:3000
distant-mirror_1  | [1] Use Ctrl-C to stop
distant-mirror_1  | [1] - Worker 0 (PID: 24) booted in 0.01s, phase: 0
distant-mirror_1  | [1] - Worker 1 (PID: 27) booted in 0.01s, phase: 0
distant-mirror_1  | [1] - Worker 2 (PID: 34) booted in 0.01s, phase: 0
distant-mirror_1  | [1] - Worker 3 (PID: 45) booted in 0.01s, phase: 0
distant-mirror_1  | [1] - Worker 4 (PID: 57) booted in 0.01s, phase: 0
distant-mirror_1  | [1] - Worker 5 (PID: 70) booted in 0.01s, phase: 0
distant-mirror_1  | [1] - Worker 6 (PID: 81) booted in 0.01s, phase: 0
distant-mirror_1  | [1] - Worker 7 (PID: 92) booted in 0.01s, phase: 0

But when I go to localhost:3000 on Chrome, I'm getting this screen: localhost screen

Here is my docker-compose.yml file for reference:

version: '3'
services:
  db:
    image: postgres:14.2-alpine
    container_name: demo-postgres-14.2
    volumes:
      - postgres_data:/var/lib/postgresql/data
    command: 
      "postgres -c 'max_connections=500'"
    environment:
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
    ports:
      - "5432:5432"
  distant-mirror:
    build: .
    command: "./bin/rails server"
    environment:
      - RAILS_ENV=${RAILS_ENV}
      - POSTGRES_HOST=${POSTGRES_HOST}
      - POSTGRES_DB=${POSTGRES_DB}
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - RAILS_MASTER_KEY=${RAILS_MASTER_KEY}
    volumes:
      - app-storage:/rails/storage
    depends_on:
      - db
    ports:
      - "3000:3000"

volumes:
  postgres_data: {}
  app-storage: {}

And here is my Dockerfile just in case:

# syntax = docker/dockerfile:1

# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
# docker build -t my-app .
# docker run -d -p 80:80 -p 443:443 --name my-app -e RAILS_MASTER_KEY=<value from config/master.key> my-app

# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.3.4
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base

# Rails app lives here
WORKDIR /rails

# Install base packages
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y curl libjemalloc2 libvips postgresql-client && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Set production environment
ENV RAILS_ENV="production" \
    BUNDLE_DEPLOYMENT="1" \
    BUNDLE_PATH="/usr/local/bundle" \
    BUNDLE_WITHOUT="development"

# Throw-away build stage to reduce size of final image
FROM base AS build

# Install packages needed to build gems
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential bash bash-completion tzdata postgresql nodejs npm yarn git libpq-dev pkg-config && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install && \
    rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
    bundle exec bootsnap precompile --gemfile

# Copy application code
COPY . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/

# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile




# Final stage for app image
FROM base

# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN groupadd --system --gid 1000 rails && \
    useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
    chown -R rails:rails db log storage tmp
USER 1000:1000

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]

I read suggestions to move the Dockerfile into the app folder, but I tried that and it still wasn't working. Also I'm doubting that that would be the solution, since I work on another Rails project, and the Dockerfile is clearly in the main project folder and NOT within /app.

There wasn't really much for me to try other than changing the port numbers. I'm very new to how Docker works for Rails projects.

Any help would be appreciated!

1
  • When did you add the ports section? May be after container was already built?
    – mechnicov
    Commented Jul 11 at 21:38

0