Docker Intro

Docker Intro

·

4 min read

Photo by Juan Pablo Daniel on Unsplash

Hi there, I'm Artemijs , currently working as an SRE in Unblu Inc. (an awesome company btw, we're hiring) and I've been working with containers, cloud, and everything DevOps for more than 5 years now.

I've always been and still am a firm believer that knowledge is always > certifications but I have decided to dedicate this year to get certified in the topics I like to believe I'm a subject matter expert in, being Cloud, DevOps, and Microservices.

The first exam on the list is the Docker Certified Associate (DCA) exam, I've decided to document the knowledge necessary for this exam in a series of articles. The series will follow the official learning guide and will include include

  • Docker Orchestration, 25% of Exam
  • Docker Image Creation, 20% of Exam
  • Docker Installation and Configuration, 15% of Exam
  • Docker Networking, 15% of Exam
  • Docker Security, 15% of Exam

The goal is to have a clear, comprehensive guide/framework to prepare for the DCA

Let's begin with one of the 100s of thousands of Docker introduction articles and some of the core concepts that this awesome technology brings to the table.

Docker is a tool that makes the "worked on my machine" go away, it gives you the opportunity to put everything that's required for your application to run in a nifty, small package.

It's a virtualisation platform with a simple difference from Hypervisors like Hyper-V or VirtualBox - you don't need an additional guest operating system on your host to run the application, Docker will leverage something called namespaces and Cgroups ( will touch them in the following articles ) to isolate itself in a so-called container on the host and reuse its resources to run.

image.png

Installation

Operating System specific installation instructions can be found on the official Docker Page

After installation, by default, you will not be able to run docker commands, to add necessary access rights to your user run the following command

sudo usermod -aG docker $(whoami)

Running a Container

running a container is as easy as running docker run $IMAGE_NAME, let's try it out using alpine image

docker run -d alpine

-d here will run the container in detached mode, after the command is executed you should receive a docker container id

❯ docker run -d alpine
2e9f3d36b011e491db836bcb233850562fc29574959a973ac5637b7ef9e478bb

and we can view currently running docker containers using docker ps command

❯ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

Nothing is there, what gives? The thing is that alpine is an image that is not doing anything by default. It's commonly used to build on top of ( arguably a more secure and mainstream way of building docker images now is using so-called distroless images but that's a topic for another article ). What Alpine image does in our case is that it starts, checks whether it should do anything ( and it doesn't ) and exits, we can check that that's actually the case using the docker ps -a command, it will show us all of containers that are present, even the ones that are either stopped or exited.

❯ docker ps -a
CONTAINER ID   IMAGE     COMMAND     CREATED         STATUS                     PORTS     NAMES
2e9f3d36b011   alpine    "/bin/sh"   7 seconds ago   Exited (0) 6 seconds ago             condescending_pike

we can see that CONTAINER ID is the same as the first 12 symbols of the string that was returned by the docker run -d command.

Now let's make the container actually do something, since it behaved badly the first time, let's tell it to inform us that it is running every 10 seconds.

docker run -d alpine /bin/sh -c 'while true;do date +"%Y-%m-%d %T running"; sleep 10s;done'

Now when you run the docker ps command, you'll actually see that our container is running. You can check it's logs using docker logs $CONTAINER_ID command

❯ docker run -d alpine /bin/sh -c 'while true;do date +"%Y-%m-%d %T running"; sleep 10s;done'
bbe2a250fdaa3f18872bed8f30c37d512bbab1e5201613f875ca9c36491afc72
❯ docker logs bb
2021-08-05 15:47:17 running
2021-08-05 15:47:27 running
2021-08-05 15:47:37 running
2021-08-05 15:47:47 running

According to the container ID it's time to say bb and continue on to the next article.

docker stop $CONTAINER_ID will stop the container and docker rm $CONTAINER_ID will remove it.

❯ docker stop bb
bb
❯ docker rm bb
bb
❯ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

There's also my favourite command that will get rid of all containers, whether they are running or not - docker rm $(docker ps -aq) -f