Docker Community Forums

Share and learn in the Docker community.

Exposing only one port

I have a web application that runs on localhost:8080. I have put it in a Docker container, and want the port 8080 exposed on the host machine. The only way I can seem to get this port on the hosts localhost is with:

$ sudo docker run --network=host container_name

This of course displays the html page, however the applicaation also runs a redis server on port 6379. When I start docker this port is available on the host, however the application cannot access it from within docker.

How do I make only the port 8080 exposed to the host machine, and other ports available for the application.

This is what makes Docker share every port with its host. Remove that, and instead explicitly publish one or more ports, like --publish 8080:8080 or -p 8080:8080. (This also allows you to change the port mapping, like -p 1234:8080 to map port 1234 on the host to port 8080 in the container.)

See Container networking | Docker Documentation.

To add to @avbentem you can also include the flag –expose which is a runtime flag that lets you expose a specific port or a range of ports inside the container.
In your case that would be

--expose 8080

@ibosk, does --expose limit what is exposed by --network=host? (It does not expose ports to the host.)

If you specify --net=host both the --expose and --publish flags will not take effect, or rather will be ignored.
In addition, if you provide just the --expose flag that only opens the port on the container, but it will not be mapped unless you also specify the -p flag, however you can use -p standalone if the port/ports are already exposed in the Dockerfile.