Reputation: 4671
I have a simple Node.js server application which I have containerized.
The Node.js server runs on port 3000 so it answers at URL http://localhost:3000
.
In another application I have a docker-compose.yml
file which pull the Node.js app image and run it, like this:
version: '3'
services:
myserver:
container_name: myserver_nodejs
image: registry.gitlab.com/cs-repos/work/myserver:v1.0.2-latest
ports:
- 3000:3000
It pulls the image from GitLab container registry, locally, and runs the container.
In order to make a request to the server I need to add the port number like this: localhost:3000
, then the path.
I wonder, is there a way so that the docker-compose.yml
handles the port for me, so that I don't have to use it in my requests?
So to make a request like this: http://localhost/sales
, and internally the request goes to port 3000.
Also i added a record in the /etc/hosts
:
127.0.0.1 my.server.com
.Instead of using the localhost
, I can use the mapped address my.server.com
but I need the port number at the end as well.
The ports property just makes a mapping between the host and the container, it is needed but it's not solving the issue:
ports:
- 3000:3000
Upvotes: 0
Views: 1156
Reputation: 14723
Albeit the question is phrased in terms of docker-compose.yml
, the standard answer actually has not much to do with Docker:
If you're relying on the HTTP protocol, which has 80 as default port, you will be able to connect to the server with a "port-free" URL if it listens to port 80.
Then, in terms of Docker Compose, one could write:
ports:
- "80:3030"
then browsing http://localhost
should work.
If you're relying on HTTPS / TLS, which has 443 as default port, you will be able to connect to the server with a "port-free" URL if it listens to port 443.
So, one could write:
ports:
- "443:3030"
then browsing https://localhost
should work (assuming the container can serve resources in HTTPS).
Note that if your Docker Compose service is not TLS-aware, a typical solution amounts to completely removing the ports:
invocation for that service, make it serve in HTTP, and put in front of this service a (dockerized) TLS termination proxy.
For more details on this architecture, see e.g. these two older answers of mine:
Upvotes: 2