Reading Time: 5 mins.
Docker is an OS-level Virtualization designed for developers and sysadmins to build, share and run applications with containers. This use of containers for easy deployment of applications is called Containerization. Basically, Docker offered a set of PaaS (Platform as a Service) works using its container-based technology to address the limitations of Virtualization that had been experienced over the years now. Docker is easy-to-install software in most of the well-known environments such as Ubuntu, Windows, Mac OS, etc. Haven’t installed Docker on your system yet, visit our article on How to Install Docker on Ubuntu 18.04
Step 1: Let’s start with the simple Docker images
It’s by using Docker Images, the Docker containers are built and the images are usually pulled from the Docker Hub (which offer both private and public registries). If you need, you can save (push) the Docker images in your configured registry. Since everyone can use push images in the Docker Hub, you will find all the necessary images for most of your applications and Linux distributions.
To verify your Docker Hub access and download image options, enter the following command:
docker run hello-world
The output for successful running status of Docker
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 0e03bdcc26d7: Pull complete Digest: sha256:49a1c8800c94df04e9658809b006fd8a686cab8028d33cfba2cc049724254202 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: The Docker client contacted the Docker daemon. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
The failure to locally find the required image led Docker to download the image from its default repository, Docker Hub. The downloaded image creates a Docker container and an application residing in the container will be executed so as to display the message.
By entering, the docker command followed by the subcommand, search, you can look out for the images in the Docker Hub.
Step 2: Lets work with Nginx image
Docker search nginx
For search we have used Nginx image as an example here, Based on the search string, a list of images will be displayed by the Docker Hub via crawling the script. The example of one such output can be seen below.
NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 13524 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1846 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 780 [OK] linuxserver/nginx An Nginx container, brought to you by LinuxS… 123 bitnami/nginx Bitnami nginx Docker Image 87 [OK] tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 85 [OK] jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 73 alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 71 [OK] nginxdemos/hello NGINX webserver that serves a simple page co… 57 [OK] jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 53 [OK] nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 37 privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 31 [OK] schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 18 [OK] nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 16 nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 14 centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13 raulr/nginx-wordpress Nginx front-end for the official wordpress:f… 13 [OK] centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 13 mailu/nginx Mailu nginx frontend 7 [OK] sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 7 [OK] bitwarden/nginx The Bitwarden nginx web server acting as a r… 6 bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 6 [OK] flashspys/nginx-static Super Lightweight Nginx Image 6 [OK] wodby/nginx Generic nginx 1 [OK] ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 1 [OK]
Once you have identified the image you would like to use, use the pull subcommand to download it. For example, to download the official Nginx image, type:
docker pull nginx
Using default tag: latest latest: Pulling from library/nginx 6ec8c9369e08: Pull complete d3cb09a117e5: Pull complete 7ef2f1459687: Pull complete e4d1bf8c9482: Pull complete 795301d236d7: Pull complete Digest: sha256:0e188877aa60537d1a1c6484b8c3929cfe09988145327ee47e8e91ddf6f76f5c Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest
After an image has been downloaded, you can then run a container using the downloaded image with the run subcommand. As you saw with the hello-world example, if an image has not been downloaded when docker is executed with the run subcommand, the Docker client will first download the image, then run a container using it.
To view the downloaded images, type:
docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 8cf1bfb43ff5 7 days ago 132MB hello-world latest bf756fb1ae65 6 months ago 13.3kB
You can modify and generate new images using the docker files with the help of docker-compose, after that either those images will be uploaded to Docker Hub or it can be directly run as docker containers. Now, we’ll see how to run an Nginx image as a Docker container in the following section.
Step 3: Running the Docker Container
In the above section, we have pulled an image called nginx. Now, we are going to execute that image as a container.
syntax docker container run -p 8080:80 [image_name]
Let’s run nginx image,
docker container run -p 8080:80 nginx
After running the Nginx image as a container ,to get the shell access type the below command:
docker run -it nginx bash
The change in the command prompt as shown in the below output confirms that you are working inside the container.
Note: Either a container ID or name is sufficient to identify and remove that container. You need not necessarily get into the container to perform the removal task, just knowing the container ID and name will do the task.
Now that you are already inside the container and performing as a root user, you need not to add sudo to the command. Let’s try updating the package database inside the container without any prefix to the command.
root@99f31acc3a24:/# apt-get update
Next, we will try installing an application. For example, PHP.
root@99f31acc3a24:/# apt-get install php
To confirm whether PHP has been successfully installed, type:
root@99f31acc3a24:/# php -v
You will get to see the version number in your terminal.
PHP 7.3.19-1~deb10u1 (cli) (built: Jul 5 2020 06:46:45) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.19, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.3.19-1~deb10u1, Copyright (c) 1999-2018, by Zend Technologies
Note: It is to your remembrance that any changes you make within the container will be applicable only to that container.
This Docker configuration article would have started with the basic procedure on how to verify your access to Docker Hub and thereby to search images using the Docker commands followed by searching the Nginx official image for demonstration purpose. To get further information about the docker configuration, visit, https://docs.docker.com/get-started/overview. Eventually, you might have learned how to download that image using the Docker pull command, and how to successfully run a container using that downloaded image.