<?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>DEV Community: Otobong Edoho</title>
    <description>The latest articles on DEV Community by Otobong Edoho (@otobong_edoho_7796fec1f41).</description>
    <link>https://dev.to/otobong_edoho_7796fec1f41</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%2F1751708%2Fc993aeaa-1edc-4b96-96bd-d474899942ea.jpg</url>
      <title>DEV Community: Otobong Edoho</title>
      <link>https://dev.to/otobong_edoho_7796fec1f41</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/otobong_edoho_7796fec1f41"/>
    <language>en</language>
    <item>
      <title>Using Dockerfile and Docker Compose For Local Development with Node.js, MongoDB and MongoExpress</title>
      <dc:creator>Otobong Edoho</dc:creator>
      <pubDate>Tue, 15 Oct 2024 11:03:43 +0000</pubDate>
      <link>https://dev.to/otobong_edoho_7796fec1f41/using-dockerfile-and-docker-compose-for-local-development-with-nodejs-mongodb-and-mongoexpress-2ajg</link>
      <guid>https://dev.to/otobong_edoho_7796fec1f41/using-dockerfile-and-docker-compose-for-local-development-with-nodejs-mongodb-and-mongoexpress-2ajg</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this post, I'll show you how to set up a local development environment using Docker with Node.js, MongoDB, and MongoExpress. Docker is a powerful tool that makes it easy to package applications and their dependencies, ensuring consistency across different environments.&lt;/p&gt;

&lt;p&gt;The goal of this guide is to help you spin up a simple Node.js app connected to a MongoDB database. We'll also use MongoExpress as a lightweight web-based interface to manage the database, all running inside Docker containers. By the end of this post, you’ll have a fully functional environment that can be set up and torn down with just a few commands.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;br&gt;
Before we dive in, please make sure you have the following installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Docker: You can download and install Docker from here.&lt;/li&gt;
&lt;li&gt;Basic understanding of Node.js and MongoDB.&lt;/li&gt;
&lt;li&gt;An EC2 Cloud instance for Non-ubuntu users&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’re new to Docker, there's no need to worry! This guide will walk you through the essential commands you need to know to get your environment up and running.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up the Project
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Setup for Docker&lt;/strong&gt;&lt;br&gt;
So we will start setting up the project, The first thing we need to do is pull the mongodb images and mongo express UI image from dockerhub.&lt;/p&gt;

&lt;p&gt;Let's install docker but first we need to update the package lists&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt upgrade &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install Required Dependencies&lt;br&gt;
Install packages that allow apt to use repositories over HTTPS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; apt-transport-https ca-certificates curl software-properties-common
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add Docker’s Official GPG Key&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-fsSL&lt;/span&gt; https://download.docker.com/linux/ubuntu/gpg | &lt;span class="nb"&gt;sudo &lt;/span&gt;gpg &lt;span class="nt"&gt;--dearmor&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; /usr/share/keyrings/docker-archive-keyring.gpg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Set Up the Docker Repository&lt;br&gt;
Add the Docker repository to apt sources:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="s2"&gt;"deb [arch=&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;dpkg &lt;span class="nt"&gt;--print-architecture&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt; signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu &lt;/span&gt;&lt;span class="se"&gt;\&lt;/span&gt;&lt;span class="s2"&gt;
  &lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;lsb_release &lt;span class="nt"&gt;-cs&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt; stable"&lt;/span&gt; | &lt;span class="nb"&gt;sudo tee&lt;/span&gt; /etc/apt/sources.list.d/docker.list &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install Docker Engine&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that docker is installed its time to pull the images&lt;br&gt;
Let's pull the mongodb image first&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Flpbt0fhreprpedi3lyvm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Flpbt0fhreprpedi3lyvm.png" alt="mongodb image" width="800" height="139"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;The docker run command does two things, it pulls the image and runs the image which is what we call a container. A container is the running instance of an image.&lt;/p&gt;

&lt;p&gt;Docker containers run in an isolated network meaning if we are running two different containers and we want them to be able to communicate we must put them in the same network.&lt;/p&gt;
&lt;h3&gt;
  
  
  Explanation of the Screenshot:
