DEV Community

Dean
Dean

Posted on • Originally published at veducate.co.uk on

Using Docker to update and commit to a container image

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 aCI 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
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
Enter fullscreen mode Exit fullscreen mode

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.

Prerequisites
Pull the image you want to update
docker pull {image location/name}
Enter fullscreen mode Exit fullscreen mode

docker pull image

Check your images and get the ID
docker images
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 busyboxand it’s tools when troubleshooting in a container environment.

apt install net-tools
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Commit your container image changes
docker commit {container_id} {tag}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
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]
Enter fullscreen mode Exit fullscreen mode

To push your image:

docker push {image name/tag}

# Example
docker image saintdle/k8s-ci:0.1
Enter fullscreen mode Exit fullscreen mode

docker push repo-image_name-tag

dockerhub repo

Regards

Follow @Saintdle

Dean Lewis

The post Using Docker to update and commit to a container image appeared first on vEducate.co.uk.

Top comments (0)