Introduction to Docker - Day 1

This is a basic introduction to Docker for Mac. Before we starts to understand Docker, we must know about virtual machine. Readers for this blog is familiar with VirtualBox and Parallel. Recently, we are testing various new Linux distribution via virtual machine. So far we have installed more than 10 different flavor of Linux distribution. It takes up more than 80GB of hard disk space.  Since, a lot of them are different flavor of Debian/Ubuntu distribution, there are a lot of repetitive installation.

We are not going to show the standard layered chart. If you preferred the official layered chart, please check out the tutorial from docker. The chart below is an example of what happened to by Macbook Pro.

Linux AppLinux AppLinux AppLinux AppLinux AppLinux App
AntergosFreeBSDManjaroUbuntuElementaryMX Linux
Mac OS X
Macbook Pro Hardware

If we use Docker, then we can dispense with installing different flavor of Linux. We can still have different flavor of Linux in a docker but the foot print is very much smaller and there will be no repetition of base code.

Linux AppLinux AppLinux AppLinux AppLinux AppLinux App
AppAppDocker with Linux Base
Mac OS X
Macbook Pro Hardware

So instead of virtual machine or virtual OS, we have virtual app. This is the basic concept of Docker.

Docker is useful if we are to test an app in a controlled environment. Another use case is to implement development environment. For example, we need to install Octave and Python 3 in our laptop for machine learning. In addition, we also need to test out Tensor-flow a machine learning framework from Google. We can install Python 3 and Octave for Mac. We can also install Tensor-flow. However, all these development environment will be in the same machine and each component may have impact on each other. So it is best if we can isolate each development environment. This is where Docker comes in.

Install Docker

Now, lets dive into Docker. First we need to install Docker. To install Docker for Mac please refer to the nice tutorial from Docker. If you want install Docker in other system please refer to the main page of Docker documentation.

Docker for Mac

To explore the details on Docker for Mac, please refer to this site: . This site contains everything about Docker for Mac. However, we will go through them on a gradual progressing pattern.

Basic Command for Docker

Now that you have Docker installed in your system. You have a little whale on your top menu bar. Lets try some basic command. Docker uses terminal heavily, please fire up your terminal.

Check Docker Version

Use the following command to check the version of various component of Docker:

# Check the version of Docker
docker --version
# Check the version of Docker Compose
docker-compose --version
# Check the version of Docker Machine
docker-machine --version
# Check if current Docker is running experimental server
docker version -f {{.Server.Experimental}}

The result should be something similar as below:

Test Docker

To test if your Docker is running properly use the command below:

docker run hello-world

You should received the following message

Now lets examine the command line:
  • The first word docker tell bash shell that this is a docker command.
  • The second command is to instruct Docker to run something.
  • The third word hello-world is the name of a docker image.

A docker image is an image of an app.  When we execute the command, docker will check if there is any image in your local repositories that are named hello-world. If docker can't find any, it will pull the image from the main docker server and run it. If we run the same command again, you will received similar message:

Please note that the message unable to find image 'hello-world:latest' locally disappear. This is because we have previously downloaded the image. Docker just run the same image again.

Docker Image and Container

So lets get acquainted with 2 terms, Docker Image and Docker Container. A Docker Image contains an app. It is similar to the dmg file we download for Mac app. Usually, we run an image just like running an app.  Although technically, we can include more than one app in an image, but this defeat the purpose of isolation. Docker recommended that we put one app per image.

When we run an app like hello-world, it is actually running in a Docker Container. A Docker Container is an isolating environment or mini OS where an image can be run on.  So image is like an installation package and the container is just like a mini runtime environment. Once we exit the app, then the container is consider spent.

Checking Container

Now lets run the following command to check our container:

docker container ls

As you can see, there is no container. Actually, there is no active container. The command ls only list down any active container we have.

To check all containers including spent/used containers:

docker container ls -a

Now the system show 2 used container as below:

Every time we run an image it will create a new container.

As you can see from above, both container has the same image name. The only way to differentiate both containers are container ID and container names.

When we run the image, we do not give the container any name. By default, the system will give each container a different random name if we fail to provide a name while running it.

Now, let's remove the first container. Since we can identify them using container ID, we will remove the first container using container ID. Use the following command:

docker rm db65a18fc87a

After removing the container, when we list the container. Only one container left as shown below:

Next, we remove the remaining container using container name.

docker rm romantic_banach

Now there is no container left as shown below:

Now lets check out any images we stored locally. Use the command:

docker images

We have one image stored locally. Similarly, we can identify the image using image name or image ID.

To remove the local image, use the command

docker image rm hello-world
# Alternative command
docker rmi hello-world

There will be no image left.


Lets recap what we have learn:
  1. A docker image contains the code of an application. We run the image in order to execute the application.
  2. A docker container is an isolating environment which a docker image runs on.
  3. To run an image for testing use command: docker run hello-world
  4. To list active container: docker container ls
  5. To list all container including inactive container: docker container ls -a
  6. To remove a container: docker rm <container_id/name>
  7. To list all locally stored image: docker images
  8. To remove a local image: docker image rm <image_name/id>

We will stop for now. In the next post, we will look into images and pulling images.



Popular posts from this blog

Revive Old Mac Mini (2009) with Linux

Configure Unattended Upgrades on Raspberry Pi

Install and Configure RealVNC in Linux Ubuntu 18.04 LTS