Docker Basic Commands

1. Check Docker Version

docker --version

Example: This command shows you the installed version of Docker on your machine.

  • Output: Something like Docker version 20.10.7, build f0df350.

2. Pull an Image

docker pull ubuntu

Example: This command downloads the latest Ubuntu image from Docker Hub to your local machine.

  • Output: You’ll see messages indicating that layers are being downloaded.

3. Run a Container

docker run ubuntu

Example: This command creates and starts a container using the Ubuntu image. If you don't specify a command, it will exit immediately since there’s no default command.

  • Output: You might not see anything if it exits quickly.

4. Show Running Containers

docker ps

Example: This command lists all currently running containers.

  • Output: You'll see details like Container ID, Image, Command, and Status.

5. Show All Containers

docker ps -a

Example: This command shows all containers, including those that are stopped.

  • Output: You’ll see both running and stopped containers in the list.

6. Run a Container in Detached Mode

docker run -dit ubuntu:latest

Example: This command starts a new Ubuntu container in detached mode (running in the background). -d is for detached, -i for interactive, and -t for terminal.

  • Output: You’ll get a Container ID, and the container will run in the background.

7. List Images

docker images

Example: This command shows all Docker images you have downloaded.

  • Output: You’ll see a list of images with details like Repository, Tag, and Size.

8. Remove a Container

docker rm <container_id>

Example: This command removes a container. Replace <container_id> with the actual ID of the container you want to remove.

  • Output: No output if successful; otherwise, you may see an error if the container is still running.

9. Add User to Docker Group

sudo usermod -aG docker ubuntu && newgrp docker

Example: This command adds the user ubuntu to the Docker group so that you can run Docker commands without sudo. The newgrp command refreshes the group membership.

  • Output: No output if successful. You’ll be able to run Docker commands directly as the ubuntu user afterwards.

"I believe this article will be beneficial, allowing you to uncover fresh insights and gain enriching knowledge."

Happy Learning🙂

Parth Sharma