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.
I will explain in the above environment
The repository is here
The following 5 files are required to build the hot reload development environment this time.
$ go get -u github.com/oxequa/realize
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"]
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
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 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")
}
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.
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