Docker Series - Part 1

Basics of Docker and manipulating docker containers & Images

Published: 25 March 2020

docker containers devops images docker hub 

What is Docker


In its simplest form, Docker is a way to run a mini computer on your local or a remote machine in the cloud. This mini computer is created from a set of instructions like installing certain binaries, running programs or pieces of software. By default, it is isolated from the machine it is running on.

This ensures anyone who installs and runs the same mini computer will have same dependencies and same system requirements, no room for conflict.

For example, if you are running NodeJS version 12 on your machine but the project you are working on requires version 8, you will have to do additional installing and management of node versions. This gets even more time consuming when you have multiple dependencies and their respective versions together with complex installation processes.

Docker allows us to set in stone the requirements and dependencies we need for a given project and it takes care of installing and running those as needed.

The overall generalised process is as follows:

  • A Dockerfile is a pre-defined set of instructions which are put together for a given project, to tell Docker what to install and how to run a specific project.
  • It can then be deployed with the project for others to download and use.
  • A docker image is created every time Docker file is run. There are many images which exist on Docker Hub that allow us to use them to enhance our own custom Docker files.
  • Once dockerfile is downloaded on to a machine, all the instructions inside the image are run, all dependencies installed and finally a container (mini computer) is formed to encapsulate all that functionality.

A Docker container (mini computer) can be thought of as a dedicated space on a machine's Kernel which is used by Docker to run its own processes for a specific project.

Commands


Let's look at some basic but essential commands used in manipulation of Docker containers and images.

docker run <image-name>

docker run hello-world

This command will grab an image from Docker Hub and install it on the local machine and then run any default commands specified by that image.

So, let's say we have an image for our NodeJS project and by default once the image is installed, we baked a node index.js command to run.

That is exactly what Docker will do for any user who downloads that image - start the nodejs server.

We can override that command though:

docker run <imageName> <commandName>

docker run busybox echo I love Docker

docker run busybox ls

Whatever default command was suppose to run for busybox image will be over-ridden by echo I love Docker or ls or anything else we would like.

NOTE: echo or ls commands will only run if that specific container environment has those commands available by default. It all depends on what image was used to create the container.

docker run in depth


the run command can be broken up into create and start commands:

docker run <imageName> === docker create <imageName> + docker start <containerID>

By default, docker run will first create an image and then also start the container made from that image. It will also show output for any commands that are run by default inside the container.

docker create will only download the image and print a containerID into our terminal

docker start will simply start the container without showing any output from any defaults commands that might run inside the container.

To see the same output as docker run, we need to attach -a flag like so:

docker start -a <containerID>

NOTE: We can also use the start command to restart a stopped container by simply passing in the container's ID. However, can't override the default command now.

Other Userful commands


If we want to see the list of running containers on our machine, we can run:

docker ps

For a full list of all containers regardless of their status:

docker ps --all

It will show a table like so:

CONTAINER ID    IMAGE         COMMAND       CREATED      STATUS       PORTS       NAMES
af1b3c50a414   busybox        "sh"       6 seconds ago    Exited (0) 4 seconds ago

To get logs from a container:

docker logs <containerID>

To stop a running container, there are two ways:

  • Stop - allows the container to do some cleanup work and then close down
  • Kill - kills the container straight away
docker stop <containerID>
docker kill <containerID>

NOTE: if the stop command isn't able to close down the container in 10seconds, Docker will issue a kill command automatically.

To delete a container which isn't running:

docker rm <containerID>

docker rm af1b3c50a414

If you want to delete all containers which aren't running at once:

docker system prune

Issue commands inside a running container


So far, we have seen how to handle containers but not the processes running inside them. Being able to execute commands inside a running container become useful when we have additional things we need to install or check.

This can be achieved like so:

docker exec -it <containerID> <commandName>
docker exec -it af1b3c50a414 ls

-it flag allows us to get access inside our container so we can write keep issuing commands. If we leave out this flag, docker will run the command and then bring us back to our local machine's terminal.

NOTE: it flag is made up of -i flag and -t flag. -i flag allows us to issue commands into the container whereas -t simply makes any output we get back to be nicely formatted.

For debugging purposes, we can also get full terminal access to our container like so:

docker exec -it <containerID> sh
OR
docker exec -it <containerID> zsh

Docker get started

Docker run reference

Docker exec reference