&lt;/h3&gt;
&lt;h4&gt;
  
  
  &lt;code&gt;docker run&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;This command starts a new container from a Docker image.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;code&gt;-d&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;-d&lt;/code&gt; flag tells Docker to run the container in "detached" mode (in the background), so it won't block the terminal.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;code&gt;-p 27017:27017&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;This option maps port &lt;code&gt;27017&lt;/code&gt; on the host machine to port &lt;code&gt;27017&lt;/code&gt; in the container. MongoDB uses this port for communication.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The syntax is &lt;code&gt;host_port:container_port&lt;/code&gt;, which means MongoDB will be accessible via &lt;code&gt;localhost:27017&lt;/code&gt; on the host machine.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  &lt;code&gt;--network mongo-network&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;This option connects the container to a Docker network named &lt;code&gt;mongo-network&lt;/code&gt;. The network allows multiple containers to communicate with each other. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the network doesn't exist, create it with &lt;code&gt;docker network create mongo-network&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  &lt;code&gt;--name mongodb&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;This assigns a name (&lt;code&gt;mongodb&lt;/code&gt;) to the running container. It allows you to refer to the container by name rather than by its container ID.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;code&gt;-e MONGO_INITDB_ROOT_USERNAME=admin&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;-e&lt;/code&gt; flag sets environment variables inside the container. In this case, it sets &lt;code&gt;MONGO_INITDB_ROOT_USERNAME&lt;/code&gt; to &lt;code&gt;admin&lt;/code&gt;, which specifies the MongoDB root user's username.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;code&gt;-e MONGO_INITDB_ROOT_PASSWORD=changethis123&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;Similar to the previous option, this sets the environment variable &lt;code&gt;MONGO_INITDB_ROOT_PASSWORD&lt;/code&gt; to &lt;code&gt;changethis123&lt;/code&gt;, defining the password for the MongoDB root user.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;code&gt;mongo&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;This is the name of the image to use. In this case, it is the official MongoDB image from Docker Hub.&lt;/p&gt;

&lt;p&gt;Now that we have some knowledge about docker let's proceed to pull the mongodb express image and run it&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F1hyk9zg5mzg0gcrhfhob.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F1hyk9zg5mzg0gcrhfhob.png" alt="mongo express image" width="800" height="150"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To check the network available, we can run &lt;br&gt;
&lt;code&gt;docker network ls&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It will output the name of the network that we just created, so they can talk to each other using just the container name.&lt;/p&gt;

&lt;p&gt;So we can access the mongo express server from our browser using &lt;code&gt;localhost:8081&lt;/code&gt;&lt;/p&gt;



&lt;p&gt;&lt;a href="https://media.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%2Fgm423x2r4hj9ulzdp7cg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fgm423x2r4hj9ulzdp7cg.png" alt="mongoexpress" width="800" height="421"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;Now there's a quicker and better setup, that is using dockerfile and docker compose rather than just running the commands in the terminal. Just to be clear a Docker Compose file uses YAML syntax, it defines how to configure and run multi-container applications on Docker while a Dockerfile is a text file that contains instructions for building a container image.&lt;/p&gt;
&lt;h3&gt;
  
  
  Setting up the Project
&lt;/h3&gt;

