Mastering Docker: 41 Essential Commands for Developers

Docker has become an integral tool in the world of software development, providing a lightweight and efficient way to package, distribute, and run applications. Whether you're a seasoned Docker user or just getting started, mastering these essential commands will streamline your Docker workflow. Let's dive in!

1. Install Docker:

$ sudo apt-get update 
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Make sure to follow the official documentation for installation on your specific operating system.

2. Verify Installation:

$ docker --version 
$ docker info

Ensure that Docker is installed correctly and view detailed information about the Docker installation.

3. Pull Docker Image:

$ docker pull imageName:tag

Download a Docker image from Docker Hub. Replace imageName:tag with the desired image and version.

4. List Docker Images:

$ docker images

Display a list of locally available Docker images.

5. Run a Container:

$ docker run imageName:tag

Start a container based on the specified image.

6. Run in Detached Mode:

$ docker run -d imageName:tag

Run a container in the background (detached mode).

7. List Running Containers:

$ docker ps

View a list of running containers.

8. List All Containers:

$ docker ps -a

Show all containers, including those that are stopped.

9. Stop a Running Container:

$ docker stop containerID

Gracefully stop a running container. Replace containerID with the actual container ID.

10. Remove a Container:

$ docker rm containerID

Delete a stopped container. Use the -f flag to force removal of a running container.

11. Execute a Command Inside a Running Container:

$ docker exec -it containerID command

Run a command inside a running container. The -it flag allows interaction with the container.

12. Container Logs:

$ docker logs containerID

View the logs of a specific container.

13. Container Statistics:

$ docker stats containerID

Display a live stream of a container's resource usage.

14. Inspect Container:

$ docker inspect containerID

Get detailed information about a container, including configuration and network settings.

15. Container Shell Access:

$ docker exec -it containerID /bin/bash

Open a shell inside a running container for debugging or exploration.

16. Build Docker Image:

$ docker build -t imageName:tag path/to/Dockerfile

Build a Docker image from a specified Dockerfile.

17. Remove Docker Image:

$ docker rmi imageName:tag

Delete a local Docker image.

18. Port Mapping:

$ docker run -p hostPort:containerPort imageName:tag

Map a host port to a container port.

19. Volume Mounting:

$ docker run -v /host/path:/container/path imageName:tag

Mount a host directory into a container.

20. Network Inspection:

$ docker network inspect networkName

View details about a Docker network.

21. Create a Docker Network:

$ docker network create networkName

Create a custom Docker network.

22. Remove a Docker Network:

$ docker network rm networkName

Delete a Docker network.

23. Docker Compose Up:

$ docker-compose up

Start services defined in a Docker Compose file.

24. Docker Compose Down:

$ docker-compose down

Stop and remove services defined in a Docker Compose file.

25. Build and Run with Docker Compose:

$ docker-compose up --build

Rebuild images and start services.

26. List Docker Compose Containers:

$ docker-compose ps

List containers related to a Docker Compose project.

27. Docker Compose Logs:

$ docker-compose logs serviceName

View the logs of a specific service in a Docker Compose project.

28. Docker Compose Scale:

$ docker-compose up --scale serviceName=3

Scale a service to run a specified number of replicas.

29. Docker Compose Variables:

$ export VAR_NAME=value $ docker-compose up

Use environment variables in a Docker Compose file.

30. Docker Compose Override:

$ docker-compose -f docker-compose.override.yml up

Use an override file to customize a Docker Compose configuration.

31. Docker Hub Login:

$ docker login

Log in to Docker Hub.

32. Docker Hub Logout:

$ docker logout

Log out from Docker Hub.

33. Docker Image Tagging:

$ docker tag imageName:tag newImageName:newTag

Tag a local image with a new name and/or tag.

34. Docker Image Push:

$ docker push newImageName:newTag

Push a Docker image to a container registry.

35. Search Docker Hub for images

$ docker search ImageName

List existing images matching ImageName

36. Docker Prune:

$ docker system prune

Remove all stopped containers, unused networks, and dangling images.

37. Show the history of an image:

$ docker history ImageName

Show the history of the image

38. Docker Disk Usage:

$ docker system df

Display Docker disk usage information.

39. Docker Pause/Unpause:

$ docker pause containerID 
$ docker unpause containerID

Pause and unpause a running container.

40. Docker Export/Import:

$ docker export containerID > container.tar 
$ docker import container.tar newImageName:newTag

Export and import a container as a tarball.

41. Docker Events:

$ docker events

Show real-time events from the Docker daemon.

Resources

https://docs.docker.com/

https://docs.docker.com/get-started/docker_cheatsheet.pdf

https://dockerlabs.collabnix.com/docker/cheatsheet/