Docker is a widely used containerization platform that provides portable, lightweight, and high-performance container technology so that applications can be deployed and run quickly and reliably in different environments. Opening ports is an important step when using Docker as it helps us access applications or services inside the container. This article will explain how to open a port in an Ubuntu container.
First, you need to enter the terminal of the Docker container. You can use the docker exec
command to get the terminal of the Ubuntu container. This command requires entering the container ID (or container name) and the command to run, for example:
docker exec -it <container_id> /bin/bash
The -it
option in this command is used to start an interactive session and replace the standard Input, output and error streams are connected to the container. This command will open the container terminal, allowing the user to execute commands within it.
Ubuntu has a firewall installed by default, called ufw
. In the Ubuntu container, you need to confirm that the firewall status is enabled. You can check using the following command:
sudo ufw status
If the output shows that the firewall status is active, you need to configure the firewall to allow access to the specific port.
In the Ubuntu container, you can use the following command to allow specific ports through the firewall.
For example, if we want to allow access to port 80 in the container, we can run the following command:
sudo ufw allow 80
If we also want to allow access to a specific port number, we can specify the port Number:
sudo ufw allow <port_number>
For example:
sudo ufw allow 8080
In addition, you can also specify the port protocol, such as TCP or UDP, etc. For example:
sudo ufw allow proto tcp to any port 80
This command will allow the TCP protocol to access port 80 through the firewall.
After opening the port, we can use the following command to check if the port is open:
sudo netstat -ntlp
This command will list Network connections and ports, as well as running processes and their PID numbers.
If the output includes the port you opened, it means that the port has been opened successfully.
Summary
In this article, we introduced how to open a port in a Docker Ubuntu container. By running the appropriate command, you can allow specific ports to connect to an application or service running in the container, as well as check whether the port has been successfully opened. Whether you are deploying Docker containers in a development, test, or production environment, opening ports will be a necessary step.
The above is the detailed content of How docker opens ubuntu port. For more information, please follow other related articles on the PHP Chinese website!