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:
|
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 --version2. 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 info3. 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:latest4. 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 images5. 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:latest6. 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 ubuntu7. Start a Container |
To start a created container, use the docker start command followed by the container ID or name.
docker start mycontainer8. 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 ubuntu9. 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 mycontainer10. Remove a Container |
To delete a stopped container, use the docker rm command.
docker stop mycontainer11. 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 -a12. 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
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 mycontainer14. Execute Commands in a Running Container |
Use the docker exec command to run a command inside an active container.
docker exec -it mycontainer /bin/bash15. 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/bash16. Commit a Container |
Create a new image from a container’s changes with the docker commit command.
docker commit mycontainer mynewimage17. Tag an Image |
Use the docker tag command to tag an image with a new name or tag.
docker tag mynewimage myrepository/mynewimage:latest18. Push an Image |
To upload an image to a Docker registry, use the docker push command.
docker push myrepository/mynewimage:latest19. Login to Docker Hub |
Before pushing images, you need to log in to Docker Hub using the docker login command.
docker login20. Logout from Docker Hub |
To log out from Docker Hub, use the docker logout command.
docker logout21. 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 mynewimage23. Load an Image from a Tar Archive |
To load an image from a tar archive, use the docker load command.
docker load -i myimage.tar24. Network List |
To list all networks, use the docker network ls command.
docker network ls25. Prune Docker System |
Clean up unused containers, networks, images, and build cache with the docker system prune command.
docker system pruneSummary Table of Commands
|
No comments:
Post a Comment