I'm running a spring boot application in the background as an API server, but I cannot access it from localhost:8080.
Container status
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c494c8304e0 corpobids.com/server:latest "java -jar server.jar" 5 seconds ago Up 4 seconds 0.0.0.0:8080->8080/tcp, 0.0.0.0:18080->18080/tcp server_api-server_1
8d1883ebcb0b postgres:9.6 "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 0.0.0.0:5432->5432/tcp server_database-server_1
The ports are already exposed and mapped. The application is running fine in the container.
2018-10-31 15:25:21.255 INFO 1 --- [ main]
o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s):
8080 (http) with context path ''
2018-10-31 15:25:21.256 INFO 1 --- [ main]
com.api.server.Application : Started Application in
13.861 seconds (JVM running for 14.25)
How do I call the API of the application from my host computer?
Related
With docker service I can get the following running tasks and associated nodes. I am wondering how each running task can retrieve its node ID and task name? Is there any environment variable to access those? If not how can I set one?
$ docker service ps appservice
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
0qihejybwf1x appservice.1 appservice:3.0.5 manager1 Running Running 8 seconds
bk658fpbex0d appservice.2 appservice:3.0.5 worker2 Running Running 9 seconds
5ls5s5fldaqg appservice.3 appservice:3.0.5 worker1 Running Running 9 seconds
8ryt076polmc appservice.4 appservice:3.0.5 worker1 Running Running 9 seconds
1x0v8yomsncd appservice.5 appservice:3.0.5 manager1 Running Running 8 seconds
71v7je3el7rr appservice.6 appservice:3.0.5 worker2 Running Running 9 seconds
4l3zm9b7tfr7 appservice.7 appservice:3.0.5 worker2 Running Running 9 seconds
9tfpyixiy2i7 appservice.8 appservice:3.0.5 worker1 Running Running 9 seconds
3w1wu13yupln appservice.9 appservice:3.0.5 manager1 Running Running 8 seconds
8eaxrb2fqpbn appservice.10 appservice:3.0.5 manager1 Running Running 8 seconds
I was able to achieve this by setting environment variables in the docker-compose. To retrieve task id I added following line in the service configuration:
environment:
- MYTASKID={{.Task.ID}}
I have created a docker image for deployment of microservice.
I am right now testing ti by trying to deploy on my local machine.
The docker container ogt created successfully and I am getting started application
o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-25 12:41:53.867 INFO [] 1 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2019-10-25 12:41:53.901 INFO [] 1 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2019-10-25 12:41:53.949 INFO [] 1 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references
2019-10-25 12:41:54.230 INFO [] 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
But when I try to hit any rest service like so localhost:8080/myApi/test
It gives me error
localhost refused to connect.
Search Google for localhost 8080 payment
ERR_CONNECTION_REFUSED
What could possibly be going wrong ?
Checking list of running images docker ps gives me an entry with empty port details .Could this be the reason ?
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2feab85c47db test_3 "java -Dserver.port=…" 18 minutes ago Up 18 minutes wizardly_leavitt
To access services inside containers you need to forward host ports to container ports.
It can be done with -p HOST_PORT:CONTAINER_PORT parameter in the command line.
docker run -it -p 8080:8080 test_3
REF: https://docs.docker.com/config/containers/container-networking/
Solved at bottom
But why do I have to append :4000?
I'm following the docker get-started Guide here, https://docs.docker.com/get-started/part4/
I'm fairly certain I've done everything correctly, but am wondering why I can't connect to view the app after deploying it.
I've set my env to my VM, myvm1, for reference to following commands.
docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
099e16249604 beresj/getting-started:part2 "python app.py" 12 seconds ago Up 12 seconds 80/tcp getstartedlab_web.5.y0e2k1r1ev47u24e5iufkyn3i
6f9a24b343a7 beresj/getting-started:part2 "python app.py" 12 seconds ago Up 12 seconds 80/tcp getstartedlab_web.3.1pls3osj3uhsb5dyqtt4ts8j6
docker image ls -a
REPOSITORY TAG IMAGE ID CREATED SIZE
beresj/getting-started <none> e290b6208c21 22 hours ago 131MB
docker stack ls
NAME SERVICES ORCHESTRATOR
getstartedlab 1 Swarm
docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
myvm1 * virtualbox Running tcp://192.168.99.100:2376 v18.09.6
myvm2 - virtualbox Running tcp://192.168.99.101:2376 v18.09.6
docker stack ps getstartedlab
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
vkxx79fh3h85 getstartedlab_web.1 beresj/getting-started:part2 myvm2 Running Running 3 minutes ago
qexbaa3wz0pd getstartedlab_web.2 beresj/getting-started:part2 myvm2 Running Running 3 minutes ago
1pls3osj3uhs getstartedlab_web.3 beresj/getting-started:part2 myvm1 Running Running 3 minutes ago
ucuwen1jrncf getstartedlab_web.4 beresj/getting-started:part2 myvm2 Running Running 3 minutes ago
y0e2k1r1ev47 getstartedlab_web.5 beresj/getting-started:part2 myvm1 Running Running 3 minutes ago
curl 192.168.99.100
curl: (7) Failed to connect to 192.168.99.100 port 80: Connection refused
docker info
Containers: 2
Running: 2
Paused: 0
Stopped: 0
Images: 1
Server Version: 18.09.6
...
Swarm: active
NodeID: 0p9qrax9h3by0fupat8ufkfbq
Is Manager: true
ClusterID: 7vnqdk85n8jx6fqck9k7dv2ka
Managers: 1
Nodes: 2
Default Address Pool: 10.0.0.0/8
...
Node Address: 192.168.99.100
Manager Addresses:
192.168.99.100:2377
...
Kernel Version: 4.14.116-boot2docker
Operating System: Boot2Docker 18.09.6 (TCL 8.2.1)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 989.4MiB
Name: myvm1
I would expect to see what I was able to see when I just ran it on my local machine instead of on a VM in a swarm (I think I have the lingo correct?)
Not sure how to check open ports.
Again: this works if I simply remove the stack, unset the docker-machine environment, and just run:
docker stack deploy -c docker-compose.yml getstartedlab
not on the vm.
Thank you in advance. (Also, I'm new hence the get-started guide so I appreciate any help)
Edit
It works if I append :4000 to the VM IP in my url, ex: 192.168.99.100:4000 or 192.168.99.101:4000. It shows the two container Id's listed in 'docker container ls' for myvm1, and the other three are from myvm2. Could anyone tell me why I have to append 4000? Is it because I have ports: "4000:80" in my docker-compose.yml?
Not sure if this will help but if you use docker inspect <instance_id_here>, you can see what ports are exposed.
Exposed ports aren't open ports. You would need to bind a host port to a container port in the docker-compose.yml in order for it to be to be open.
I installed docker and issues a 'docker swarm init' command.
I'm trying to launch a stack using the following command: docker stack deploy -c docker-compose.yml mystack
The docker-compose file can be found here, the first docker file here and the second here
The output of 'docker ps' is:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f582b3e8d33e tons/ip2country:latest "/bin/sh -c 'java -D…" 8 seconds ago Up 6 seconds 8080/tcp ip2flag_country-service.1.t5rvuqaw8tj7v20u0xo0dgy6x
bbf2c8304f1a tons/ip2flag:latest "/bin/sh -c 'java -D…" 10 seconds ago Up 8 seconds 8080/tcp ip2flag_app.1.z00gz8adj2yshpgimaw2o55d3
cbc7eaace4bf portainer/portainer "/portainer" 39 minutes ago Up 39 minutes 0.0.0.0:9000->9000/tcp portainer
The output of 'docker service ls' is:
ID NAME MODE REPLICAS IMAGE PORTS
ex51pyh1oyyo ip2flag_app replicated 1/1 tons/ip2flag:latest *:8080->8080/tcp
yhbt97lmjqan ip2flag_country-service replicated 1/1 tons/ip2country:latest
Since I'm running this on localhost I'd expect http://localhost:8080/ to return some sort of data. But it just times out. If I attach to the container and execute something like wget localhost:8080/some/path it works as expected. So the service is running and within the container listening to port 8080. However the port isn't exposed outside of dockers net. Further more I can add that launching with 'docker-compose up' works just fine too. But not with 'docker stack deploy'. Any clue about what I'm doing wrong?
Using the Docker tutorial I'm stuck at this part: https://docs.docker.com/get-started/part3/#run-your-new-load-balanced-app
I use curl -4 http://localhost but i get a curl: (7) Failed to connect to localhost port 80: Connection refused error.
output of previous step:
docker service ps getstartedlab_web
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
kqu5qggifnlm getstartedlab_web.1 s1mpl3/get-started:part2 moby Running Running 29 minutes ago
prhrmm6hpop3 getstartedlab_web.2 s1mpl3/get-started:part2 moby Running Running 29 minutes ago
ytrwy5gxp2rk getstartedlab_web.3 s1mpl3/get-started:part2 moby Running Running 29 minutes ago
mayvauijghbj getstartedlab_web.4 s1mpl3/get-started:part2 moby Running Running 29 minutes ago
r625x2k7n6ta getstartedlab_web.5 s1mpl3/get-started:part2 moby Running Running 29 minutes ago
So error and ports are empty.
What should I analyse to fix this issue?
For part 4 when you deploy to your swarm, you get an URL with docker-machine ls.
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
myvm1 * virtualbox Running tcp://192.168.99.100:2376 v17.10.0-ce
myvm2 - virtualbox Running tcp://192.168.99.101:2376 v17.10.0-ce
Change in docker-compose.yml file 80:80 to 4000:80
Use 192.168.99.100:4000 and it should be working.