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.
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 Syntax | Explanation | Example |
---|---|---|
docker –version | Verify the Docker version | |
docker info | View 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 command | docker run hello-world |
docker run -p <port_no> –name <image_name> | Run the docker image on a given port number and a given name | docker run -p 9090:8080 –name hello-world some-name |
docker start <container_name_or_container_id> | Starts one or more stopped containers | docker start hello-world |
docker stop <container_name_or_container_id> | Stops one or more running containers | docker stop hello-world |
docker restart <container_name_or_container_id> | Restarts a Docker container | docker restart 03ca6feb6efa |
docker build -t <directory_of_dockerfile> | Builds the image from the Dockerfile and tags the image | docker 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 image | docker pull hello-world |
docker push <docker_username/image_name> | Pushes the image to the Docker Hub | docker 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 container | docker exec -it mongo |
docker search <image_name> | Searches the Docker Hub for images | docker search centos |
docker images | List all the downloaded Docker images | |
docker ps | List all the running Docker containers | |
docker ps -a | List 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 command | docker rm mongo |
docker rmi <image_name_or_image_id> | Remove or delete the Docker images | docker 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 logs | docker logs -f bead9d5fba90 |
docker login | To log in with your Docker Id to push and pull images from Docker Hub | |
docker logout | To log out from Docker Hub | |
docker volume create <volume_name> | To create a volume that Docker container will use to store the data | docker volume create postgres-data |
docker volume ls | To 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
You can download the full source code of this example here: Docker Basic Commands
Last updated on Feb. 27th, 2022