Docker Containers-02: Tutorial Step by Step

Sezgin Erdem, Ph.D.
8 min readAug 16, 2020

--

What are Containers?

A Docker container, is a standard unit of software that stores up code and all its dependencies so the application runs fast and reliably from one computing environment to different ones. A Docker container image is a lightweight, standalone, executable package of software that has everything you need to run an application — code, runtime, system tools, system libraries, and settings.

Available for both Linux- and Windows-based applications, containerized software will always run the same, regardless of the infrastructure. Containers isolate software from its environment and ensure that it works uniformly despite differences.

What are the Benefits of Docker Containers?

Docker containers are popular now because they have Virtual Machines beat. VMs contain full copies of an operating system, the application, necessary binaries, and libraries — taking up tens of GBs. VMs can also be slow to boot. In contrast, Docker containers take up less space (their images are usually only tens of MBs big), handle more applications, and use fewer VMs and Operating Systems. Thus, they’re more flexible and tenable. Additionally, using Docker in the cloud is popular and beneficial. In fact, since various applications can run on top of a single OS instance, this can be a more effective way to run them.

Another distinct benefit of Docker containers is their ability to keep apps isolated not only from each other but also from their underlying system. This lets you easily dictate how an allocated containerized unit uses its system resources, like its CPU, GPU, and network. It also easily ensures data and code remain separate.

Due to these benefits, containers (& Docker) have seen widespread adoption. Companies like Google, Facebook, Netflix and Salesforce leverage containers to make large engineering teams more productive and to improve utilization of compute resources. In fact, Google credited containers for eliminating the need for an entire data center.

Run your first container

We’re going to start with checking that Docker is working correctly, and then we’re going to take a look at the basic Docker workflow: creating and managing containers. We’ll take a container through its typical lifecycle from creation to a managed state and then stop and remove it.

Firstly, let’s check that the docker binary exists and is functional:
Input :

docker info

Output:

Here, we’ve passed the info command to the docker binary, which returns a list of any containers, any images (the building blocks Docker uses to build containers), the execution and storage drivers. Docker is using, and its basic configuration.

Your Container

Now let’s try and launch our first container with Docker. We’re going to use the docker run command to create a container. The docker run command provides all of the “launch” capabilities for Docker. We’ll be using it a lot to create new containers.

💡Tips: You can find a full list of the available Docker commands here or by typing docker help. You can also use the Docker man pages (e.g., man docker-run). You can access all commands related to docker container via docker container --help command.

input :

docker run -it ubuntu sh

First, we told Docker to run a command using docker run. We passed it two command line flags: -i and -t.

The -i flag keeps STDIN open from the container, even if we’re not attached to it. This persistent standard input is one half of what we need for an interactive shell.

The -t flag is the other half and tells Docker to assign a pseudo -tty to the container we’re about to create. This provides us with an interactive shell in the new container. This line is the base configuration needed to create a container with which we plan to interact on the command line rather than run as a daemonized service.

💡Tips: You can find a full list of the available Docker run flags by typing docker run help. You can also use the Docker man pages (e.g., man docker-run).

We told Docker which image to use to create a container, in this case, the ubuntu image. The ubuntu image is a stock image, also known as a “base” image, provided by Docker, Inc., on the Docker Hub registry.

So what was happening in the background here? Firstly, Docker checked locally for the ubuntu image. If it can’t find the image on our local Docker host, it will reach out to the Docker Hub registry run by Docker, Inc., and look for it there. Once Docker had found the image, it downloaded the image and stored it on the local host.

Docker then used this image to create a new container inside a filesystem. The container has a network, IP address, and a bridge interface to talk to the local host. Finally, we told Docker which command to run in our new container, in this case launching a Bash shell with the sh command.

Working with Container

We are now logged into a new container, with the catchy ID of b69c772c73fc, as the root user. This is a fully-fledged Ubuntu host, and we can do anything we like in it. Let’s start by asking for its hostname.

Installing a package in container

apt-get update && apt-get install nano

You can keep playing with the container for as long as you like. When you’re done, type exit, and you’ll return to the command prompt of your Ubuntu host.

exit

It has now stopped running. The container only runs for as long as the command we specified, sh, is running. Once we exited the container, that command ended, and the container was stopped.

docker ps -a command

The container still exists; we can show a list of current containers using the docker ps -a command.

docker ps -a

By default, when we run just docker ps, we will only see the running containers. When we specify the -a flag, the docker ps -a command will show us all containers, both stopped and running.

We learn quite a bit of information about our container: its ID, the image used to create it, the command it last run, when it was created, and its exit status (in our case, 0, because it was exited normally using the exit command). We can also see that each container has a name.

There are three ways containers can be identified:

  • a short UUID (b69c772c73fc),
  • a longer UUID (like b69c772c73fc21548190053c4053f93fda09c3e5fa6bd670acbc7697a96b9961), and
  • a name (keen_curran).

Container naming

Docker will automatically generate a name at random for each container we create. We see that the container we’ve created is called keen_curran. If we want to specify a particular container name in place of the automatically generated name, we can do so using the — name flag.

docker run --name serdem -it ubuntu sh

You can check it in another terminal and verify the following output.

This would create a new container called serdem. A valid container name can contain the following characters: a to z, A to Z, the digits 0 to 9, the underscore, period, and dash (or, expressed as a regular expression: [a-zA-Z0–9_.-]).

We can use the container name in place of the container ID in most Docker commands. It is also much easier to remember a specific container name than a container ID or even a random name.

Names are unique. If we try to create two containers with the same name, the command will fail. We need to delete the previous container with the same name before we can create a new one. We can do so with the docker rm command.

Starting a stopped container

If we want, we can restart a stopped container like so:

docker start serdem

We could also refer to the container by its container ID instead.

docker start b69c772c73fcor docker start b69

If we want, we can stop a container like so:

docker stop serdem

We could also refer to the container by its container ID instead.

docker stop b69c772c73fcordocker stop b69

If we run the docker ps command without the -a flag, we’ll see our running container.

Attaching to a container

Our container will restart with the same options we’d specified when we launched it with the docker run command. So there is an interactive session waiting on our running container. We can reattach to that session using the docker attach command. So, we’ll be brought back to our container’s Bash prompt.

docker attach serdem

We could also do it via ID.

docker attach b69c772c73fc or docker attach b69

Finding out more about our container

In addition to the information we retrieved about our container using the docker ps command, we can get a whole lot more information using the docker inspect command.

The docker inspect command will interrogate our container and return its configuration information, including names, commands, networking configuration, and a wide variety of other useful data. We can also selectively query the inspect results hash using the -f or --format flag. This will return the running state of the container, which in our case is false.

💡Tips: In addition to inspecting containers, you can see a bit more about how Docker works by exploring the /var/lib/docker directory. This directory holds your images, containers, and container configuration. You’ll find all your containers in the /var/lib/docker/containers directory.

Deleting a container

If we are finished with a container we must stop the container via docker stop command and then we can delete it using the docker rm command.

docker serdem ordocker rm b69c772c73fc or docker attach b69

On deletion, you should see the ID or name echoed back to you.

Or you can use detete container viadocker rm -f command without docker stop command.

References:

--

--

Sezgin Erdem, Ph.D.
Sezgin Erdem, Ph.D.

Written by Sezgin Erdem, Ph.D.

DevOps Engineer || 2 x Certified AWS SAA || Certified Kubernetes Administrator || Certified HashiCorp Terraform

No responses yet