<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: mo</title>
    <description>The latest articles on Forem by mo (@momellouk).</description>
    <link>https://forem.com/momellouk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F950629%2F38d4c0c7-bbd1-43fb-8190-f2908038107c.jpeg</url>
      <title>Forem: mo</title>
      <link>https://forem.com/momellouk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/momellouk"/>
    <language>en</language>
    <item>
      <title>Dockerize a postgreSQL database in an aws ec2 instance</title>
      <dc:creator>mo</dc:creator>
      <pubDate>Wed, 26 Oct 2022 20:30:17 +0000</pubDate>
      <link>https://forem.com/momellouk/dockerize-a-postgresql-database-in-an-aws-ec2-instance-5dej</link>
      <guid>https://forem.com/momellouk/dockerize-a-postgresql-database-in-an-aws-ec2-instance-5dej</guid>
      <description>&lt;h2&gt;
  
  
  What is this article about ?
&lt;/h2&gt;

&lt;p&gt;Well, let's start with the one basic truth in every developer's life, &lt;strong&gt;Databases are a pain to setup in your local machine&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That's why we have docker, where we can easily run a pre-configured database with any DBMS we want in a few minutes.&lt;/p&gt;

&lt;p&gt;But still, at a certain point even a docker container will consume enough resources that may hinder our machine's performance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hence, the powerful combo of &lt;em&gt;AWS EC2 + Docker + PostgreSQL&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8g6ifdzx371m6vusvwhh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8g6ifdzx371m6vusvwhh.png" alt="aws+docker+postgresql" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article, we'll see how to dockerize a postgreSQL database in an aws ec2 instance and how to consume the database resources in our project.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvbf6c528n9c2l5g8mr6t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvbf6c528n9c2l5g8mr6t.png" alt="remote database server" width="800" height="268"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1. Launching an AWS EC2 instance :
&lt;/h2&gt;

&lt;p&gt;Select an ubuntu distribution instance which is included in the aws free tier.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frd1ht6moo5jmo07udex9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frd1ht6moo5jmo07udex9.png" alt="aws" width="791" height="768"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then select the &lt;strong&gt;Create new key pair&lt;/strong&gt; option to generate the ssh keys that's allow you to connect to your ec2 instance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg2i9f6bd0urcdhso8s58.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg2i9f6bd0urcdhso8s58.png" alt="postgresql" width="610" height="643"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;br&gt;
&lt;strong&gt;N.B., You can adapt the Network Settings to limit the access to your ec2 instance or leave them as default&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Then you can simply launch the instance and you'll see it running in your ec2 dashboard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjl4oeqx7d5zbf61909t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjl4oeqx7d5zbf61909t.png" alt="docker" width="800" height="41"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2. Connecting to your ec2 instance via ssh keys :
&lt;/h2&gt;

&lt;p&gt;Now that our ec2 instance is up &amp;amp; running, we can simply connect to it by clicking on &lt;strong&gt;connect&lt;/strong&gt; in your ec2 instance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Farn7yh6omp71eshtmji7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Farn7yh6omp71eshtmji7.png" alt="docker" width="652" height="241"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can open the ssh client tab, to see &amp;amp; follow the steps to connect to your ec2 instance via the ssh keys you created &amp;amp; downloaded in Step 1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flh8u6a4bbfs44umc4gog.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flh8u6a4bbfs44umc4gog.png" alt="postgresql" width="647" height="95"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congrats !! you're successfully connected to your instance via your terminal.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 3. Installing docker in your ec2 instance:
&lt;/h2&gt;

&lt;p&gt;We will install docker in our ec2 instance using the repository.&lt;/p&gt;

&lt;p&gt;You can run the following commands in your terminal that's connected to the your instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Updating the apt package index
sudo apt-get update

# Installing the packages to allow apt to download the repository over https
sudo apt-get install ca-certificates curl gnupg lsb-release

# Add Docker’s official GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

