33

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

6 Answers 6

41

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
2
  • 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
    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
    Nov 16, 2022 at 22:53
11

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
  • 5
    ``` $ Mount the image and make a container bash: command not found: Mount ```
    – user123
    Nov 26, 2022 at 20:26
5

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

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
1

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
    Mar 24, 2021 at 10:23
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} -

Your Answer

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

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