23

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
  • 415
  • 1
  • 3
  • 9
  • 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

5 Answers5

22

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
  • 20,422
  • 11
  • 104
  • 124
  • 4
    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
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;

Bruno Leyne
  • 193
  • 6
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,437
  • 3
  • 11
  • 28
0

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
  • 337
  • 3
  • 9
  • 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

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
  • 95
  • 1
  • 6