This post will cover topic related to ‘Managing Docker Containers’ with multiple docker command examples and different scenerios. So this will help you to understand the command docker and options available in it. Also this post will explain you how to use docker command.
In Docker, managing containers is fundamental to deploying and running applications efficiently. Containers are isolated environments that package an application along with its dependencies, ensuring consistent operation across different computing environments.
Example 1: Creating a Docker Container
To create a new Docker container, use the docker container create
command followed by the name of the container and the image to be used. For instance:
docker container create --name mynginx nginx
This command creates a Docker container named mynginx
using the nginx
image.
Verification:
To verify that the container was created successfully, execute docker ps -a
to list all containers, including stopped ones. You should see mynginx
in the list.
Example 2: Starting a Docker Container
To start a Docker container that is currently stopped, use docker container start
followed by the container ID or name:
docker container start mynginx
This command starts the mynginx
container.
Verification:
Use docker ps
to list running containers. mynginx
should now appear in the list with a status of “Up”.
Example 3: Stopping a Docker Container
To stop a running Docker container, use docker container stop
followed by the container ID or name:
docker container stop mynginx
This command halts the execution of the mynginx
container.
Verification:
Again, use docker ps
to confirm that the container is no longer listed under running containers.
Example 4: Removing a Docker Container
To completely remove a Docker container (after stopping it), use docker container rm
followed by the container ID or name:
docker container rm mynginx
This command deletes the mynginx
container from your system.
Verification:
Use docker ps -a
to ensure that mynginx
is no longer listed, indicating it has been removed.
Example 5: Listing Docker Containers
To list all running Docker containers, use docker ps
:
docker ps
This command displays a list of containers currently running on your Docker host.
Verification:
Executing docker ps
should output a table showing the active containers.
Example 6: Inspecting Docker Containers
To gather detailed information about a specific Docker container, use docker container inspect
followed by the container ID or name:
docker container inspect mynginx
This command provides a JSON representation of the container’s configuration and status.
Verification:
Review the JSON output to understand the container’s properties and state.
Example 7: Renaming a Docker Container
To rename an existing Docker container, use docker container rename
followed by the current container name and the new name:
docker container rename mynginx nginx-web
This command changes the name of the mynginx
container to nginx-web
.
Verification:
Check the container list using docker ps -a
to ensure the container’s name has been updated.
Example 8: Pausing and Unpausing Docker Containers
To temporarily pause and later unpause a running Docker container, use docker container pause
and docker container unpause
respectively:
docker container pause nginx-web docker container unpause nginx-web
These commands pause and then resume the execution of the nginx-web
container.
Verification:
Use docker ps
to observe the container’s status change from paused to running.
Example 9: Executing Commands in a Docker Container
To run a command inside a Docker container without starting a new shell session, use docker container exec
followed by the container ID or name and the command:
docker container exec nginx-web ls /var/www
This command lists the contents of the /var/www
directory inside the nginx-web
container.
Verification:
Check the output of the command to confirm that it correctly lists the contents of the specified directory.
Example 10: Attaching to Docker Containers
To attach your terminal to a running Docker container’s process, use docker container attach
followed by the container ID or name:
docker container attach nginx-web
This command allows you to interact directly with the process running inside the nginx-web
container.
Verification:
After attaching, any commands executed in your terminal will directly affect the container’s process. You can verify by observing the output or changes within the attached session.
Also check similar articles.
Managing Docker Checkpoints
Managing Docker Builds
Searching Docker Hub for Images
Logging out from Docker Registries
Logging in to Docker Registries
Discussion about this post