Build Rust container images faster using layer caching

In the previous blog post I described how to define GitHub Actions pipeline to benefit from caching Rust dependencies and container images’ layers. But the final result may also depend on your Dockerfile. Namely, Docker Buildx Action supports BuildKit and buildx and in order to benefit from this, your Dockerfile has to explicitly cache layers. Actually, this is quite easy to achieve, let’s see the example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 # syntax=docker/dockerfile:1.3 FROM rust:1.61.0 AS builder ARG TARGETPLATFORM WORKDIR /root RUN --mount=type=cache,target=/usr/local/cargo/registry,id=${TARGETPLATFORM} \ cargo install cargo-strip COPY . . RUN --mount=type=cache,target=/usr/local/cargo/registry,id=${TARGETPLATFORM} --mount=type=cache,target=/root/target,id=${TARGETPLATFORM} \ cargo build --release && \ cargo strip && \ mv /root/target/release/<your-crate-name> /root FROM gcr.io/distroless/cc-debian11 COPY --from=builder /root/<your-crate-name> / ENTRYPOINT ["./<your-crate-name>"] EXPOSE 3000 The crucial part here is the annotation # syntax=docker/dockerfile:1.4, this will turn on the advanced features. ARG TARGETPLATFORM is optional and will be used just if you have matrix build with different platforms. If you build for single platform, you can remove this line and also ,id=${TARGETPLATFORM} from the remaining lines of the Dockerfile. The next important point is --mount=type=cache,target=/usr/local/cargo/registry. This defines which path should be actually cached as container image layer. Replace <your-crate-name> placeholder with your real crate name and you are ready to go. If you expose your service on a different port than 3000, please modify the final line EXPOSE 3000 to the correct port, or if there are no exposed ports, just delete the line. ...

2022-06-03 · 2 min · Boban Acimovic

Running stack of microservices using docker-compose and acim/go-reflex image

Go is very attractive language for microservices’ development, but using acim/go-reflex image and docker-compose you can run multiple microservices including your gateway on your local machine. This image can run virtually any Go server based application and also reacts on changes in the code and recompiles the binary on the fly. If you want to debug your microservices’ stack or develop a new feature where you have to touch multiple services, this is very good tool to do so. Let’s take a look at the docker-compose stack example: ...

2021-10-23 · 3 min · Boban Acimovic

Run Docker as normal user on Ubuntu 20.04

In lot of instructions how to run docker on Ubuntu 20.04 (same applies to Linux Mint Ulyana) you may find that after installing docker you have to add your user to docker group by the following command: 1 sudo usermod -aG docker $USER If you still get permission errors, you may try to set ACL: 1 sudo setfacl -m user:$USER:rw /var/run/docker.sock

2020-11-17 · 1 min · Boban Acimovic

New kind on the Block

Few months ago I wrote an article about replacing Minikube with MicroK8s , but now we have even better soluion, kind . Like dind allows running Docker inside Docker containers, kind allows running Kubernetes inside Docker containers. kind basically abstracts nodes as Docker containers and then runs Kubernetes inside. After you install Golang , it is quite easy to install and setup kind: 1 2 GO111MODULE="on" go get sigs.k8s.io/kind@v0.5.1 kind create cluster After some 30 seconds your single node cluster will be ready for development and testing. But that’s not all, kind allows much complex setup, for example three node cluster with one master and two workers. Let’s first create declarative object: ...

2019-10-03 · 1 min · Boban Acimovic