I'm trying to setup development environment for Rust project with workspaces using cargo-watch
and docker-compose
. There are my configs:
Main Cargo.toml
:
[workspace]
resolver = "2"
members = [
"lib_bot",
]
docker-compose.yml
:
version: '3.8'
services:
lib_bot:
build:
dockerfile: ./Dockerfile
container_name: lib_bot
env_file:
- ./.env
volumes:
- cargo_registry_lib_bot:/usr/local/cargo/registry
- ./lib_bot/src:/app/lib_bot/src
- ./lib_bot/Cargo.toml:/app/lib_bot/Cargo.toml
- ./target:/app/target
- ./Cargo.lock:/app/Cargo.lock
- ./Cargo.toml:/app/Cargo.toml
volumes:
cargo_registry_lib_bot: {}
Dockerfile
:
FROM rust:1.76.0
# Install required dependencies to solve the problem "linking with `cc` failed"
RUN apt-get update && \
apt-get install -y \
libssl-dev \
pkg-config \
libclang-dev \
llvm-dev \
&& \
rm -rf /var/lib/apt/lists/*
WORKDIR /app
# Install cargo-watch
RUN cargo install cargo-watch
COPY ./Cargo.lock ./Cargo.lock
COPY ./Cargo.toml ./Cargo.toml
COPY ./lib_bot/src ./librium_bot/src
COPY ./lib_bot/Cargo.toml ./lib_bot/Cargo.toml
RUN cargo clean
RUN cargo build
CMD ["cargo", "watch", "-x", "run", "-p", "lib_bot"]
But this configs lead new similar error while recompile:
error: could not write output to /app/target/debug/deps/lib_bot-ba0cae4639db1016.2s99ixstsj1t5hnk.rcgu.o: No such file or directory
As you see, I'm linked target folder with local folder, and when I checked it, all folders are exists, but error is happening time by time. Sometime, everything compiled correctly.
If I run it outside docker container, on MacOS, everything works fine.
Oh, I found an answer!
This is a problem because my local system is MacOS and the docker container is Linux, and when I linked the target
folder, it seems that the local MacOS is trying to change the target
folder in parallel (not sure why).
So, fix looks like this change in docker-compose.yml
file:
version: '3.8'
services:
lib_bot:
build:
dockerfile: ./Dockerfile
container_name: lib_bot
env_file:
- ./.env
volumes:
- cargo_registry_lib_bot:/usr/local/cargo/registry
- ./lib_bot/src:/app/lib_bot/src
- ./lib_bot/Cargo.toml:/app/lib_bot/Cargo.toml
- /app/target
- ./Cargo.lock:/app/Cargo.lock
- ./Cargo.toml:/app/Cargo.toml
volumes:
cargo_registry_lib_bot: {}
No local folder linked with target
folder any more and it is solved the problem