34

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

Ngahy Be
  • 545
  • 1
  • 4
  • 10
  • Possible duplicate of [Copying files from host to Docker container](https://stackoverflow.com/questions/22907231/copying-files-from-host-to-docker-container) – MartenCatcher Jun 19 '19 at 14:48
  • 1
    If you add at the end of the Dockerfile it won't recompile everything – xiawi Jun 19 '19 at 14:49

6 Answers6

42

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
Tamlyn
  • 22,122
  • 12
  • 111
  • 127
  • 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 03 '21 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. –  Nov 16 '22 at 22:53
13

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;

Bruno Leyne
  • 329
  • 1
  • 7
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

gagan
  • 325
  • 3
  • 9
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

Brhaka
  • 1,622
  • 3
  • 11
  • 31
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

santhosh_dj
  • 405
  • 5
  • 10
  • 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 '21 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} -
dvorak4tzx
  • 493
  • 7
  • 8