Docker Community Forums

Share and learn in the Docker community.

How would I?: Staged builds and removal of source code

So I have a Dockerfile that runs fine.
The target is an executable MQTT client that requires a number of other libs and executeables in order to run.
To build the deps, the Dockerfile downloads sources, runs the usual .configure, make and make install.
So ideally I could create a staged build using a series of FROM x AS y but doing so copies all the source code from the previous layer thus unnecessarily increasing the images / layer sizes.
To address this I could use FROM and then a COPY --from=0 , assuming 0 was the previous step, which would copy essential files like the target build libraries and executables. Problem is , I dont know where all the files are created , a lot of directories whizz past as make and make install fly past my screen (it is not my source code but cloned git repos).

What’s the best way to do this?


# Base image is gcc build env
FROM gcc:8.3.0 AS gcc_dev

# Build dir

WORKDIR /usr/src/energenie_mqtt

# log4c

RUN mkdir ./log4c \
    && cd ./log4c \
    && curl -SL \
    |  tar -xzC . \
    && cd ./log4c-1.2.4 \
    && ./configure \
    && make && make install \
    && cd ..

# bcm2835

#FROM gcc_dev AS bcm2835
RUN mkdir ./bcm2835 \
    && cd ./bcm2835 \
    && curl -SL \
    |  tar -xzC . \
     && cd ./bcm2835-1.64 \
     && ./configure \
     && make && make install \
     && cd ..

# mosquitto

# Setup mosquitto repo
#FROM bcm2835 AS mosquitto
RUN mkdir ./mosquitto \
    && cd ./mosquitto \
    && curl -SL | apt-key add - \
   && curl -SL  -o /etc/apt/sources.list.d/mosquitto.list \
   && apt update && apt install -y mosquitto mosquitto-dbgsym mosquitto-clients mosquitto-dev \
   && cd ..

# MQTT client

#FROM mosquitto AS client
RUN git clone \
                && cd ./engMQTTClient \
                && make \
                && cd ..

# Run
#FROM client as main
#WORKDIR /usr/src/energenie_mqtt
ENV LD_LIBRARY_PATH=/usr/local/lib
#RUN LD_LIBRARY_PATH=/usr/local/lib ./engMQTTClient/engMQTTClient
#ENTRYPOINT LD_LIBRARY_PATH=/usr/local/lib ./engMQTTClient/engMQTTClient
#ENTRYPOINT ["/usr/src/energenie_mqtt/engMQTTClient/engMQTTClient"]
ENTRYPOINT tail -f /dev/null

EXPOSE 1883/tcp


version: '3'
    image: energenie_mqtt:1.0
    container_name: energenie-mqtt
    build: .
    working_dir: /usr/src/energenie_mqtt
    network_mode: host
      - "1883:1883"
    command: sh -c 'echo "!!-- BUILD COMPLETE"'

Plus, any other ideas on best-practice/improvement, welcomed!