Docker Shell Commands: Docker Containers

What is Docker?

Docker is a platform for developers and sysadmins to develop, ship, and run applications in containers. Containers are lightweight, portable, and self-sufficient, which makes them an efficient way to package and distribute software.

Docker enables developers to package an application and its dependencies in a container, allowing it to run consistently across different environments. This helps to eliminate "works on my machine" issues and makes it easier to move applications between development, testing, and production environments.

Docker uses a containerization approach, which is an operating-system-level virtualization method for running multiple isolated systems on a single host. Containers are lightweight because they share the host operating system kernel and only include the libraries and settings required for the application to run.

Docker has a public registry called Docker Hub, where developers can store and distribute their images. Docker also includes a command-line tool that allows users to manage containers, images, and networks.

Docker is widely used in DevOps and cloud computing, it enables developers to quickly and easily create, deploy, and run applications in a variety of different environments, such as on-premises servers, private and public clouds, and hybrid environments.

What is a Docker Hub?

Docker Hub is a Container Image Library or App Containerization to create, manage and deliver containerized application at ease. It is a service provider by Docker to pull and push container images.

Docker Hub Container Image Library is a open source projects, independent software vendors for building, managing and distributing.

Create an account or Login to your Docker Hub @ https://hub.docker.com/

Docker Container:

Container is an instance of a Docker image. This represents single application, process, or service. It includes, application executable code, the runtime environment (such as .NET Core, Python, Java), system tools, settings. One can create multiple instances of a container from the same image

Docker Container Image: It is a package with all the dependencies and information required to create a container.

  • Dependencies include frameworks and the deployment and execution configuration that a container runtime uses.
  • Usually, an image derives from multiple base images that are layers stacked on top of each other to form the container's file system.
  • An image is immutable once it has been created.

Docker Build: Action of building a container image based on the information and context provided in the Dockerfile.

Docker Pull: Process of downloading a container image from a container registry.

Docker Push: Process of uploading a container image to a container registry.

Dockerfile: Dockerfiles enable you to create your own images. It is like a batch script. It refers to a text file that contains set of instructions on how to build a Docker image. The first line identifies the base image. The rest of the file includes the build actions.

Docker Commands Cheat Sheet

Command Description
docker attach Attach local standard input, output, and error streams to a running container
docker build Build an image from a Dockerfile
docker commit Create a new image from a container’s changes
docker cp Copy files/folders between a container and the local filesystem
docker create Create a new container
docker diff Inspect changes to files or directories on a container's filesystem
docker events Get real-time events from the server
docker exec Run a command in a running container
docker export Export a container’s filesystem as a tar archive
docker history Show the history of an image
docker images List images
docker import Import the contents from a tarball to create a filesystem image
docker info Display system-wide information
docker inspect Return low-level information on Docker objects
docker kill Kill one or more running containers
docker load Load an image from a tar archive or STDIN
docker login Log in to a Docker registry
docker logout Log out from a Docker registry
docker logs Fetch the logs of a container
docker network Manage Docker networks
docker node Manage Swarm nodes
docker pause Pause all processes within one or more containers
docker plugin Manage plugins
docker port List port mappings or a specific mapping for the container
docker ps List containers
docker pull Pull an image or a repository from a registry
docker push Push an image or a repository to a registry
docker rename Rename a container
docker restart Restart one or more containers
docker rm Remove one or more containers
docker rmi Remove one or more images
docker run Run a command in a new container
docker save Save one or more images to a tar archive (streamed to STDOUT by default)
docker search Search the Docker Hub for images
docker service Manage Docker services
docker start Start one or more stopped containers
docker stats Display a live stream of container(s) resource usage statistics
docker stop Stop one or more running containers
docker swarm Manage Swarm
docker tag Tag an image to a repository
docker top Display the running processes of a container
docker unpause Unpause all processes within one or more containers
docker update Update configuration of one or more containers
docker version Show the Docker version information
docker volume Manage Docker volumes
docker wait Block until one or more containers stop, then print their exit codes

These are all the Docker commands available for managing and working with containers, images, and other Docker objects. By using these commands efficiently, you can streamline your Docker workflow and be more productive in your development work.

Steps to Install Docker on Linux:

Install Docker:

Connect to Linux Server and Install docker by following the steps below:

Update the package index by by running the command 'sudo yum -y update'

sudo yum -y update

Install the latest version of Docker by running the command 'sudo yum install -y docker'

sudo yum install -y docker

Verify Docker Installation:

To verify that Docker is installed and running correctly on a Linux server by running the command docker --version in the terminal.

