Docker Workflow - Running your First Container!

Martyn Fewtrell
5 min readDec 20, 2021

If you read and followed my last blog post, An Introduction to Docker on Windows you will hopefully now have Docker installed on your workstation. This post will focus on running your first container and helping you to understand the workflow that takes place for this to happen.

Hello from Docker!

In keeping with tradition our first container will be a Docker “Hello World” example!

When working with Docker, much of your work will be completed from a Command Window. To do this you can use:

  • the Command Prompt;
  • a PowerShell window; or
  • a really great tool is Visual Studio Code (a free, open source code editor provided by Microsoft.

I’ll be using Visual Studio Code for my examples but feel free to use whatever tool you feel most comfortable with.

Open Visual Studio Code and if you need to, open a new Terminal. Do this by clicking on Terminal in the File menu and then selecting New Terminal.

A new Terminal Window should open towards the bottom of Visual Studio Code.

Type docker run hello-world in the Terminal Window and then press enter to execute the command.

Docker will run the command and you should see the following output.

Congratulations you have just run your first container!

So what was the workflow?

If you examine the output from the Terminal Window you will see the first line reads:

“Unable to find image ‘hello-world:latest’ locally”.

Docker stores local copies of images for any containers it runs on the local drive. A benefit of this is that if you wish to run the container again it does not need to download the image from the internet again.

To see this type cls in the Terminal Window and then press enter to clear the screen.

Type docker run hello-world in the Terminal Window again and notice that this time the output is shorter.

The top part of output is absent, when you ran the command this time, Docker found the image on your local drive and used this image to create the container instead of downloading a new one from Docker hub.

If we look again at the top section of the output from the first time we ran the command we notice that the second line reads:

“Pulling from library/hello-world”

When we ran the command the first time there was no local copy of the container image, so Docker downloaded a new image from Docker hub.

The following lines read:

“Pull complete”

and

“Downloaded newer image for hello-world:latest”

This output is telling us that Docker has successfully found a suitable image in the Docker hub and downloaded it to the local drive.

Finally the last part of the output reads:

“Hello from Docker…….”

This part of the output is the actual result from running the Hello-World container and is hopefully self explanatory!

Terminology

In the text above I have introduced some terms which require explanation. I will provide greater depth in later posts but I think there is value in providing a quick overview at this stage.

A container image is a lightweight, read only, executable package of software which becomes a container at runtime.

In most cases you will get a base image from the Docker hub and build upon that by adding layers. Images are built from layers and as each layer is added to the image the layer becomes read only. Each new layer represents a change in the functionality provided by the existing layers.

Docker is able to use layers as a cache to provide great performance. If there are no changes to the layers then Docker can run the container from an existing state and has no need to build the container from the image again.

Docker hub is a central repository for obtaining, managing and storing container images. It includes both private and public repositories so that you can share your container images either within your team or to a much wider audience.

Summary

When you use the Docker Run command, Docker executes a number of steps in its workflow.

  1. Docker checks the local image library for the container image that you have requested it to run.
  2. If the container image that you want to run doesn't exist locally, Docker pulls (or copies) a new copy of the image to the local image library.
  3. Once a local copy of the container image exists Docker creates a running container from the local image.

In this post we have taken a quick look at the workflow involved when running a container. In my next post I will take a closer look at the anatomy of a container image.

--

--

Martyn Fewtrell

I am a software developer based in the South West of the United Kingdom predominantly working with Microsoft technologies.