38

I've created a docker image of a project (very large project that takes time to compile) and I forgot to add a file in it.

I does not have to do with the build, it's just a test file.

Is it possible to add this file into my image without rebuilding everything ?

Thanks

2

7 Answers 7

48

Here's a full example to pull an image, add a local file to it, retag the image and push it back:

export IMAGE_URL=example.com/your_image:your_tag
docker pull $IMAGE_URL
docker create --name temp_container $IMAGE_URL
docker cp /host/path/to/file temp_container:/container/path/to/file
docker commit temp_container $IMAGE_URL
docker push $IMAGE_URL
5
  • 7
    This is by far the most succinct answer so far. Yes - it covers much of the same ground as the previous answers but doesn't assume I have expert mastery of the docker cli. e.g. "Mount your container"
    – Stevko
    Commented Feb 3, 2021 at 19:25
  • Note: This may change unrelated values in your images. In my case, it changed the entrypoint command and "AttachStdout"/"AttachStderr" values (as seen when comparing diff <(docker inspect old) <(docker inspect new). It's possible this happened to me because the image was created with a different version of Docker, though.
    – user11638666
    Commented Nov 16, 2022 at 22:53
  • I like it... Simple... Easy to remember... Commented Nov 24, 2023 at 14:08
  • I learned a new word today: succinct
    – chandler
    Commented Jul 10 at 13:26
  • does the path need to exist within the container before copying or does it create that path?
    – user_01_02
    Commented Aug 15 at 21:35
15

Yes its possible, do the steps:

  1. Mount the image and make a container
  2. Do the command:

    docker cp textFile.txt docker_container_name:/textFile.txt

  3. Commit the container to build a new image with new tag version or another name;

1
  • 9
    ``` $ Mount the image and make a container bash: command not found: Mount ```
    – user123
    Commented Nov 26, 2022 at 20:26
6

Add the image using docker commit of a container as described above, or using Dockerfile.

  1. Create a directory say temp and navigate into it
  2. Move file file-to-be-added.extension to be added to the docker image into the newly created temp directory
  3. Create a Dockerfile with below contents

Dockerfile

FROM your-registry/your-image:tag
COPY file-to-be-added.extension /destination/path/of/file-to-be-added.extension

Run below command to build the new image:

docker build -t your-registry/your-image:new-tag .

If required, push the image

docker push your-registry/your-image:new-tag

2

The ADD command is used to copy files/directories into a Docker image. It can copy data in three ways:

1. List item Copy files from the local storage to a destination in the Docker image.

2. Copy a tarball from the local storage and extract it automatically inside a destination in the Docker image.

3. Copy files from a URL to a destination inside the Docker image.

enter image description here

source: https://www.educative.io/edpresso/what-is-the-docker-add-command

1
  • 1
    OP doesn't want to re-build the docker image. He just wants to add a file to an existing image. Your suggestion requires the docker image to be rebuilt.
    – Basti
    Commented Mar 24, 2021 at 10:23
1

You should try to use docker commit.

Example: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]

There's docker cp too, but it only works in running containers.


I hope this helps!

Brhaka

0
0

Just patch your docker image:

export TAG=/your/docker/tag
export FILE=/path/to/your/file/to/add

cd `dirname $FILE`
FILENAME=`basename $FILE` echo "FROM ${TAG}\nADD ${FILENAME} /some_dir/{FILENAME}" | docker build -t ${TAG} -
-1

Yes,It is possible. You can just Drag and Drop into the Docker Desktop ..

  1. Go into the images files in Docker Desktop ..
  2. Drag the file from the local machine into the Docker Desktop .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.