This guide covers common Docker commands for running and managing containers.
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.
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.
docker run <image_name>
- Explanation: Runs the image with the given name.
Visit hub.docker.com to explore available images.
docker run -it IMAGE_NAME bash
- Explanation: Runs a container interactively with a terminal.
cat /etc/*release*
- Explanation: Displays the operating system release information within the container.
exit
- Explanation: Exits the container.
docker ps
- Explanation: Lists all currently running containers.
docker run image_name sleep 20
- Explanation: Runs the container and makes it sleep for 20 seconds.
clear
- Explanation: Clears the terminal screen.
docker ps -a
- Explanation: Lists all containers, including those that are stopped.
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.
docker stop container_id/name
- Explanation: Stops the container with the specified ID or name.
docker ps
: Shows only running containers.docker ps -a
: Shows all containers, including stopped ones.
docker pull ubuntu
- Explanation: Pulls the Ubuntu image but does not run it. The image is stored locally and can be run later.
docker rm container_name
- Explanation: Removes the specified container.
docker images
- Explanation: Lists all locally available images.
docker rmi image_name
- Explanation: Removes the specified Docker image from the local system.
docker pull image_name
- Explanation: Pulls the specified image from Docker Hub but does not run it.
docker run ubuntu
- Explanation: Runs the Ubuntu container.
docker run -d ubuntu
- Explanation: Runs the Ubuntu container in detached mode.
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.