Monday, 2 December 2024

Essential Docker Commands with Simple Explanations

 Docker has become a cornerstone of modern software development, enabling developers to build, package, and deploy applications seamlessly. To help you get the most out of Docker, here’s a list of essential commands explained in simple terms.

1. docker version

Command:

docker version

Displays detailed version information for both the Docker client and server. Useful for ensuring compatibility and troubleshooting issues.

2. docker info

Command:

docker info

Provides detailed information about the Docker installation, including the number of containers, images, and system resources.

3. docker images

Command:

docker images

Lists all locally available Docker images. It shows image names, tags, and sizes, helping you manage your images.

4. docker pull

Command:

docker pull <image-name>

Downloads an image from Docker Hub or another container registry. For example, docker pull nginx fetches the latest NGINX image.

5. docker run

Command:

docker run -d -p 80:80 --name my-nginx nginx

Runs a container from the specified image. In this example:

  • -d: Runs the container in detached mode (in the background).
  • -p 80:80: Maps port 80 on the host to port 80 in the container.
  • --name my-nginx: Names the container “my-nginx”.

6. docker ps

Command:

docker ps
docker ps -a
  • docker ps: Lists all running containers.
  • docker ps -a: Shows all containers, including stopped ones.

7. docker stop

Command:

docker stop <container-id>

Stops a running container gracefully by sending a termination signal.

8. docker kill

Command:

docker kill <container-id>

Forcefully stops a container by sending a kill signal. Use it when docker stop doesn’t work.

9. docker rm

Command:

docker rm <container-id>

Removes a stopped container. Add the -f flag to force-remove a running container.

10. docker rmi

Command:

docker rmi <image-id>

Deletes a Docker image from the local system. Use this to free up space.

11. docker exec

Command:

docker exec -it <container-id> /bin/bash

Opens an interactive shell session in a running container, enabling you to troubleshoot or make changes within the container.

12. docker logs

Command:

docker logs <container-id>

Fetches logs from a container. Add the -f flag to stream logs in real-time.

13. docker build

Command:

docker build -t my-app:1.0 .

Builds a Docker image from a Dockerfile in the current directory (.). The -t flag assigns a name and tag (e.g., my-app:1.0) to the image.

14. docker network ls

Command:

docker network ls

Lists all Docker networks. Useful for managing how containers communicate with each other.

15. docker volume ls

Command:

docker volume ls

Lists all Docker volumes. Volumes are used to persist data outside containers.

16. docker inspect

Command:

docker inspect <container-id>

Shows detailed information about a container or image, such as configuration, network settings, and environment variables.

17. docker-compose up

Command:

docker-compose up -d

Starts all services defined in a docker-compose.yml file. The -d flag runs them in detached mode.

18. docker-compose down

Command:

docker-compose down

Stops and removes all services and containers defined in a docker-compose.yml file.

19. docker stats

Command:

docker stats

Displays real-time resource usage statistics (CPU, memory, etc.) for all running containers.

20. docker prune

Command:

docker system prune

Cleans up unused containers, images, networks, and volumes to free up disk space. Add the -a flag to include all unused images.

Mastering Docker commands is essential for managing containers efficiently. From creating and running containers to monitoring and cleaning up resources, these commands cover all the basics you need to get started.

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home