How to create a custom image from a Docker container


Getting your own custom image makes working with containers easier and it adds an extra layer of security. Jack Wallen shows you how.

Illustration: Lisa Hornung/TechRepublic

Your containers are all based on images, most of which you probably pull from DockerHub or some other public repository. But why not create your own custom images? After all, it’s easier for you to not only get the exact image you need to work with, it’s also a better route to security. It’s also a great way to create a base image you can use for a repeatable development environment (so, it has exactly the tools you need).

SEE: 40+ open source and Linux terms you need to know (TechRepublic Premium)

I’m going to show you how you can do just that–create an image from a container using the Docker runtime engine.

What you’ll need

I’ll be demonstrating on Ubuntu Server 20.04, but you can pull this off on any platform that supports Docker. The only thing you’ll need to alter is the Docker installation process. You’ll also need a user with sudo privileges (for the installation).

With that said, let’s get to work.

How to install Docker

Let’s first get Docker installed. We’ll install the latest version of the community edition of Docker. Log into your server and install the necessary dependencies with:

sudo apt-get install ca-certificates curl gnupg lsb-release -y

Once that completes, add the Docker GPG key:

curl -fsSL | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Next, add the Docker repository with the command:

"deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] 
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Update apt and install Docker-ce with:

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli -y

Add your user to the Docker group with:

sudo usermod -aG docker $USER

Log out and log back in for the changes to take effect.

How to create your base container

We’re going to use the official Ubuntu image to create our container (which will be used to create the new image). Let’s pull down the latest version of Ubuntu from DockerHub and create a container named ubuntu-test with the command:

docker run -ti --name=ubuntu-test ubuntu:latest

At this point, you should find yourself at the bash prompt of the running Ubuntu container. Let’s say this new image will be used for Java development. Install the latest Java runtime environment. First update apt with:

apt-get update

Next, install the latest JRE with:

apt-get install default-jre -y

After the installation completes, exit from the container with:


How to create the new image

The first thing to be done is to create a commit for the running container. Do this with:

docker commit ubuntu-test

Next, we need to locate the container ID for our running instance with the command:

docker ps -a

Start the container with:

docker start ID

Where ID is the container ID for ubuntu-test.

Next, we need to find the image ID with the command:

docker images

When we made the commit earlier, it created an image without a tag or name. You’ll see an image listed with <none> as both the ID and name. That’s the image we want to tag. Using the first four characters of the image ID of the new image, tag it with:

docker tag ID ubuntu-test-base

Where ID is the first four characters of the container ID for ubuntu-test. Now, if you issue the command docker images, you’ll see your new image with the name ubuntu-test-base. You could then deploy a new container, using that new image, with a command like:

docker create --name ubuntu-jre ubuntu-test-base

And that’s all there is to creating a custom Docker image, based on a modified container.

Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.



Source link

Leave a reply

Please enter your comment!
Please enter your name here