Written by 11:00 Containers, Docker, Tools & technologies

Introduction to Docker and Docker Toolbox

The article gives some knowledge about getting started and utilizing Docker containers. The focus is all about understanding  Docker and its basics. Once you start grasping the basics and learn to use them, you will easily understand how to use Docker with other products.

In this article, we are going to review the following points:

  1. Introduction to Docker
  2. Pre-requisites to be done for the installation
  3. Installation of Docker toolbox on Windows
  4. Basics on getting started with Docker
  5. Main docker commands to be used
  6. Methods to build Ubuntu images from the docker hub repository
  7. And much more…

Introduction

Docker is a portable, open platform, lightweight and simple to configure, build and split an operating system into small containers that can be used to deploy applications in isolated and secure containers.

These containers are designed to be portable so they can be shipped from one place to anotherю Thus, Docker is a tool that passes these containers to and from your systems. A container is a self-contained sealed unit of the required software. It has the combination of everything needed to run that code, as well as contains all the dependencies that your system needs bundled up in that container. Moreover, it even includes an operating system to run your code. So, it takes all the services that make up an operating system such as network configuration, storage, libraries, code, and IPC (Inter Process Communication) protocols.

Docker is built into a client-server application architecture. The Docker client initiates a request to a Docker daemon (Server). Both Docker client and Docker server can be run on the same or remote system.

Docker has a program, which builds containers from a code. It takes your code along with its dependencies, bundles it up, and seals into a container.

The tool is a good choice for DevOps pipeline to speed up the consistent delivery of applications. It offers a platform to streamline the development lifecycle by providing only required applications and services. Containers are great to be used for continuous integration and continuous delivery (CI/CD) workflows.

Install Docker Toolbox on Windows

For Windows 7 (or higher) operating systems, Docker provides Docker Toolbox, an installer with a platform to configure and launch a Docker environment. Additionally, it can set up and start a Docker environment on older Mac and Windows systems. Docker Toolbox installs a program known as Docker that helps manage Docker Virtual Machines and others components, including the VirtualBox, Docker Machine programs etc. Docker Toolbox also installs Docker Machine, which has several useful commands for managing the VMs (Virtual Machines).

The components of Docker Toolbox are as follows:

  1. Docker Machine
  2. Docker Engine
  3. Docker Compose
  4. Kinematic
  5. Docker Quickstart Terminal App
  6. Oracle VirtualBox

Prerequisites

Windows Operating System must be Windows OS 7 or higher – 64 bit with the enabled virtualization. You can check the virtualization technology in two different ways:

  • for Windows 8/8.1/10
    You can confirm the virtualization status in Task Manager -> Performance -> CPU tab.

Install Docker Toolbox

Installing Docker Toolbox on Windows is pretty straightforward. To download Docker Toolbox, select the Toolbox for windows button.

Locate the installer and follow the installation steps.

Double-click the executable file and click Next to continue.

Select a folder for the installation. Click Next.

Select the components to be installed and click Next.

Continue with the default additional tasks and click Next.

In the Setup – Docker Toolbox window, click Install to set up Docker toolbox.

Click Finish to exit the setup process. This indicates the successful installation of all the components.

That’s all…

As you can see, it is really simple to install the Docker toolbox.

Th next step is to launch the Docker Quickstart Terminal.

Getting Started

Let’s walk through the Docker commands. The Docker Flow is the fundamental concept of Docker. It all starts with an image, a file that makes up the operating system.

Open Docker Quickstart Terminal and run Docker commands.

$docker run hello-world

The docker run command will download the “Hello world” container. Take a look at the Docker images using the Docker images command.

Now, execute the docker run command to invoke a Linux distro Ubuntu from a library. We can see the docker run command pulling the image from the library as it is unable to find an image locally.

$docker run –it  ubuntu:latest bash

The components of the command are as follows:

  1. it: Interactive terminal
  2. Ubuntu is the name of the image
  3. Latest is the tag which looks up to the latest repo from the library
  4. Bash is the shell to run with this image by default

To check what the container Linux distros is, use the cat command. To exit, type exit or press CTRL + D.

#cat /etc/*release

To list all the docker containers, type the following command.

$docker ps –a

a lists all the containers

Now, create a simple text file. The file size can be listed using the docker ps –s command.

$docker ps –s

-s displays the total file size.

For the demo, I have created another file named docker2. Invoke the docker ps –s command.

