Top 10 Docker Commands: A Beginner's Guide

Docker commands are the backbone of container management. They allow you to create, manage, and troubleshoot containers and images efficiently. By mastering these commands, you can streamline your development process, reduce errors, and improve productivity. This guide is designed to help beginners get started while providing valuable insights for advanced users.


1. Docker Run Command

The docker run command is one of the most frequently used Docker commands. It creates and starts a new container from a specified image.

Syntax:

docker run [options] <image-name> [command]

Example:

To run a container from the official Ubuntu image and open a bash shell, use:

docker run -it ubuntu /bin/bash

Pro Tip:

  • Use the -d option to run the container in detached mode.
  • Use --name to assign a custom name to your container.

2. Docker Ps Command

The docker ps command lists all running containers. It’s essential for monitoring active containers.

Syntax:

docker ps [options]

Example:

To list all running containers:

docker ps

To list all containers (including stopped ones):

docker ps -a

Pro Tip:

  • Use docker ps -q to list only container IDs, which is useful for scripting.

3. Docker Stop Command

The docker stop command gracefully stops a running container.

Syntax:

docker stop [container-id/container-name]

Example:

To stop a container with the ID abcd1234:

docker stop abcd1234

Pro Tip:

  • Use docker kill for an immediate stop, but this is not recommended for production environments.

4. Docker Rm Command

The docker rm command removes a stopped container, freeing up system resources.

Syntax:

docker rm [container-id/container-name]

Example:

To delete a container with the ID abcd1234:

docker rm abcd1234

Pro Tip:

  • Use docker rm -f to force-remove a running container.

5. Docker Images Command

The docker images command lists all Docker images stored locally.

Syntax:

docker images [options]

Example:

To list all available images:

docker images

Pro Tip:

  • Use docker images -q to list only image IDs.

6. Docker Pull Command

The docker pull command downloads an image from a Docker registry (e.g., Docker Hub).

Syntax:

docker pull [image-name]

Example:

To download the official Ubuntu image:

docker pull ubuntu

Pro Tip:

  • Always specify a version tag to avoid pulling the latest image, which may not be stable.

7. Docker Tag Command

The docker tag command creates a new tag for an existing image, useful for versioning.

Syntax:

docker tag [image-name] [new-tag]

Example:

To create a new tag my-ubuntu for the official Ubuntu image:

docker tag ubuntu my-ubuntu

Pro Tip:

  • Use tags to manage different versions of your application.

8. Docker Exec Command

The docker exec command runs a command inside a running container.

Syntax:

docker exec [options] [container-id/container-name] [command]

Example:

To open a bash shell inside a running container with the ID abcd1234:

docker exec -it abcd1234 /bin/bash

Pro Tip:

  • Use docker exec for debugging and troubleshooting running containers.

9. Docker Logs Command

The docker logs command displays the logs of a container, which is crucial for debugging.

Syntax:

docker logs [options] [container-id/container-name]

Example:

To view the logs of a container with the ID abcd1234:

docker logs abcd1234

Pro Tip:

  • Use docker logs -f to follow the logs in real-time.

10. Docker Inspect Command

The docker inspect command provides detailed information about a container or image.

Syntax:

docker inspect [options] [container-id/container-name/image-name]

Example:

To inspect a container with the ID abcd1234:

docker inspect abcd1234

Pro Tip:

  • Use --format to filter specific information, such as IP addresses or mounted volumes.

Additional Resources

To further enhance your Docker skills, check out these resources:


Conclusion

Mastering Docker commands is a game-changer for developers and DevOps engineers. By understanding and practicing these top 10 Docker commands, you’ll be well-equipped to manage containers and images efficiently. Whether you’re deploying applications or troubleshooting issues, these commands will save you time and effort.

Start experimenting with these commands today, and don’t forget to explore the additional resources provided. Happy containerizing!

docker docker commands Top 10 Docker Commands devops