Helo everyone 👋🏻
Today I want to share with you how we can observe changes in Rust using Docker Containers. This is a continuation of the article Hot Reload in Rust with Cargo Watch
1 - First and foremost, we must create a DockerFile in the project's root directory and include the code below.
# Using official rust base image
FROM rust:alpine3.16
# Set the application directory
WORKDIR /app
# Install musl-tools to make many crates compile successfully
RUN apk add --no-cache musl-dev
# Install cargo-watch
RUN cargo install cargo-watch
# Copy the files to the Docker image
COPY ./ ./
Dockerfile is a plain text file that contains instructions that Docker will use to build an image. see more
2 - The following step is to create the image using the command below.
docker build -t rust-observable-image .
You should then see on your terminal the response confirming that the image has been successfully built.
[+] Building 430.0s (10/10) FINISHED
We can verify that the image is created.
docker images
3 - The next step would be to run the container and run the "cargo watch" command inside the container, but we're going to do something more automated using Docker Compose. So we don't need to run step 2 because when we start the container with Docker Compose it will run automatically.
So we need to create a docker-compose.yml file inside the root directory of the project and add the following instructions:
version: "3.9"
services:
app:
build: .
container_name: "cargo-watch-example"
volumes:
- .:/app
command: sh -c "cargo watch -x run"
4 - The final step is run the container with Docker Compose
docker compose up
From now on you will be able to make changes to your project and observe the changes from your container.
If you like my content and want to support my work, you can give me a cup of coffee ☕️ 🥰
Follow me in
Twitter: @devjcastro
Top comments (1)
On my machine, it always take more than 1 minute each time cargo watch re-run the application.
If I run cargo watch on the host directly, it only take ~2 seconds.
Did you faced the same problem on your project?