#docker ps -s

The size column displays an aggregation of the total files size.

The commands such as docker run and docker commit are complementary to each other. The docker run command takes images to containers while the docker commit command takes containers back to new images. Also, the docker run command creates a container from the image library.

Let’s verify the size of the Ubuntu container that should be 112 MB.

Next, build a new image from the Ubuntu container. As you can see, a few files have already been created on the container. Thus, we are going to install the wget utility on the Ubuntu container.

The container id is 6cb22da152ef. Let us create a new image with the container id.

#apt-get update

#apt-get install wget

The docker commit command requires the container ID as its parameter.

$docker commit 6cb22da152ef

The output is an image ID that is different from the container ID. Thus, the new image is created from the original Ubuntu container.

The next step is to use the tag command to give a more meaningful name to the image.

To do this, copy the image ID, everything after the colon, and paste it as an argument to the docker tag command.

$ docker tag 70be1bb02ec5c24f8b1f9609bf93e772297f0c8c90f251297b100b04d25d7f2e dockerimage

We received a new image called a docker image. Also, we can notice that the image size changes.

$docker images

Next, run the docker image to see the files and use the wget utility to download a docker file.

$docker run -it dockerimage bash

We receive the docker.txt and docker2 files using the ls command.

#ls

Using the wget utility successfully downloaded the docker Ubuntu CE deb package.

#wget https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/docker-ce_18.03.0~ce-0~ubuntu_amd64.deb

We can start and stop the docker container using the docker start and stop commands.

$docker stop d29c09a07862
$docker ps –a 
$docker start d29c09a07862

The most important docker command is docker run. It takes a container out of the image and runs a process in that container. This is the main process of the container. When it ends, the container creation is successfully done.

Let’s see another command where lots of people use the container but don’t want to keep the container afterward. This can be done using the –rm options.

$ docker run --rm  -it ubuntu bash 
$docker ps –a

As we can see, there is a container entry 25e9e27cb114 in the docker process command. On exiting the console, the container automatically exits.

This section describes the steps to build images using docker files.

For the demo, perform the following steps:

  • Pull the CentOS image
  • Use the Vi text editor and type the text from the below screenshot
  • Build the test image received from mydockerfile
FROM centos:latest
RUN yum -y install httpd; yum clean all; systemctl enable httpd.service
CMD ["/usr/sbin/init"]

$docker build -t test - < mydockerfile

Run the following command to receive the image.

$docker run –it test bash

I would like to note that when I was trying to install the image to SQL Server docker on the docker container, the status column was the exited (1). Later when I noticed that it was not running, I could check what went wrong with docker logs.

$docker logs <container ID>

For example, we execute the command below:

$docker logs 6cf4db041167

Let’s prepare a small script to remove the containers using bash scripting.

The container ID column is selected and passes an argument to it to remove the container.

$docker ps -a | grep "centos"|awk '{print $1}' |xargs docker rm

Removing images using the pattern match

To list all the images, use the docker image –a command. If you want to find the images that match the specific pattern, use grep in combination with docker image. Now, select the IMAGE ID column and pass it as an argument to the docker rmi command.

$docker images | grep "latest"|awk '{print $3}'|xargs docker rmi image

Additionally, you can forcefully remove the image using the –f parameter.

$docker images | grep "latest"|awk '{print $3}'|xargs docker rmi –f image

Summary

This article is an effort to quickly give a glimpse of Docker containers. With the advent of Docker containers, developers can focus more on writing code without thinking about the underlying infrastructure which their code will be executed on. The Docker container-based platform facilitates the developers to test their workloads on local machines, remote servers, virtual machines or on the cloud. Also, the Docker images can be created and customized using a Docker file that, in turn, allows packaging only required components.

When speaking about the pros, I would like to note that the evolution of containers would speed up the entire SDLC (Software Development Life Cycle) process. It’s very helpful in testing and adopting agile practices due to its portability and lightweight nature.

That’s all for now…

Stay tuned for more updates! The Part 2 – Docker and SQL Server on Linux.

References

https://support.cmfirstgroup.com/hc/en-us/articles/115002363383-How-to-Create-and-Run-MS-SQL-Server-Docker-Container

https://docs.docker.com/toolbox/overview/

https://docs.docker.com/engine/reference/commandline/docker/

Tags: , Last modified: October 21, 2021
Close