Two ways to modify an existing docker image.
The first way is a standard docker build.
The second way is in case you don't have the original image tar or the docker file.
We will be using the standard Fedora docker image.
1 – Rebuilding with dockerfile
Update dockerfile
Commands
echo -e '\nRUN ["dnf", "-y", "install", "python3"]' >> Dockerfile
Explanation
This simply adds the following line to the end of the docker file:
RUN ["dnf", "-y", "install", "python3"]
You can edit the file however you'd like.
Build the new image
Commands
docker build -t relay70/fedoraupdated .
docker image ls
Explanation
This begins the build process. Using the tar file and info in the dockerfile, the new image will be created.
"-t relay70/fedoreupdate" is tagging the image.
Since we added the RUN line, the last thing the build will do is to execute "dnf install -y python3". Our new image should have python3 installed. The last line if the build, should show:
Successfully built <newImageID>
"docker image ls" should show the new image listed.
Run/Check new image
Commands
docker run -it --entrypoint sh <newImageID>
python3 --version
Explanation
Using the image ID from the build step start the container.
"-it --entrypoint sh" tells docker to execute a shell and attach to it.
Since python does not come installed on the standard fedora image, output from the python command shows that our image was successfully updated.
2 – Docker Commit
Pull image from docker hub
Commands
docker pull fedora
docker image ls
Explanation
Download the image from docker hub
List images and find/note the image ID for the new Fedora image.
Run new image
Commands
docker run -it --entrypoint sh <imageID>
Explanation
Run the docker image.
"-it --entrypoint sh" tells docker to execute a shell and attach to it.
Install python
Commands
dnf install -y python3
Explanation
Just like any other dnf install.
Commit image
***Do not disconnect from your container. The next commands need to be executed in another terminal***
After running the commit command you can exit from your running container.
Commands
docker ps
docker commit <containerID> relay70/fedorapython
Explanation
Execute docker ps to find the container id you've changed.
docker commit <containerID> <repoName> simply saves the container to a new image.
Run/Check new image
Commands
docker image ls
docker run -it --entrypoint sh <newImageID>
python3 --version
Explanation
List docker images and note the ID for the new image.
Using the image ID from the build step start the container.
"-it --entrypoint sh" tells docker to execute a shell and attach to it.
Since python does not come installed on the standard fedora image, output from the python command shows that our image was successfully updated.
Leave a Reply