Docker

Docker Basic Commands

Welcome readers, in this tutorial, we will learn some Docker basic commands.

1. Introduction to Docker

In the present world, Docker is an important term:

  • Often used in CI/CD platform that packages and runs the application with its dependencies inside a container
  • Is a standard for Linux Containers
  • A Container is a runtime that runs under any Linux kernel and provides a private machine-like space under Linux

1.1 Docker Terminology

  • Image: Representation of Docker container i.e. a JAR or WAR file in Java
  • Container: Runtime of Docker i.e. a deployed and running Docker image. For example, an executable Spring Boot jar
  • Engine: The code that manages, creates and runs the Docker containers
  • Hub: A public developers registry to distribute their code
  • Repository: A collection of Docker related images i.e. different versions of the same application

1.2 Docker Command Syntax

Here’s an example command.

Docker Basic Commands - Basic structure
Fig. 1: Basic structure

1.3 Need for using Docker

  • For environment replication, while the code runs locally on the machine
  • For numerous deployment phases i.e. Dev/Test/QA
  • For version control and distributing the application’s OS within a team

1.4 Setting up Docker

If someone needs to go through the Docker installation, please watch this video.

To start with this tutorial, we are hoping that users at present have the Docker installed on a Windows operating system.

2. Docker Basic Commands Tutorial

The following table will illustrate the basic commands.

Docker Command SyntaxExplanationExample
docker –versionVerify the Docker version 
docker infoView system-wide information for Docker. For example, Docker’s root directory, Version, RAM, etc 
docker run <image_name>Run the docker image mentioned in the commanddocker run hello-world
docker run -p <port_no> –name <image_name>Run the docker image on a given port number and a given namedocker run -p 9090:8080 –name hello-world some-name
docker start <container_name_or_container_id>Starts one or more stopped containersdocker start hello-world
docker stop <container_name_or_container_id>Stops one or more running containersdocker stop hello-world
docker restart <container_name_or_container_id>Restarts a Docker containerdocker restart 03ca6feb6efa
docker build -t <directory_of_dockerfile>Builds the image from the Dockerfile and tags the imagedocker build -t hello-world:web-app1 .
docker pull <image_name>Pulls the latest version of an image from the Docker Hub. Though developers can specify the particulars of an imagedocker pull hello-world
docker push <docker_username/image_name>Pushes the image to the Docker Hubdocker push my_username/new-hello-world
docker exec -it <container_name_or_container_id>Access the docker container and run commands in a run-time containerdocker exec -it mongo
docker search <image_name>Searches the Docker Hub for imagesdocker search centos
docker imagesList all the downloaded Docker images 
docker psList all the running Docker containers 
docker ps -aList all the running and stopped Docker containers 
docker rm <container_name>Remove or delete a stopped Docker container. To remove a running container developer can use the -f option in the commanddocker rm mongo
docker rmi <image_name_or_image_id>Remove or delete the Docker imagesdocker rmi hello-world
docker logs <container_name_or_container_id>To fetch the container logs. Developers can use the -f option in the command to fetch the live logsdocker logs -f bead9d5fba90
docker loginTo log in with your Docker Id to push and pull images from Docker Hub 
docker logoutTo log out from Docker Hub 
docker volume create <volume_name>To create a volume that Docker container will use to store the datadocker volume create postgres-data
docker volume lsTo check if the volume got created or not 

Try out these commands in your development environment to practice and learn.

3. Summary

That is all for this tutorial and I hope the tutorial will serve you to understand the basic Docker commands. Happy Learning and do not forget to share!

4. Download the source code

Download
You can download the full source code of this example here: Docker Basic Commands

Last updated on Feb. 27th, 2022

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button