Skip to main content
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.

Running a Container

The Docker run command creates and starts a container from a specified image. For example, to start an Nginx container, simply execute:
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:
Once the image is downloaded, any subsequent run command will use the cached image.

Listing Containers

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:
To list all containers, including those that have stopped or exited, add the -a flag:
The docker ps command is a quick way to get insights into the container statuses, while -a reveals a complete list including inactive ones.

Stopping and Removing Containers

To stop a running container, provide the container ID or name. First, confirm the container details with:
Then, stop the container by running:
After stopping, running docker ps will show no active containers. To permanently remove a stopped container, use:
The removed container will no longer appear in your Docker listings.

Managing Docker Images

Viewing local Docker images is straightforward with the docker images command. This command displays each image along with its size, creation time, and more:
To delete an image no longer needed, ensure no container is using it and execute:
Additionally, if you want to download an image for later use without running a container immediately, use the docker pull command:

Running Ubuntu Containers

When using the Ubuntu image, simply running:
will start a container that immediately exits. This occurs because Ubuntu, by default, has no long-running process. Check the container status with:
To keep the container active, instruct it to run a specific command, such as sleeping for a defined duration:
Here, the container runs the sleep command for five seconds before exiting.

Executing Commands in a Running Container

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:
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.

Running a Web Application Container

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:
The output might resemble:
To run the web application in detached (background) mode, add the -d option:
This will output a container ID similar to:
You can then check running containers with:
If you need to reattach to the container, use the docker attach command along with the container ID (a shortened unique prefix is acceptable):

Conclusion

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.

Watch Video