Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-compose port range forward

The documentation is really lacking here.

ports:
  - "20000-20100"

works fine for one port (just "20000" does not work tho. It seemingly binds to a random port above 40k somewhere), but I can't find a reliable way to forward a range of ports instead of just one.

- "20000-20100"
- "10000-10100:20000-20100"
- "20000-20100:20000-20100"

None of these works

I have also exposed 20000-30000 in the Dockerfile, but I'm under impression that this shouldn't really matter. Am I being stupid here? This seems like such an easy thing, but I've been hammering for hours now unable to get the connections working.

Edit:

Using - "20000-20010" exposes these ports:

0.0.0.0:43809->20000/tcp, 0.0.0.0:43808->20001/tcp, 0.0.0.0:43807->20002/tcp, 0.0.0.0:43806->20003/tcp, 0.0.0.0:43805->20004/tcp, 0.0.0.0:43804->20005/tcp, 0.0.0.0:43803->20006/tcp, 0.0.0.0:43802->20007/tcp, 0.0.0.0:43801->20008/tcp, 0.0.0.0:43800->20009/tcp, 0.0.0.0:43799->20010/tcp

Using - "20000-20010:20000-20010" exposes these ports:

0.0.0.0:20000-20010->20000-20010/tcp

Which seems correct, but I'm unable to actually make any connections to them.

Edit2: Docker-compose

version: '3.2'
services:
  sshd:
    build: .
    ports:
      - "23:22"
      - "20000-20010:20000-20010"
    environment:
      REDIS_ADDRESS: redis
      DEBUG: 'sshd:*,ioredis:*'
  web:
    image: controller_web
    ports:
      - target: 3000
        published: 3000
        protocol: tcp
        mode: host
    environment:
      REDIS_ADDRESS: redis
      DEBUG: 'sshd:*,ioredis:*'
  redis:
    image: "redis"

Dockerfile

FROM node:alpine

# add openssh and clean
RUN apk add --update openssh \
&& rm  -rf /tmp/* /var/cache/apk/*

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

ARG NODE_ENV
ENV NODE_ENV $NODE_ENV
COPY package.json /usr/src/app/

RUN apk add --no-cache --virtual .gyp \
    python \
    make \
    g++

RUN npm install && npm cache clean --force
COPY . /usr/src/app

#make sure we get fresh keys
RUN rm -rf /etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_dsa_key

EXPOSE 22
EXPOSE 20000-30000
ENTRYPOINT ["sh", "entrypoint.sh"]
CMD ["npm", "start"]
like image 760
Excludos Avatar asked Mar 01 '18 15:03

Excludos


People also ask

What is the port range for Docker?

The default reserved ports are 22 for SSH, the Docker ports 2375 and 2376, and the Amazon ECS container agent ports 51678-51680.

Does Docker compose expose ports?

As presented above, we can also publish multiple container ports at once using different variants of the short syntax and configuring it more precisely. Docker Compose exposes all specified container ports, making them reachable internally and externally from the local machine.

Does Docker use port 80?

By default, the httpd server listens on port 80. It's not mandatory to perform port mapping for all Docker containers. Often, we will avoid opening host ports in order to keep the services of the container private or only visible to sibling containers in the same Docker network.

Is Docker compose deprecated?

You can still find Docker Compose V1 in the `master` branch. Compose V1 is marked as deprecated, and we'll begin patching only high-severity vulnerabilities or fixing critical bugs until the next milestone. Developers can continue to alias docker-compose to use docker compose.


1 Answers

I agree that the docker-compose ports documentation does not provide sufficient info on the syntax for the range mapping of ports. To understand the syntax, check the docker run documentation on ports.

In particular,

- "20000-20100" means: Expose the container ports in the range 20000 to 20100 into random ports on the host machine
- "10000-10100:20000-20100" means: Expose the container ports in the range 20000 to 20100 into random ports on the host machine in the range of 10000 to 10100
- "20000-20100:20000-20100" similar to the above

In your case, all these should allow you to access the containerized application

like image 113
yamenk Avatar answered Oct 07 '22 07:10

yamenk