Using Docker to update and commit to a container image

This post was originally published on this site

I was helping a customer build some customized automation tasks using vRealize Automation Codestream. These tasks required the use of a container image with certain tools installed, usually we can include a CI task to download the tools into the container image on the fly. However, my customer’s environment is offline, so I needed to provide them a container image with everything installed by default.

Before we dive into the process of running a container and committing the changes, it is recommended where possible to create a new docker file that would build your docker image as needed with the associated commands such as the below:

FROM node:12-alpine
RUN apk add --no-cache python g++ make
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]

Committing changes to a container image in this way can cause the image to become bloated. But sometimes there’s a need to do just do it this way.

Pull the image you want to update
docker pull {image location/name}

docker pull image

Check your images and get the ID
docker images

For the next command, we will need the Image ID.docker images

Run your image as an active container
docker run -it {Image_ID} /bin/bash

This will then drop you into the tty of the running container.

docker run -it image_id bash

Modify your container

As an example for this blog post, I am installing net-tools package. Typically, I wouldn’t install this tools package into a container. Where needed, I would use busybox and it’s tools when troubleshooting in a container environment.

apt install net-tools

docker install updates to container

Clean up/tmp and apt download files location as needed.

Exit out of your container.

docker exit out of exec container

Get your container ID
docker ps -a

Commit your container image changes
docker commit {container_id} {tag}

In my example, I’ve included in my tag a version number as well.

docker commit container_id tag - docker images

Saving your image for a manual import elsewhere
docker save {image_name:tag} > {filename}.tar

# To compress your image further
docker save {image_name:tag | gzip > {filename}.tar.gz
docker save
Pushing your image to a repository

You can now push this new commit to your repository, the following example will push to DockerHub.

Log into your repository as necessary.

docker login {server_address} --username {username} [--password]/[--password-stdin]

To push your image:

docker push {image name/tag}

# Example
docker image saintdle/k8s-ci:0.1

docker push repo-image_name-tag

dockerhub repo


The post Using Docker to update and commit to a container image appeared first on