Posted on January 15, 2023
Prerequisite
- Follow the previous article if you need to understand "What is Docker?"
- Install Docker on Windows
Once installation is finished and your environment setup is completed.
An image contains all the software you need to run within your container. Container images can be stored locally on your machine, as well as in a container registry. There are public registries, such as https://hub.docker.com/, or private registries such as ACR (Azure Container Registries).
You can pull existing images from the docker hub or private registries using
docker pull
In the following example, we will pull an image from the public Docker Hub repository and run the actual container.
#First, we will pull an image
docker pull docker/whalesay
#We can then look at which images are stored locally
docker images
#Then we will run our container
docker run docker/whalesay cowsay boo
Following is the command output
Let's look at what happing behind the scene.
- Docker first pulled your image in multiple parts and stored it locally on the machine it was running on.
- When you run the actual application, it used that local image to start a container.
- If we look at the command in detail, you will see that docker pull took in a single parameter, docker/whalesay. if you don't provide a private container registry, Docker will look into the public Docker Hub for images.
- The docker run command took in a couple of arguments, first docker/whalesay, which is the reference to the image. The next two arguments, cowsay boo, are the commands that were passed to the running container to execute
In the previous example, you saw that it is possible to run a container without building an image first. however, it is very common in the practical world that you will want to build an image first. To do this, you use a Dockerfile.
Docker images can be created using a special file format called a “Dockerfile”. This file has commands that allow you to:
- use a pre-existing Docker image as a base
- add files to the image
- run installation commands
- set environment variables
In the next example, you will build a custom Docker Image. This custom image will display inspirational quotes in the whale output.
FROM docker/whalesay:latest
RUN apt-get -y -qq update
RUN apt-get install -qq -y fortunes
CMD /usr/games/fortune -a | cowsay
Let's look at what happing behind the scene.
- The first line will instruct Docker on which image to use as a source/base image for this new image.
- The next two steps are commands that are run to add new functionality to our image
- In this case, update your apt repository and install an application called fortunes. The fortunes application is a small command-line tool that generates inspirational quotes. We will use that to include quotes in the output rather than user input.
- Finally, the CMD command tells Docker which command to execute when a container based on this image is run.
You typically save a Dockerfile in a file called Dockerfile, without an extension.
You will now see Docker execute a number of steps
#To run your container
docker run smartwhale
Following is the command output with inspirational quotes and if you run the container multiple times, you will see different quotes appear.
That concludes our overview and demo of containers. In this section, you started with an existing container image and launched it on the machine. Afterward, you took that a step further and built your own container image, then started containers using that image. You have now learned what it takes to build and run a container.