0
0
Ruby on Railsframework~10 mins

Docker deployment in Ruby on Rails - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Docker deployment
Write Dockerfile
Build Docker Image
Run Container from Image
App Starts Inside Container
Access App via Browser or API
Stop or Restart Container
This flow shows how a Rails app is packaged into a Docker image, run as a container, and accessed.
Execution Sample
Ruby on Rails
FROM ruby:3.2
WORKDIR /app
COPY Gemfile* ./
RUN bundle install
COPY . .
CMD ["rails", "server", "-b", "0.0.0.0"]
This Dockerfile builds a Docker image for a Rails app and starts the Rails server.
Execution Table
StepActionDetailsResult
1Read DockerfileFROM ruby:3.2Base image set to Ruby 3.2
2Set working directoryWORKDIR /appWorking directory inside container is /app
3Copy GemfileCOPY Gemfile* ./Gemfile and Gemfile.lock copied to /app
4Install gemsRUN bundle installGems installed inside image
5Copy app filesCOPY . .All app files copied to /app
6Set start commandCMD ["rails", "server", "-b", "0.0.0.0"]Rails server will start on container launch
7Build imagedocker build -t myrailsapp .Docker image 'myrailsapp' created
8Run containerdocker run -p 3000:3000 myrailsappContainer runs, Rails server listens on port 3000
9Access appOpen http://localhost:3000Rails app homepage loads in browser
10Stop containerCtrl+C or docker stopContainer stops, app no longer accessible
💡 Container stops when user stops it or system shuts down
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 8Final
Docker ImageNoneBase Ruby imageImage with app files and gemsImage running as containerStopped container
ContainerNoneNoneNoneRunning Rails serverStopped
Key Moments - 3 Insights
Why do we copy Gemfile separately before copying all files?
Copying Gemfile first allows Docker to cache the bundle install step if Gemfile hasn't changed, speeding up rebuilds (see execution_table steps 3 and 4).
Why do we bind Rails server to 0.0.0.0 instead of localhost?
Binding to 0.0.0.0 makes the server listen on all network interfaces inside the container, allowing access from outside (step 6).
What happens if we don't expose port 3000 when running the container?
Without port mapping (step 8), the app inside the container won't be reachable from the host machine's browser.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result after step 4?
AGems installed inside image
BApp files copied to /app
CRails server started
DDocker image created
💡 Hint
Check the 'Result' column for step 4 in the execution_table
At which step does the Rails server start running inside the container?
AStep 6
BStep 8
CStep 7
DStep 9
💡 Hint
Look for when the container runs and the server listens on port 3000
If we change the CMD to bind Rails server to localhost (127.0.0.1), what happens?
AContainer fails to start
BApp is accessible from host machine
CApp is not accessible from host machine
DDocker image build fails
💡 Hint
Refer to key_moments about binding address in step 6
Concept Snapshot
Docker deployment for Rails:
- Write Dockerfile with base Ruby image
- Copy Gemfile, run bundle install for caching
- Copy app files
- Set CMD to start Rails server binding 0.0.0.0
- Build image with docker build
- Run container with port mapping
- Access app via localhost:3000
- Stop container to end app
Full Transcript
Docker deployment for a Rails app involves writing a Dockerfile that starts from a Ruby base image. We set the working directory and copy the Gemfile first to install gems efficiently. Then we copy the rest of the app files. The CMD instruction starts the Rails server binding to 0.0.0.0 so it listens on all interfaces inside the container. We build the Docker image using 'docker build' and run it with 'docker run' mapping port 3000 to access the app from the host browser. Stopping the container stops the app. This process packages the Rails app in a container for easy deployment and consistent environment.