Introduction 

In this article, My experience with Docker, I touched on what Docker is and how it works. I didn’t get to actually building a Docker container. In this article, I am going to look at how one can get started in using Docker based on the research I did and also on my personal experience.

Installation

First thing first, you need to install the Docker engine. In order to install Docker CE, you need the 64-bit version of either Ubuntu Artful 17.10 (Docker CE 17.11 Edger and higher only) or Xenial 16.04 (LTS) or Trusty 14.04 (LTS). Here is how you install Docker CE:

  • sudo apt-get update :> updates the apt package index
  • sudo apt-get install apt-transport-https ca-certificates curl software-properties-common -y :> install packages to allow apt to use a repository over HTTPS
  • curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add – :> add Docker’s official GPG key
  • sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable” :> this sets up a stable repository.
  • sudo apt-get update :> updates the apt package index
  • NOTE: in order to install a specific version of Docker, run the following command:
    • apt-cache madison docker-ce
  • sudo apt-get install docker-ce=17.12.1~ce-0~ubuntu -y :> install Docker version 17.2.1. NOTE: this will vary depending on the version you would have selected from the above command.
  • sudo docker run hello-world :> confirm that Docker installation was successful.

Docker concepts

There following are concepts one needs to know first before getting hands dirty with Docker:
Note: for more detailed explanation click here.

Docker Engine  is a client-server application with the following major components:

  • A server which is a type of long-running program called a daemon process.
  • A REST API which specifies interfaces that programs can use to talk to the daemon and instruct it what to do.
  • A command line interface (CLI) client -> the docker command.

Docker Daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks and volumes. It can also communicate with other daemons to manage Docker services.

Docker client (docker) is the means by which many Docker users interacts with Docker. Commands such as docker run, when executed, they are sent to dockerd by the client so that the daemon can carry out the commands.

Docker image is a read-only template with instructions for creating a Docker container.

Docker container is a runnable instance of an image.

Docker registry stores Docker images.

Getting started

Now that Docker is installed and have verified that all is good with the installation, to get started with creating a container, first thing one needs to do is to create a file called Dockerfile. This file is used to create the container and it is in this file where instructions on what is needed for the container to be operational are set. For instance, here is how a simple file would look like:

# Use an official Ubuntu 14.04 runtime as a parent image
FROM ubuntu:14.04

# Run echo when the container starts
CMD echo “Hi there, just started my first container!!!”

The instructions here are simple, use Ubuntu 14.04 as a parent image and then output “Hi there, just started my first container!!!” when the container launches. Once the file has been created, the next step is to build an image by running the command below:

docker build -t my-first-image .

The command will build the image with a tag my-first-image which gives the image a friendlier name. Once the build is done, run any one of the following commands and they will list the images you have locally:

  • docker images
  • docker image ls

From the above, we can see that the image has been created. The next step is to run the image and create a container as follows:

docker run –name my-first-container my-first-image and it will output Hi there, just started my first container!!!

By just running the command above, a container has been created and to see the container, run the following command:

docker ps -a

By just running the command above, it will list containers and will provide details such as container id (auto generated by Docker client), image (the image used by the container), created (time the container was created), status (provides the status if the container is running or not), ports (provides ports that are exposed for the container) and name (container name).

So far we created a Dockerfile, the file that has  instructions on what is needed to get the container operational, ran the build command to create Docker image and finally ran the command to build Docker container.

The commands we ran so far are as a follows:

  • docker images or docker image ls :> lists docker images
  • docker build [options] :> builds the image from the Dockerfile
  • docker ps -a :> lists the containers

These are just a few commands, there are more just run docker and it will give the list commands you can run and more information on what you can do with the Docker client.

Conclusion

From this article we saw how one can install Docker, create a Docker image and a Docker container. Next article will look on how Docker can be part of continuous integration.

Source:
Docker overview