/
Docker walkthrough

Docker walkthrough

Use this template walkthrough to explain Docker at work

Running a Container

We’ll begin with hello-world from hub.docker.com

docker pull hello world

The above command pulls down from the docker register an image called hello-world.

Followed by

docker run hello-world

This creates an executable version of the image. Think of the image as a piece of software on a CD and the container and installed version of that software that can be run on your machine. You have to install the software from the CD before you can run it, this is what the docker run command is doing. You can also think of it as the relationship between Classes (the image) and Objects (container).

The above command will print the following results

So where is the image, type

docker images

Seeing what containers are currently running

This will list any containers currently running, if this is the first time you have run docker then it should be an empty list.

docker ps has useful switches that will cover in more detail later.

Where do images come from?

As you’ve just seen, they can come from container image registers or be made by yourself locally. The most common registers are docker.io, Googles, RedHat, and there are so many now.

Let’s pull the ubuntu image from hub.docker and then run it.

Or with a single command

This command will pull the image if it’s not already on your (host) machine.

Running an image / Creating a container

Now run

Nothing shows. This is because we ran the ubuntu Linux shell, but the shell didn’t actually do anything, so the container exited as expected. To stop the container from exiting run the command

Notice that your prompt has changed, it should have the following structure root@<hexadecimal number>.

Open another terminal and enter the command docker ps -a. Compare the container ID to the <hexadecimal number>, they are the same. The prompt will show the container’s ID whenever you shell into a container.

Any arguments after the image name are passed into the container to be executed once the container is running and available. In this case, we are running a bash shell command

docker run switches used

-i : an interactive terminal

-t : assign the keyboard to the shell

--rm : remove the container when the container exits

Type docker ps --help to see what switches can be used with docker ps

Image names

Run the ubuntu image one more time docker run -it ubuntu bash. Exit out of it immediately. Now run docker ps -a, you should the list of containers that have exited. Notice the strange names given to each container. if you do not give a container a name, Docker will automatically assign one to it. You should get into the practice of giving your containers names. The name is useful for managing and referencing a container. It also means they can be used in script files.

To give a container a name use the --name switch with the docker run command

Cleaning up / removing exited containers

We need to clean up our space. Exited containers take up very little disk space, but it can be very confusing when you are trying to see what containers you have. You should regularly clean up / remove exited containers that you no longer need. This can be done in a number of ways.

Here we use two separate commands

 

Here we use two commands but the output from one command feeds into another command

This command removes all containers, this might not be what you want.

 

This command will remove only those containers who have an exited status