Development setup using Docker
July 20, 2024 ยท View on GitHub
Warning
This is meant for advanced users already familiar with Docker. If you're looking for general development instructions, please see README.md.
Initial setup
The following commands should just be run for the initial setup only. Rebuilding the docker images is only necessary when upgrading, if there are changes to the Dockerfile, or if gems or npm packages have been added or updated.
- Install Docker Community Edition if it is not already installed, or you can try Orbstack or colima if you're using a Mac.
- Clone the respository to your local machine and change into the application
directory:
cd circulate. - Run
docker-compose up -d databaseto start the database service. - Run
docker-compose run --rm web rails db:resetto create the dev and test databases, load the schema, and run the seeds file. - Run
docker-compose up -dto start all the remaining services. - Run
docker-compose psto view status of the containers. All should have state "Up". Check the logs if there are any containers that did not start. - The web application will be available at localhost:3000.
For ongoing development:
- Run
docker-compose up -dto start all services. - Run
docker-compose up -d --force-recreateto start services with new containers. - Run
docker-compose up -d --force-recreate webto start a container running the new image. - Run
docker-compose psto view status of containers. - Run
docker-compose stopto stop all services. - Run
docker-compose rm <service>to remove a stopped container. - Run
docker-compose rm -f <service>to force remove a stopped container. - Run
docker-compose restart webto restart the web server. - Run
docker-compose down -vto stop and remove all containers, as well as volumes and networks. This command is helpful if you want to start with a clean slate. However, it will completely remove the database and you will need to go through the database setup steps again above.
Running commands
In order to run rake tasks, rails generators, bundle commands, etc., they need to be run inside the container:
$ docker-compose exec web rails db:migrate
If you do not have the web container running, you can run a command in a one-off container:
$ docker-compose run --rm web bundle install
However, when using a one-off container, make sure the image is up-to-date by
running docker-compose build web first. If you have been making gem updates
to your container without rebuilding the image, then the one-off container will
be out of date.
Viewing logs
To view the logs, run:
$ docker-compose logs -f <service>
For example:
$ docker-compose logs -f web
Accessing services
Postgres database
$ docker-compose exec database psql -h database -Upostgres circulate_development
Rails console
$ docker-compose exec web rails c
Testing Suite
Run the unit testing suite from within the container:
$ docker-compose exec web rails test
NOTE: System tests are not currently set up with docker.
Run one test with:
$ docker-compose exec web rails test <filepath>