Intro to Docker — Part 1

Sandeep Baldawa
ITNEXT
Published in
6 min readNov 10, 2020

--

A quick intro to docker containers

This is a short & simple tutorial on Docker.

Goal

Learn Docker concepts quickly without getting lost in minute details. Provide tools one would need to create and run your first containerized application using Docker, enabling one to look for more by yourself when needed.

Prerequisites

Nothing as such. Just install Docker from here

Basic Concepts

A container is what we eventually want to run and host with the Docker engine.

From a conceptual point of view, a container runs inside the Docker host isolated from the other containers and even the host OS. It cannot see the other containers, physical storage, or get incoming connections unless you explicitly state that it can.

Here it is important to note that the Linux kernel is shared with the OS and all other containers. The host can see/manipulate everything in the container; processes, filesystem, etc.

PS:- Docker is way different than virtual machines, some details on the same here

Your typical Docker server would look like this — a host for many containers:

Any container that runs is created from an image. An image describes everything that is needed to create a container; it is a template for containers. You may create as many containers as needed from a single image.

Images are stored in a registry; registries are optimized for image management.

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image

We will look into all these in detail—three important concepts to latch onto, i.e., Dockerfile, Docker image & Docker container. Let’s move on.

Some Container concepts

Would highly recommend you try these command on your command-line terminal.

Q. What happens when we run

docker run hello-world
  1. Your command asks Docker to create and run a container based on the hello-world image.
  2. Since the hello-world image wasn’t already present on your disk, Docker downloaded it from a default registry, the Docker Hub.
  3. Docker created a container based on the hello-world image.
  4. The hello-world image states that, when started, it should output some text to the console, so this is the text you see as the container is running.
  5. The container stopped.

If you rerun the same command, you’ll see that all the above steps are being repeated except for step 2; this is because the image does not need to be downloaded as it is already present on your machine from the first time you ran the command. Try this yourself and validate if the above is true.

Q. Some of the basic commands for container management?

docker run — help

You may use the following commands for container management:

docker ps: lists the containers that are still running. Add the -a switch in order to see containers that have stopped

docker logs: retrieves the logs of a container, even when it has stopped

docker inspect: gets detailed information about a running or stopped container

docker stop: deletes a container that is still running

docker rm: deletes a container

Q. Post-Mortem Inspection?

Let’s explore some container management commands on your hello-world container.

Get all running containers

docker ps

Get all containers

docker ps -a

CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS                      PORTS                    NAMES33f2ca665467        hello-world                  "/hello"                 14 seconds ago      Exited (0) 12 seconds ago                            dazzling_morse

Get container logs

docker logs 33f2ca665467`

Get container info

docker inspect 33f2ca665467

Remove container

docker rm 33f2ca665467

Try logs and inspect command now; Docker does not know about this container anymore.

Q. Docker Run (short-lived containers)?

You can think of the docker run command as the equivalent of buying a new computer, executing some command on it, then throwing it away. Each time a container is created from an image, you get a new isolated and pristine environment to play with inside that container.

What you get inside the container depends on which image your container is based on. After the image name, you can pass the commands you want to execute inside the container.

docker run alpine printenv

We are basically asking for a container to be created using the alpine image and for the container to execute the printenv command, one of the binary programs packed in the alpine image.

To no surprise, the alpine image is downloaded to create the container since it was not already present on my disk.

PS:- It would be great to understand that Docker is a tool that allows you to get the equivalent of a disposable, single-time use computer. Once that’s clear, a whole new world opens for you. You come from a world where obtaining a new machine and configuring it required enough effort to justify keeping it, despite all the side effects that come with each subsequent use. In the container world, getting a brand-new environment is cheap enough to get many of them.

Q. How to clean/delete containers?

There are multiple ways, one such way using

docker container prune -f

This is the equivalent of running one docker rm command for each stopped container. The -f switch is an implicit confirmation to proceed and delete all stopped containers right away, instead of asking to confirm that operation.

Q. Docker Run (long-lived containers)?

We just saw how to run short-lived containers. They usually do some processing and display some output. However, there’s a widespread use for long-lived containers: server containers. Whether you want to host a web application, an API, or a database, you want a container that listens for incoming network connections and is potentially long-lived.

In short, a server container

  • is long-lived
  • listens for incoming network connections

Let’s try running the same.

docker run alpine ping www.docker.com

Let’s try pinging the same

docker run -d alpine ping www.docker.com

Let’s list all running containers

docker ps -a

Let’s now try getting logs; this is the best way to debug any container

docker logs <container_id>docker logs — since 10s

Listening for Incoming Network Connections

By default, a container runs in isolation, and as such, it doesn’t listen for incoming connections on the machine where it is running. You must explicitly open a port on the host machine and map it to a port on the container.

Exercise

Suppose we would like to run the NGINX web server, which listens for incoming HTTP requests on port 80 by default. If we simply run the server, our machine does not route incoming requests to it unless we use the -p switch on the docker run command.

Stop here and try this yourself before looking at the solution

Solution

The -p switch takes two parameters; the incoming port you want to open on the host machine and the port to which it should be mapped inside the container. For instance, here is how I state that I want my machine to listen for incoming connections on port 8085 and route them to port 80 inside a container that runs NGINX:

docker run -d -p 8085:80 nginx

Reach the service using

http://localhost:8085

Since that container is running in the background, its output isn’t displayed on my terminal. However, we can use the

docker logs <container_id>

Let’s now stop and remove the container.

docker stopdocker rm

Did you notice we now have the equivalent of a brand-new server essentially? This means we can install whatever we want on it and trash it whenever we like.

Containers allow us to use any software without polluting our machines. Usually, you would hesitate before trying a new piece of software on your machine since installing several dependencies that may interfere with existing software and be leftover should you change your mind and uninstall the main software. Thanks to containers, we can even try big pieces of server software without polluting my machine.

Q. How much time does it take to set up Jenkins?

Jenkins has dependencies like java and others, which might be painful to manage; Docker makes it very simple.

docker run -p 8088:8080 jenkinshttp://localhost:8088

We will continue this series to look into more concepts on Docker and its related orchestration.

--

--

whoami >> Slack, Prev — Springpath (Acquired by Cisco), VMware, Backend Engineer, Build & Release, Infra, Devops & Cybersecurity Enthusiast