I'm running my Docker container and expecting to hit it's endpoints.
In this question, I provided my Dockerfile and gradle.build.
How to improve gradle.build file?
The Docker image is built successfully and when I run it I see how Spring Boot is starting including "Spring Boot logo" and Tomcat started on port(s): 9090 (http) with context path ''
I run my image with pavelpolubentcev$ docker run -i -t -e SERVER_PORT=9090 messenger-auth-auth
Nevertheless, I'm not able to access my endpoints, when I try http://localhost:9090 then no "Could not get any response".
When I run docker ps -a I can see my image running:
9d31b3e2aa63 messenger-auth-auth "java -jar /app/mess…" 6 minutes ago Up 6 minutes 8080/tcp practical_nightingale
But for some reason I also see 8080/tcp
What should I do to run it properly and finally get answer from my endpoints?
Thanks for your help, I appreciate it, I really need to solve the issue.
The 8080/tcp you see on output when listing running containers is defined in EXPOSE on the image Dockerfile. But this instruction doesn't actually publish the port.
You have to map your host (machine) port to the container port in order to forward TCP/UDP from host to container.
That said, in your case, the command to run the container is:
docker run -e SERVER_PORT=9090 -p 9090:9090 messenger-auth-auth
Map the container port to an external port : docker run -p 9090:9090 ...
The first port is the outside port (which one you want to access now from the host machine) and the second is the inside port (the port on the container).
You can specify the same external port or not.
Related
I am building a Spring Boot application for providing some REST services and I'd like to import it in Docker. If I run my application within IntelliJ pressing the run button, I can load the endpoints correctly.
My app is listening on the port 8091, as my dockerfile is the following
FROM openjdk:11
ADD out/artifacts/web_services_main_jar/web_services.main.jar lib_image.jar
EXPOSE 8091
ENTRYPOINT ["java","-jar","lib_image.jar"]
The code for building and running the docker container is the following
docker build --build-arg JAR_FILE=out/artifacts/web_services_main_jar/web_services.main.jar -t lib_proj .
docker run -p 8090:8091 lib_proj
The problem is that when the application is running in docker and I try to load "localhost:8090/user" in a browser, chrome returns the ERR_EMPTY_RESPONSE message.
If I open Docker and open the CLI of the container and I run
curl localhost:8091/user
even here an error is printed "curl: (7) Failed to connect to localhost port 8091: Connection refused". Any suggestion?
EDIT: my application.properties already specifies address and port as follows:
server.address=0.0.0.0
server.port=8091
Ok a workaround I found is to leave all the port to the default 8080. So i replaced the 8091 in the dockerfile and the 8090 in the "docker run ..." command to 8080 and now it works here. I assume because tomcat behind the scene only listens to this one, and I'm figuring out how to change this.
I'm trying to access the welcome page of Wildfly running on a Docker container in Windows 10 Pro. This is what I did:
Pulled the image:
docker pull jboss/wildfly
Run Wildfly container (this works fine, in the Wildfly log I can see it started correctly):
docker run -it -p 8080:8080 jboss/wildfly
Find the container ID:
docker ps
Inspect the IP address:
docker inspect -f "{{ .NetworkSettings.IPAddress }}" cac63ed21d78
The IP address is 172.17.0.2, in a browser I go to http://172.17.0.2:8080/ but the browser hangs and times out. What am I missing?
UPDATE
I also tried with 127.0.0.1:8080 and it's not working either
UPDATE2
Console log:
docker --version
#Docker version 19.03.1, build 74b1e89e8a
docker run hello-world
#Hello from Docker!
docker run --detach --publish 8080:80 --name webserver nginx
#Unable to find image 'nginx:latest' locally
#latest: Pulling from library/nginx
#8ec398bc0356: Pull complete
#465560073b6f: Pull complete
#f473f9fd0a8c: Pull complete
#Digest: sha256:b2d89d0a210398b4d1120b3e3a7672c16a4ba09c2c4a0395f18b9f7999b768f2
#Status: Downloaded newer image for nginx:latest
#c5cdb6de11240b5fe33bc424779721e1b44948797fd6ff389004d0766b71dd17
docker ps
#CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c5cdb6de1124
#nginx "nginx -g 'daemon of" 10 minutes ago Up 10 minutes 0.0.0.0:8080->80/tcp webserver
By default, when you create a container, it does not publish any of its ports to the outside world. Network 172.17.X.X is internal. If you need to bind IP address to host ip run docker container with -p flag, like that:
-p 8080:8080
Map TCP port 8080 in the container to port 8080 on the Docker host
From: https://docs.docker.com/docker-for-windows/networking/
Port forwarding works for localhost; --publish, -p, or -P all work. Ports exposed from Linux are forwarded to the host.
So it should be accessible from http://localhost:8080
If that doesn't work, then try the windows example
docker run -d -p 80:80 --name webserver nginx
Which should be accessible http://localhost:80
https://docs.docker.com/docker-for-windows/index#explore-the-application-and-run-examples
If even that fails try:
docker-machine ip default
And use http://[docker-machine-ip]:80
The key information is Docker container in Windows 10 Pro - this is very generic specification how your Docker on Windows works. There can be many options e.g. Docker for Windows with/without Linux containers, Docker toolbox, remote instance, ....
Generally Docker containers on Windows are running in some kind of virtual machine (Hyper-V, Virtualbox, ...), usually. So there is additional network layer, which may not be accessible directly from your Windows network namespace. But linked example runs on Linux machine, where this additional network layer doesn't exist. And that is a reason why copy/pasted Linux example doesn't work on Windows. So run container as usual and expose port 8080:
docker run -it -p 8080:8080 jboss/wildfly
But IP for access will be different and container IP can't be used, because that internal docker network is very likely not accessible from Windows. Rather try to use IP of your Windows OS. Eventually check documentation of used Docker on Windows solution and find which IP is used for exposed ports.
If you have advance windows/linux networking skills, then you may somehow route/forward port from container network namespace, through intermediate VM network layer to Windows network layer. But it can be pretty complicated.
I had a peer working on a windows system that faced the same issue. He put a lot of effort into it and just couldn't get anywhere. There's a lot of weird stuff that happens with Docker Desktop for windows it seems. We finally just installed the linux subsystem for windows and installed docker on there and it worked like a charm for him from then on. If you wish to stick to windows and you don't mind working on the linux subsystem, I suggest you go down that route. We went further than what Jan Garaj suggested and just found it to be a waste of time. Docker Desktop for Windows is meant to be a env where you completely work within it - using dev containers etc. Host-Container stuff is a pain with it.
I have deployed a very simple MSSQL docker container using the following docker run command :
docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=NotYourBusiness" -p 2433:1433 --name sql1 -d mcr.microsoft.com/mssql/server:2017-latest
I have SSMS installed on my machine and i'm able to connect to this instance with the following URL :
MyHostName,2433
I am able to run my spring boot app from my machine also with the following connection string :
dataSource.setJdbcUrl("jdbc:sqlserver://localhost:2433;database=SomeDatabase;");
With only MSSQL in a docker container, my application works perfectly from my machine.
Now, i want to put my spring boot app into a container as well. I have therefore built the following docker file :
FROM openjdk:11-jre-slim
VOLUME /tmp
EXPOSE 8082
ADD target/tno-1.0.jar tno-1.0.jar
ENTRYPOINT ["java","-jar","tno-1.0.jar"]
This is the build command i use to create the image :
docker build -f Dockerfile -t tno .
And this is the command i use to build the container :
docker run -t --name tno --link sql1:mssql -p 8082:8082 tno
Using the same connection string that works on my machine, the spring boot app fails to start with a connection refused error. I did look at many posts and they pointed out that the term "localhost" no longer really applies when running from a container since it refers to that container only. The only way i was able to make it work is to replace localhost:2433 with the container IP address : 1433.
This is perfectly acceptable but is there a way for a container to behave like my dev machine and be able to connect to another container as if the connection was coming from the outside world?
Thanks
If you can access to the database from the application running in other containers, try to configure your jdbc url from localhost to mssql (name of db link).
jdbc:sqlserver://mssql:2433;database=SomeDatabase;
Let me know or check this how to link container in docker?
So I have dockerized a service "myxxx-service" along with Tomcat.
Dockerfile:
FROM tomcat:8-jre8
EXPOSE 8080
COPY ./myxxx-service/target/myxxx-service-1.0-SNAPSHOT.war /usr/local/tomcat/webapps
cmd mkdir -p /opt/myxxx/db
copy ./myxxx-service/src/test/resources/* /opt/myxxx/db/
CMD ["catalina.sh", "run"]
When I run the docker image, I am able to get the catalina.out logs. The tomcat becomes up. I am able to open the standard tomcat page on 8080 port.
But my service shows 404 error reponse.
I want to check my service specific logs that generally comes in the logs/myxxx-service.logs file.
Is there a way to do that?
Note: I have tried docker service logs. I am not sure if that works.
What's the output of your log? Is it file?
You can always get into the instance by:
docker exec -it <container-id/container-name> sh
And then you can navigate to your logs. If you use vendors like AWS then, in your container settings, you can use aws-log driver and send your logs to CloudWatch.
I have developed a small Java demo web app, using gradle, which I want to dockerize with WildFly. I have followed instructions from here.
The Dockerfile is:
FROM jboss/wildfly
MAINTAINER Me <me#qmail.com>
RUN /opt/jboss/wildfly/bin/add-user.sh admin Admin#70365 --silent
ADD build/libs/my-demo.war /opt/jboss/wildfly/standalone/deployments/
When I start the image with Docker and browse localhost:8080 or localhost:9990, I get a This site can’t be reached.
Yet, my application runs successfully on localhost:8080 when I use gradle appRun.
What is missing?
I am under Windows 10 Home Edition. I have tried on another laptop under Ubuntu 16 and face the same issue.
Three things:
1st
the base image EXPOSEs only port 8080, so to be able to access port 9990 you need to add EXPOSE 9990 to your Dockerfile or --expose 9990 to your docker runcall.
2nd
You didn't post your cmd line call, so I can only guess but you need to map the container port to a host port, example (including the additional exposed port)
docker run --expose 9990 -p 9990:9990 -p 8080:8080 -it my-demo
3rd
If your working with docker-machine as it is still the case with Win 10 home as far as I recall, you won't have your application on localhost but at the IP of the docker-machine VM. You can find out which IP that is by calling
docker-machine ip
On linux you will have your app on localhost:PORT once you add the port mapping.