DEV Community

Cover image for Run A Postgres Docker Container on Oracle Cloud Infrastructure
Rohit Jacob Mathew
Rohit Jacob Mathew

Posted on • Originally published at blog.rohitjmathew.space

Run A Postgres Docker Container on Oracle Cloud Infrastructure

In this article, I want to show how I quickly ran a Docker container for free on Oracle Cloud Infrastructure. I made use of a VM in the Always Free Tier of OCI and for a side project set up a dockerized Postgres database.

Why Oracle Cloud Infrastructure

Oracle offers an Always Free cloud services. You can see the details below:

Oracle Cloud Free Offering

Note: the workload of a container has to fit in the shape of this always free VM: VM.Standard.E2.1.Micro, 1/8 OCPU, 1 GB RAM and up to 480 Mbps network bandwidth (see docs). The boot volume offers just over 45GB of disk storage. In order for the container to be accessible, the ports mapped on the VM to container also have to be configured in ingress rules in the security list. We need to install Docker ourselves in the VM; it is provisioned with just an Oracle Linux image.

Let's get started

1) Get yourself a tenancy and create a VM

The first thing we ought to do is create a VM. If you've got a cloud tenancy then you probably already know how to create an instance. If you're new to Oracle Cloud then watch the below video and create an "always free" VM by signing up at https://cloud.oracle.com/free:

Note: Most of the details like availability zone, image details, networking options are already pre-filled by Oracle and kept but can be adjusted if you want something specific. I went ahead with the standard settings.

The VM will now be provisioned — as is indicated:

Example of provisioned VM from [Oracle Developer Blog](https://medium.com/oracledevs/run-always-free-docker-container-on-oracle-cloud-infrastructure-c88e36b65610)

After a little while, the VM is up and running — and has a public IP address assigned to it:

Example of running VM from [Oracle Developer Blog](https://medium.com/oracledevs/run-always-free-docker-container-on-oracle-cloud-infrastructure-c88e36b65610)

The situation at this point can be visualized as is shown in the below figure:

Visualization of the VM setup from [Oracle Developer Blog](https://medium.com/oracledevs/run-always-free-docker-container-on-oracle-cloud-infrastructure-c88e36b65610)

2) Setup Ingress Rules in Security List for VM to open up the ports required for whatever container you want to run

The VM is associated with a public subnet in a Virtual Cloud Network. The security list(s) for this subnet should be configured with ingress rules that make the required traffic possible to the port(s) that will be mapped to the container image. Open the details page for the public subnet. Click on the security list (or create a new one)

Subnet Screen from [Oracle Developer Blog](https://medium.com/oracledevs/run-always-free-docker-container-on-oracle-cloud-infrastructure-c88e36b65610)

We will run the Postgres container image. The port we will map in the VM to the Postgres container is one we can choose ourselves. Let’s pick 5432 which is the default port for Postgres. we need to configure an ingress rule as below:

Ingress Rule Screenshot

Source CIDR is set to 0.0.0.0/0; along with Source Port Range left blank (i.e. All) this means that this rule applies to any client.

3) SSH into the VM, install Docker

At this point, we have a running VM instance with just a Linux Operating System but no Docker. Let’s SSH into the VM using this command:

ssh opc@public-id-address -i private-key-file
Enter fullscreen mode Exit fullscreen mode

Replace the public-id-address with the public IP assigned to the VM. Replace private-key-file with a reference to the file that contains the SSH private key

Now to install Docker, execute these commands:

sudo yum-config-manager --enable ol7_addons 
sudo yum install docker-engine -y 
sudo systemctl start docker 
sudo systemctl enable docker
Enter fullscreen mode Exit fullscreen mode

Docker installation screenshot from [Oracle Developer Blog](https://medium.com/oracledevs/run-always-free-docker-container-on-oracle-cloud-infrastructure-c88e36b65610)

To run Docker as a non-root user, read these instructions.

Run Docker Container Image

With Docker installed, we can now run the Postgres container image.

Run the container image with this command. This might pull the docker image from Docker Hub if not present already. Don't forget to add a different password for POSTGRES_PASSWORD:

sudo docker run -d -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=mysecretpassword postgres
Enter fullscreen mode Exit fullscreen mode

Use sudo docker ps to verify if the container is running. The above command will start a PostgreSQL database and map ports using the following pattern: -p <host_port>:<container_port>. Port 5432 of our container will be mapped on port 5432 of our host or server.

Now, let's access the container on your host or server. We will create a database inside our Postgres container.

sudo docker exec -it postgres bash
Enter fullscreen mode Exit fullscreen mode

Now you are ‘inside’ your container. We can access Postgres and create the database.

root@12d48fde2627:/# psql -U postgres
psql (13.3 (Debian 13.3-1.pgdg100+1))
Type "help" for help.

postgres=# CREATE DATABASE testdb;
CREATE DATABASE
postgres=# \q
Enter fullscreen mode Exit fullscreen mode

We are finished. You can exit your container (\q) and go to your local machine. Here you need some PostgreSQL Client tool installed like DBeaver or pgAdmin. Connect to the DB server by using the public IP as the host, 5432 as the port, postgres as the username, the POSTGRES_PASSWORD you entered while running the container as the password and connect to the testdb. Save the connection and you should now be able to access your DB.

Congrats, you have now run A Postgres Docker Container on Oracle Cloud Infrastructure!

Thanks for reading! I really hope that you find this article useful. I invite you to participate in the discussion in the comments below, I'm always interested to know your thoughts and happy to answer any questions you might have in your mind. If you think this post was useful, please like the post to help promote this piece to others.

Thanks for reading! :)

P.S Do feel free to connect with me on LinkedIn or Twitter

Appendix

This article leans heavily on the following material:

Top comments (0)