amigoscode / full-stack-professional

https://amigoscode.com/courses/full-stack-professional
432 stars 205 forks source link

make your containers talk to each other (without docker-compose) #10

Open hazartilirot opened 1 year ago

hazartilirot commented 1 year ago

Okay, the issue I had personally (you most likely have the same) I didn't use his settings in docker-compose.yml file for database. I actually had a container with many databases running so I decided to stick to my existent database.

docker run -d --name YOUR_DOCKER_CONTAINER_NAME-api --rm -p 8080:8080 --network DATABASE_NETWORK YOUR_DOCKER_ACCOUNT/YOUR_CONTAINER_NAME-api --spring.datasource.url=jdbc:postgresql://THING_WE_ARE_LOOKING_FOR:5432/customer

-d - detached mode

--name YOUR_DOCKER_CONTAINER_NAME-api - in your case it might be different.

--network DATABASE_NETWORK -

if you have your database container running, just use the command:

docker network ls

it will list all of your networks. You're interested in the Name of your database ID's container. In my case it's hibernate-starter_default Basically, we need to connect the container to this bridge.

YOUR_DOCKER_ACCOUNT/YOUR_CONTAINER_NAME-api - see the info in the docker repository it is a new container you have pushed.

--spring.datasource.url=jdbc:postgresql://THING_WE_ARE_LOOKING_FOR:5432/customer - it's really tricky one.

docker inspect ID_DATABASE_CONTAINER -f "{{json .NetworkSettings.Networks }}"

It will give you something like this:

{"hibernate-starter_default":{"IPAMConfig":null,"Links":null,"Aliases":["hibernate-starter-db-1","db","8543027d7b75"],"NetworkID":"4ad36a6716d855dfc93e1192acfae8024fa043b5118602559b5d6f130ed9994d","EndpointID":"7d4f4d8c66f7213a513774952a3ea55c790d0715a705eacd5fc01e0ede6e45b5","Gateway":"172.18.0.1","IPAddress":"172.18.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:12:00:02","DriverOpts":null}}

You may use any Aliases name, like hibernate-starter-db-1 or db or 8543027d7b75 so in my case, I use

--spring.datasource.url=jdbc:postgresql://db:5432/customer