Docker – Logs, Remove, and Port Mapping
Port Mapping
When we run an application inside a container, we define the port on which the application will run. The container runs the application on that port.
It is not possible to access an application running inside Docker from outside the container unless port mapping is configured.
Port mapping is specified using the -p option.
Syntax:
docker run -p <host_port>:<container_port>
Example:
docker run -p 8888:8080
Here:
-
8888is the host (machine) port used to access the application from outside the container. -
8080is the port defined in the application and exposed inside the container.
If the host port is already in use, Docker displays an error message.
How to Check Which Ports Are Being Used by Docker Containers
docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"
Detached Mode
To run a container in detached mode, use the -d option.
docker run -d -p 2000:2001
Detached mode means the container runs in the background.
Docker Logs
Logs contain information about the activities happening inside a container.
View Logs of a Specific Container
docker logs <container_id>
Follow Logs Continuously
docker logs -f <container_id>
-f stands for follow.
View Logs from a Specific Time Range
Seconds (s): docker logs --since 30s <container_id>
Minutes (m): docker logs --since 5m <container_id>
Hours (h): docker logs --since 2h <container_id>
Docker supports seconds (s), minutes (m), and hours (h) for relative time.
For days, weeks, months, or years, use an ISO 8601 date or timestamp.
Weeks: docker logs --since 2026-06-14 <container_id>
Months: docker logs --since 2026-05-21 <container_id>
Years: docker logs --since 2025-06-21 <container_id>
Combined Time Units
docker logs --since 1h30m <container_id>
Displays logs from 1 hour and 30 minutes ago.
Exact Time
docker logs --since "2026-06-21T17:30:00" <container_id>
Displays logs generated since the specified date and time.
Docker Inspect
docker inspect is used to view detailed information about a container, image, network, or volume.
docker inspect <container_id>
Access a Running or Exited Container
To open a shell inside a running container:
docker exec -it <container_id> sh
-
-i= Interactive mode -
-t= Allocate a terminal
Docker Remove
Delete an Exited Container
docker rm <container_id>
Delete a Running Container
docker rm -f <container_id>
-f forcefully stops and removes the container.
Alternative Method
First stop the container:
docker stop <container_id>
Then remove it:
docker rm <container_id>
Delete Multiple Containers
docker rm -f <container_id1> <container_id2> <container_id3>
Alternative Method (Windows)
FOR /F "tokens=*" %i IN ('docker ps -aq') DO docker rm -f %i
This command removes all containers.
Listing Containers
View Running Containers
docker ps
ps stands for Process Status.
This command lists only running containers.
View All Containers
docker ps -a
This command lists all containers, including exited containers.
-a stands for all.
View Only Container IDs
docker ps -aq
This command lists all container IDs.
Delete Images
docker rmi -f <image_id>
This command forcefully removes an image.
Naming a Container
To assign a name to a container:
docker run -d --name anyname busybox:1.36
Example:
docker run -d --name my-container busybox:1.36
This creates a container with the name my-container.
Top comments (0)