Docker Technology, Architecture, Components and Essential Commands

Docker is a platform or command-line tool for creating, delivering, and running applications consistently across several environments. So, if an application works in a development environment, it should work similarly in the test, deployment and other environments.

It is easy to package applications on any machine and run them with docker. Docker is remarkable software management because it allows more and more apps to execute on the same infrastructure while it also bundles and deploys the apps across the board.

Even if container ideology in software development existed before Docker, the invention of Docker.

Docker Development Backbone

  • Build: in Docker, build means compilation or bundling of Artefacts/source code/libraries/frameworks/dependencies in the form of a docker image to make the docker image complete within itself.

  • Ship: Software builds are deployed from one environment to another such as production, staging, or deployment, or from a local machine to a remote public or private repository.

  • Run: this is the actual execution of docker images in containers.

Docker Client versus Server

Docker Client: docker client is a command line interface which interacts with the docker engine. The CLI communicates with the server.

Docker Server: this is a runtime environment which executes the docker containers.

Docker vs Virtual Machines

Both docker and virtual machines are virtualization technologies, but the piece of the operating system they virtualize is what makes them vary.

  • Docker

Docker is more flexible and provides a unique and bounded environment in which an application can run by sharing resources between the application layer of the operating system in which the container is running and the kernel of the host operating system.

Docker organises code and dependencies at the application stack, and because they work under virtualized layers within the operating system, different Containers can run on the same machine and access the OS, hardware, and kernel resources. Containers are lightweight and take less memory from the operating system because they only utilize the application layer.

With docker containers, it is possible to run different applications in isolation, start new containers, and set OS dependencies for each container.

  • Virtual machines:

A virtual machine is an abstract concept of a physical machine that works with the help of a hypervisor or bare metal to enable it to run.

Virtual machines maintain the application layer as well as their operating system, and each VM has a complete copy of an operating system, the application, as well as all essential binaries and libraries these features consume a large amount of storage space, causing virtual machines to boot slowly when hardware resources are not well enough.

Virtual machines do not make the best use of available storage space and with a higher likelihood of improper isolation of applications, which can lead to configuration conflicts, security concerns, and excessive resource consumption.

Concepts Surrounding Docker The Technology

  1. Docker image

A docker image is a framework for development. This development environment could consist of a server, an application, a tool, etc. Docker images are similar to templates in that they may be reproduced in numerous forms. As a result, docker images contain the application and everything it needs to run, including the source code, dependencies, binaries, and so on. This image is the application that will be launched as a container in Docker.

  1. Dockerfile

A dockerfile is a file format with instructions for packaging software artefacts into docker images. The dockerfile creates an image from an application. Docker identifies the application and runs it in the form of a container by using the packaged image and the container's file system, which is provided by an image. This application, once built in a local repository, may be pushed up into a central or remote local repository - docker hub - and then deployed to a test/production environment.

  1. Docker Containers

A container is a running copy of a docker image. Images are stand-alone concepts that can only be executed in the form of a container. Multiple containers can run a single docker image.

  1. Docker Engine

Docker images contain the nitty-gritty an application needs to be able to run but it does not contain the operating system suitable for the docker image. Hence, the docker engine is responsible for providing the operating system and operating system packages while it executes the image.

Docker engine is made up of three components - The server, within the server architecture, exists the container runtime, the volumes, the networking and builds for images. An API for interaction between the docker client and the server, and the Command Line Interface (CLI), the CLI is the docker client which takes user commands, processes within the docker engine and sends it to the server.

  1. Docker Registry

Docker registry is a remote repository where docker images are stored, and images built in the local machines are “pushed” up to this repository and “pulled” when needed.

  1. DOCKER VOLUMES

Docker volumes enable us to access contents, such as files and directories, between hosts and containers and also share contents among containers. In a host machine where an nginx application is running, through docker volumes, it is achievable to have a file on the host appear in the running container, and it will work by adding the file to the container's actual volume.

Docker Run Essentials

  1. Running containers in detached mode

This is to run a container in the background. It accepts and displays no input or output through the display shell. The best approach is to run containers in detached mode. This ensures that the apps running within your container can continue to function smoothly without any interruptions. To run a container in detached mode, -d is appended in the docker run command. For instance $ docker run -d nginx

  1. Assigning port numbers to containers

Assigning port numbers to containers together with the transmission Control Protocol allows you to view the running container from a local browser within your machine. Given that the container instance is an nginx container, Nginx is a web browser and reverse proxy. In docker, it is an application with a default web page, we may assign a port number or multiple port numbers to the nginx container we want to run and use the localhost:<port number> to display the nginx default web page.

  1. Running containers in interactive mode

