How can I expose more than 1 port with Docker? - docker

So I have 3 ports that should be exposed to the machine's interface. Is it possible to do this with a Docker container?

To expose just one port, this is what you need to do:
docker run -p <host_port>:<container_port>
To expose multiple ports, simply provide multiple -p arguments:
docker run -p <host_port1>:<container_port1> -p <host_port2>:<container_port2>

Step1
In your Dockerfile, you can use the verb EXPOSE to expose multiple ports.
e.g.
EXPOSE 3000 80 443 22
Step2
You then would like to build an new image based on above Dockerfile.
e.g.
docker build -t foo:tag .
Step3
Then you can use the -p to map host port with the container port, as defined in above EXPOSE of Dockerfile.
e.g.
docker run -p 3001:3000 -p 23:22
In case you would like to expose a range of continuous ports, you can run docker like this:
docker run -it -p 7100-7120:7100-7120/tcp

if you use docker-compose.ymlfile:
services:
varnish:
ports:
- 80
- 6081
You can also specify the host/network port as HOST/NETWORK_PORT:CONTAINER_PORT
varnish:
ports:
- 81:80
- 6081:6081

Use this as an example:
docker create --name new_ubuntu -it -p 8080:8080 -p 15672:15672 -p 5432:5432 ubuntu:latest bash
look what you've created(and copy its CONTAINER ID xxxxx):
docker ps -a
now write the miracle maker word(start):
docker start xxxxx
good luck

Only one point to add. you have the option to specify a range of ports to expose in the dockerfile and when running it:
on dockerfile:
EXPOSE 8888-8898
Build image:
docker build -t <image_name>:<version> -f dockerfile .
When running the image:
docker run -it -p 8888-8898:8888-8898 -v C:\x\x\x:/app <image_name>:<version>

If you are creating a container from an image and like to expose multiple ports (not publish) you can use the following command:
docker create --name `container name` --expose 7000 --expose 7001 `image name`
Now, when you start this container using the docker start command, the configured ports above will be exposed.

Related

Docker port mapping issue

Simple docker file:
FROM openjdk:8u151
EXPOSE 8080:8080
ADD /target/myJar.jar myJar.jar
ENTRYPOINT ["java", "-jar", "myJar.jar"]
Docker run command:
docker run myjar-image -p 8080:8080
Docker ps:
PORTS
8080/tcp
It should be:
PORTS
0.0.0.0:8080->8080/tcp
There is no indication of an error (silent failure). Any thoughts would be much appreciated!
Docker-specific options come before the image name.
docker run -p 8080:8080 myjar-image
Anything passed after the image name is interpreted as the "command" and passed as additional arguments to the entrypoint; as you've launched it your container actually starts (with no published ports)
java -jar myJar.jar -p 8080:8080
which could in principle be useful but isn't what you're trying for here.

Unable to connect to Rabbit MQ instance when running from docker container built by dockerfile

We are attempting to put an instance of rabbit mq into our Kubernetes environment. To do so, we have to implement it into our build and release process, which includes creating a docker container by Dockerfile.
During our original testing, we created the docker container manually with the following commands, and it worked correctly:
docker pull rabbitmq
docker run -p 5672:5672 -d --hostname my-rabbit --name some-rabbit rabbitmq:3
docker start some-rabbit
To create our docker file, we have tried various iterations, with the latest being:
FROM rabbitmq:3 AS rabbitmq
RUN rabbitmq-server -p 5672:5672 -d --hostname my-rabbit --name some-rabbit
EXPOSE 5672
We have also tried it with just the Run rabbitmq-server and not the additional parameters.
This does create a rabbit mq instance that we are able to ssh into and verify it is running, but when we try to connect to it, we receive an error: "ExtendedSocketException: An attempt was made to access a socket in a way forbidden by its access permission" (we are using rabbit's default of 5672).
I'm not sure what the differences could be between what we've done in the command line and what has been done in the Dockerfile.
Looks like you need to expose quite a few other ports.
I was able to generate the Dockerfile commands for rabbitmq:latest (rabbitmq:3 looks the same) using this:
ENV PATH=/usr/lib/rabbitmq/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
ENV GOSU_VERSION=1.10
ENV RABBITMQ_LOGS=-
ENV RABBITMQ_SASL_LOGS=-
ENV RABBITMQ_GPG_KEY=0A9AF2115F4687BD29803A206B73A36E6026DFCA
ENV RABBITMQ_VERSION=3.7.8
ENV RABBITMQ_GITHUB_TAG=v3.7.8
ENV RABBITMQ_DEBIAN_VERSION=3.7.8-1
ENV LANG=C.UTF-8
ENV HOME=/var/lib/rabbitmq
EXPOSE 25672/tcp
EXPOSE 4369/tcp
EXPOSE 5671/tcp
EXPOSE 5672/tcp
VOLUME /var/lib/rabbitmq
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["rabbitmq-server"]
Dockerfile is used to build your own image, not to run a container. The question is - why do you need to build your own rabbitmq image? If you don't - then just use the official rabbitmq image (as you originally did).
I'm sure it already has all the necessary EXPOSE directives built-in
Also note command line arguments "-p 5672:5672 -d --hostname my-rabbit --name some-rabbit rabbitmq:3" are passed to docker daemon, not to the rabbitmq process.
If you want to make sure you're forwarding all the necessary ports - just run it with -P.

Ngnix Docker not serving content

I'm trying to build a docker container out of an angular 4 application. When running it with
docker run -p 80:80 -v /Users/mles/Documents/devicelab/dist/devicelab:/usr/share/nginx/html:ro nginx
I can see the index.html in the browser with localhost:80.
To make it portable I've made a DOCKERFILE:
FROM nginx
COPY dist/devicelab /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
I'm building it with
docker build --no-cache --rm -t dockerregistry.test.com/devicelab/devicelab .
then running it with
docker run dockerregistry.test.com/devicelab/devicelab:latest
Now when I hit localhost:80 nothing is happening. Nothin shows up in the browser and there is no log on the console. What is wrong with my Dockerfile?
You forgot the port on docker run
docker run -p 80:80 dockerregistry.test.com/devicelab/devicelab:latest
#rafaelncarvalho was correct in regards to the -p param, but for clarification the -p is to publish the already exposed port to the outside the network. Without the -p nginx will be accessible if you exec -it bash in, but not outside. The -p aka --publish also allows gives you control for mapping the container port to what you choose. ex docker container run -p 8080:80 nginx will publish the nginx server on port 8080

How to configure jenkins run on port 80 use docker?

i use below command , but it doesn't work, we must use --ip to pass ip to pass given ip to docker
docker run -p 80:80080 --ip xx.xx.xx.xx jenkins
finnally i have resloved with add an environment parameter with docker command
-e JENKINS_OPTS="--httpPort=80"
docker run -p 80:8080 -d jenkins/jenkins:latest
docker run --name jenkinsci -p 8081:8080 jenkins/jenkins:lts
If 8080 is already in use, you can use 8081 but forward it to 8080, as jenkins starts on 8080
-p 8081:8080

Is posible define range of port to use in expose `-P` in docker?

Is posible something to limit the range of ports to expose with -P parameter, like as:
docker daemon --range-ports=2000-2099...
docker run -P... <- ports used between 2000 and 2099
or
docker daemon...
docker run -P --range-ports=2000-2099... <- ports used between 2000 and 2099`
you can give range of ports to be mapped;
docker run -d -p 8000-9000:5000 training/webapp python app.py
FYI I have created follow issue:
docker#20091
The answer was:
docker run .... -p 2000-2099:22 -p 2000-2099:23 -p 2000-2099:24 ....
Thank you for your answer

Resources