Reputation: 2512
I have downloaded a Docker image and I want to change it in a way so that I can copy a folder with its contents from my local into that image or maybe edit some file in the image.
I wondered if I can extract the image somehow, do the changes and then create one image, but I am not sure if it will work like that. I tried looking for options but couldn't find a promising solution to it.
The current Dockerfile for the image is somewhat like this:
FROM abc/def
MAINTAINER Humpty Dumpty <@hd>
RUN sudo apt-get install -y vim
ADD . /home/humpty-dumpty
WORKDIR /home/humpty-dumpty
RUN cd lib && make
CMD ["bash"]
Note: I am looking for an easy and clean way to change the existing image only and not to create a new image with the changes.
Upvotes: 15
Views: 44014
Reputation: 891
I think you can use the docker cp
command to make changes to the container which was ran from your docker image and then commit the changes.
Here is a reference,
Guide for docker cp
:
https://docs.docker.com/engine/reference/commandline/cp/
Guide for docker commit
: https://docs.docker.com/engine/reference/commandline/container_commit/
Remember, a Docker image is readonly so you cannot make any changes to that. The only way is to modify your docker file and recreate the image but in that case you lose the data (if not mounted on a Docker volume). But you can make changes to the container which is not readonly.
Upvotes: 2
Reputation: 2512
As an existing docker image cannot be changed, what I did was that I created a dockerfile for a new Docker image based on my original Docker image for its contents, and modified it to include a test folder from local into the new image.
This link was helpful:
Build your own image - Docker Documentation
FROM abc/def:latest
The above line in the Docker file tells Docker which image your image is based on. So, the contents from parent image are copied to new image.
Finally, for including the test folder from local drive, I added the following command in my Docker file
COPY test /home/humpty-dumpty/test
...and the test folder was added into that new image.
Here is the dockerfile used to create the new image from the existing one.
FROM abc/def:latest
# Extras
RUN sudo apt-get install -y vim
# copies local folder into the image
COPY test /home/humpty-dumpty/test
Update: For editing a file in the running docker image, we can open that file using vim editor installed through the docker file shown above:
vim <filename>
Now, the vim commands can be used to edit and save the file.
Upvotes: 16
Reputation: 2100
First of all, I will not recommend messing with other image. It would be better if you can create your own. Moving forward, You can use copy
command to add folder from host machine to the docker image.
COPY <src> <dest>
The only caveat is <src>
path must be inside the context of the build; you cannot COPY ../something /something
, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.
FROM abc/def
MAINTAINER Humpty Dumpty <@hd>
RUN sudo apt-get install -y vim
// Make sure you already have /home/humpty-dumpty directory
// if not create one
RUN mkdir -p /home/humpty-dumpty
COPY test /home/humpty-dumpty/ // This will add test directory to home/humpty-dumpty
WORKDIR /home/humpty-dumpty
RUN cd lib && make
CMD ["bash"]
Upvotes: 3
Reputation: 263666
You don't change existing images, images are marked with a checksum and are considered read-only. Containers that use an image point to the same files on the filesystem, adding on their on RW layer for the container, and therefore depend on the image being unchanged. Layer caching also adds to this dependency.
Because of the layered filesystem and caching, creating a new image with just your one folder addition will only add a layer with that addition, and not a full copy of a new image. Therefore, the easy/clean/correct way is to create a new image using a Dockerfile.
Upvotes: 7