This guide explores fundamental Docker commands essential for managing containers and images effectively.
In this guide, we explore a range of fundamental Docker commands that are essential for effectively managing containers and images. By understanding these core commands, you can efficiently run, monitor, and manage your Docker environment. At the end of this article, you can test your knowledge with a hands-on quiz.We’ll start by explaining the Docker run command and progressively move through container management commands, image operations, and executing commands within running containers.
The Docker run command creates and starts a container from a specified image. For example, to start an Nginx container, simply execute:
Copy
Ask AI
docker run nginx
If the Nginx image already exists on your host, Docker immediately starts a new container. However, if the image is not present, Docker will pull it from Docker Hub. When pulling, you might see output similar to the following:
Copy
Ask AI
docker run nginxUnable to find image 'nginx:latest' locallylatest: Pulling from library/nginxfc71811084d0: Already existsd2e987ca2267: Pull complete0b760b431b11: Pull completeDigest: sha256:96fb261b66270b900ea5a2c17a26abbfabe95506e73c3a3c65869a6dbe83223aStatus: Downloaded newer image for nginx:latest
Once the image is downloaded, any subsequent run command will use the cached image.
You can view running containers using the docker ps command. This command provides an overview, including container IDs, image names, statuses, and container names. For instance:
Copy
Ask AI
docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES796856ac413d nginx "nginx -g 'daemon of..." 7 seconds ago Up 6 seconds 80/tcp silly_sammet
To list all containers, including those that have stopped or exited, add the -a flag:
Copy
Ask AI
docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS NAMES796856ac413d nginx "nginx -g 'daemon of..." 7 seconds ago Up 6 seconds silly_sammetcff8ac918a2f redis "docker-entrypoint.s..." 6 seconds ago Exited (0) 3 seconds ago
The docker ps command is a quick way to get insights into the container statuses, while -a reveals a complete list including inactive ones.
To stop a running container, provide the container ID or name. First, confirm the container details with:
Copy
Ask AI
docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES796856ac413d nginx "nginx -g 'daemon of..." 7 seconds ago Up 6 seconds 80/tcp silly_sammet
Then, stop the container by running:
Copy
Ask AI
docker stop silly_sammet
After stopping, running docker ps will show no active containers. To permanently remove a stopped container, use:
Copy
Ask AI
docker rm silly_sammetdocker ps
The removed container will no longer appear in your Docker listings.
Viewing local Docker images is straightforward with the docker images command. This command displays each image along with its size, creation time, and more:
Copy
Ask AI
docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest f68d6e55e065 4 days ago 109MBredis latest 4760dc956b2d 15 months ago 107MBubuntu latest f975c503748 16 months ago 112MBalpine latest 3fd9065eaf02 18 months ago 4.14MB
To delete an image no longer needed, ensure no container is using it and execute:
There may be times when you need to execute a command inside a running container. For instance, if you have a container running Ubuntu with the command sleep 100, inspect its details using:
Copy
Ask AI
docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS NAMES538d037f94a7 ubuntu "sleep 100" 6 seconds ago Up 4 seconds distracted_mcclintock
To interact with the running container—for example, to view the contents of /etc/hosts—use the docker exec command. This command allows you to run commands inside a running container without starting a new one.
Using docker exec is particularly useful for debugging or modifying container states during runtime.
Consider running a simple web application container. For instance, the repository “kodekloud/simple-webapp” contains a sample web application. Running the container in the foreground displays the application’s output directly in your terminal:
Copy
Ask AI
docker run kodekloud/simple-webapp
The output might resemble:
Copy
Ask AI
This is a sample web application that displays a colored background.* Serving Flask app "app" (lazy loading)* Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
To run the web application in detached (background) mode, add the -d option:
This article covered several basic Docker commands for running containers, listing and stopping containers, managing images, and executing commands within running containers. With these fundamentals, you’ll be well-equipped to interact with Docker’s CLI more effectively and explore further configurations in your containerized environments.Next, dive into our hands-on exercises to solidify your understanding of these commands and boost your Docker expertise.