Docker port mapping issue - docker

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.

Related

How to properly expose/publish ports for a WebUI-based application in Docker?

I'm trying to port this webapp to Docker. I wrote the following Dockerfile:
FROM anapsix/alpine-java
MAINTAINER <name>
COPY aard2-web-0.7-java6.jar /home/aard2-web-0.7-java6.jar
COPY start.sh /home/start.sh
CMD ["bash", "/home/start.sh"]
EXPOSE 8013/tcp
Here are the contents of start.sh:
#!/bin/bash
java -Dslobber.browse=true -jar /home/aard2-web-0.7-java6.jar /home/dicts/*.slob
Then I built the image:
docker build -t aard2-docker .
And I used the following command to run the container:
docker run --name Aard2 -p 127.0.0.1:8013:8013 -v /home/<name>/dicts:/home/dicts aard2-docker
The app is running normally, prompting that it's listening at http://127.0.0.1:8013. However, I opened the address only to find that I couldn't connect to the app.
I tried using the EXPOSE command (as shown in the Dockerfile snippet above) and variants of the -p flag, such as -p 127.0.0.1:8013:8013, -p 8013:8013, -p 8013:8013/tcp, but none of them worked.
How can I expose/publish the port to 127.0.0.1 properly? Thanks!
Here's the response from the original author:
you need to tell the server to listen on all network interfaces instead of localhost - that is you are missing -Dslobber.host=0.0.0.0
this works for me:
FROM anapsix/alpine-java
COPY ./build/libs/aard2-web-0.7.jar /home/aard2-web-0.7.jar
CMD ["bash", "-c", "java -Dslobber.host=0.0.0.0 -jar /home/aard2-web-0.7.jar /dicts/*.slob"]
EXPOSE 8013/tcp
and then run like this:
docker run -v $HOME/Downloads:/dicts -p 8013:8013 --rm aard2-web
-Dslobber.browse=true opens default browser, I don't think this has any effect in docker so don't need that.
https://github.com/itkach/aard2-web/issues/12#issuecomment-895557949

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.

Spring Boot in Docker

I am learning how to use Docker with a Spring Boot app. I have run into a small snag and I hope someone can see the issue. My application relies heavily on #Value that are set in environment specific properties files. In my /src/main/resources I have three properties files
application.properties
application-local.properties
application-prod.properties
I normally start my app with:
java -jar -Dspring.profiles.active=local build/libs/finance-0.0.1-SNAPSHOT.jar
and that reads the "application-local.properties" and runs properly. However, I am using this src/main/docker/DockerFile:
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD finance-0.0.1-SNAPSHOT.jar finance.jar
RUN sh -c 'touch /finance.jar'
EXPOSE 8081
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /finance.jar" ]
And then I start it as:
docker run -p 8081:80 username/reponame/finance
-Dspring.profiles.active=local
I get errors that my #Values are not found:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.datasource.driverClassName' in value "${spring.datasource.driverClassName}"
However, that value does exist in both *.local & *.prop properties files.
spring.datasource.driverClassName=org.postgresql.Driver
Do I need to do anything special for that to be picked up?
UPDATE:
Based upon feedback from M. Deinum I changing my startup to be:
docker run -p 8081:80 username/reponame/finance
-eSPRING_PROFILES_ACTIVE=local
but that didn't work UNTIL I realized order matter, so now running:
docker run -e"SPRING_PROFILES_ACTIVE=test" -p 8081:80 username/reponame/finance
works just fine.
You can use docker run Using Spring Profiles. Running your freshly minted Docker image with Spring profiles is as easy as passing an environment variable to the Docker run command
$ docker run -e "SPRING_PROFILES_ACTIVE=prod" -p 8080:8080 -t springio/gs-spring-boot-docker
You can also debug the application in a Docker container. To debug the application JPDA Transport can can be used. So we’ll treat the container like a remote server. To enable this feature pass a java agent settings in JAVA_OPTS variable and map agent’s port to localhost during a container run.
$ docker run -e "JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n" -p 8080:8080 -p 5005:5005 -t springio/gs-spring-boot-docker
Resource Link:
Spring Boot with Docker
Using spring profile with docker for nightly and dev build:
Simply set the environment varialbe SPRING_PROFILES_ACTIVE when starting the container. This will switch the active of the Spring Application.
The following two lines will start the latest Planets dev build on port 8081 and the nightly build on port 8080.
docker run -d -p 8080:8080 -e \"SPRING_PROFILES_ACTIVE=nightly\" --name nightly-planets-server planets/server:nightly
docker run -d -p 8081:8080 -e \"SPRING_PROFILES_ACTIVE=dev\" --name dev-planets-server planets/server:latest
This can be done automatically from a CI system. The dev server contains the latest build and nightly will be deployed once a day...
There are 3 different ways to do this, as explained here
Passing Spring Profile in Dockerfile
Passing Spring Profile in Docker
run command
Passing Spring Profile in DockerCompose
Below an example for a spring boot project dockerfile
<pre>FROM java:8
ADD target/my-api.jar rest-api.jar
RUN bash -c 'touch /pegasus.jar'
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=dev","-jar","/rest-api.jar"]
</pre>
You can use the docker run command
docker run -d -p 8080:8080 -e "SPRING_PROFILES_ACTIVE=dev" --name rest-api dockerImage:latest
If you intend to use the docker compose you can use something like this
version: "3"
services:
rest-api:
image: rest-api:0.0.1
ports:
- "8080:8080"
environment:
- "SPRING_PROFILES_ACTIVE=dev"
More description and examples can be found here

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

How can I expose more than 1 port with 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.

Resources