Docker compose with Spring Boot Applications - spring-boot

I have two images- caller and callable.
When using docker compose and hitting the url of callable from caller image container, it is not working.
My docker compose file
version: "3.7"
services:
callerc:
image: caller:1.0
ports:
- "8000:8084"
environment:
- URL=callmec/callMe
callmec:
image: callable:1.0
ports:
- "5000:8082"
I get URI not absolute. When i try URL parameter as - http://callmec/callMe" then also it doens't work and says Connection refused.
But when I give the URL as my Machine IPv4 address - http://172.XX.XX.XX:5000/callMe then it works.
While trying "localhost:8000/callOtherService" gave Connection refused error - "callmec:5000/callMe": Connection refused
Kindly guide in case I have missed something.

You don't specify the port when you reference the container : http://callmec/callMe.
It uses the default HTTP port (80) such as http://callmec:80/callMe.
But no socket appears to listen that port on the callmec container.
What you want is : http://callmec:8082/callMe

callmec is not a name entry for 172.XX.XX.XX
If you want to use a local name for the Docker IP 172.XX.XX.XX then you have to add it to your hosts file:
On Linux you have to modify the /etc/hosts file.
Please read this document to see how you must do it on other OS: https://support.rackspace.com/how-to/modify-your-hosts-file/

Related

How to change docker port?

I have installed docker desktop on my macOS, now it's running in port 8000, how can I change to another port? because I want to use the 8000 port in another application. thanks
Docker Desktop for MAC itself does not run on a port.
It's a container running on a port.
The container has an internal port (within the container) and you can map that to a port on the host.
Mapping is done with "ports" on docker-compose.
For example, the file below has port 3011 internally on the Container, and this is mapped to 80 on the host
version: "3.9"
services:
web:
build: .
container_name: "Web"
env_file:
- env.settings
ports:
- "8080:3011"
(This script is missing a Dockerfile obviously. Let me know if you want a full working example)
Suppose this is running a simple NodeJS/Express Webserver.
If you are in the container, then access that Webserver on port 3011.
But on your host the Webserver is accessed on port 8080.

Springboot WebClient Broken in docker container

Iv created two Springboot applications that Iv dockerized and created local containers.
When I run the applications locally through intellij on my machine they work ok. Application A, on localhost:8080 has a Spring WebClient with a baseUrl localhost:8081 configured to call Application B running on port 8081. This works great.
The problem starts when I add those container to a docker compose file and spin then up
version: "3.7"
services:
appa:
image: application/myapp:1
hostname: localhost
ports:
- 8080:8080
appb:
image: application/myapp:2
hostname: localhost
ports:
- 8081:8081
I can hit localhost:8080 from the browser, but when the client in the application tried to call application b using the WebClient, it falls over with
Error has been observed at the following site(s):
| |_ checkpoint ⇢ Request to GET
http://localhost:8081/api/feed [DefaultWebClient]
| Stack trace:
| Caused by: java.net.ConnectException: finishConnect(..) failed:
Connection refused
I can hit both apps from the browser or curl, but it seems they cant communicate internally inside the docker containers
Any help appreciated
If you are using localhost inside one container to try to communicate with a service running inside another container, that's the wrong host to use. localhost inside a container refers to the container itself not its host. To establish a connection between containers, you'll need to use the IP address of the container that you want to connect to rather than localhost. This networking tutorial may be of interest.

ERROR: for csi_redis Cannot start service redis: Ports are not available: listen tcp 127.0.0.1:6379: bind: address already in use

I'm trying to install a project that runs:
PHP,
Laravel Framework,
Postgres DB,
NPM/NODE
I've installed postgresql and redis with brew.
When I get to docker-compose up -d I'm getting this error below: (nothing else is running..I'm able to get this to work on another computer but this one doesn't want to play nice, everything similar I've looked up here hasn't worked yet.)
Starting csi_redis ... error
ERROR: for csi_redis Cannot start service redis: Ports are not available: listen tcp 127.0.0.1:6379: bind: address already in use
ERROR: for redis Cannot start service redis: Ports are not available: listen tcp 127.0.0.1:6379: bind: address already in use
ERROR: Encountered errors while bringing up the project.```
When you install Redis with Homebrew, it listens on port 6379 on the host. If your docker-compose.yml file has a section like
version: '3'
services:
redis:
image: redis
ports:
- '127.0.0.1:6379:6379'
that also tries to listen to port 6379 on the host, which produces the error you're seeing.
You probably don't need two Redises for your project, so possibly the simplest answer is to brew uninstall redis, or at least brew services stop redis. You can interact with the containerized Redis the same way as you would have the Brew Redis.
If you need the host Redis for local development but the Docker Redis to run your project, you can pick a different port (or, potentially, delete the ports: entirely)
ports:
- '6380:6379' # keep the second port number the same

Setting redis configuration with docker in windows

I want to set up redis configuration in docker.
I have my own redis.conf under D:/redis/redis.conf and have configured it to have bind 127.0.0.1 and have uncommented requirepass foobared
Then used this command to load this configuration in docker:
docker run --volume D:/redis/redis.conf:/usr/local/etc/redis/redis.conf --name myredis redis redis-server /usr/local/etc/redis/redis.conf
Next,
I have docker-compose.yml in my application in maven Project under src/resources.
I have the following in my docker-compase.yml
redis:
image: redis
ports:
- "6379:6379"
And i execute the command :
docker-compose up
The Server runs, but when i check with the command:
docker ps -a
it Shows that redis Image runs at 0.0.0.0:6379.
I want it to run at 127.0.0.1.
How do i get that?
isn't my configuration file loading or is it wrong? or my commands are wrong?
Any suggestions are of great help.
PS: I am using Windows.
Thanks
Try to execute:
docker inspect <container_id>
And use "NetworkSettings"->"Gateway" (it must be 172.17.0.1) value instead of 127.0.0.1.
You can't use 127.0.0.1 as your Redis was run in the isolated environment.
Or you can link your containers.
So first of all you should not be worried about redis saying listening on 0.0.0.0:6379. Because redis is running inside the container. And if it doesn't listen on 0.0.0.0 then you won't be able to make any connections.
Next if you want redis to only listen on localhost on localhost then you need to use below
redis:
image: redis
ports:
- "127.0.0.1:6379:6379"
PS: I have not run container or docker for windows with 127.0.0.1 port mapping, so you will have to see if it works. Because host networking in Windows, Mac and Linux are different and may not work this way

boot2docker access elasticsearch on localhost

I'm running boot2docker on a Mac for my development. I built a Docker image containing a Jetty server which is connecting to elasticsearch at localhost together with Redis and MySQL.
I'm running docker-compose with a host bridge configuration which looks like the following:
api:
image: api
ports:
- "8080:8080"
environment:
JETTY_ENVIRONMENT: dev
net: "host"
What I want is accessing elasticsearch which I installed on my Mac via localhost:9200.
Try this? I know we're not supposed to answer with links, but I thought it'd be OK since it's a link to a boot2docker file on Github.

Resources