Reputation: 6095
My application consists of a Spring Boot app and a database. I can successfully run them in Docker using docker-compose. I now want to use a similar Docker compose file along with testcontainers to write some automated tests. I am failing because the hostname in the application.yml file of the Spring Boot app doesn't match the random name assigned to the database container and therefore the app is unable to connect to the database.
version: '2'
services:
api:
image: simon/api:1.0.0-SNAPSHOT
networks:
- my_network
api-db:
image: simon/api-db:1.0.0-SNAPSHOT
networks:
- my_network
networks:
my_network:
external: false
@ClassRule public static DockerComposeContainer<?> dockerEnvironment =
new DockerComposeContainer<>(new File("docker-compose.yml"))
.withPull(false)
.withLocalCompose(true)
.withExposedService("api", "8080");
spring:
profiles: docker
datasource:
url: jdbc:postgresql://api-db:5432/api
When the test runs, the containers are assigned names such as:
wtdopq2hneev_api_1 wtdopq2hneev_api-db_1
The fact that it seems to be assigning a random network name (wtdopq2hneev) rather than using my_network, is ultimately my problem.
I can specify the name of the container in the docker-compose.yml file, but then I can't 'expose' the services which I need to be able to so that I can call my API from my tests:
Am I going about this in the wrong way?
The Docker version is a little old but I don't think that's the issue
Upvotes: 3
Views: 3617
Reputation: 1244
The container name shouldn't matter here, it's the hostname that is important, and in your case this will be api
and api-db
. For this simple use case there is probably no need for a custom network, did you try it without one?
Upvotes: 1
Reputation: 688
Just set a container name and then you will know it:
services:
redis:
image: redis:alpine
container_name: whatever-you-fancy
Upvotes: -1