1. Check Container Status
docker ps
Displays running containers.
docker ps -a
Displays all containers, including stopped containers.
2. Restart a Container
docker restart my-nginx
Stops and starts the container automatically.
3. View Container Logs
docker logs my-nginx
Displays logs generated by the Nginx container.
To continuously monitor logs:
docker logs -f my-nginx
Press Ctrl + C to stop monitoring.
4. Inspect Container Details
docker inspect my-nginx
Displays detailed JSON information, including:
-
Container ID
-
IP Address
-
Network configuration
-
Port mappings
-
Mount points
-
Environment variables
To display only the container IP:
docker inspect -f "{{.NetworkSettings.IPAddress}}" my-nginx
5. Execute Commands Inside a Container
docker exec -it my-nginx bash
You are now inside the Nginx container's Linux shell.
Execute:
ls
Then:
exit
This takes you back to Windows Command Prompt.
6. Remove a Container
First, stop the container:
docker stop my-nginx
Then remove it:
docker rm my-nginx
Verify:
docker ps -a
The my-nginx container should no longer appear.
Commands learned
|
Command |
Purpose |
|---|---|
|
|
Restart container |
|
|
View logs |
|
|
View container details |
|
|
Execute commands inside container |
|
|
Remove container |
This error occurs because the my-nginx container is still running. Docker does not allow you to remove a running container using docker rm.
Solution
Step 1: Stop the container
Execute:
docker stop my-nginx
Expected output:
my-nginx
Step 2: Remove the container
docker rm my-nginx
Expected output:
my-nginx
Step 3: Verify
docker ps -a
The my-nginx container should no longer appear.
Alternative: Force Remove
You can also use:
docker rm -f my-nginx
This forcibly stops and removes the container. Use it carefully.
For learning purposes, use the first approach (stop → remove).