25 Basic Docker Commands for Beginners

25 Basic Docker Commands

Discover the 25 basic Docker commands every beginner needs to know. This comprehensive guide covers everything from pulling images to managing containers, complete with examples and a summary table.

Table of Contents

Introduction

Docker has revolutionized the way developers build, ship, and run applications. It simplifies the process by encapsulating applications and their dependencies into containers. For beginners, mastering Docker commands is crucial for efficient container management. In this blog post, we’ll dive into 25 basic Docker commands that every beginner should know. These commands will help you get started with Docker and manage your containers effectively.

Prerequisites

Before diving into Docker commands, ensure you have the following:

  1. Docker Installed: Docker must be installed on your system. You can download and install Docker from the official Docker website.
  2. Basic Command Line Knowledge: Familiarity with command line operations will help you understand and execute Docker commands smoothly.
  3. Docker Hub Account: For pulling and pushing images, having an account on Docker Hub is recommended.

With these prerequisites in place, you are ready to explore the fundamental Docker commands.

25 Basic Docker Commands

1. Docker Version

To verify your Docker installation, use the docker version command. This command displays detailed information about the Docker client and server versions.

				
					docker --version
				
			

2. Docker Info

Get a comprehensive overview of your Docker installation with the docker info command. It provides details about the Docker installation, including the number of containers, images, and the storage driver.

				
					docker info
				
			

3. Pull an Image

To download an image from Docker Hub, use the docker pull command. For instance, to pull the latest Ubuntu image, run:

				
					docker pull ubuntu:latest
				
			

4. List Images

You can list all the images stored on your machine with the docker images command. This helps you keep track of available images.

				
					docker images
				
			

5. Remove an Image

To delete an image from your local machine, use the docker rmi command followed by the image ID or name.

				
					docker rmi ubuntu:latest
				
			

6. Create a Container

Use the docker create command to create a new container from an image. This does not start the container immediately.

				
					docker create --name mycontainer ubuntu
				
			

7. Start a Container

To start a created container, use the docker start command followed by the container ID or name.

				
					docker start mycontainer
				
			

8. Run a Container

The docker run command creates and starts a container in one step. It is widely used for running containers.

				
					docker run -it ubuntu
				
			

9. Stop a Container

If you need to stop a running container, use the docker stop command followed by the container ID or name.

				
					docker stop mycontainer
				
			

10. Remove a Container

To delete a stopped container, use the docker rm command.

				
					docker stop mycontainer
				
			

11. List Containers

You can list all running containers with the docker ps command. To include stopped containers, add the -a flag.

				
					docker ps
docker ps -a
				
			

12. Inspect a Container

To get detailed information about a container, use the docker inspect command followed by the container ID or name.

				
					docker inspect mycontainer

				
			
25 Basic Commands

Photo by admingeek from Infotechys

13. View Container Logs

Access the logs of a running container with the docker logs command. This is helpful for debugging purposes.

				
					docker logs mycontainer
				
			

14. Execute Commands in a Running Container

Use the docker exec command to run a command inside an active container.

				
					docker exec -it mycontainer /bin/bash
				
			

15. Copy Files from a Container

To copy files from a container to your local machine, use the docker cp command.

				
					docker exec -it mycontainer /bin/bash
				
			

16. Commit a Container

Create a new image from a container’s changes with the docker commit command.

				
					docker commit mycontainer mynewimage
				
			

17. Tag an Image

Use the docker tag command to tag an image with a new name or tag.

				
					docker tag mynewimage myrepository/mynewimage:latest

				
			

18. Push an Image

To upload an image to a Docker registry, use the docker push command.

				
					docker push myrepository/mynewimage:latest
				
			

19. Login to Docker Hub

Before pushing images, you need to log in to Docker Hub using the docker login command.

				
					docker login
				
			

20. Logout from Docker Hub

To log out from Docker Hub, use the docker logout command.

				
					docker logout
				
			

21. Build an Image

Create a new image from a Dockerfile using the docker build command.

				
					docker build -t mynewimage .

				
			

22. Save an Image to a Tar Archive

Use the docker save command to save an image to a tar archive.

				
					docker save -o myimage.tar mynewimage
				
			

23. Load an Image from a Tar Archive

To load an image from a tar archive, use the docker load command.

				
					docker load -i myimage.tar
				
			

24. Network List

To list all networks, use the docker network ls command.

				
					docker network ls

				
			

25. Prune Docker System

Clean up unused containers, networks, images, and build cache with the docker system prune command.

				
					docker system prune
				
			

Summary Table of Commands

CommandDescription
docker versionDisplay Docker version information
docker infoDisplay system-wide information
docker pullPull an image from a registry
docker imagesList images
docker rmiRemove an image
docker createCreate a new container
docker startStart a container
docker runRun a new container
docker stopStop a running container
docker rmRemove a container
docker psList containers
docker inspectDisplay detailed information about a container
docker logsFetch the logs of a container
docker execRun a command in a running container
docker cpCopy files from a container
docker commitCreate a new image from a container’s changes
docker tagTag an image
docker pushPush an image to a registry
docker loginLog in to a Docker registry
docker logoutLog out from a Docker registry
docker buildBuild an image from a Dockerfile
docker saveSave an image to a tar archive
docker loadLoad an image from a tar archive
docker network lsList all networks
docker system pruneRemove unused data

Conclusion

Mastering these basic Docker commands will significantly enhance your ability to manage containers efficiently. Whether you are pulling images, creating and running containers, or inspecting and troubleshooting, these commands form the foundation of Docker management. Practice these commands to become proficient in Docker and streamline your development workflow.

Did you find this article useful? Your feedback is invaluable to us! Please feel free to share your thoughts in the comments section below.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *