问题
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"]
回答1:
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
来源:https://stackoverflow.com/questions/49053618/docker-compose-port-range-forward