Exposing Docker Container External Networks in Ubuntu 22.04.3 LTS
In this article, we will discuss how to expose Docker container external networks in Ubuntu 22.04.3 LTS. Specifically, we will focus on the scenario where you have a Docker container running on a VM and want to make its ports available externally.
Prerequisites
Before we begin, it is assumed that you have the following:
- A Docker container running on a VM with Ubuntu 22.04.3 LTS installed.
- Ports 5042 and 7247 exposed on the Docker container.
- The
curlcommand installed on the Ubuntu 22.04.3 LTS system, which can be installed using the commandsudo apt install curl.
Checking if the Docker Container has Users
First, let's check if the Docker container has any users by running the following command:
curl http://172.17.0.2:5042/v1/user/
If you get a 200 OK response, this means that there are users on the Docker container. If you get a 404 Not Found response, this means that there are no users on the Docker container.
Exposing Docker Container Ports to the Host System
To expose Docker container ports to the host system, we need to use the -p or --publish flag when running the docker run command. This flag maps a container port to a port on the host system.
For example, if we want to expose port 5042 on the Docker container to port 8080 on the host system, we would use the following command:
docker run -d -p 8080:5042 my-docker-container
This will map port 5042 on the Docker container to port 8080 on the host system. Any incoming traffic on port 8080 on the host system will be forwarded to port 5042 on the Docker container.
In our case, since we have two ports that we want to expose (5042 and 7247), we can use the following command:
docker run -d -p 8080:5042 -p 8081:7247 my-docker-container
This will map port 5042 on the Docker container to port 8080 on the host system and port 7247 on the Docker container to port 8081 on the host system.
Verifying that the Docker Container Ports are Exposed
To verify that the Docker container ports are exposed, we can use the following command:
docker ps
This will show a list of all the running Docker containers and the ports that are exposed. In our case, we should see the following:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33acbd3712f3 my-docker-container "nginx -g 'daemon of…" 10 minutes ago Up 10 minutes 0.0.0.0:8080->5042/tcp, 0.0
.0.0.0:8081->7247/tcp curious\_hilbert
This shows that ports 8080 and 8081 on the host system are mapped to ports 5042 and 7247 on the Docker container, respectively.
- To expose Docker container ports to the host system, we can use the
-por--publishflag when running thedocker runcommand. - This flag maps a container port to a port on the host system.
- To verify that the Docker container ports are exposed, we can use the
docker pscommand.