Use Docker for Accessing Database in AWS CodeBuild
Jan 31, 2020 · 2 Min Read · 7 Likes · 6 CommentsAWS CodeBuild is an extraordinary tool for building your code. Recently I have been using it to build projects and store docker images in AWS ECR. During those CodeBuild processes, I needed Database for running tests. As the need of the Database was for a limited time and I didn’t want to pay extra for it(that is why I did not use AWS RDS), I decided to create database using docker inside CodeBuild. In this post, I am going to describe how I did it.
Create ‘buildspec.yml’ file
You need to create a buildspec.yml
file in the root of the project. There you need to define instruction on how to pull docker image of the database you want to run. Here is an example:
version: 0.2
env:
variables:
PGDATABASE: "XXXXX"
PGUSER: "XXXXX"
PGPASSWORD: "XXXXX"
phases:
install:
runtime-versions:
python: 3.8
commands:
- docker pull postgres:latest
pre_build:
commands:
- echo Running postgres
- docker run -e PGPASSWORD -e PGUSER -e PGDATABASE -d -p 5432:5432 postgres
build:
commands:
- echo Build started on `date`
- echo Installing dependencies
- pip install -r requirements.txt
- echo Running tests
- python manage.py test
post_build:
commands:
- echo Build completed on `date`
reports:
myReport:
files:
- "**/*"
cache:
paths:
- "/root/.cache/pip/*"
As you can see in the install
section of buildspec.yml
, I pulled the Postgres docker image. Then in pre_build
section, I ran the image with environment variables which were defined in env
section. After that, I ran tests in django.
Configure ‘CodeBuild’
Now its time to configure the CodeBuild
in AWS console. I think the configuration is quite straightforward. But you need to make sure to tick mark the privileged
section of the settings. It should look like this:
Start build
After configuring CodeBuild, now it is time to start the build process from the dashboard. It can be either triggered from git(if you configured it like this), or you need to start the build manually. That is it, inside CodeBuild processes, it will start a Postgres Database, and allow django to run migrations.
In conclusion
Spinning up a docker container for database, seems to me like a very efficient solution. Similarly you can spin up any docker container to use as a temporary service, for example run redis, memcache etc.
Last updated: Dec 29, 2024
Hi Arnab,
Hi Jackie, due to bug in this commenting system, some of the lines from your comment was truncated. It is fixed now. It would be great if you could share your comment again. Thanks
What address do you use in django settings for the database ? I can’t get it to work …
You can use
localhost
, because you are basically running the Postgres Server at port 5432 which is mapped to 5432 port of localhost, so your Django application should be able to connect to it at urllocalhost:5432
.Thanks for sharing this. One thing though; I had trouble connecting to the database until I changed the environment variables to:
Not sure how this works for you. CodeBuild runs in a docker. Creating another docker from within build means the networking is “special”. A container cannot access the network of another container by simply using “localhost”. You have to use the actual IP address assigned to the other container.
Plus, this start command: docker run -e PGPASSWORD -e PGUSER -e PGDATABASE is invalid. The environment variables need to have a “var=val” definition.