Welcome back to another Docker lecture. In this session, we’ll dive into the core Docker command, docker
, and explore how it can be harnessed to work with containers. By the end of this article, you’ll have a strong grasp of the docker
command and its various options.
List Containers with docker ps
To begin our exploration, let’s look at how you can list running containers. The primary command to do this is docker ps
. This command will display a list of currently active and running containers. However, it won’t show containers that have been stopped or exited.
If you want to see all containers, including stopped ones, you can use the -a
option:
docker ps -a
Additionally, if you’re interested in listing only the IDs of the containers without the additional details, you can use the -q
(quiet) mode:
docker ps -q
Running Containers with docker container run
To create and run Docker containers, you use the docker run
command. In recent Docker releases, an alternative way of running containers is through the docker container run
command. Both commands work seamlessly for now, but it’s advisable to get accustomed to using docker container run
as it aligns with other Docker subcommands like docker image
and docker volume
.
Here’s an example of running a simple container using the docker container run
command:
docker container run hello-world
In this command, hello-world
is the name of the container image you want to run. When you execute this command, Docker will fetch the hello-world
image from the Docker registry, launch a container from it, and run its default command, which in this case, displays a simple “Hello from Docker!” message.
Running Containers with Custom Names
Docker allows you to assign user-friendly names to your containers. By default, Docker generates container names, but if you want more control, you can use the --name
option when running a container:
docker container run --name my-custom-name alpine echo "Hello Docker!"
In this example, my-custom-name
is the name you’ve assigned to the container. You’re also running an Alpine Linux container and executing the command echo "Hello Docker!"
. Docker will create a container with the name you specified and execute the given command.
Conclusion
Understanding the docker
command is fundamental to working with Docker containers. We’ve explored how to list running containers, run containers using docker container run
, and assign custom names to them. By mastering these concepts, you’ll be well-prepared to navigate and utilize Docker’s capabilities for containerization. In future lectures, we’ll continue to delve deeper into Docker’s features and functionality.