Search code examples
dockerrustdockerfile

launching new containers from a container


I have a docker file of my api on rust

FROM lukemathwalker/cargo-chef:latest-rust-1.70.0 as chef
WORKDIR app

FROM chef AS planner
COPY . .
RUN cargo chef prepare --recipe-path recipe.json

FROM chef AS builder
COPY --from=planner /app/recipe.json recipe.json
RUN cargo chef cook --release --recipe-path recipe.json
COPY . .
RUN cargo build --release
RUN apt-get update && apt-get install -y docker.io
VOLUME /var/run/docker.sock

FROM gcr.io/distroless/cc-debian11
COPY --from=builder /app/target/release/blue_traktor_api /

ENV DOCKER_HOST unix:///var/run/docker.sock

CMD ["./blue_traktor_api"]

despite the fact that I say that the container will use the Docker socket from the host, mounting it in /var/run/docker.sock, I do not get access to it from the container and cannot launch containers from inside the current container. How do I solve the problem? this command

 let docker_ps_output = Command::new("docker")
        .arg("ps")
        .arg("-a")
        .output()

should be executed in the api, but Failed to execute docker ps: Os { code: 2, kind: NotFound, message: "No such file or directory" } .. The error indicates that my application is trying to run docker ps, but Docker is not available in the runtime environment.


Solution

  • The problem was that I was installing Docker only in the build environment, not in the runtime environment. Since I am using a base image without a distribution for the final runtime, it did not include the Docker binary or any other binaries needed to execute Docker commands. I used a different base image:

    FROM lukemathwalker/cargo-chef:latest-rust-1.75.0-alpine3.18 as chef
    WORKDIR /app
    
    FROM chef AS planner
    COPY . .
    RUN cargo chef prepare --recipe-path recipe.json
    
    FROM chef AS builder
    COPY --from=planner /app/recipe.json recipe.json
    RUN cargo chef cook --release --recipe-path recipe.json
    COPY . .
    RUN cargo build --release
    
    FROM alpine:3.18
    RUN apk update && apk add docker-cli
    COPY --from=builder /app/target/release/blue_traktor_api /
    VOLUME /var/run/docker.sock
    ENV DOCKER_HOST unix:///var/run/docker.sock
    CMD ["./blue_traktor_api"]