Up and running with docker in 5 minute
Setup PostgreSQL database with docker
We use docker at PMT to develop our backend APIs for our transport and logistic applications. Docker greatly improves our productivity by offering a consistent development environment for your entire team; support for multiple stacks, and easy management of development dependencies, and not to mention, ease of deployment.
In this article, we will see how to install docker and launch PostgreSQL in five minutes.
1. Go to the official website and download the right version for your machine.
Personally, I use Ubuntu and Snapcraft works for me:
# snapd can be installed from the command line $ sudo apt update $ sudo apt install snapd
# use snap to install docker $ sudo snap install docker
2. Verify that Docker Engine is installed correctly by running the hello-world image.
sudo docker run hello-world
This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.
3. (download and) launch the docker images. PostgreSQL 13 in my case
docker run -d --name pg13 -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust postgres:13
4. Connect to the docker container
docker exec -it pg13 /bin/bash psql -h localhost -U postgres psql -U postgres -h localhost -p 5432 -c "show server_version" postgres server_version
#5. Now that you are in the database shell, you can execute SQL commands. Of course, replace mydb, myuser and mypass with the desire database name, database user and user's password (as required in your dot-env file).
sudo -u postgres psql postgres=# CREATE DATABASE mydb; postgres=# CREATE USER myuser WITH ENCRYPTED PASSWORD 'mypass'; postgres=# GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
5. Let’s stop the instance
docker stop pg13
6. And let’s clear any data generated and stored by our instance
docker rm pg13
7. To clean or prune unused (dangling) images
docker image prune
To remove all images which are not in use containers, add - a
docker image prune -a
To Purne your entire system
docker system prune
Download a Docker cheatsheet for more commands at your fingertip.
Thanks for reading and happy coding!