Docker is a container solution developed by the Docker Inc and backed by the open source community. The idea of the docker solution is to provide a container based tool with an image-based deployment model. You can create a complete development/production ready environment with a simple set of configuration and wrap it into an image that can be distributed on your team or to the community.
In this article we are going to build the configuration of a development environment to a simple rails application using a PostgreSQL database.
Configuration steps
First you need some application to work on created and Docker/Compose installed.
Create the following files at the project root.
Dockerfile
The Dockerfile configures the image with all dependencies of the project.
Copy1ARG RUBY_VERSION=3.1.2 2 3FROM ruby:$RUBY_VERSION-alpine 4 5RUN apk add --update --no-cache \ 6 bash \ 7 build-base \ 8 sudo \ 9 libpq-dev \ 10 tzdata 11 12RUN mkdir -p /app 13WORKDIR /app 14 15RUN gem install bundler 16
The FROM statement defines the base image of our container which is the alpine version of the official ruby image of the 3.1.2 version of the language. We are also installing the dependecies to run the application and preparing the system to receive the code.
docker-compose.yml
The compose file configures the services of the application using a YAML syntax.
Copy1services: 2 db: 3 image: postgres:13 4 environment: 5 - POSTGRES_HOST_AUTH_METHOD=trust 6 volumes: 7 - postgres:/var/lib/postgresql/data 8 web: 9 build: 10 context: ./ 11 args: 12 - RUBY_VERSION=3.1.2 13 environment: 14 - DATABASE_USERNAME=postgres 15 - DATABASE_PASSWORD= 16 - DATABASE_HOST=db 17 - DATABASE_PORT=5432 18 depends_on: 19 - db 20 entrypoint: ./entrypoint.sh 21 ports: 22 - 5000:3000 23 volumes: 24 - .:/app:cached 25 tty: true 26 stdin_open: true 27 28volumes: 29 postgres: 30
With this config file we are defining two services:
- The database service, using the version 13 of PostgreSQL and with a volume mounted storing the database data.
- The application service thats going to use our previouslly created Dockerfile to build its container. Here we can define some parameters of the application run:
- The environment variables with the
environment
key. - The ports binding from the host machine with the container.
- The entrypoint script, which will bootstrap the application during the services startup.
- The environment variables with the
entrypoint.sh
Here we just install all rails/node dependencies, configure the database and start the application server process.
Copy1#! /bin/bash 2set -e 3 4bundle install 5 6[ -e tmp/pids/server.pid ] && rm tmp/pids/server.pid 7 8bundle exec rails db:prepare 9bundle exec rails server -p 3000 -b 0.0.0.0 10
Adding new dependencies
At this point the application is configured to run the rails server connected to a PostgreSQL database. If you need more dependencies, such Redis, ElasticSearch or MongoDB you can simply add a new service that uses an image that wraps the depedency.
The Docker Hub platform is a public respository used by many companies and individual developers to share Docker images. You can use it to search the service you need to attach to your application.
Wrapping up and running the application
To run the application you can use the following commands:
docker compose up
will raise all services described on the compose file.docker compose exec SERVICE_NAME COMMAND
will run the given command on the command line of the indicated service.docker compose down
is going to stop all services.
Now you have a fully functional and ready to use configuration to your project. I used a Ruby on Rails application as example, but the concepts can be applied to any stack you use, you just need to adjust the Dockerfile to your project dependencies and configure the services on the compose file.