How to Set Up a Postgresql Database Container with Docker Machine
2020-07-17 13:50:05 |
Tested On
- Linux Ubuntu 20.04
Problem: You want to quickly set up a Postgresql database for development purposes. You also want the database to run in a container so that it's free of conflicts and easy to clean up. Finally, when you're finished programming and testing, you want to be able to spin down and clean up all of the database resources in a repeatable way.
Solution: Docker Machine and Docker containerization. With just a few short commands, we'll have a Postgresql database running in an isolated container that we can easily connect to and run queries against.
Creating the Postgresql DB Container
Follow these instructions to install docker-machine on your preferred operating system. Make sure to replace Ch4ng3M3 with your preferred password.
# Create virtual machine
docker-machine create --driver virtualbox --virtualbox-disk-size "40000" dev
# Set environment variables
docker-machine env dev
eval "$(docker-machine env dev)"
# Create postgresql database container (make sure to replace Ch4ng3M3 with your preferred password)
docker run --name pgdb -e POSTGRES_USER=pguser -e POSTGRES_PASSWORD=Ch4ng3M3 -e POSTGRES_DB=pgdb -p 5432:5432 -d postgres
# List containers to confirm db is running
docker ps
Your terminal should output the following information about your docker container:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
62f3577b7e10 postgres "docker-entrypoint.s…" 22 seconds ago Up 1 second 0.0.0.0:5432->5432/tcp pgdb
If you get the following error, this means something is already running on port 5432. It could be another postgres instance, a postgres docker container, or a separate process entirely.
docker: Error response from daemon: driver failed programming external connectivity on endpoint pgdb (ceb965e08c68a3e38f868346a2be60fd0dae125ed0685e8a5ac3c202ac70cd4c): Bind for 0.0.0.0:5432 failed: port is already allocated.
Please refer to this article to identify and terminate the process running on port 5432. If you would rather keep that existing process running, you can instead run the pgdb docker container on a different port, such as 5433, with the following commands.
# Delete the pgdb container
docker rm -f pgdb
# Run pgdb container on port 5433
# Replace Ch4ng3M3 with your own secure password
docker run --name pgdb -e POSTGRES_USER=pguser -e POSTGRES_PASSWORD=Ch4ng3M3 -e POSTGRES_DB=pgdb -p 5432:5433 -d postgres
Connecting to the DB Container
The following commands will allow you to print the docker machine's IP address and port number to the screen. With those two values and the password, above, you'll have everything needed to connect to the database.
# Get the virtual machine's IP address
docker-machine ip dev
# Identify which port the pgdb container is running on
docker ps
# Connect to the DB (enter your password when prompted)
# Make sure to substitute IP_ADDRESS with the dev docker machine's ip...
# ...and PORT_NUMBER with the pgdb container port
psql -h IP_ADDRESS -U pguser -d pgdb -p PORT_NUMBER -W
Once you're connected, you'll be able to run your queries. If you want to connect from a program you've written, you can use the same connection parameters. For an example of how to do this with Python 3, check out this article.
Conclusion
That's the end of this tutorial. We hope you found it helpful. Make sure to check out our other tutorials, as well.
Comments
You must log in to comment. Don't have an account? Sign up for free.
Subscribe to comments for this post
Info