How can I get hot reloading working, when running an application via Docker Compose?
I am running Docker on Linux pop-os 5.17.5-76051705-generic.
Inside Docker, I have a Vue app as a client and a Golang as a back-end API.
I managed to get hot reloading working on Vue, but then it stopped working.
In this question, I will focus on Golang API, as I have spent most of my time debugging this one.
I am using cosmtrek/air library for hot Go hot reloading and it works well when running on my laptop.
The hot reload is not working, when I am running the application inside the Docker container and editing code in VSC on my laptop.
But, if I go inside the container via terminal and then edit the file names in VSC on my laptop, I can see the file names change inside the container.
If I install Nano inside the container and then edit code inside the running container, then the hot reloading is working.
And I can also see the changes in VSC on my laptop, meaning that the bind mount is working.
If I have Nano open inside the container and I edit code in it, then I see the changes in VSC on my laptop. But if I edit code in VSC on my laptop, then I don't see changes in Nano, unless I close Nano and reopen it.
It feels like file changes do not get passed to the application that has the file open.
But once the application closes the file, the changes get applied.
I asked my brother to install this project on his Macbook and for him, hot reloading was working with the docker-compose.
Here is my Golang docker file:
FROM golang:1.18.0
RUN apt update && apt upgrade -y && \
apt install -y git \
make openssh-client
WORKDIR /app
RUN curl -fLo install.sh https://raw.githubusercontent.com/cosmtrek/air/master/install.sh \
&& chmod +x install.sh && sh install.sh && cp ./bin/air /bin/air
CMD air
And docker-compose.yaml relating to this Go API:
api:
container_name: golang_api
build:
context: ./api
depends_on:
- database
volumes:
- ./api:/app
ports:
- ${PORT}:${PORT}
restart: unless-stopped