Bird
Raised Fist0
Ruby on Railsframework~20 mins

Docker deployment in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Docker Deployment Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when you run this Dockerfile for a Rails app?

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?

Ruby on Rails
FROM ruby:3.1
WORKDIR /app
COPY Gemfile* ./
RUN bundle install
COPY . .
CMD ["rails", "server", "-b", "0.0.0.0"]
AThe container builds but never runs because CMD is missing an entrypoint.
BThe container runs but the Rails server binds only to localhost, so it's inaccessible outside.
CThe container starts a Rails server accessible on all network interfaces inside the container.
DThe container fails because Gemfile is not copied before bundle install.
Attempts:
2 left
💡 Hint

Think about what the CMD command does and the effect of the -b 0.0.0.0 option.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Docker Compose service for Rails

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/mydb

What is the syntax error here?

Ruby on Rails
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
AThe environment variable DATABASE_URL must be under quotes.
BThe volumes syntax is invalid; it should use double slashes.
CThe ports mapping should be integers, not strings.
DThe command should be an array: command: ["rails", "server", "-b", "0.0.0.0"]
Attempts:
2 left
💡 Hint

YAML allows strings but Docker Compose recommends arrays for commands with arguments.

🔧 Debug
advanced
2:00remaining
Why does the Rails app fail to connect to the database in this Docker setup?

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: mydb

What is the most likely cause?

Ruby on Rails
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: mydb
AThe web service tries to connect before the db service is ready to accept connections.
BThe DATABASE_URL is missing the protocol prefix.
CThe db service lacks a volume for data persistence causing immediate failure.
DThe depends_on key is invalid and ignored by Docker Compose.
Attempts:
2 left
💡 Hint

depends_on only waits for container start, not readiness.

❓ state_output
advanced
2:00remaining
What is the effect of this Dockerfile layer caching strategy on Rails app build time?

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?

Ruby on Rails
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"]
AEvery build reruns bundle install because Gemfile is copied after app code.
BOnly the last two layers rebuild, so bundle install is cached, speeding up builds.
CAssets precompile runs before bundle install causing errors.
DThe CMD layer caches the entire build, so no rebuilds happen.
Attempts:
2 left
💡 Hint

Docker caches layers and reuses them if previous steps don't change.

🧠 Conceptual
expert
2:00remaining
Which Docker networking mode allows a Rails container to share the host's network stack?

You want your Rails Docker container to use the host machine's network directly, without port mapping. Which Docker network mode achieves this?

Ahost
Bnone
Cbridge
Doverlay
Attempts:
2 left
💡 Hint

Think about which mode removes network isolation between container and host.

Practice

(1/5)
1. What is the main purpose of using Docker for deploying a Rails application?
easy
A. To package the app and its environment together for consistent deployment
B. To write Rails code faster
C. To replace the Rails framework
D. To automatically fix bugs in the app

Solution

  1. Step 1: Understand Docker's role

    Docker packages the app with all dependencies so it runs the same everywhere.
  2. Step 2: Compare options

    Options B, C, and D describe unrelated or incorrect uses of Docker.
  3. Final Answer:

    To package the app and its environment together for consistent deployment -> Option A
  4. Quick Check:

    Docker deployment = packaging app + environment [OK]
Hint: Docker bundles app + environment for consistent runs [OK]
Common Mistakes:
  • Thinking Docker speeds up coding
  • Confusing Docker with Rails framework
  • Believing Docker fixes app bugs automatically
2. Which line correctly exposes port 3000 in a Dockerfile for a Rails app?
easy
A. EXPOSE port 3000
B. EXPOSE '3000'
C. EXPOSE:3000
D. EXPOSE 3000

Solution

  1. Step 1: Recall Dockerfile syntax for exposing ports

    The correct syntax is simply EXPOSE followed by the port number without quotes or extra words.
  2. Step 2: Evaluate options

    EXPOSE 3000 matches the correct syntax. Options A, B, and D have invalid syntax.
  3. Final Answer:

    EXPOSE 3000 -> Option D
  4. Quick Check:

    Expose port syntax = EXPOSE 3000 [OK]
