Hi there! I noticed you’re using rake db:create – could you check that your database.yml has username/password statements set for all environments? Some more discussion available about this here.
That looks right honestly, works well locally.
My main question is, how I refer to the main container from inside the container that is running the app? Same way I’d use docker.for.mac.localhost
If I understand you correctly, you are running Docker in Docker, and you want your inner container (app) to reach out to the outer one (CircleCI build container) to connect to a database. I have a similar thing (Docker in VPS) where a Dockerised blog needs to connect to a MySQL instance on the VPS.
To do this:
# Get the host IP address
export DOCKER_HOSTIP=`ifconfig docker0 | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1`
echo "Connecting to database on Docker host ${DOCKER_HOSTIP}"
docker run \
--label $CONTAINER_LABEL \
--add-host=docker:${DOCKER_HOSTIP} \
--network dockernet \
--network-alias myblog \
--detach \
--restart always \
myblog
I can’t remember if network is required for that, but it’s add-host that does the work. Then, inside my container, DOCKER_HOSTIP is available to connect to.
Incidentally, this is not for CircleCI - just general infrastructure - but the principle should be the same for you.
Hmm, that’s odd. By virtue of installing Docker in your CircleCI container, I’d expect to see a docker0 in your networks, since your container has become a Docker host itself.
No, I’ve just spotted how much of a pickle your config is in!
I originally assumed that by virtue of the explicit docker commands, you were running one CircleCI container and inside that running Docker-in-Docker. However, it looks like you are also running a secondary PostgreSQL container.
If this configuration were to work, it would give you two PostgreSQL databases, and I don’t think that is what you want. One would be at the same “Docker level” as your primary build container, and one would be in a Docker system inside your primary build container (which itself is running in Docker, on the build host).
I wonder if your using Docker explicitly is not necessary? I would assume that docker.for.mac.localhost does not resolve (if it does then I don’t know why) and this should be just localhost.
(This is a CircleCI-specific behaviour that merges the networking stacks of all containers into one. It’s slightly messy, but it is pretty convenient (since the address of all services you add can be hardwired in your build config).