Running containers in interactive mode is particularly useful when running an application with complex configurations to validate the setup. Also, running an interactive container on bash is advantageous to use the copy of the file system in the operating system within a running container. For instance, To run an Ubuntu container in interactive mode to be able to use an actual Ubuntu OS in docker, $ docker run -it ubuntu /bin/bash. This would give you an interactive operating system and

  1. Running a Container From an Image

Let's say we created a docker image locally within our server or pulled an nginx docker image from the docker registry,

$ docker pull nginx

Docker assigns a default tag "latest" to images pulled from a remote registry without a tag.

To run a container from an image, we use the command

$ docker run nginx: latest

Where $ docker run is a command to set the container running, nginx is the docker image and lastest is the tag.

Unlike most terminal commands that show the process of a running command, the terminal. The terminal hangs on that command because the running container process has started. If you open a new terminal, you can check the running container with $ docker ps -a

To show a list of all the running containers and those that have stopped running and be sure to see the Nginx container still up with information about the container ID, the image of the running container and tag, the time the such container was created, the current status and how long it has been up or exited, the port which the container image is listening to and the name of the container.

  1. To stop the container from running,

$ docker kill <docker container id>

Or simply use control ^C from your keyboard. Check the status again $ docker ps -a

  1. To also confirm there are no running containers

$ docker container ls

  1. To assign a single port,

$ docker run -d -p 5000:80 nginx

  1. To assign multiple ports

$ docker run -d -p 5000:80 -p 3000:80 nginx

Where $ docker run is the command, -d to run the container in a detached mode, -p 5000:80 is to specify the protocol to use, nginx is the container image and its tag.

Managing containers

  • To stop a container

$ docker stop <container id or name>

  • To start a container that was initially stopped,

$ docker start <container id or name>

  • To delete a container

$ docker rm <container id or name>

  • Delete all the docker containers quietly in a single command.

$ docker rm $(docker ps -aq)

Note: You cannot remove a running container or use this last single command to remove the list of containers when there is a running container within it. The best practice is to stop the running container before removal or parse the -f flag to force the removal. $ docker rm -f $(docker ps -aq) or $ docker rm -f <container id or name>

  • To save a container image in a .tar format

$ docker save <image name> -o <new image name>.tar

  • To back up a docker image

$ docker save <image name> > <backup name>.tar

  • To check out the layers in a docker image

$ docker image history <image name>

  • Naming a container

By default, docker assigns a random name to containers when we fail to assign a name to our containers upon creation. The default name appears in the last column when we list our containers with $ docker ps

To give our container a name before they start running, we have to parse the name tag in the command. For instance, using the Nginx case we have been working on within the article,

$ docker run --name web-server -d -p 5000:80 -p 3000:80 nginx:latest

We have successfully given our container a name and this makes it easier to use the name instead of the ID and also helps in container identification.

PROJECT ONE

Create a volume between the host and the container.

  1. In the host machine, I have created a folder with the name docker-volume. Inside the folder, I have Html, CSS and other files for a static one-page website whose source code was downloaded from https://startbootstrap.com/theme/coming-soon saved in there. My website file which is in the /home/diogo/docker-volume will be shared with the file in the container pointing at /usr/share/nginx/html. In this way, the docker container will serve the website on my host machine.

  2. Start a container and mount the folder into the container. From the folder where the website files were created, I got the container running and mounted a volume to mirror the current directory where the website files are through this command.

$ docker run - -name my-website -v $(pwd):/usr/share/nginx/html:ro -d -p 7000:80 nginx:latest

Where $ docker run is the docker command for running containers, - -name my-website is the name I gave the docker container, -v $(pwd):/usr/share/nginx/html:ro is for volume showing my current working directory where the web content files are, pointing to the folder where the container is mounted on and set to read-only, since it is a static website, visitors can only view the webpage but can not interact with it and also, more files cannot be added to the container folder. -d is to run the container in a detached mode, -p 7000:80 is the port and the protocol, nginx:latest is the docker image and its default tag.

PROJECT 2

Create volume between containers

It is possible to share the content of a folder or file between two containers while configuring them. In project one, a second container will be launched to duplicate the folder from the previous operating container, my-website. This is accomplished by using the additional command $ --volumes-from to identify the container from which the new container will share the folder.

$ docker run - -name my-second-website --volumes-from my-websit -d -p 7700:80 nginx:latest