Skip to content

Latest commit

 

History

History
184 lines (125 loc) · 3.79 KB

basic docker command.md

File metadata and controls

184 lines (125 loc) · 3.79 KB

Docker Commands Guide

This guide covers common Docker commands for running and managing containers.


Basic Docker Commands

1. Running an NGINX Container

docker run nginx
  • Explanation: Runs the NGINX image. If the image is not present, it will pull the image from Docker Hub. This only happens the first time, afterward the same image will be reused for subsequent operations.

2. List Running Containers

docker ps
  • Explanation: Lists all running containers and basic information about them such as:
    • Docker ID
    • Created date
    • Status
  • Note: Each container gets a unique random ID.

3. Running an Image by Name

docker run <image_name>
  • Explanation: Runs the image with the given name.

4. Explore Docker Hub

Visit hub.docker.com to explore available images.


Interacting with Containers

5. Run a Container Interactively

docker run -it IMAGE_NAME bash
  • Explanation: Runs a container interactively with a terminal.

6. Check Operating System Information

cat /etc/*release*
  • Explanation: Displays the operating system release information within the container.

7. Exit the Container

exit
  • Explanation: Exits the container.

8. List Running Containers Again

docker ps
  • Explanation: Lists all currently running containers.

Additional Container Operations

9. Run a Container with a Sleep Command

docker run image_name sleep 20
  • Explanation: Runs the container and makes it sleep for 20 seconds.

10. Clear Terminal

clear
  • Explanation: Clears the terminal screen.

11. List All Containers (Running and Stopped)

docker ps -a
  • Explanation: Lists all containers, including those that are stopped.

Understanding Detached Mode and Stopping Containers

12. Running a Container in Detached Mode

docker run -d docker_image sleep 2000
  • Explanation: Runs the container in detached mode (-d), allowing it to run in the background for 2000 seconds.

13. Stopping a Running Container

docker stop container_id/name
  • Explanation: Stops the container with the specified ID or name.

14. Difference Between docker ps and docker ps -a

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

Pulling, Removing Containers and Images

15. Pulling an Ubuntu Container Without Running It

docker pull ubuntu
  • Explanation: Pulls the Ubuntu image but does not run it. The image is stored locally and can be run later.

16. Removing a Container

docker rm container_name
  • Explanation: Removes the specified container.

17. List All Docker Images

docker images
  • Explanation: Lists all locally available images.

18. Removing a Docker Image

docker rmi image_name
  • Explanation: Removes the specified Docker image from the local system.

Additional Docker Commands

19. Pull an Image Without Running It

docker pull image_name
  • Explanation: Pulls the specified image from Docker Hub but does not run it.

20. Running an Ubuntu Container

docker run ubuntu
  • Explanation: Runs the Ubuntu container.

21. Running an Ubuntu Container in Detached Mode

docker run -d ubuntu
  • Explanation: Runs the Ubuntu container in detached mode.

22. Execute a Command in a Running Container

docker exec container_name cat /etc/*release*
  • Explanation: Runs a command inside the specified container to display the OS release information.

This README provides an overview of basic Docker commands for working with containers and images.