Using Docker to update and commit to a container image
Tags: kubernetes, commit, container, docker
Published: 2021-04-13 22:26:24

---
title: Using Docker to update and commit to a container image
published: true
date: 2021-04-12 11:20:43 UTC
tags: Kubernetes,commit,container,docker
canonical_url: https://veducate.co.uk/docker-update-container/?utm_source=rss&utm_medium=rss&utm_campaign=docker-update-container
---

I was helping a customer build some customized automation tasks using [vRealize Automation Codestream](https://docs.vmware.com/en/vRealize-Automation/8.3/Using-and-Managing-CodeStream/GUID-3625AE99-C60C-4517-803B-18C526ADCFF1.html). These tasks required the use of a container image with certain tools installed, usually we can include a[CI task](https://docs.vmware.com/en/vRealize-Automation/8.3/Using-and-Managing-CodeStream/GUID-7957EC89-00DF-415F-BB65-FB4839DE4A20.html) 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"]
```

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

- [Docker installed](https://docs.docker.com/get-docker/)

###### Pull the image you want to update

```
docker pull {image location/name}
```

[![docker pull image](https://i2.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-pull-image.jpg?resize=565%2C181&ssl=1)](https://i2.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-pull-image.jpg?ssl=1)

###### Check your images and get the ID

```
docker images
```

For the next command, we will need the Image ID. ![docker images](https://i1.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-images.jpg?resize=604%2C182&ssl=1)

###### 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](https://i2.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-run-it-image_id-bin-bash.jpg?resize=460%2C66&ssl=1)](https://i2.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-run-it-image_id-bin-bash.jpg?ssl=1)

###### 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](https://busybox.net/)and it’s tools when troubleshooting in a container environment.

```
apt install net-tools
```

[![docker install updates to container](https://i1.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-instlal-updates-to-container.jpg?resize=604%2C206&ssl=1)](https://i1.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-instlal-updates-to-container.jpg?ssl=1)

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

Exit out of your container.

[![docker exit out of exec container](https://i0.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-exit-out-of-exec-container.jpg?resize=182%2C32&ssl=1)](https://i0.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-exit-out-of-exec-container.jpg?ssl=1)

###### Get your container ID

```
docker ps -a
```

[![](https://i0.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-ps-a.jpg?resize=604%2C37&ssl=1)](https://i0.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-ps-a.jpg?ssl=1)

###### 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](https://i1.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-commit-container_id-tag-docker-images.jpg?resize=597%2C93&ssl=1)](https://i1.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-commit-container_id-tag-docker-images.jpg?ssl=1)

###### 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](https://i0.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-save-.jpg?resize=476%2C50&ssl=1)](https://i0.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-save-.jpg?ssl=1)

###### 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](https://docs.docker.com/engine/reference/commandline/login/) 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](https://i1.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-push-repo-image_name-tag.jpg?resize=604%2C135&ssl=1)](https://i1.wp.com/veducate.co.uk/wp-content/uploads/2021/04/docker-push-repo-image_name-tag.jpg?ssl=1)

[![dockerhub repo](https://i1.wp.com/veducate.co.uk/wp-content/uploads/2021/04/dockerhub-repo.jpg?resize=604%2C268&ssl=1)](https://i1.wp.com/veducate.co.uk/wp-content/uploads/2021/04/dockerhub-repo.jpg?ssl=1)

Regards

[Follow @Saintdle](https://twitter.com/saintdle)<script async="" src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

[Dean Lewis](https://uk.linkedin.com/in/saintdle?trk=profile-badge)

The post [Using Docker to update and commit to a container image](https://veducate.co.uk/docker-update-container/) appeared first on [vEducate.co.uk](https://veducate.co.uk).