#Setting up the repository
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list &amp;gt; /dev/null

# Updating the apt package index
sudo apt-get update

#Installing the latest version of the docker-cli, compose, etc
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;N.B.&lt;/strong&gt; after setting up docker you'll have to run every docker command as root (i.e., sudo docker ps)&lt;/p&gt;

&lt;p&gt;If you don't want that, you can add the user to the docker group, you'll find the steps in the &lt;a href="https://docs.docker.com/engine/install/linux-postinstall/" rel="noopener noreferrer"&gt;docker documentation&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4. Running a PostgreSQL DB in a docker container :
&lt;/h2&gt;

&lt;p&gt;To run a postgreSQL container in our ec2 instance, you can execute the following docker command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo docker run --name postgresql -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=post123 -p 5432:5432 -v /data:/var/lib/postgresql/data -d postgres:alpine
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;--name postgresql&lt;/strong&gt; : name of our container&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;-e POSTGRES_USER=postgres&lt;/strong&gt; : name of the user&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;-e POSTGRES_PASSWORD=post123&lt;/strong&gt; : password of the user&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;-p 5432:5432&lt;/strong&gt; : binding ec2 instance port to docker port, our postgreSQL DB is exposed in this port&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;-v /data:/var/lib/postgresql/data&lt;/strong&gt; : volume binding to persist &amp;amp; saving our data in the ec2 instance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;-d&lt;/strong&gt; : running in detached mode, so that our container always runs in the background&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;postgres:alpine&lt;/strong&gt; : postgres docker image. we chose the alpine image, due to its small size.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After the command has finished executing, you can see your running container by running :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo docker ps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxzqikcj68ug2n7b7fq79.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxzqikcj68ug2n7b7fq79.png" alt="aws ec2" width="800" height="62"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In my case, you can see that the postgreSQL is running on the 5432 port &lt;strong&gt;of the ec2 instance&lt;/strong&gt;, which means that our postgreSQL DB is still inaccessible by any external client &lt;em&gt;(our db container is only exposed inside the instance at this point)&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Which leads us to our next &amp;amp; final step.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5. Exposing your DB server :
&lt;/h2&gt;

&lt;p&gt;Now, in order to make our DB accessible to external clients, we need to change the security groups of the ec2 instance, to do that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;select your ec2 instance &amp;amp; select the security group related to the instance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1eq36n4tqlk6whaa9xop.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1eq36n4tqlk6whaa9xop.png" alt="docker" width="800" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select the "Inbound rules" tab, then click "Edit inbound rules".
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa4dd5qlaycpw183k4j3a.png" alt="postgresql" width="800" height="128"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now you need to add the following rule to your inbound rules.&lt;br&gt;
&lt;small&gt;&lt;br&gt;
&lt;strong&gt;- Protocol : TCP&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;- Port : 5432&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;- Source : 0.0.0.0/0&lt;/strong&gt;&lt;br&gt;
&lt;/small&gt;&lt;/p&gt;

&lt;p&gt;N.B.,&lt;strong&gt;0.0.0.0/0&lt;/strong&gt; allows any external client to access your db, you can limit the access by selecting the exact ip adresses.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdlf7h4n1imbemae8dlfj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdlf7h4n1imbemae8dlfj.png" alt="docker" width="800" height="126"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Once you've done that, well congrats, your PostgreSQL is fully accessible.
&lt;/h3&gt;

&lt;p&gt;To access it, we will use the postgreSQL terminal &lt;strong&gt;(psql)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;psql -h public_ip_adress -p 5432 -U postgres
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;public_ip_adress&lt;/strong&gt; : you can find it in your ec2 dashboard.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Thank you for reading this article, hope it helped you.&lt;br&gt;
If you have any questions leave them in the comments or contact me directly in &lt;a href="//www.amedd.me"&gt;amedd.me&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>docker</category>
      <category>aws</category>
      <category>postgres</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
