How to get "Workshops" Rails application up and running on your local machine for development.
Goal: Set up a local development environment for the Workshops application using Docker.
1. Prerequisites:
- Git: To clone the repository.
- Docker: To build and run containerized applications. (Install Docker)
- Docker Compose: To manage multi-container Docker applications. It's usually included with Docker Desktop.
2. Clone the Repository:
git clone <repository-url> workshops
cd workshopsReplace <repository-url> with the actual URL of your Git repository.
3. Configuration Setup:
This application uses several configuration files provided as examples. You need to copy them to their actual names and customize them.
-
Copy
.examplefiles: TheREADME.mdandentrypoint.sh.examplemention several.examplefiles. The most crucial ones for Docker setup are:docker-compose.yml.example->docker-compose.ymlDockerfile.example->Dockerfileentrypoint.sh.example->entrypoint.shnginx.conf.erb.example->nginx.conf.erb(Used by Passenger within Docker)Passengerfile.json.example->Passengerfile.json(Used by Passenger)config/initializers/custom_env.rb.example->config/initializers/custom_env.rb(While Docker usesdocker-compose.ymlfor environment variables, having this file copied is good practice for Rails development awareness, though potentially less critical inside the Docker flow ifdocker-compose.ymldefines everything).
You can copy them using:
cp docker-compose.yml.example docker-compose.yml cp Dockerfile.example Dockerfile cp entrypoint.sh.example entrypoint.sh # Make entrypoint executable chmod +x entrypoint.sh cp nginx.conf.erb.example nginx.conf.erb cp Passengerfile.json.example Passengerfile.json cp config/initializers/custom_env.rb.example config/initializers/custom_env.rb -
Review
docker-compose.yml:- Purpose: Defines the services needed to run the application (web server, database, background job worker).
- Action: Open
docker-compose.yml. Pay close attention to theenvironment:sections for thedb,web, andqueservices. - Crucially: Change the default passwords (like
POSTGRES_PASSWORD,DB_PASS) and secret keys (SECRET_KEY_BASE,DEVISE_SECRET_KEY, etc.) to secure, random values. You can generate secrets using commands likeopenssl rand -hex 64orrake secret. - Review port mappings (e.g.,
8000:8000means host port 8000 maps to container port 8000). - Review volume mappings. It seems set up to mount your local code into the container (
./:/home/app/workshops) which is good for development. It also uses named volumes (pgdata,rubygems) for database data and gems to persist them and potentially improve performance on some systems (like macOS).
-
Review
Dockerfile:- Purpose: Provides instructions to build the Docker image for the
webandqueservices. It specifies the base image, installs system dependencies (likepostgresql-client,nodejs,yarn), sets up Ruby, copies application code, and installs gems/packages. - Action: You probably don't need to change this initially, but understand it's how the application's environment inside the container is defined.
- Purpose: Provides instructions to build the Docker image for the
-
Review
entrypoint.sh:- Purpose: This script runs inside the
webcontainer when it starts up (docker compose up). It performs essential setup tasks. - Action: Read through it. You'll see it:
- Installs Ruby gems (
bundle install). - Runs database migrations (
rails db:migrate). - Initializes application settings (
rake ws:init_settings). Important: This populates thesettingstable with defaults defined inlib/tasks/init-settings.rake. It's crucial for the first run. - Creates admin users (
rake ws:create_adminsorbirs:create_admins). Important: Checklib/tasks/ws.rake(orbirs.rakeif it exists) for the default credentials it creates. You'll need these to log in initially. - Installs JS dependencies (
yarn install). - Precompiles assets (
rake assets:precompile). - Starts the Rails server using Passenger.
- Installs Ruby gems (
- Note: For subsequent runs after the first successful setup, you might comment out
db:migrate,init_settings, andcreate_adminsin your localentrypoint.shif you don't want them running every time, although migrations are often safe to run repeatedly.
- Purpose: This script runs inside the
-
Review
entrypoint-que.sh:- Purpose: Runs inside the
quecontainer to start the Que background job worker. - Action: Usually doesn't need changes initially.
- Purpose: Runs inside the
4. Build and Run with Docker Compose:
-
Build the Image: Open your terminal in the project's root directory (
workshops/) and run:docker compose build
This command reads the
Dockerfileand builds the Docker image needed for thewebandqueservices. This might take a while the first time as it downloads the base image and installs dependencies. -
Create Docker Volumes (if using named volumes and they don't exist): The
docker-compose.yml.examplementions creating data volumes manually beforehand, especially for performance on macOS. If you stick with the named volumes (pgdata,rubygems), Docker Compose should create them automatically on the firstup. If you encounter issues, you might need to create them manually as hinted in the comments ofdocker-compose.yml.example:# Example commands (adapt image tags if needed): # docker volume create --name=workshops_pgdata # docker volume create --name=workshops_rubygems # Then update docker-compose.yml to use these external volumes if desired. # For now, rely on docker compose creating them automatically.
-
Start the Application:
docker compose up
This command does the following:
- Creates and starts containers for the services defined in
docker-compose.yml(db,web,que). - Runs the database initialization scripts found in
db/pg-init/inside thedbcontainer (this happens only the first time thedbcontainer starts with an empty data volume). It creates the databases and the user specified in yourdocker-compose.ymlenvironment variables. - Runs the
/sbin/entrypoint.shscript inside thewebcontainer. This performs the Rails setup (gems, migrations, settings, admins, assets). - Runs the
/sbin/entrypoint-que.shscript inside thequecontainer to start the background worker. - Attaches your terminal to the logs of all containers. You'll see output from the database, Rails server, and Que worker.
Troubleshooting: If
docker compose upfails, carefully read the error messages. Common issues include: * Incorrect passwords/secrets indocker-compose.yml. * Port conflicts (if port 8000 or 5432 is already in use on your host). * Errors duringbundle installordb:migrate. * File permission issues (less common with Docker volume mounts now, but possible). - Creates and starts containers for the services defined in
5. Access the Application:
- Once the
entrypoint.shscript finishes and you see messages indicating the Passenger server has started, open your web browser and navigate tohttp://localhost:8000(or whatever host/port you configured).
6. Initial Login:
- You should see the login page.
- Log in using the credentials for the admin user created by the
rake ws:create_admins(orbirs:create_admins) task run byentrypoint.sh. Check the definition of that task inlib/tasks/ws.rake(orbirs.rake) for the default email/password if you didn't change it. - After logging in as an admin, navigate to
/settings(usually via a user dropdown menu) to review and adjust application settings stored in the database.
7. Development:
- Your local code directory is mounted into the container. Changes you make locally should be reflected when you refresh the browser (Rails development mode usually handles code reloading). For changes to assets or configuration, you might need to restart the containers (
docker compose down && docker compose up). - Running Rake Tasks/Console: Open a new terminal window and run:
This gives you a shell inside the running
docker compose exec web bashwebcontainer. From there you can runrails c,rake -T,bundle exec rspec, etc. - Database: The database is running in its own container (
db). You can connect to it using standard PostgreSQL tools if needed, using the credentials and host (db) defined indocker-compose.yml.