The Config Rails app uses Docker from start to finish

Tram Ho

If you are a developer who has had a foot in many projects at the same time, you must have encountered annoying problems with the version or library used in each project. Then, every time you switch back and forth between environments, you have to spend time reinstalling everything. Or more simply, the code on the company computer runs, but when you get home, you open the machine to code the unfinished code and do not understand why it cannot run anymore. That’s when you think about docker. With the slogan “build once, run anywhere” , you will definitely not have a headache any more. Today we will build a Ruby on Rails application in a Docker environment.

Docker compose

Before getting into the main point, we need to define which containers will be created as well as the method of communication between them.

  • The first is the container app that handles the main logic of the application.
  • Next is the db container which will be the place to store the data.
  • Finally, the nginx container functions as a web server.

Those are also the most basic components of any web application. It is also important to identify these components, because it gives you an overview of the entire system and functionality of each component to optimize your containers accordingly. Below is the content of the file docker-compose.yml , it is like a guide to build the image as well as the relationship between the containers:

In the file docker-compose.yml , in each block we pay attention to the following details:

  • build :
    • context : This is where the Dockerfile will be run.
    • dockerfile : Specifies the docker file to be used for the build.
    • args : Added values ​​in Dockerfile
  • depends_on : Definition to bind containers together, used instead of links .
  • volumes : Specifies how much space the server will mount into the container.
  • env_file : The env file location, you can use these env variables anywhere in the docker-compose.yml file.
  • environment : A place to define the environment variables in the container.
  • command : This is the file sh that will be run when the container is started.


For convenience of management, we will create folders corresponding to each component, with app and nginx their respective folders are docker/app , docker/nginx .

Config app

Below is the content docker/app/Dockerfile where we will specify the components that make up the image app from which to build the app container:

As seen in the docker-compose.yml file’s content, we use the context . for the image app, this means that the docker/app/Dockerfile will run as if it were outside the app’s root directory. That’s why we can use the commands:

We also use a docker/app/ to run the commands before starting the server:

Notice that we previously put this file from the outside into the container and set its permissions with the command:

Config Nginx

At this point we can also run our application, but in order for everything to be closer to the new deploy environment, we need to have another container called ngnix :

Above is the content in the file docker/nginx/Dockerfile , it is the instructions to build the necessary components for the nginx container. Next, we will prepare the file docker/nginx/nginx.conf to configure for nginx with the following content:

We copy this file into the / tmp / directory of the container with the command:

Since Nginx doesn’t support us pass the environment variables into the config file, so to fix this, we used envsubst to replace the content in the /tmp/nginx.conf file before including it in /etc/nginx/nginx.conf :


Prepare a .env file to store all the environment variables used in the application, it should look like this:

Everything looks good, now it’s time to enjoy our work, first to build images:

If all the images have been successfully built, then run the following command:

Open up your browser and type in the link http: // localhost if the screen shows “Yay! You’re on Rails” then we are considered successful.


Recently, we went together to build a Rails application using Docker. Above I use the latest versions of Rails, Ruby and Docker. Hopefully the article will be somewhat useful for you to manually configure Docker for your project.

Share the news now

Source : Viblo