Reputation: 2912
Working on Docker-izing an nodejs App and I am trying to set it up so that it will respond from a non-standard port therby avoiding potential conflicts for team members that are already running a local Redis container or service.
Redis usually runs on 6379 (regardless of docker or no). I want it to listen on 6380. Even though I don't have it in the docker-compose file I want to do the same thing with RethinkDB.
I do not want to have to create a new Dockerfile for EITHER Redis or RethinkDB.
Here is my Docker-Compose file.
nodejsapp:
image: some-node-container
container_name: nodejsapp
ports:
- "5200:5200" #first number must match CHEAPOTLE_PORT env variable for the cheapotle service
depends_on:
- redis
- rethinkdb
volumes:
- ./:/app
environment:
- NODEJSAPP_PORT=5200 #must match first port number for cheapotle service above.
- REDIS_PORT=6380 #must match first number in redis service ->ports below.
- RETHINKDB_PORT=28016 #must match first number in redis service ->ports below.
redis:
image: redis:3.2-alpine
container_name: redis_cheapotle
ports:
- "6380:6379"
expose:
- "6380" # must match alternate "first" port above to avoid collisions
rethinkdb:
image: rethinkdb
container_name: rethinkdb_cheapotle
ports:
- "28016:28015" #The first number needs to match the RETHINKDB_PORT in environment variables for cheapotle above. You must change both or none, they must always be the same.
- "8090:8080" #this is where you will access the RethinkDB admin. If you have something on 8090, change the port to 8091:8080
expose:
- "28016" # must match alternate "first" port above to avoid collisions
After doing a few dockerizations I thought this would be easy. I would set my environment variables, use proces.env.whatever in my JS files and be out the door and on to the next.
Wrong.
While I can get to the RethinkDB admin area at 0.0.0.0:8090 (notice the 8090 alternate port), none of my containers can talk to each other over their specified ports.
At first I tried the above WITHOUT the 'expose' portion of the YAML but I had the same result I get WITH the 'expose' YAML added.
It seems like docker / the containers are refusing to forward the traffic coming into the Host->Alternate Port to the Container->Standard Port. I did some googling around and did not find anything in the first 20 minutes so I figured I would post this while I continue my search.
Will post an answer if I find it myself in the process.
Upvotes: 2
Views: 3282
Reputation: 2912
Ok. So I was able to solve this, it seems like there may be a bug with how official rethinkDB and Redis containers handle port forwarding since the normal port:"XXXXX:YYYYY" YAML specification is disregarded and the traffic is not sent from the modified host port to the standard docker port.
The solution was to modify the Command used to start Redis / RethinkDB containers to use the command line ports flag (which differs for each system) to change to my alternate port.
At first I tried using an environment variables Env file (but apparently those are not available immediately at run-time). I also wanted users to be able to see ALL ports / settings for their stack directly in the Docker-Compose file so the above Port flag solution seems to make sense.
I still don't know why docker wont forward alternate host port traffic to the standard container port for these two services while it WILL forward an alternate host port for the rethinkDB admin page (8090:8080).
Here is the docker-compose file I ended up with:
version: "2"
services:
nodejsapp:
image: some-node-container
container_name: cheapotle
ports:
- "5200:5200" #both numbers must match CHEAPOTLE_PORT env variable for the cheapotle service
depends_on:
- redis
- rethinkdb
volumes:
- ./:/app
environment:
- CHEAPOTLE_PORT=5200 #must match cheapotle service ports above.
- RETHINKDB_PORT=28016 #must match rethinkdb service->ports below.
- REDIS_PORT=6380 #must match redis service ->ports below.
- RESQUE_PORT=9292
entrypoint: foreman start -f /app/src/Procfile
redis:
image: redis:3.2-alpine
container_name: redis_cheapotle
ports:
- "6380:6380" #both numbers must match port in command below AND REDIS_PORT cheapotle service variable
command: redis-server --port 6380 #must match above ports AND REDIS_PORT cheapotle service variable
rethinkdb:
image: rethinkdb
container_name: rethinkdb_cheapotle
ports:
- "28016:28016" #The both numbers must match the RETHINKDB_PORT in environment variables for cheapotle above + command below. You must change allor none, they must always be the same.
- "8090:8080" #this is where you will access the RethinkDB admin. If you have something on 8090, change the port to 8091:8080
command: rethinkdb --driver-port 28016 --bind all #must match above ports AND REDIS_PORT cheapotle service variable
The docs for the command line utilities for RethinkDB can be found here: https://www.rethinkdb.com/docs/cli-options/ While the docs for the Redis command line can be found here: https://redis.io/topics/config
With the above I can start everything up on alternate ports that will not collide in the likely hood that other devs on the team are already running rethinkDB and / or Redis.
This is not a production grade setup so use at your own risk for the moment. Obviously RethinkDB would require additional configuration to allow other nodes to join the cluster at some other port than 29015.
Also! As a warning before anyone working with rethinkDB command line flags: for rethinkDB to accept a change in port the "--driver-port 28016" MUST be before the "--bind all" otherwise it's as if it is not even there and ignored.
Upvotes: 7