containerd_rails
is a Rails application designed to run in a Docker container. This guide will walk you through the steps to set up and run the application both as a single container and as a multi-container application using Docker Compose.
-
Create a new Rails application using SQLite3 as the database:
rails new containerd_rails (app_name)
-
Build the Docker image:
docker build -t app_image .
-
List Docker images to confirm the build:
docker images
-
Run the container to access the bash shell (useful for generating a master key if using production environment):
docker run -it --user root --rm --entrypoint bash app_image
To generate a master key:
rails credentials:edit
-
For development environment, change
ENV
to development. If using production, provide theRAILS_MASTER_KEY
:docker run -p 3000:3000 -e RAILS_MASTER_KEY=1234567890 app_image
-
Update the
Dockerfile
to bind the Rails server to all IP addresses:CMD [".bin/rails", "server", "-b", "0.0.0.0"]
Run the container with bind mounts to reflect changes in real-time without rebuilding the image:
docker run -p 3000:3000 -v $(pwd):/rails app_image (name of the image)
- Create a
docker-compose.yml
file. - Update the
Gemfile
andDockerfile
to use PostgreSQL instead of SQLite3. - Modify
database.yml
to set the host as the service name defined indocker-compose.yml
. - Create a
.env
file for environment variables.
-
Start the application and access the web container's bash shell:
docker-compose exec web bash
-
Create the database and generate a scaffold:
bin/rails db:create bin/rails g scaffold Post title:string content:text bin/rails db:migrate
-
Restart the web service:
docker-compose restart web
View logs for the database and web services:
sh docker-compose logs db docker-compose logs web
To restart the application:
-
Bring down the containers:
docker-compose down
-
Bring the containers back up, rebuilding if necessary:
docker-compose up -d --build
docker system prune -a