I wanna make hot reloading for Golang on docker. I have created a Docker file looks like this:
# Go Version
FROM golang:latest
# Environment variables which CompileDaemon requires to run
ENV PROJECT_DIR=/app
GO111MODULE=on
CGO_ENABLED=0
# Basic setup of the container
RUN mkdir /app
COPY .. /app
WORKDIR /app
# Get CompileDaemon
RUN go get github.com/githubnemo/CompileDaemon
RUN go install github.com/githubnemo/CompileDaemon
# The build flag sets how to build after a change has been detected in the source code
# The command flag sets how to run the app after it has been built
ENTRYPOINT CompileDaemon -build="go build -o api" -command="./api"
and docker-compose.yml is:
version: '3.8'
name: live-reload-example
services:
api:
container_name: api
build:
context: .
dockerfile: Dockerfile.dev
ports:
- 8501:8501
volumes:
- ./:/app
The container not reloading after the change
2
Answers
I used air package instead of CompileDaemon and problem solved.
in the first step, the .air.toml file does not exist, and you must create it with air init in the docker container.
Container should not be reloaded. It runs
CompilerDaemon
. The daemon watches the mounted files and recompiles+restarts the app upon change.I’m not sure about
COPY .. /app
– what’s there in..
? I replaced it withCOPY . /app
and it worked with my duppy app.Just a few points:
/app
,WORKDIR
creates it automatically,/app
upon build sincego build
is invoked after start, and the contents of/app
is overloaded by the mounted volumeHere is my Dockerfile
Here is the log of
docker compose up
:As you see, when the container started, it built my application and launched it. The output of the application:
Then I edited the app and changed the line to
Version 2
:IMHO, the container works as expected: it watches changes in sources and rebuilds and restarts the application.