Hint: Use EXPOSE followed by port number without quotes [OK]
Common Mistakes:
  • Adding quotes around port number
  • Writing extra words like 'port'
  • Using colon instead of space
3. Given this Dockerfile snippet for a Rails app:
FROM ruby:3.1
WORKDIR /app
COPY . .
RUN bundle install
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]

What happens when you run this container and visit http://localhost:3000 on your host machine?
medium
A. You can access the Rails app running inside the container
B. The app will not be accessible because the port is not exposed
C. The app will crash because of missing files
D. The server will bind only to localhost inside the container

Solution

  1. Step 1: Analyze Dockerfile commands

    The Dockerfile exposes port 3000 and runs Rails server bound to 0.0.0.0, allowing external access.
  2. Step 2: Understand port exposure and binding

    Exposing port 3000 and binding to 0.0.0.0 lets the host access the app via localhost:3000.
  3. Final Answer:

    You can access the Rails app running inside the container -> Option A
  4. Quick Check:

    Expose + bind 0.0.0.0 = accessible app [OK]
Hint: Bind Rails to 0.0.0.0 and expose port to access app [OK]
Common Mistakes:
  • Thinking EXPOSE alone publishes port to host
  • Binding server only to localhost inside container
  • Assuming missing files cause crash without evidence
4. You wrote this Dockerfile for your Rails app:
FROM ruby:3.1
WORKDIR /app
COPY . .
RUN bundle install
EXPOSE 3000
CMD ["rails", "server"]

Why might your app not be accessible from your host machine at http://localhost:3000?
medium
A. Because the port 3000 is not exposed in the Dockerfile
B. Because the Rails server is not bound to 0.0.0.0, so it listens only inside the container
C. Because the WORKDIR is incorrect
D. Because bundle install was not run

Solution

  1. Step 1: Check server binding in CMD

    The CMD runs 'rails server' without '-b 0.0.0.0', so it binds to localhost inside container only.
  2. Step 2: Understand port exposure and binding

    Even though port 3000 is exposed, binding only to localhost prevents external access from host machine.
  3. Final Answer:

    Because the Rails server is not bound to 0.0.0.0, so it listens only inside the container -> Option B
  4. Quick Check:

    Missing bind 0.0.0.0 = no host access [OK]
Hint: Always bind Rails server to 0.0.0.0 for external access [OK]
Common Mistakes:
  • Assuming EXPOSE publishes port to host automatically
  • Thinking WORKDIR affects network access
  • Believing bundle install affects accessibility
5. You want to deploy a Rails app with Docker and ensure the app reloads code changes during development inside the container. Which Dockerfile and command setup is best?
hard
A. Copy code once in Dockerfile and run: rails server without binding
B. Expose port 3000 but do not bind Rails server to 0.0.0.0
C. Use a volume to mount your app code and run: rails server -b 0.0.0.0
D. Run Rails server with rails server -b localhost and no volume

Solution

  1. Step 1: Understand code reload needs

    For code changes to reflect immediately, the app code must be mounted as a volume, not copied once.
  2. Step 2: Ensure external access

    Binding Rails server to 0.0.0.0 allows access from host machine.
  3. Step 3: Evaluate options

    Use a volume to mount your app code and run: rails server -b 0.0.0.0 mounts code and binds correctly. Others miss volume or binding, causing no reload or no access.
  4. Final Answer:

    Use a volume to mount your app code and run: rails server -b 0.0.0.0 -> Option C
  5. Quick Check:

    Volume + bind 0.0.0.0 = live reload + access [OK]
Hint: Mount code volume and bind server to 0.0.0.0 for dev [OK]
Common Mistakes:
  • Copying code only once, missing live reload
  • Not binding server to 0.0.0.0
  • Binding server to localhost disables host access
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard