devops-basics

Introduction 👋

Installation 🔨

Table of Contents 🔖

Docker General Commands

1. docker info

2. docker –help

Docker Registry

3. docker login

ID Command Description
1 docker login Log in to the default Docker registry
2 docker login myRegistry -u username Log in to a specified registry with the specified username

4. docker logout

ID Command Description
1 docker logout Log out from the default Docker registry
2 docker logout myRegistry Logout from a specified registry

Docker Images

5. docker build

ID Command Description
1 docker build -t myImage . Build an image using a Dockerfile in the current directory
2 docker build -t myImage:v0.1.0 . Build a Docker image from a Dockerfile with specified tag
3 docker build -t myImage -f demo/Dockerfile Build an image using a Dockerfile in the demo directory

6. docker tag

ID Command Description
1 docker tag myImage:latest myImage:v2 Create a new tag “v2” for the Docker image “myImage” with tag “latest”
2 docker tag myImage:latest myRegistry/myImage Tag the Docker image “myImage” with tag “latest” to a registry image tag latest
3 docker tag myImage:latest myRegistry/myImage:1.0.0 Tag the Docker image “myImage” with tag “latest” to a registry image tag 1.0.0

7. docker images

ID Command Description
1 docker images List all Docker images on the system
2 docker images -a List all Docker images, including intermediate ones

8. docker pull

ID Command Description
1 docker pull nginx Pull the latest nginx image from Docker Hub
2 docker pull nginx:latest Pull the latest nginx image from Docker Hub explicitly
3 docker pull myRegistry/myImage:tag Pull a specific image from a private registry

9. docker push

ID Command Description
1 docker push myImage Push the “myImage” image to the default registry
2 docker push myRegistry/myImage:tag Push a specific tagged version of the “myImage” image to a private registry

10. docker save

ID Command Description
1 docker save -o myImage.tar myImage Save a Docker image to a local .tar file
2 docker save -o /path/to/output.tar myImage Save an image to a specific location on your system

11. docker load

ID Command Description
1 docker load -i myImage.tar Load a Docker image from a local .tar file
2 docker load -i /path/to/image_archive.tar Load an image from a specific location on your system

12. docker rmi

ID Command Description
1 docker rmi myImage Remove the Docker image named “myImage”
2 docker rmi myImage:tag Remove a specific tagged version of the “myImage” image

Docker Container

13. docker run

ID Command Description
1 docker run -d --name mycontainer nginx Run nginx image in detached mode with container name “mycontainer”
2 docker run -p 8080:80 --name mycontainer nginx Run nginx image with port mapping from host 8080 to container 80
3 docker run -e MYSQL_ROOT_PASSWORD=password -v /mydata:/var/lib/mysql mysql Run MySQL image with setting root password
4 docker run -v /mydata:/var/lib/mysql mysql Run MySQL image and mounting a host directory to container
5 docker run --network=host myImage Run a container using the host network
6 docker run --privileged myImage Run a container with extended privileges using the Docker image “myImage”
7 docker run -it myImage /bin/bash Run an image and open a bash shell inside a container

14. docker ps

ID Command Description
1 docker ps List running containers
2 docker ps -a List all containers, including stopped ones

15. docker stop/start/restart

ID Command Description
1 docker stop mycontainer Stop a running container named “mycontainer”
2 docker restart mycontainer Restart a container named “mycontainer”
3 docker start mycontainer Start a stopped container named “mycontainer”

16. docker rm

ID Command Description
1 docker rm mycontainer Remove a stopped container named “mycontainer”
2 docker rm -f mycontainer Force-remove a running container named “mycontainer”

17. docker logs

ID Command Description
1 docker logs mycontainer Retrieve logs from a container named “mycontainer”
2 docker logs --tail 100 mycontainer Retrieve last 100 lines of logs from “mycontainer”

18. docker exec

ID Command Description
1 docker exec -it mycontainer /bin/bash Start an interactive Bash shell in “mycontainer”
2 docker exec mycontainer ls -l /app List files in directory “/app” in “mycontainer”

19. docker cp

ID Command Description
1 docker cp mycontainer:/app/logs/log.txt ./local_dir/ Copy the file “log.txt” from the container “mycontainer” to a local directory
2 docker cp ./local_file.txt mycontainer:/app/data/ Copy the file “local_file.txt” from the local filesystem to the container “mycontainer”

Docker cleanup

20. docker system prune

ID Command Description
1 docker system prune Remove all stopped containers, dangling images, and unused networks
2 docker system prune -a Remove all stopped containers, all unused images, and all unused networks

What’s next?

Which Docker command do you find yourself using the most? Let us know in the comments below. Your feedback and suggestions are highly appreciated. Thank you, and happy coding! 💖