Problem to connect postgres instance in local CodeBuild
I have buildspec.yml
version: 0.2
phases:
install:
commands:
- docker pull postgres
pre_build:
commands:
- docker run -p 5432:5432 --name sample-postgres -e POSTGRES_PASSWORD=sample -e POSTGRES_USER=sample -e POSTGRES_DB=sample -d postgres >> container.txt
build:
commands:
- sleep 20s
- apt-get install -y telnet iproute2 net-tools
- docker inspect sample-postgres >> container.txt
- ip addr show >> container.txt
- route >> container.txt
- npm i --save pg
- node index.js >> container.txt
post_build:
commands:
- docker rm -f sample-postgres
artifacts:
files:
- container.txt
Postgres already created and running.
Postgres connection string:
postgresql://sample:[email protected]:5432/sample
Can`t connect to postgres in CodeBuild. I got next error:
Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5432
}
Docker inspect command of sample-postgres
"NetworkSettings": {
"Bridge": "",
"SandboxID": "bf27197fe81dc940a55351c128c87022a963261b9528e5c38f35fad70d1a1c77",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"5432/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "5432"
}
]
},
"SandboxKey": "/var/run/docker/netns/bf27197fe81d",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "95c48a5070ccd6475292c259bcde4514174f3ff3b042a2d54dcde50b0e80ea3a",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:03",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "930c686de17cc1c7eed52251f1c860c3564b8b34348fea6c3906d8e9436778a3",
"EndpointID": "95c48a5070ccd6475292c259bcde4514174f3ff3b042a2d54dcde50b0e80ea3a",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.3",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:03",
"DriverOpts": null
}
}
}
I trying connect with telnet in buildspec.yml to database container sample-postgres with IPGateway, IPAddress and got error connection refused
Hello, I tried with adding the following commands to the build spec.
- ipAddress=
docker inspect --format '{{ .NetworkSettings.IPAddress }}' sample-postgres - telnet $ipAddress 5432 I have the following response in my log:
Trying 172.18.0.2...
Connected to 172.18.0.2. Escape character is '^]'.
Seems like it works. Could you try it out and see if it works or not?
Thanks! Xin
@xinw0911, thanks for the answer
[Container] 2020/03/25 06:45:51 Running command ipAddress=$(docker inspect --format '{{ .NetworkSettings.IPAddress }}' sample-postgres)
[Container] 2020/03/25 06:45:51 Running command telnet $ipAddress 5432
telnet: Unable to connect to remote host: Connection timed out
Trying 172.17.0.3...
CodeBuild logs
Build Command:
docker run -it -v /var/run/docker.sock:/var/run/docker.sock -e "IMAGE_NAME=aws/codebuild/standard:3.0" -e "ARTIFACTS=/home/user/CodeBuildLocal/" -e "SOURCE=/home/user/CodeBuildLocal/sample-web-app" -e "BUILDSPEC=/home/user/CodeBuildLocal/sample-web-app/buildspec.yml" -e "INITIATOR=user" amazon/aws-codebuild-local:latest
Removing network agent-resources_default
Removing volume agent-resources_source_volume
Removing volume agent-resources_user_volume
Creating network "agent-resources_default" with the default driver
Creating volume "agent-resources_source_volume" with local driver
Creating volume "agent-resources_user_volume" with local driver
Creating agent-resources_agent_1 ... done
Creating agent-resources_build_1 ... done
Attaching to agent-resources_agent_1, agent-resources_build_1
agent_1 | WARNING: no logs are available with the 'local' log driver
build_1 | WARNING: no logs are available with the 'local' log driver
But it works in AWS CodeBuild ( non local )
@Insidexa are you running OSX? I wasn't able to connect to postgres either until I changed host to host.docker.internal. https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds
@joni- I am not using OSX, I use Fedora 32
Ive run into the same issue. Using docker-compose to bring up containers and connect to them does not work using 'localhost' in the local runner but found that it works fine in the actual codebuild project. minimal reproducible example here https://github.com/ThrowsException/aws-docker-in-docker just doing a simple curl from the script to a running nginx container.