0

first off english is not my first language so sorry for any mispelling mistakes i am not very experienced using docker and i am having a ton of problems while triying to run my code. i am trying to make a web app but i need to rebuild the image from scratch every single time i make a change in the code and that process usually takes a good amount of hours in my computer and that slows my development time to unbeliable amounts. can someone tell me what i am doing wrong?

this is whats inside my docker file.

# python ver
FROM python:3.8-alpine

# create code folder
RUN mkdir /dashboard

# copy code of service inside the folder
ADD . /dashboard

# move to the folder
WORKDIR /dashboard

# install dependencies 
RUN apk --no-cache add musl-dev linux-headers g++

# install requirements
RUN pip install -r requirements.txt

# ejecute
CMD ["python", "main.py"]

i run this command to make the image

docker build -t cliente .

then i run this other command to conect the image to the database

docker run --name cliente -v "C:\Users\DELL AL\OneDrive\Escritorio\Tareas_Arquitectura\cliente_ETL":/dasboard -p 0.0.0.0:5000:5000 --link dgraph:dgraph cliente

from what i know after reviewing google by creating a volume whit the -v option it should be able to show any modified chages in the code inmediatly but instead it remains the same state as the last time "docker build -t cliente ." was used so my question is how to update the volume whitout needing to rebuild the image from scratch every single time

  • ur copying ur source to /dashboard in ur docker file and then mounting a volume to the same directory. the volume mount will override the files in /dashboard dir. if you want to achieve this, you should copy the source files to someplace like /usr/src/dashboard and then on ur entrypoint or cmd just copy the files to /dashboard directory. that way you can maintain the build files state – DC- Mar 17 '23 at 04:11

1 Answers1

1

Docker's layer caching system means that docker build can avoid re-running steps if the image is unchanged from a previous build. However, anything you ADD or COPY into the image can invalidate the cache if any of the files you add change.

In your case you're copying in the entire source tree very early in the Dockerfile, so if any files at all change then the expensive network fetches will happen.

I'd reorder this to

  1. Install any OS-level packages (that don't depend on any of your files at all)
  2. Install your library dependencies (only depending on build-system files)
  3. Install the rest of the application (and build it if needed)

With that reordering you will get:

FROM python:3.8-alpine

# 1. Install OS-level dependencies
RUN apk --no-cache add musl-dev linux-headers g++

# 2. Install library dependencies
WORKDIR /dashboard
COPY requirements.txt ./
RUN pip install -p requirements.txt

# 3. Install the application
COPY ./ ./

# Metadata on how to run the application
CMD ["./main.py"]

(Also note that WORKDIR creates the directory so you do not need an explicit RUN mkdir command; better style is to prefer COPY over ADD unless you specifically want network downloads or automatic archive extraction; and you can refer to the WORKDIR with a relative path on the right-hand side of COPY or ADD.)

When you re-run docker build, the apk add line will only get re-run if its text changes in the Dockerfile; the pip install only if apk add ran or requirements.txt changed; and while the final COPY will run on every rebuild probably, it should be almost free.

Since the source code is in your image, you do not need the docker run -v volume mount to overwrite the code, and I'd remove this option. The docker run --link option is obsolete and I'd also replace that with a Docker network setup.

docker build -t cliente .
docker run --name cliente -p 0.0.0.0:5000:5000 --net some-net cliente
David Maze
  • 130,717
  • 29
  • 175
  • 215
  • thanks for the answer however while trying to run the application i get the error exec ./main.py: exec format error if is useful the part it takes longer is in the installing requirements part of the building image process more specifically in the building wheel for pandas (pyproject.toml) part and the numpy aswell the versions in my requirements.txt are these: numpy==1.23.4 pandas==1.5.1 – Roberto Pacheco Mar 17 '23 at 21:10
  • I believe you can directly install wheels without compiling them yourself if you use the default Debian-based Python image, but not the Alpine variant. – David Maze Mar 18 '23 at 10:34