Build a hot reload development environment with Docker-compose using Realize of Go

Introduction

This article is the 14th day article of ITRC Advent Calendar 2020. Previous article → Help Dockerman (Network) Hello RIN1208 This time, we want to build a hot reload of the development environment in the Docker-compose using the Realize.

Required environment

I will explain in the above environment

To construct

The repository is here

The following 5 files are required to build the hot reload development environment this time.

First install

$ go get -u github.com/oxequa/realize

Write a Dockerfile

First, create a Dockerfile. We do not copy files here. The reason will be explained later.

FROM golang:1.14

RUN go get github.com/oxequa/realize 

ENV CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64 \
    GO111MODULE=on

EXPOSE 8080

CMD ["realize", "start", "--build","--run"]

Write docker-compose.yml

Next, write docker-compose.yml. Set volumes here. Please note that if you copy with Dockerfile, it will be copied when the container is built, so you cannot develop by hot reload. I was addicted to it without noticing it

version: '3'
services:

  api:
    build: .
    ports:
      - "8080:8080"
    tty: true
    volumes:
      - ./:/app
    working_dir: /app

Write .realize.yml

Then create .realize.yml. Detailed explanation will be explained in the comments

settings:
  legacy:
    force: true
    interval: 0s
schema:
- name: test
  path: .
  commands:
    install: #Executed when the file is modified. This time I build it and specify it in a file called main
      status: true
      method: go build -o main main.go 
    run: #Running the built file
      status: true
      method: ./main 
  watcher: #Here we have selected the files to monitor
    extensions:
    - go
    - env
    paths: #Here, the directory to be monitored is selected./If you want to monitor only the files under the api/It's okay if you change it to api
    - /
    ignored_paths: #Here we have selected the files to exclude from monitoring
    - .git
    - .realize
    - vendor

Create a go project

Create a go project as you like. This time I will create an api server using gin

package main

import (
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	r.GET("/", hoge)
	r.Run() 
}

func hoge(c *gin.Context) {
	c.JSON(200, "hogeaaaaaae")
}

Try to start

Let's start it.

$ docker-compose build
$ docker-compose up -d 

Execute the above command to start the container. If you check http: // localhost: 8080/with your browser or curl, it will return as hogeaaaaaae. Also, if you change it to huga, it will come back as huga, and you can build a hot reload development environment.

At the end

Thank you for reading this far. I mentioned Realize itself more than half a year ago, but I didn't write it in the article, so I wrote it in this article. I hope it helps those who are trying to build a hot reload development environment with Docker-compose using Realize. Also, if you have any mistakes, please let us know in the comments.

Recommended Posts

Build a hot reload development environment with Docker-compose using Realize of Go
Build a Wordpress development environment with Docker
Build an environment of "API development + API verification using Swagger UI" with Docker
Build a WordPress development environment quickly with Docker
[Win10] Build a JSF development environment with NetBeans
Build a Java development environment with VS Code
[Environment construction] Build a Java development environment with VS Code!
Try to build a Java development environment using Docker
Build a local development environment for Open Distro for Elasticsearch with multiple nodes using Docker
Build a "Spring Thorough Introduction" development environment with IntelliJ IDEA
Build a Node.js environment with Docker
Build a Tomcat 8.5 environment with Pleiades 4.8
Build jooby development environment with Eclipse
[Node.js] Docker-compose up makes it easy to build a development environment
Build Go development environment with WSL2 + Docker Desktop + VSCode (Remote --Containers)
Build a browser test environment using Capybara in the Docker development environment
Build a development environment for Django + MySQL + nginx with Docker Compose
Steps to build a Ruby on Rails development environment with Vagrant
One file of Docker x Laravel threat! Build a local development environment with the minimum configuration
Build and manage RStudio environment with Docker-compose
Build a Java development environment on Mac
Build Nuxt TypeScript + Vuetify environment with docker-compose
Build a simple Docker + Django development environment
A record of setting up a Java development environment with Visual Studio Code
I tried to build a Firebase application development environment with Docker in 2020
[Copy and paste] Build a Laravel development environment with Docker Compose Part 2
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
Build a local development environment for Rails tutorials with Docker (Rails 6 + PostgreSQL + Webpack)
[Copy and paste] Build a Laravel development environment with Docker Compose Participation
Build a development environment on AWS EC2 with CentOS7 + Nginx + pm2 + Nuxt.js
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
Template: Build a Ruby / Rails development environment with a Docker container (Ubuntu version)
Template: Build a Ruby / Rails development environment with a Docker container (Mac version)
Build a development environment for Docker + Rails6 + Postgresql
Let's get started with Java-Create a development environment ②
Build a simple Docker Compose + Django development environment
Build a development environment to create Ruby on Jets + React apps with Docker
A reminder of Docker and development environment construction
Build a development environment for Docker, java, vscode
[First team development ②] Build an environment with Docker
Django development environment construction using Docker-compose (personal memorandum)
Create a Spring Boot development environment with docker
Install Ubuntu 20.04 in virtual box on windows10 and build a development environment using docker
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
[Rails] [Docker] Copy and paste is OK! How to build a Rails development environment with Docker
How to quit Docker for Mac and build a Docker development environment with Ubuntu + Vagrant
Build Java development environment with VS Code on Mac
Build a Ruby on Rails development environment on AWS Cloud9
Build an environment of Ruby2.7.x + Rails6.0.x + MySQL8.0.x with Docker
Stable development environment construction manual for "Rails6" with "Docker-compose"
Easily build a Vue.js environment with Docker + Vue CLI
Build ruby debug environment with VS Code of Windows 10
Environment construction of Rails5 + MySQL8.0 + top-level volumes with docker-compose
[Note] Build a Python3 environment with Docker in EC2
Build Java development environment with WSL2 Docker VS Code
How to build Java development environment with VS Code
Build Java program development environment with Visual Studio Code
[2021] Build a Docker + Vagrant environment for using React / TypeScript
Build a local development environment for Rails tutorials with Docker-Introduce Bootstrap and Font Awesome with Webpack-
[Oracle Cloud] Build a 4-Node RAC environment of Oracle Database 19c with Docker on OCI Compute
I tried to create a java8 development environment with Chocolatey