Search code examples
dockerdockerfiledocker-image

How to modify a Docker image?


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.


Solution

  • 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.