Home > Back-end >  Which port of localhost can I select in order to create a docker container?
Which port of localhost can I select in order to create a docker container?

Time:02-06

I'm learning Kurbernetes and Docker at the moment, KinD in particular. To start with, I just want to run docker run --rm --name <container's name> -p 8080:80 -d <image name> to create a container from the image.

I know that ports are used in the TCP/IP protocol (or Internet Protocol) to address a specific program (software). Port 80 is a default port to run web servers.

Now, my question is why 8080 or why 5000? How to determine which port should be an OUTSIDE port in this case? Is it just random or are there any rule/restrictions?

CodePudding user response:

port 8080 is normally used to host your personal webserver/service and it is alternate of port 80 you can also use other ports instead of 8080. when some one try to connect to your webserver from outside, and if you use port 8080, they don't need to specify port number because by default it will look for port 8080.

If you use any other port number, when someone try to connect to your webservice/server from outside, they should specify the custom port number you specified to access your webservice/server

CodePudding user response:

For the docker run -p option (and Compose ports:), for the first port number you can pick any port that isn't already in use on your host system. As you've noted, port 80 is the standard HTTP port, and what gets used in http://hostname/ URLs without an explicit port number. Various frameworks use port 3000 or 5000 or 8000 or 8080 as their default but none of them is "standard" or "special" in any way.

The second port number must be the port number the server process is listening on. The server process must be listening on the special 0.0.0.0 "all address" address, if that's a configurable option; if it's listening on 127.0.0.1 (as many developer-oriented servers do by default) it will not be reachable from outside its container. This number often is included in an EXPOSE line in the Dockerfile, but that directive has no other effect. (There is no reason to include a docker run --expose option or Compose expose: block and it's always safe to delete it.)

There is no particular requirement that the two ports match. If you want to use host port 8888 because it's available, and your application is a Node application using the default Express port 3000, it will work to

docker run -p 8888:3000 ...

If you really don't care you can use docker run -p with only the container port number, but this is unusual. docker port will tell you what port Docker chose.

docker run -p 3000 --name my-container ...
docker port my-container 3000

You mention Kubernetes in here as well. In Kubernetes, all communications between Pods go through a Service, in effect an in-cluster load balancer. I'd recommend always making the Service use the "normal" port for whatever protocol you're using, port 80 for unencrypted HTTP. Each Service has its own in-cluster IP address so there's no risk of conflict between Services or Pods. If you're using a NodePort-type Service to make it accessible from outside the cluster, you are usually constrained to using ports 30000 through 32767.

apiVersion: v1
kind: Service
metadata: { name: the-service-name }
spec:
  selector: { ... }
  ports:
    - port: 80           # for HTTP, regardless of how the service is implemented
      targetPort: http   # matching the Pod's `containerPorts:` name
      # nodePort: 30080  # if the Service has type: NodePort, optional

Now calls from another Pod through this Service can use http://the-service-name/ as the URL with the default port.

  •  Tags:  
  • Related