Search code examples
dockercopy

Copy files from a docker image to another


I need to hide my base code from the user. So I decided to just copy all files into a docker image and import those files from that image later.

# Use a base image (you can specify any base image that suits your needs)
 FROM ubuntu:latest

# Set the working directory inside the container
 WORKDIR /app

# Copy the files from the local machine to the container
COPY . /app

If I do like that can I copy these files in another docker file when I needed? If yes how can we do that?


Solution

  • You can use COPY --from with any image, not just other stages in your current Dockerfile.

    FROM python:3.11
    COPY --from=registry.example.com/source-only-image /app /app
    WORKDIR /app
    CMD ["./main.py"]
    

    Once you do this the files will be present in the destination image and you can do anything with them you could with files in a Docker image, like docker run ... cat them or docker cp them to the host. That is, there is no security benefit to doing things this way.