docker 
or
docker -v
or
docker --version

This command returns the information about both the version of the Docker client and the version of the Docker server that is running on your machine.

Docker version refers to the release number of the Docker software. It typically consists of three parts: major version, minor version, and patch version. For example, "Docker version 19.03.13" refers to major version 19, minor version 03, and patch version 13.

Each new version of Docker may include new features, bug fixes, and performance improvements. It is important to keep your Docker version up to date to ensure that you have the latest security fixes and features.

Start Docker Service:

Once can start the docker service by running the following command below.

sudo service docker start

To run the Docker command without sudo, add the current user to the docker group

sudo usermod -a -G docker "user"

Docker Info:

'docker info' is a command that provides information about the Docker daemon and the system it is running on. This command gives you an overview of the system, including the number of containers and images, the amount of memory and storage used, and the version of the Docker daemon.

One can check the detailed information on the running/stopped containers by running the following command in the terminal.

docker info

Download Docker Image:

Docker images can be downloaded from Docker Hub using docker commands. Lets pull an image from docker hub using pull command.

Download a image from docker hub - Ngnix Official Docker Hub Image

Syntax : docker pull <<image name>>

use the following command to run ngnix image.

docker pull nginx

Reference link: Nginx - Official Image | Docker Hub

Docker Run:

Run an image in docker container using docker run command

syntax: docker run <<image name>>

cmd: docker run --name nginxservice -d nginx

--name: to specify a name for the running service. In this example, it is nginxservice

-d : to run the service in the background.

Check Docker Images:

You can verify the downloaded docker images using the following command

docker images 

View all the commands that were run with an image via a container.

syntax: docker history <<image name>> 

cmd: docker history nginx

Docker PS:

The docker ps command shows a list of all containers that are currently running on the system. Each container is represented by a single line of output, which includes the container ID, image name, command that is running, created time, status, ports, and names.

use the following command to get the list of running containers (Health Check).

docker ps

The -a option can be added to the command to show all containers, not just running ones. Use the following command to get the list of all containers.

docker ps -a

Remove Docker Image:

One can remove an image using the docker rmi command:

Syntax: docker rmi <image name>

cmd: docker rmi nginx

This command will remove the specified image from the system. If the image is in use by a running container, you will need to stop and remove the container first before removing the image.

Remove Multiple Docker Image:

One can remove multiple images at once using the docker rmi command:

docker rmi $(docker images -q)

Docker Stop:

use the following command to stop the docker service.

sudo service docker stop 

Uninstall Docker:

use the following command to uninstall docker.

uninstall docker

Remove Docker:

use the following command to remove docker completely from your system.

sudo yum remove docker

Docker Container Commands:

use the following command to get the list of running containers

docker ps

syntax: docker inspect <<container name> >

cmd: docker inspect nginxservice

Print the stats for a running Container

syntax: docker stats <<container name>> 

docker stats nginxservice

Pause the processes in a running container

syntax: docker pause <<container name >> 

docker pause nginxservice

Unpause the processes in a running container

syntax: docker unpause <<container name >> 

docker unpause nginxservice

Docker Commands (Container 1):

Kill the processes in a running container -

syntax: docker kill <<container name >>

docker kill nginxservice

Start the same container -

syntax: docker start <<container name>> 

docker start nginxservice

Stop the running container -

synatx: docker stop <<container name >> 

docker stop nginxservice

List all containers (This includes containers in a all states): We will be able to see the container we just stopped listed here.

docker ps -a 

Delete a container -

syntax: docker rm <<Container Name >> 

cmd: docker rm nginxservice

To remove all stopped containers -

cmd: docker container prune

Note: Instead of using the Container Name, all the above commands can be executed with the container
id as well.

Docker Commands (Container 2):

Export a container -

syntax: docker export <<Container Name>> <<file_Name>>.tar 

Lets run a service using docker run command.

cmd: docker run --name newnginxservice -d nginx

cmd: docker export newnginxservice > test.tar

Import a container -

syntax: docker import <<Remote URL/Image Name.tar>> 

cmd: docker import test.tar

Diagnose Run Issues -

In case you are having a problem with downloading the images and running them, please follow these steps to check whether the docker service is running on your system or not:

use the following command to check the running status of docker.

service docker status

use the following command to Restart Docker service on your machine or server.

service docker restart

Related Articles:

👉 Install Azure PowerShell and Azure AD PowerShell

👉 How to Find Azure VM Username and Password

👉 Install Azure CLI on Windows (Simple & Best Method)

👉 Azure Cloud Shell: Setup Guide and Tutorial