Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


CompTIA Linux+ XK0-005 - 3.2 - Container Management: Exposing Ports

In Docker, containers can run applications or services that need to communicate with the outside world. Exposing ports allows network traffic to reach the container and enables access to services running inside. In this guide, we will explore how to expose ports in Docker containers, connect to the exposed ports, and showcase examples using SSH and HTTP protocols.

Exposing Ports

To expose ports in a Docker container, you can use the -p or --publish option followed by the <host-port>:<container-port> format. This syntax binds the specified <host-port> to the corresponding <container-port> inside the container.

Exposing SSH Port

To expose the SSH port (default: 22) of a container, you can use the following command:

docker run -d -p 2222:22 my-container

In this example, the SSH port inside the container (22) is mapped to the host port 2222. Now, SSH connections to port 2222 on the host will be forwarded to the SSH service running inside the container.

Exposing HTTP Port

To expose a HTTP service running on port 80 inside a container, you can use the following command:

docker run -d -p 8080:80 my-container

In this example, the HTTP port inside the container (80) is mapped to the host port 8080. This allows you to access the HTTP service by connecting to port 8080 on the host machine.

Connecting to Exposed Ports

Once the ports are exposed in the Docker container, you can connect to the exposed ports using various tools and protocols.

Connecting to SSH Port

To connect to the SSH port (e.g., port 2222 in the previous example), you can use an SSH client and specify the host and port. For instance:

ssh -p 2222 user@localhost

Replace user with the appropriate username for your container.

Accessing HTTP Service

To access the HTTP service exposed on port 8080 (as per the previous example), open a web browser and enter the following URL:

http://localhost:8080

This will direct your browser to the HTTP service running inside the Docker container.

Conclusion

Exposing ports in Docker containers enables communication between the containerized services and the host or external networks. By using the -p option during container creation, you can easily map specific ports between the host and the container. Connecting to the exposed ports allows you to interact with the services running inside the container from your local machine or network.

Remember to adjust the port numbers and protocols based on your specific use case and container configuration.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.