Search code examples
dockerfile

Docker: rebuild only the specific layer


I found that IDEA can rebuild only the specific layer of the Dockerfile. There is a part of my Dockerfile:

# Use the custom base image
FROM arbot-base-image AS sabrdance-build

# Set working directory to /app
WORKDIR /app

# Copy only the necessary directory for the C++ library
COPY ./SABRdance/src /app/src

# Compile the C++ library
RUN echo -e "Compiling the C++ library..." && \
    cd src && \
    g++ -std=gnu++17 -shared -fpic -O3 -o libSABR.so libSABR.cpp

# Use the custom base image
FROM arbot-base-image AS solana-interact-build

# Set working directory to /app
WORKDIR /app

# Copy only the necessary directory for the Rust library
COPY ./solana_interact /app

# Compile the Rust solana interact library
RUN echo -e "Compiling the Rust solana interact library..." && \
    RUSTFLAGS="-A warnings" cargo build --release

So, using IDEA I able to rebuild only, for example, sabrdance-build: IDEA

And it works! No other layers will be rebuilt.

How IDEA doing this?

I've found a lot of solutions, but all of them is about making changes in a step and rebuild from it down to the end of Dockerfile. But I want to make a script that can do it only for the dedicated layer.


Solution

  • What you are here referring to as a layer is a build stage. I suggest that you read the docker multi stage documentation and have a look at their guide on layers.

    To build a specific stage, you can specify the --target flag. Like this:

    $ docker build -t sabrdance-your-tag-name --target sabrdance-build .