&lt;p&gt;First, let's create a simple Node.js application. If you don't already have Node.js installed, you can download it &lt;a href="https://nodejs.org/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start by creating a project folder:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;mkdir &lt;/span&gt;docker-node-mongo
   &lt;span class="nb"&gt;cd &lt;/span&gt;docker-node-mongo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Initialize a new Node.js project:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Install the necessary dependencies. For this setup, we’ll need &lt;strong&gt;Express&lt;/strong&gt; for our web server and &lt;strong&gt;Mongoose&lt;/strong&gt; to interact with MongoDB:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   npm &lt;span class="nb"&gt;install &lt;/span&gt;express mongoose
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Create an &lt;code&gt;index.js&lt;/code&gt; file with a simple Express server and a MongoDB connection using Mongoose:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="c1"&gt;// MongoDB connection&lt;/span&gt;
   &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongodb://mongo:27017/testdb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="na"&gt;useNewUrlParser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="na"&gt;useUnifiedTopology&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Connected to MongoDB&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Failed to connect to MongoDB&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;

   &lt;span class="c1"&gt;// Routes&lt;/span&gt;
   &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello from Node.js and MongoDB&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;

   &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`App running at http://localhost:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This sets up a basic Express server and connects it to a MongoDB instance running on &lt;code&gt;mongodb://mongo:27017/testdb&lt;/code&gt;. Now let's Dockerize it.&lt;/p&gt;


&lt;h3&gt;
  
  
  Creating a Dockerfile for Node.js
&lt;/h3&gt;

&lt;p&gt;Next, we need to create a &lt;code&gt;Dockerfile&lt;/code&gt; that will define the environment for our Node.js app. A &lt;code&gt;Dockerfile&lt;/code&gt; is essentially a blueprint for building the Docker image that will contain your application.&lt;/p&gt;

&lt;p&gt;Create a file called &lt;code&gt;Dockerfile&lt;/code&gt; in the root of your project directory and add the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# Use the official Node.js image from Docker Hub&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; node:16&lt;/span&gt;

&lt;span class="c"&gt;# Set the working directory inside the container&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /app&lt;/span&gt;

&lt;span class="c"&gt;# Copy package.json and install dependencies&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; package*.json ./&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt;

&lt;span class="c"&gt;# Copy the rest of the app files&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; . .&lt;/span&gt;

&lt;span class="c"&gt;# Expose the port the app runs on&lt;/span&gt;
&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; 3000&lt;/span&gt;

&lt;span class="c"&gt;# Command to run the app&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["node", "index.js"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;Dockerfile&lt;/code&gt; will:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use the official Node.js image.&lt;/li&gt;
&lt;li&gt;Set the working directory to &lt;code&gt;/app&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Copy the &lt;code&gt;package.json&lt;/code&gt; and install the necessary dependencies.&lt;/li&gt;
&lt;li&gt;Copy the rest of the files and set the entry point to run the Node.js app.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Setting up MongoDB and MongoExpress with Docker Compose
&lt;/h3&gt;

&lt;p&gt;Instead of running all services separately, we'll use &lt;strong&gt;Docker Compose&lt;/strong&gt; to define and manage our multi-container environment. Docker Compose allows us to define services, networks, and volumes in a &lt;code&gt;docker-compose.yml&lt;/code&gt; file, making it easy to orchestrate our entire stack. One thing to remember is that when using docker compose we don't need to create a network it automatically creates a network for our multiple containers defined in the yaml file.&lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;docker-compose.yml&lt;/code&gt; file in the project root:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3'&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;nodeapp&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;.&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3000:3000'&lt;/span&gt;
    &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;.:/app&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;mongo&lt;/span&gt;
  &lt;span class="na"&gt;mongo&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mongo&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;27017:27017'&lt;/span&gt;
  &lt;span class="na"&gt;mongo-express&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mongo-express&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;8081:8081'&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;ME_CONFIG_MONGODB_ADMINUSERNAME&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;root&lt;/span&gt;
      &lt;span class="na"&gt;ME_CONFIG_MONGODB_ADMINPASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;example&lt;/span&gt;
      &lt;span class="na"&gt;ME_CONFIG_MONGODB_SERVER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mongo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration defines three services:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;nodeapp&lt;/strong&gt;: Our Node.js application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mongo&lt;/strong&gt;: A MongoDB instance running on port &lt;code&gt;27017&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;mongo-express&lt;/strong&gt;: A web-based interface to manage MongoDB, accessible on port &lt;code&gt;8081&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Running the Application
&lt;/h3&gt;

&lt;p&gt;With everything set up, let’s run the app using Docker Compose.&lt;/p&gt;

&lt;p&gt;Run the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To shut down the multiple containers currently using Docker Compose&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Docker Compose will pull the necessary images, build the Node.js app, and start all services. After the process completes, you should see logs from MongoDB, Node.js, and MongoExpress.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Visit &lt;code&gt;http://localhost:3000&lt;/code&gt; to see the Node.js app running.&lt;/li&gt;
&lt;li&gt;Visit &lt;code&gt;http://localhost:8081&lt;/code&gt; to access MongoExpress and manage your database.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Connecting Node.js to MongoDB
&lt;/h3&gt;

&lt;p&gt;Our Node.js app is already set up to connect to MongoDB with the following connection string inside &lt;code&gt;index.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongodb://mongo:27017/testdb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;useNewUrlParser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;useUnifiedTopology&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;mongo&lt;/code&gt; hostname refers to the MongoDB service defined in our &lt;code&gt;docker-compose.yml&lt;/code&gt;. Docker Compose automatically creates a network for the services, allowing them to communicate by their service names.&lt;/p&gt;




&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In this post, we’ve successfully set up a local development environment using Docker for &lt;strong&gt;Node.js&lt;/strong&gt;, &lt;strong&gt;MongoDB&lt;/strong&gt;, and &lt;strong&gt;MongoExpress&lt;/strong&gt;. Using Docker Compose, we orchestrated multiple containers to work together seamlessly, making it easier to spin up a fully functional stack for development.&lt;/p&gt;

&lt;p&gt;With this setup, you can easily add more services, manage your databases with MongoExpress, and have an isolated environment without needing to install MongoDB or other dependencies locally.&lt;/p&gt;

&lt;p&gt;Happy Reading!!! Please Like, save, share and follow!!!&lt;/p&gt;




</description>
      <category>docker</category>
      <category>mongodb</category>
      <category>containers</category>
      <category>devops</category>
    </item>
    <item>
      <title>How to Publish a Java Artifact Built with Gradle to a Nexus Repository part 1</title>
      <dc:creator>Otobong Edoho</dc:creator>
      <pubDate>Wed, 28 Aug 2024 12:41:29 +0000</pubDate>
      <link>https://dev.to/otobong_edoho_7796fec1f41/how-to-publish-a-java-artifact-built-with-gradle-to-a-nexus-repository-part-1-17p6</link>
      <guid>https://dev.to/otobong_edoho_7796fec1f41/how-to-publish-a-java-artifact-built-with-gradle-to-a-nexus-repository-part-1-17p6</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is a Nexus repository?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Nexus repository is a publicly accessible repository manager from Sonatype that organizes, stores and helps in distribution of artifacts used in software development.&lt;/p&gt;

&lt;p&gt;It has built in support many for artifact formats such as Docker, Java, Go, PHP, Python etc.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Artifacts simply put refers to outcome of software build and packaging process &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Importance of Publishing Java Artifacts to a Central Repository&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Publishing Java artifacts to a central repository, like Nexus, is an important practice in modern software development for several reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Team Collaboration: In a team environment, a central repository allows developers to share libraries, frameworks, or any reusable code easily.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Integration with CI/CD Pipelines: Publishing to a central repository is often a key step in CI/CD pipelines. Artifacts can be automatically deployed to a repository after a successful build, making them immediately available for testing or production deployment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Version Control: Central repositories allow developers to manage multiple versions of an artifact. This enables the use of specific versions, ensuring compatibility and stability across different project development.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Java Development Kit 17 installed.&lt;/li&gt;
&lt;li&gt;Gradle installed and configured.&lt;/li&gt;
&lt;li&gt;Access to a Nexus Repository (public or private).&lt;/li&gt;
&lt;li&gt;Basic understanding of Gradle build scripts (build.gradle).&lt;/li&gt;
&lt;li&gt;An EC2 Instance or any other Cloud Service&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Install Nexus&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So before we install nexus we will setup an EC2 instance where we can install and run nexus.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fx46mg954eeyc648601a5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fx46mg954eeyc648601a5.png" alt="This is an EC2 instance"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To install Java on the instance &lt;/p&gt;

&lt;p&gt;&lt;code&gt;apt install openjdk-17-jre-headless&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When that is done you can install nexus using this command&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

cd /opt
wget https://download.sonatype.com/nexus/3/nexus-3.71.0-06-unix.tar.gz
ls


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It comes as a tar ball so we need to extract it&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tar -xvzf nexus-3.71.0-06-unix.tar.gz&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Run &lt;code&gt;ls&lt;/code&gt; and you should see this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fk46jf8bnsg16xdwgoy8b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fk46jf8bnsg16xdwgoy8b.png" alt="terminal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Run &lt;code&gt;ls -l&lt;/code&gt; to see the files and the owners, the root has ownership to the files so we need to give ownership to a nexus user which we will create. It is best practice to always create a user for any service we want to run.&lt;/p&gt;

&lt;p&gt;Let's create a nexus user and add the user to nexus group&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

adduser nexus
usermod -aG nexus nexus


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;while still in the /opt directory run this command&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

chown -R nexus:nexus nexus-3.71.0-06
chown -R nexus:nexus sonatype-work


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.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%2Ftaj5kwey339s8s8xzly7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Ftaj5kwey339s8s8xzly7.png" alt="prompt terminal"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Set nexus config so it will run as a nexus user&lt;br&gt;
&lt;code&gt;vim nexus-3.71.0-06/bin/nexus.rc&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This should open up the file in vim edit the &lt;code&gt;run_as_user="nexus"&lt;/code&gt; like this&lt;/p&gt;

&lt;p&gt;Now switch to the nexus user and run the nexus service&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

su - nexus
/opt/nexus-3.71.0-06/bin/nexus start


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Use the command below to check the service and on which port running.&lt;br&gt;
&lt;a href="https://media.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%2Ffqhfqsjxipv80fvp9fs0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Ffqhfqsjxipv80fvp9fs0.png" alt="terminal netstat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that nexus is running in port 8081, if the port is blocked on your EC2 instance you need to configure the security groups to allow inbound traffic on that port.&lt;/p&gt;

&lt;p&gt;Next Use the public IP address of your instance and the nexus port number like this&lt;br&gt;
&lt;code&gt;18.232.173.21:8081&lt;/code&gt;&lt;br&gt;
to be able to access the nexus interface from our browser &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fj65as6zlgedj0z2ijxy6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fj65as6zlgedj0z2ijxy6.png" alt="nexus interface"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get the default password for the admin interface navigate to the following directory&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

/opt/sonatype-work/nexus-3.71.0-06/
cat admin.password


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Use that to login, the username is admin&lt;/p&gt;

&lt;p&gt;After logging in the admin interface should look like this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fjn0j2s859drgpa5562zc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fjn0j2s859drgpa5562zc.png" alt="repository nexus"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;maven-releases repository is where we store artifacts that have been tested and are ready to be deployed to production.&lt;/li&gt;
&lt;li&gt;maven-snapshots repository is where we store artifacts that are still in the development and test phase.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's say a developer in our company want to publish an artifact to nexus we can't let them use the admin account, we must create an account for that user&lt;/p&gt;

&lt;p&gt;Navigate to the &lt;code&gt;Users&lt;/code&gt; tab and &lt;code&gt;create local user&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fachr87obf9zecnwrueqn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fachr87obf9zecnwrueqn.png" alt="Nexus users tab"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fpbnth0m28qwslktw6voo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fpbnth0m28qwslktw6voo.png" alt="Nexus user form"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go to User roles and create a role for the created user&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F1b5a4grud2ocxi3sqb5m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F1b5a4grud2ocxi3sqb5m.png" alt="User role"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Best Practice: We don't want our user having too many privileges so we will only assign a role in which the user needs to carry out a task&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fd8v3aiert3xazaryontl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fd8v3aiert3xazaryontl.png" alt="role form"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are building with gradle in this case but the role still works the same as maven so we will set the following role&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F31izgjpnzjfocqjixj2x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F31izgjpnzjfocqjixj2x.png" alt="set role"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we Assign that role that we created to the user&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fmogxfbyo2bcyqus84lju.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fmogxfbyo2bcyqus84lju.png" alt="assign role"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Configure Gradle with Nexus&lt;/strong&gt;&lt;br&gt;
Apply the following configurations and plugin to the build.gradle file&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

group = 'com.example'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '17'
targetCompatibility = '17'

apply plugin: 'maven-publish'


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This code block publishes the artifact that we have built &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

publishing {
//  The Artifacts we are going to upload
    publications {
        maven(MavenPublication) {
            artifact("build/libs/java-react-example-${version}.jar") {
                extension 'jar'
            }
        }
    }


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This code block defines the address that we are going to publish to.&lt;br&gt;
The &lt;code&gt;allowInsecureProtocol = true&lt;/code&gt;&lt;br&gt;
Allows the publishing to work regardless of the fact that we are not using &lt;code&gt;https&lt;/code&gt; &lt;br&gt;
The &lt;code&gt;credentials&lt;/code&gt; declares the user account that we created on our Nexus repository, so it will give access for our user to publish to the repository&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

// The Nexus repo that we will upload the Jar file to
    repositories {
        maven {
            name 'nexus'
            url "http://[Your Public IP]:8081/repository/maven-snapshots/"
//        So that it will all HTTP
            allowInsecureProtocol = true
            credentials {
                username project.repoUser
                password project.repoPassword
            }
        }
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let's create a &lt;code&gt;gradle.properties&lt;/code&gt; in our project file to store our user secret&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

repoUser = oty
repoPassword = xxxxxxxxxxx


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Create a &lt;code&gt;settings.gradle&lt;/code&gt; file if it doesn't exist and add the project name&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

rootProject.name ='java-react-example'


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.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%2F4g6bzhddl2df1hm2ddad.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F4g6bzhddl2df1hm2ddad.png" alt="gradle settings"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you haven't already built the project run then publish it&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

gradle build
gradle publish


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fsh17r48jxarz3t6ejuur.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fsh17r48jxarz3t6ejuur.png" alt="gradle build"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fm9pk9uvskq8s34rvud0b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fm9pk9uvskq8s34rvud0b.png" alt="build jar"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fww4apha7mn1txxsfuumb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fww4apha7mn1txxsfuumb.png" alt="gradle publish"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we can go to our nexus repository and view our publish artifact&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F43p6eqgpczbbdm1163ei.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F43p6eqgpczbbdm1163ei.png" alt="repo publish"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy Reading!!!&lt;br&gt;
Please Like and Follow if You liked the Article.&lt;br&gt;
Thank You.&lt;/p&gt;

</description>
      <category>devops</category>
      <category>operations</category>
      <category>development</category>
      <category>devsec</category>
    </item>
    <item>
      <title>Git Commit Hacks Every Developer Should Know</title>
      <dc:creator>Otobong Edoho</dc:creator>
      <pubDate>Tue, 20 Aug 2024 11:23:52 +0000</pubDate>
      <link>https://dev.to/otobong_edoho_7796fec1f41/git-commit-hacks-every-developer-should-know-249i</link>
      <guid>https://dev.to/otobong_edoho_7796fec1f41/git-commit-hacks-every-developer-should-know-249i</guid>
      <description>&lt;p&gt;&lt;strong&gt;1. Mastering Git Commit: The Foundation of Version Control&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The git commit command is the bedrock of Git. Each commit represents a snapshot of your project at a particular point in time. Here’s how to make the most of it:&lt;/p&gt;

&lt;p&gt;Atomic Commits&lt;br&gt;
Atomic commits are all about keeping each commit focused and self-contained. When you make small, focused commits, it becomes easier to track down the source of bugs and understand the evolution of the project. Each commit should ideally address one specific issue or feature.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git commit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Undoing Mistakes: Git Revert vs. Git Reset&lt;/strong&gt;&lt;br&gt;
Mistakes happen. When they do, Git provides powerful tools to help you undo them.&lt;/p&gt;

&lt;p&gt;Safe Reversal with git revert&lt;br&gt;
If you need to undo a commit without altering the commit history, git revert is your best friend. Unlike git reset, which changes your history, git revert creates a new commit that undoes the changes introduced by a previous commit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git revert &amp;lt;id&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is especially useful in shared repositories where altering history can cause issues for others.&lt;/p&gt;

&lt;p&gt;Hard Reset with &lt;code&gt;git reset --hard &amp;lt;id&amp;gt;&lt;/code&gt;&lt;br&gt;
On the other hand, &lt;code&gt;git reset --hard&lt;/code&gt; is a more drastic measure. It resets your current branch to the specified commit, discarding all changes in the working directory and the index&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git reset --hard &amp;lt;id&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Navigating History: Leveraging Git Checkout&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;git checkout &amp;lt;id&amp;gt;&lt;/code&gt; is a versatile command that allows you to switch between branches or revisit specific commits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout &amp;lt;id&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Exploring Old Versions with &lt;code&gt;git checkout &amp;lt;id&amp;gt;&lt;/code&gt;&lt;br&gt;
Sometimes, you need to look at an older version of your code to understand how a feature was implemented or to test an earlier state of the project. You can temporarily switch to an older commit using.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Understanding Git Log: Navigating Your Project’s History&lt;/strong&gt;&lt;br&gt;
The git log command is an essential tool for any developer working with Git. It allows you to view the history of commits in a repository, providing a detailed log of all the changes made over time. Here's how you can make the most out of &lt;code&gt;git log&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By understanding these commands and when to use them, you can wield Git more effectively and maintain a clean, understandable project history.&lt;/p&gt;

&lt;p&gt;Happy Reading!!!!!!!&lt;/p&gt;

</description>
      <category>devops</category>
      <category>webdev</category>
      <category>git</category>
      <category>github</category>
    </item>
    <item>
      <title>Setting Up Postgresql Database for Servers</title>
      <dc:creator>Otobong Edoho</dc:creator>
      <pubDate>Thu, 25 Jul 2024 21:23:53 +0000</pubDate>
      <link>https://dev.to/otobong_edoho_7796fec1f41/setting-up-postgresql-database-for-servers-3ao1</link>
      <guid>https://dev.to/otobong_edoho_7796fec1f41/setting-up-postgresql-database-for-servers-3ao1</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv8n6rpxysxy2wn0dhiht.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv8n6rpxysxy2wn0dhiht.png" alt="posgresql image" width="800" height="888"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;PostgreSQL is an advanced object relational database sytem that uses and also extends the SQL language. The good thing about postgreSQL that it is Open source.&lt;/p&gt;

&lt;p&gt;Developers and Database administrators alike use postgreSQL because of it's high data consistencey and data integrity which proves to be more reliable than other SQL databases.&lt;/p&gt;

&lt;p&gt;This is a stage 4 task that I worked on as HNG11 intern, It was a team based task, the team consisted of 6 members.&lt;/p&gt;

&lt;p&gt;This post will cover &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Installation of postgreSQL&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creation of the Database and user&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Configuring PostgreSQL&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;List of required tools and software (e.g., PostgreSQL, SSH client)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Basic knowledge required (e.g., familiarity with terminal commands, server access)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Install PostgreSQL along with its additional features&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install postgresql postgresql-contrib
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Switch to the PostgreSQL user to perform the needed administrative tasks&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo -i -u postgres

# opening  the sql prompt
psql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Within the PostgreSQL prompt, create the required databases for the production, staging and development environments&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE DATABASE langlearnai_be_staging_db;
CREATE DATABASE langlearnai_be_main_db;
CREATE DATABASE langlearnai_be_dev_db;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create Users and assign passwords for each enviroment&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE USER langlearnai_be_staging_user WITH ENCRYPTED PASSWORD 'staging_password';
CREATE USER langlearnai_be_main_user WITH ENCRYPTED PASSWORD 'main_password';
CREATE USER langlearnai_be_dev_user WITH ENCRYPTED PASSWORD 'dev_password';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Grant the necessary Privileges to Users&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GRANT ALL PRIVILEGES ON DATABASE langlearnai_be_staging_db TO langlearnai_be_staging_user;
GRANT ALL PRIVILEGES ON DATABASE langlearnai_be_main_db TO langlearnai_be_main_user;
GRANT ALL PRIVILEGES ON DATABASE langlearnai_be_dev_db TO langlearnai_be_dev_user;
GRANT ALL PRIVILEGES ON SCHEMA public TO langlearnai_be_staging_user;
GRANT ALL PRIVILEGES ON SCHEMA public TO langlearnai_be_main_user;
GRANT ALL PRIVILEGES ON SCHEMA public TO langlearnai_be_dev_user;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Exit the PostgreSQL prompt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\q
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configure and modified the Postgres configuration files to allow PostgreSQL to Listen on External IP&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo vim /etc/postgresql/13/main/postgresql.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Locate the listen_addresses line and change to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;listen_addresses = "*"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the pg_hba.conf file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo vim /etc/postgresql/13/main/pg_hba.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following lines under IPV4 local connections to allow external access. i.e These lines configure PostgreSQL to accept connections from the specified IP address&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# IPv4 local connections
host    all             all             0.0.0.0/0               md5
host    postgres        postgres        &amp;lt;server-ip-address&amp;gt;/32      md5
host    langlearnai_be_dev_db  langlearnai_be_dev_user  &amp;lt;server-ip-address&amp;gt;/32    md5
host    langlearnai_be_main_db  langlearnai_be_main_user  &amp;lt;server-ip-address&amp;gt;/32    md5
host    langlearnai_be_staging_db  langlearnai_be_staging_user  &amp;lt;server-ip-address&amp;gt;/32    md5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Restart to apply changes and enable the PostgreSQL service to start on system boot&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl restart postgresql
sudo systemctl enable postgresql 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Allow connections on port through firewall&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ufw allow 5432/tcp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Accessing the Database
&lt;/h2&gt;

&lt;p&gt;To connect to the PostgreSQL database remotely, use the following command&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 your_server_ip -U your_database_username -d your_database_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Happy Reading and Learning&lt;/p&gt;

</description>
      <category>devops</category>
      <category>webdev</category>
      <category>backend</category>
      <category>database</category>
    </item>
    <item>
      <title>Setting Up Postgresql Database for Servers</title>
      <dc:creator>Otobong Edoho</dc:creator>
      <pubDate>Thu, 25 Jul 2024 21:23:53 +0000</pubDate>
      <link>https://dev.to/otobong_edoho_7796fec1f41/setting-up-postgresql-database-for-servers-2bjg</link>
      <guid>https://dev.to/otobong_edoho_7796fec1f41/setting-up-postgresql-database-for-servers-2bjg</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv8n6rpxysxy2wn0dhiht.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv8n6rpxysxy2wn0dhiht.png" alt="posgresql image" width="800" height="888"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;PostgreSQL is an advanced object relational database sytem that uses and also extends the SQL language. The good thing about postgreSQL that it is Open source.&lt;/p&gt;

&lt;p&gt;Developers and Database administrators alike use postgreSQL because of it's high data consistencey and data integrity which proves to be more reliable than other SQL databases.&lt;/p&gt;

&lt;p&gt;This is a stage 4 task that I worked on as HNG11 intern, It was a team based task, the team consisted of 6 members.&lt;/p&gt;

&lt;p&gt;This post will cover &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Installation of postgreSQL&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Creation of the Database and user&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Configuring PostgreSQL&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;List of required tools and software (e.g., PostgreSQL, SSH client)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Basic knowledge required (e.g., familiarity with terminal commands, server access)&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Install PostgreSQL along with its additional features&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt install postgresql postgresql-contrib
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Switch to the PostgreSQL user to perform the needed administrative tasks&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo -i -u postgres

# opening  the sql prompt
psql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Within the PostgreSQL prompt, create the required databases for the production, staging and development environments&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE DATABASE langlearnai_be_staging_db;
CREATE DATABASE langlearnai_be_main_db;
CREATE DATABASE langlearnai_be_dev_db;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create Users and assign passwords for each enviroment&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE USER langlearnai_be_staging_user WITH ENCRYPTED PASSWORD 'staging_password';
CREATE USER langlearnai_be_main_user WITH ENCRYPTED PASSWORD 'main_password';
CREATE USER langlearnai_be_dev_user WITH ENCRYPTED PASSWORD 'dev_password';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Grant the necessary Privileges to Users&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GRANT ALL PRIVILEGES ON DATABASE langlearnai_be_staging_db TO langlearnai_be_staging_user;
GRANT ALL PRIVILEGES ON DATABASE langlearnai_be_main_db TO langlearnai_be_main_user;
GRANT ALL PRIVILEGES ON DATABASE langlearnai_be_dev_db TO langlearnai_be_dev_user;
GRANT ALL PRIVILEGES ON SCHEMA public TO langlearnai_be_staging_user;
GRANT ALL PRIVILEGES ON SCHEMA public TO langlearnai_be_main_user;
GRANT ALL PRIVILEGES ON SCHEMA public TO langlearnai_be_dev_user;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Exit the PostgreSQL prompt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\q
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Configure and modified the Postgres configuration files to allow PostgreSQL to Listen on External IP&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo vim /etc/postgresql/13/main/postgresql.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Locate the listen_addresses line and change to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;listen_addresses = "*"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the pg_hba.conf file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo vim /etc/postgresql/13/main/pg_hba.conf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the following lines under IPV4 local connections to allow external access. i.e These lines configure PostgreSQL to accept connections from the specified IP address&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# IPv4 local connections
host    all             all             0.0.0.0/0               md5
host    postgres        postgres        &amp;lt;server-ip-address&amp;gt;/32      md5
host    langlearnai_be_dev_db  langlearnai_be_dev_user  &amp;lt;server-ip-address&amp;gt;/32    md5
host    langlearnai_be_main_db  langlearnai_be_main_user  &amp;lt;server-ip-address&amp;gt;/32    md5
host    langlearnai_be_staging_db  langlearnai_be_staging_user  &amp;lt;server-ip-address&amp;gt;/32    md5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Restart to apply changes and enable the PostgreSQL service to start on system boot&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo systemctl restart postgresql
sudo systemctl enable postgresql 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Allow connections on port through firewall&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo ufw allow 5432/tcp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Accessing the Database
&lt;/h2&gt;

&lt;p&gt;To connect to the PostgreSQL database remotely, use the following command&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 your_server_ip -U your_database_username -d your_database_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Happy Reading and Learning&lt;/p&gt;

</description>
      <category>devops</category>
      <category>webdev</category>
      <category>backend</category>
      <category>database</category>
    </item>
  </channel>
</rss>
