Hi everyone, this article aims to demonstrate and instruct beginners in assembling a Docker image and, subsequently, in creating a container for a Ruby on Rails application. Good reading 😉.
What is Docker?
In short, the Docker is a platform with a high level of performance that aims to create development environment management, committing to quickly provide complete environments for an application.
To create this environment creation, Docker uses concepts such as images that are a static representation of the application and its configuration and dependencies. A
Dockerfile is responsible for inferring all the dependencies and configurations of the application.
Imagine an image as a formless box, but with all the attributes necessary to become something.
Let's create an image to show in practice.
Creating a ruby image.
Let's create a file called
Dockerfile with the following content.
FROM ruby:2.5 RUN apt-get update -qq && apt-get install -y nodejs WORKDIR /app COPY Gemfile /app/Gemfile COPY Gemfile.lock /app/Gemfile.lock RUN bundle install EXPOSE 3000 CMD ["rails", "server", "-b", "0.0.0.0"]
We use FROM to instantiate the image we want to use as a base, in this case we will use the official ruby image in version 2.5, this image can be found on the Docker Hub.
RUN is used to execute commands when starting an image instance, in this case we are running the apt-get command to perform an update and install nodejs.
WORKDIR shows which folder will be used inside the container. In this case the /app folder will be used.
Create a Gemfile file with the following content.
source 'https://rubygems.org' gem 'rails', '~> 5.2.6'
And a Gemfile.lock file with the command in your console:
~$ touch Gemfile.lock
COPY It is used to copy a Gemfile from your current host to the image.
EXPOSE inserts 3000 in the EXPOSE environment variable.
CMD The primary purpose of CMD is to provide standards for a running container.
Generating our image:
~$ docker build . --tag rails
We use the tag flag to name our image.
The . is used to search for a
Dockerfile in our directory to generate the image.
List your images to see if they were created correctly with the command:
~$ docker images
Creating our docker compose.
Docker Compose is a tool for defining and running Docker applications from multiple containers.
Create a file called docker-compose.yml with this content:
version: "3.9" services: db: image: postgres volumes: - ./tmp/db:/var/lib/postgresql/data environment: POSTGRES_PASSWORD: password web: build: . command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" volumes: - .:/app ports: - "3000:3000" depends_on: - db
We used version 3.9 for the docker compose, and created two services called db and web.
We say when composing using a postgress image to create a container and we create POSTGRES_PASSWORD as an environment variable.
The volume refers to the area we want to share between the container and our machine. We say that the current directory is referenced by /app inside the container, and so we create a connection between them.
The ports refer to which host port will be mirrored in the container.
depends_on infers that the service will only be started when the instantiated one finishes its initiation.
Build the project.
~$ docker-compose run --no-deps web rails new . --force --database=postgresql
After that the rails architecture will be downloaded to your current directory.
Run the build command again.
~$ docker-compose build
~$ sudo chown -R $USER:$USER .
docker-compose run web rake db:create
To create our database.
And so you will have a complete rails application running on a docker.
Time is very important, thanks for sharing a little bit of yours with me 😊.