Docker Home Lab | Commit changes to a Docker container

Commit changes to Docker containers

In our previous post we learned how to start and manage containers. You may have noticed every time that we run a container it is started from the same point or image, none of our changes to the file system stayed with that image.

CLICK IN THE FOLLOWING LINKS TO JUMP TO THE POST OF THE THIS SERIES: 

DOCKER HOME LAB | Where to Start ?

DOCKER HOME LAB | Managing Docker Containers

DOCKER HOME LAB | Commit changes to a container

DOCKER HOME LAB | Using / Finding / Sharing in Docker Public index

DOCKER HOME LAB | Building Containers with Dockerfile

DOCKER HOME LAB | Affect container with Dockerfile

In this post we are going to use these new commands to learn how to make changes to a container image.

docker images

docker run

docker diff

docker commit

Often you may want to work with containers that already set up the way you want instead of setting them up from scratch every time. Perhaps you want to have it configured in certain way or certain applications installed.

In this post we are going to create an image made to run a ssh server. To do this we will make a custom container image based on the stock Ubuntu image.

Let’s start by seeing what images we have with docker images

docker images

Whenever you try to run a container based on an image you don’t have, its downloaded automatically.

You may see a few since we been using the Ubuntu image you should see Ubuntu listed here

To make a new image we start by running a container to make the changes we want, using bash is the easiest way to make changes interactively.

Let’s start a container running bash

docker run -i -t ubuntu bash

Let’s check to ensure that ssh server hasn’t been install yet

sshd

Before we install anything, lets update our apt-get repository, this will ensure we are working with the latest packages.

apt-get update

Let’s install the open ssh server package with apt-get install

apt-get install openssh-server

We used the full path because sshd is particular about how we started

full path

Looks like it need a directory created, when we run it in our previous post we have to create this directory in the same compound command because the container didn’t have it. Making it now will ensure is there in the image we made.

mkdir

If you recall we also have to set a password for the root user, since we will be connecting as root and ssh will ask for a password, let’s set that here

set config

We also need to update the sshd config file to allow root to login and another small change to allow authentication to work

set login config root

That’s all the changes we will make so let’s exit

exit

The container is now stopped but it’s still around with its changes, we can inspect the changes to the file system using docker diff this show us exactly what files were changed after installing openssh server and running those commands inside the container. Let’s run this through less so we can look through its more easily

docker diff

You can see it install a number of files all over the place,  this is normally what happens when you install software and is kind of nice to Docker allow us to see this easily

To persist our changes to an image we use docker commit with the container id and an image name , the common convention is to use the unique username/image name.

docker commit

Now let’s start sshd in a container from this new image, remember that we need to open port 22 in order to be able to connect to it

docker run

The container now is created, let’s connect to it

ssh to container

 

Lastly when you commit a container to an image, it also remembers the last run parameter that you used by default. If we commit again with this last container the next time we would not need to specify the sshd command or the need for port 22, since we will have already learned that from this container

Now we can clean up the container

docker rm

As you can see this workflow is very simple, making very easy to create new containers images you can do it over and over again to customize or fork container images however you like.

In the next post we take a look for ways to share your changed containers with other hosts and the rest of the world

Docker Home Lab

Docker Home Lab

Trackbacks

  1. […] Docker Home Lab | Commit changes to a container […]

  2. […] Docker Home Lab | Commit changes to a container […]

  3. […] DOCKER HOME LAB | Commit changes to a container […]

  4. […] DOCKER HOME LAB | Commit changes to a container […]

Speak Your Mind

*

%d bloggers like this: