I am new in docker world. So I have an existing Dockerfile which somewhat looks like below:
# Base image
FROM <OS_IMAGE>
# Install dependencies
RUN zypper --gpg-auto-import-keys ref -s && \
zypper -n install git net-tools libnuma1
# Create temp user
RUN useradd -ms /bin/bash userapp
# Creating all the required folders that is required for installment.
RUN mkdir -p /home/folder1/
RUN mkdir -p /home/folder2/
RUN sudo pip install --upgrade pip
RUN python3 code_which_takes_time.py
# Many more stuff below this.
So code_which_takes_time.py
takes time to run which will download many stuff and will execute it.
So the requirement is whenever we add more statements below RUN python3 code_which_takes_time.py
will unnecessary will execute this python script everytime while building an image. So I would like to split this image into 2 Dockerfiles.
- One file you can run only once. This file will have time consuming stuff which can be run only once while building an image.
- Second one will be used to add anymore statements which will be added as more layers on top of the existing image.
Because if I run docker build -t "test" .
for the current file, it will execute my python script again and again. It’s time consuming and I don’t want to run it again and again.
My questions:
- How can split Dockerfile as I mentioned above.?
- How can I build an image with 2 image files.?
- How can I run these 2 files?
As of now I do :
Build and run: docker build -t "test" . && docker run -it "test"
Just Build : docker build -t "test" .
Just Run : docker run -it "test"