Add a file in an existing docker image
Asked Answered
A

7

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

Acorn answered 19/6, 2019 at 14:45 Comment(2)
Possible duplicate of Copying files from host to Docker containerBeginning
If you add at the end of the Dockerfile it won't recompile everythingJame
A
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;

Aikens answered 19/6, 2019 at 14:53 Comment(1)
``` $ Mount the image and make a container bash: command not found: Mount ```Louie
A
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
Antispasmodic answered 19/1, 2021 at 14:48 Comment(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"Sapphera
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.Perzan
I like it... Simple... Easy to remember...Corrosion
I learned a new word today: succinctPanelist
A
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;

Aikens answered 19/6, 2019 at 14:53 Comment(1)
``` $ Mount the image and make a container bash: command not found: Mount ```Louie
T
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

Tester answered 23/5, 2022 at 9:17 Comment(0)
R
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

Rani answered 19/2, 2021 at 6:25 Comment(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.Bedeck
C
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

Chlorohydrin answered 19/6, 2019 at 14:49 Comment(0)
D
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} -
Discant answered 27/3, 2023 at 11:22 Comment(0)
S
0

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 .
Satem answered 26/7 at 8:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.