Building a Docker Development Environment (Part 1)
Tags: docker, devops, programming, tutorial
Published: 2024-04-30 14:00:00

## Introduction
Hello, I work as a system engineer in Tokyo and am now mainly developing web applications using Ruby and JavaScript.

I have been using Docker at my workplace for around 2 years now and almost all of my projects are developed using Docker these days. Docker technology has become so helpful that I believe it will be used for a long time.

So, I will explain Docker Compose, Dockerfile, and how to build a Rails development environment with Docker and put them into two different sections. **In the first section, I will explain what exactly Docker is and the various components of DockerCompose.yml.**

---

## What is Docker?
Docker is a lightweight platform for building containerized virtual environments. This allows you to quickly build, test, and deploy applications.

Containers created by Docker are independent of the execution environment and are not affected by external influences, so everyone can build the exact same environment by simply sharing docker-compose.yml and Dockerfile.

Unlike conventional virtualization technologies, a guest OS is not launched, making it extremely lightweight compared to conventional virtualization technologies.

---

## Docker Compose
Docker Compose is a tool for Docker applications that define and run multiple containers. By setting up the complex configuration required to launch containers in YAML format in docker-compose.yml, you can create and launch application services based on the configuration with a single command.

Today, we will create two containers, one for Rails and one for Postgres, a database, in order to build a Rails development environment.

![docker-compose.yml](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0bvjpsziwlf9bmtnmslg.png)

---
## Components of Docker Compose
### version
It represents the version of the format. Therefore, if the version is changed, the file will be written differently.

### build
Specify options to be applied at build time. You can specify the path of the build context and the path of the Dockerfile to be used.

- **context** 
Specify the Dockerfile to be used in the build. In Docker Compose, any file name other than “Dockerfile” can be specified here. You can also specify the path.

- **dockerfile** 
Specify the Dockerfile to be used in the build. In Docker Compose, any file name other than “Dockerfile” can be specified here. You can also specify the path.

### image
If no Dockerfile is specified, an image is used. Find the specified image from docker images. If you do not have the image, pull the image from DockerHub to build it.

### container_name
You can name the container as you like.

### depends_on
It represents the dependencies (order of creation) of the containers. In this case, the Rails container is created after the Postgres container is created.

Also, by specifying a condition for depends_on, you can change the condition of depends_on, such as executing after the container is created and finished.

For more information, please check [here](https://github.com/compose-spec/compose-spec/blob/master/spec.md).

### ports
You can specify both host-side and container-side ports.
(host side: container side)

If only one port is specified, a random port number will be assigned to the host side.

### volumes
There are two types of volumes in DockerCompose. The first is the volumes in rails and postgres in docker-compose.yml. This is what is called a bind mount, which mounts files and directories on the host side to the container side.

The second one is defined at the bottom of the file and is used to configure data persistence. Therefore, the data can remain even if the container is deleted.

### tty
This corresponds to the -t option of the docker run command. By setting it to true, you can keep containers running. This prevents the container from being terminated immediately after it is launched.

### stdin_open
This corresponds to the -i option of the docker run command. By setting it to true, you can tie stdin and error output to the container.

---

## Summary
You have now finished creating docker-compose.yml.
However, we can’t just run this file, we need to prepare a Dockerfile, which is specified in the Rails build.
We will discuss the Dockerfile in the [second article](https://dev.to/weseek-inc/building-a-docker-development-environment-part-2-43i?preview=c1491c120ef9c33b075c08c52fc8a1128b352ddca5a885bab6a661f115ae73129d0ccda1b086ddd6762ba4fd48eed6495aaa74f8563cc933e0e21b5a).

This article is based on the Compose file reference in the official documentation: https://docs.docker.jp/compose/compose-file/index.html (Japanese)

---

## About Us💡

In addition, we want to introduce a little more about GROWI, an open software developed by **WESEEK, Inc**.

**GROWI** is a wiki service with features-rich support for efficient information storage within the company. It also boasts high security and various authentication methods are available to simplify authentication management, including **LDAP/OAuth/SAML**.

**GROWI** originated in Japan and GROWI OSS is **FREE** for anyone to [download](https://docs.growi.org/en/admin-guide/?utm_source=dev+community&utm_medium=referral&utm_campaign=Building_a_Docker_Development_Environment_Part_1) and use **in English**.

For more information, go to [GROWI.org](https://growi.org/en/?utm_source=dev+community&utm_medium=referral&utm_campaign=Building_a_Docker_Development_Environment_Part_1) to learn more about us. You can also follow our [Facebook](https://www.facebook.com/people/GROWIcloud/100089272547238/) to see updates about our service.

![GROWI.org](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1rhpa0qcdukeall2vfsj.png)