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:Listing Containers
You can view running containers using thedocker ps command. This command provides an overview, including container IDs, image names, statuses, and container names. For instance:
-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:docker ps will show no active containers. To permanently remove a stopped container, use:
Managing Docker Images
Viewing local Docker images is straightforward with thedocker images command. This command displays each image along with its size, creation time, and more:
docker pull command:
Running Ubuntu Containers
When using the Ubuntu image, simply running: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 commandsleep 100, inspect its details using:
/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:-d option:
docker attach command along with the container ID (a shortened unique prefix is acceptable):