Consider this Dockerfile snippet for a Rails app:
FROM ruby:3.1 WORKDIR /app COPY Gemfile* ./ RUN bundle install COPY . . CMD ["rails", "server", "-b", "0.0.0.0"]
What will be the result when you build and run this container?
FROM ruby:3.1 WORKDIR /app COPY Gemfile* ./ RUN bundle install COPY . . CMD ["rails", "server", "-b", "0.0.0.0"]
Think about what the CMD command does and the effect of the -b 0.0.0.0 option.
The CMD runs the Rails server binding to 0.0.0.0, which means it listens on all interfaces inside the container, making it accessible from outside if ports are mapped.
Look at this Docker Compose service snippet:
version: '3.8'
services:
web:
build: .
ports:
- "3000:3000"
volumes:
- .:/app
command: rails server -b 0.0.0.0
environment:
- DATABASE_URL=postgres://user:pass@db:5432/mydbWhat is the syntax error here?
version: '3.8' services: web: build: . ports: - "3000:3000" volumes: - .:/app command: rails server -b 0.0.0.0 environment: - DATABASE_URL=postgres://user:pass@db:5432/mydb
YAML allows strings but Docker Compose recommends arrays for commands with arguments.
Using an array for the command ensures arguments are passed correctly without shell interpretation issues.
You have a Docker Compose file with services 'web' and 'db'. The Rails app fails to connect to the database with a connection refused error.
services:
web:
build: .
depends_on:
- db
environment:
DATABASE_URL: postgres://user:pass@db:5432/mydb
db:
image: postgres:14
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: mydbWhat is the most likely cause?
services:
web:
build: .
depends_on:
- db
environment:
DATABASE_URL: postgres://user:pass@db:5432/mydb
db:
image: postgres:14
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
POSTGRES_DB: mydbdepends_on only waits for container start, not readiness.
depends_on ensures the db container starts but does not wait for the database to be ready. Rails tries to connect too early, causing connection refused.
Given this Dockerfile snippet:
FROM ruby:3.1 WORKDIR /app COPY Gemfile Gemfile.lock ./ RUN bundle install COPY . . RUN rails assets:precompile CMD ["rails", "server", "-b", "0.0.0.0"]
How does this order affect build time when only app code changes?
FROM ruby:3.1 WORKDIR /app COPY Gemfile Gemfile.lock ./ RUN bundle install COPY . . RUN rails assets:precompile CMD ["rails", "server", "-b", "0.0.0.0"]
Docker caches layers and reuses them if previous steps don't change.
Since Gemfile and Gemfile.lock are copied before bundle install, if they don't change, that layer is cached. Copying app code later means only app code changes trigger rebuilds after bundle install.
You want your Rails Docker container to use the host machine's network directly, without port mapping. Which Docker network mode achieves this?
Think about which mode removes network isolation between container and host.
The 'host' network mode makes the container share the host's network stack, so no port mapping is needed and the container uses host IP addresses directly.