'Docker build process and persistence of files to final image for use in container

I'm working on containerizing a somewhat complex application, and I'm running into some issues that are probably down to a lack of understanding of how Docker works and I've done a large amount of googling and reading but still haven't seem to gotten a solution.

I'm currently using docker-compose to launch the containers, and for building using docker-compose up --build at build time. I've got a .dockerignore file going to try and limit my build context as best I can or build-times can take a very long time. my docker-compose.yml looks something like

services:
  base_image:
    container_name: base_image_generator
    build:
      context: .

My dockerfile looks something like

FROM ubuntu:20.04 as dds_install
WORKDIR /app

COPY ./rti/rti_connext_dds-6.1.0-pro-host-x64Linux.run \
        ./rti/rti_connext_dds-6.1.0-pro-target-x64Linux4gcc7.3.0.rtipkg ./
RUN chmod +x rti_connext_dds-6.1.0-pro-host-x64Linux.run \
    && ./rti_connext_dds-6.1.0-pro-host-x64Linux.run --mode unattended
RUN /opt/rti_connext_dds-6.1.0/bin/rtipkginstall \
    -u rti_connext_dds-6.1.0-pro-target-x64Linux4gcc7.3.0.rtipkg



FROM ubuntu:20.04
ENV TZ="America/Los Angeles"
ENV NODE_OPTIONS=--max_old_space_size=16384
ARG DEBIAN_FRONTEND=noninteractive

# Copy the data from the dds build
COPY --from=dds_install \
    /opt/rti_connext_dds-6.1.0 /opt/rti_connext_dds-6.1.0

# Copy in the license file
COPY ./rti/rti_license.dat /opt/rti_connext_dds-6.0.1
# Add the NDDSHOME to our path
ENV NDDSHOME /opt/rti_connext_dds-6.1.0
ENV PATH $PATH:$NDDSHOME/bin

RUN apt-get update && apt-get install -y software-properties-common \
    && add-apt-repository ppa:deadsnakes/ppa \
    && apt-get update \
    && apt-get install -y \
    python3.6 \
    python3.6-venv \
    python3.6-dev


RUN apt-get update \
    && apt-get -y upgrade \
    && apt-get install -y \
    build-essential \
    cmake \
    cmake-gui \
    curl \
    ffmpeg \
    git \
    gnupg2 \
    gnome-keyring \
    libboost-chrono-dev \
    libboost-filesystem-dev \
    libboost-program-options-dev \
    libboost-system-dev \
    libboost-thread-dev \
    libboost-timer-dev \
    libcurl4-openssl-dev \
    libssl-dev \
    pass \
    python3-pkg-resources \
    python3-pip \
    python3-venv \
    python-is-python3 \
    tzdata \
    vim \
    wget \
    nano

RUN python3.6 -m venv /root/venv
ENV PATH="/root/venv/bin:$PATH"

# Get node 14, this is the LTS version of node and install it
RUN curl -fsSL https://deb.nodesource.com/setup_14.x | bash -
RUN apt-get update && apt-get -y install nodejs
RUN npm install -g @angular/cli

RUN pip install --upgrade pip && pip install \
    cython \
    jinja2 \
    pip-login \
    pycurl \
    setuptools \
    vcstool \
    wheel \
    xmltodict \
    gdal==2.2.3

I then tag the image that is generated, and move onto another docker-compose.yml that uses the generated image, and use another Dockerfile to copy source code into the Docker build context, and do a build process, and login to a private pip repo and pull down some python packages using pip.

The issue I'm running into is that my python venv isn't persisting. If I run the pip list command prior to the docker build finishing by issuing a RUN pip list the list that is printed is as expected. When I issue a docker-compose up, to bring a container up my app doesn't function properly though because it's missing all of the python packages that I installed, although running back using docker exec -it <container_name> bash shows that my virtualenv is being used (verified with a which pip)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source