remtsoy
remtsoy

Reputation: 465

Bind docker container port to path

Docker noob here. Have setup a dev server with docker containers. I am able to run a basic containers. For example

docker run --name node-test -it -v "$(pwd)":/src -p 3000:3000 node bash

Works as expected. As soon as I have many small projects, I would like to bind/listen to actual http localhost path instead of port. Something like that

docker run --name node-test -it -v "$(pwd)":/src -p 3000:80/node-test node bash

Is it possible? Thanks.

EDIT. Basically I want to type localhost/node-test instead of localhost:3000 in my browser window

Upvotes: 0

Views: 2300

Answers (1)

Kryten
Kryten

Reputation: 15760

It sounds like what you want is for your Docker container to respond to a URL like http://localhost/some/random/path by somehow specifying that path in the Docker --port option.

The short answer to that is no, that is not possible. The reason is that a port is not related to a path in any way - an HTTP server listens on a port, and serves resources that are found at a path. Note that there are many different types of servers and all of them listen on some port, but many (most?) of them have no concept of a path at all. For example, consider an SMTP (mail transfer) server - it often listens on port 25, but what does a path mean to it? All it does is transfer mail from one server to another.

There are two ways to accomplish what you're trying to do:

  1. write your application to respond to particular paths. For example, if you're using the Express framework in your node application, create a route for the path you want.

  2. use a proxy server to accept requests on one path and relay them to a server that's listening to another path.

Note that this has nothing to do with Docker - you'd be faced with the same two options if you were running your application on any server.

Upvotes: 5

Related Questions