I have the following docker network:
services:
service:
build:
context: ../service
dockerfile: ../service/Dockerfile
depends_on:
- database
ports:
- "8080:8080"
networks:
- mynetwork
database:
image: mcr.microsoft.com/mssql/server:2022-latest
ports:
- "14333:1433"
networks:
- mynetwork
command: /bin/bash -c "/opt/mssql/bin/sqlservr"
init-db:
profiles: [initdb]
image: mcr.microsoft.com/mssql-tools:latest
build:
context: ../database
dockerfile: ./initdb.Dockerfile
depends_on:
- database
networks:
- mynetwork
networks:
mynetwork:
It contains the following services:
- database: which is a SQL database
- service: a golang REST API, which utilizes the database (this connects successfully)
- init-db: a script that needs to be occasionally run to seed the database, consists of some shell scripts
My flow for setting up docker network consists of two commands these are run sequentially:
docker compose up
This successfully spins up service
and database
services and I can see in the logs that the connection between them is successful
Then I run:
docker compose --profile initdb up --build
Info on profiles: https://docs.docker.com/compose/compose-file/15-profiles/?highlight=profiles
This runs a bunch of shell scripts, in these scripts I try to ping the database container on host database
however it says host not found
.
Additionally, while this initdb
profile is running if I inspect the network I can only see the service and database services, which explains the conenctivity issues.
docker network inspect local-infra_mynetwork
[
{
"Name": "local-infra_mynetwork",
"Id": "",
"Created": "2024-06-14T15:00:30.235377615Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "192.168.0.0/20",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"358112012d019169748da32bf20e2bd73b8c714408c58ae4dfec88a48d2892d4": {
"Name": "local-infra-service-1",
"IPv4Address": "192.168.0.3/20",
"IPv6Address": ""
},
"952e3c1e4bb0f57f132d1f88738e5f08a52dfcf2715f4e2027a1147ce73b559e": {
"Name": "local-infra-database-1",
"IPv4Address": "192.168.0.2/20",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "mynetwork",
"com.docker.compose.project": "local-infra",
"com.docker.compose.version": "2.27.0"
}
}
]
I would expect that the init-db
service would be present in mynetwork
as it clearly has it defined in the docker-compose.yml
Question
-
Is this connectivity problem a result of using profiles and excuting these routines seperately?
-
Or is it to do with the fact that init-db isnt a continually running service like service and database?
-
Or something to do with my docker network?