ADD in Docker file seems to not copy the file in the right directory on the image

So,
I’m trying to create my Python Docker image. I need to install pyserial via pip. The problem is that one of the .py files inside the pyserial package is outdated so I need to change it with mine which is an updated version.

My Docker file first install pyserial package via pip and then copy the file I need (which is inside my source file) to the right directory inside the site-package directory of Python.

Unfortunately, even if the operation seems to be successful while building the image, the actual file hasn’t be copied.

My Docker file is below

# syntax=docker/dockerfile:1

FROM python:3.8-slim-buster

WORKDIR /app

COPY serial_find.py serial_find.py

RUN pip install pyserial

ADD list_ports_linux.py usr/local/lib/python3.8/site-packages/serial/tools/

CMD [ "python3", "./serial_find.py" ]

the folder containing the files for building the images has:

  • serial_find.py
  • list_ports_linux.py
  • Dockerfile

Am I using ADD in the wrong way? In the past, this solution worked

Yes. You have to start the destionation with “/”. Otherwise it is relative to the actual WORKDIR, which is “/app” in your case.

I also recommend using COPY instead of ADD since ADD has more function than you need and has more complicated, different results.

1 Like