DEV Community

Cover image for Docker – Port Mapping, Logs, Container Management, and Image Removal
Ramya Perumal
Ramya Perumal

Posted on

Docker – Port Mapping, Logs, Container Management, and Image Removal

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>
Enter fullscreen mode Exit fullscreen mode

Example:

docker run -p 8888:8080
Enter fullscreen mode Exit fullscreen mode

Here:

  • 8888 is the host (machine) port used to access the application from outside the container.
  • 8080 is 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}}"
Enter fullscreen mode Exit fullscreen mode

Detached Mode

To run a container in detached mode, use the -d option.

docker run -d -p 2000:2001
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Follow Logs Continuously

docker logs -f <container_id>
Enter fullscreen mode Exit fullscreen mode

-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>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Combined Time Units

docker logs --since 1h30m <container_id>
Enter fullscreen mode Exit fullscreen mode

Displays logs from 1 hour and 30 minutes ago.

Exact Time

docker logs --since "2026-06-21T17:30:00" <container_id>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Access a Running or Exited Container

To open a shell inside a running container:

docker exec -it <container_id> sh
Enter fullscreen mode Exit fullscreen mode
  • -i = Interactive mode
  • -t = Allocate a terminal

Docker Remove

Delete an Exited Container

docker rm <container_id>
Enter fullscreen mode Exit fullscreen mode

Delete a Running Container

docker rm -f <container_id>
Enter fullscreen mode Exit fullscreen mode

-f forcefully stops and removes the container.

Alternative Method

First stop the container:

docker stop <container_id>
Enter fullscreen mode Exit fullscreen mode

Then remove it:

docker rm <container_id>
Enter fullscreen mode Exit fullscreen mode

Delete Multiple Containers

docker rm -f <container_id1> <container_id2> <container_id3>
Enter fullscreen mode Exit fullscreen mode

Alternative Method (Windows)

FOR /F "tokens=*" %i IN ('docker ps -aq') DO docker rm -f %i
Enter fullscreen mode Exit fullscreen mode

This command removes all containers.


Listing Containers

View Running Containers

docker ps
Enter fullscreen mode Exit fullscreen mode

ps stands for Process Status.

This command lists only running containers.

View All Containers

docker ps -a
Enter fullscreen mode Exit fullscreen mode

This command lists all containers, including exited containers.

-a stands for all.

View Only Container IDs

docker ps -aq
Enter fullscreen mode Exit fullscreen mode

This command lists all container IDs.


Delete Images

docker rmi -f <image_id>
Enter fullscreen mode Exit fullscreen mode

This command forcefully removes an image.


Naming a Container

To assign a name to a container:

docker run -d --name anyname busybox:1.36
Enter fullscreen mode Exit fullscreen mode

Example:

docker run -d --name my-container busybox:1.36
Enter fullscreen mode Exit fullscreen mode

This creates a container with the name my-container.

Top comments (0)