<?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: NsrEZ</title>
    <description>The latest articles on DEV Community by NsrEZ (@nsrez).</description>
    <link>https://dev.to/nsrez</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%2F1773068%2F40f9bb8e-11a4-42de-a5e2-a999936b75cc.png</url>
      <title>DEV Community: NsrEZ</title>
      <link>https://dev.to/nsrez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nsrez"/>
    <language>en</language>
    <item>
      <title>Dockerize NEXTJS and Setup Drizzle ORM with PostgreSQL locally with docker in NEXT.JS...</title>
      <dc:creator>NsrEZ</dc:creator>
      <pubDate>Mon, 19 Aug 2024 16:48:02 +0000</pubDate>
      <link>https://dev.to/nsrez/dockerize-nextjs-and-setup-drizzle-orm-with-postgresql-locally-with-docker-in-nextjs-4oli</link>
      <guid>https://dev.to/nsrez/dockerize-nextjs-and-setup-drizzle-orm-with-postgresql-locally-with-docker-in-nextjs-4oli</guid>
      <description>&lt;p&gt;&lt;a href="https://docs.docker.com/engine/install/" rel="noopener noreferrer"&gt;Download Docker Desktop&lt;/a&gt; we are going to need it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 1:&lt;/strong&gt; Go into your IDE and create a new NEXT App&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;npx create-next-app@latest&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%2F6xy5s74a4mlbksrdvkn1.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%2F6xy5s74a4mlbksrdvkn1.png" alt="NEXT.JS App Initialized with TypeScript, React, Alias imports, TailwindCSS and App Router" width="667" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Name your App anything and take all the default settings.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 2:&lt;/strong&gt; Now we have to create a Dockerfile and docker-compose.yml file in our root folder of the NEXT App.&lt;/li&gt;
&lt;/ul&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%2Fuy6jx76eabvomo6rzw7e.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%2Fuy6jx76eabvomo6rzw7e.png" alt="NEXTJS Folder structure" width="290" height="620"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 3:&lt;/strong&gt; Now we have to make our docker image for our NEXT app.
Open the Dockerfile in your root folder &amp;amp; copy these commands which help us to make the docker image.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Use the Node.js 18 Alpine Linux image as the base image
FROM node:18-alpine 

# Set the working directory inside the container to /app
WORKDIR /app

# Copy package.json and package-lock.json files into the working directory
COPY package*.json ./

# Install the dependencies specified in package.json
RUN npm install

# Copy all the files from the local directory to the working directory in the container
COPY . .

# Build the project (typically used for building front-end assets)
RUN npm run build

# Copy the .next directory to the working directory in the container (if needed for a Next.js app)
COPY .next ./.next

# Run the application in development mode
CMD ["npm", "run", "dev"]

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note if you want to build in production please look at the &lt;a href="https://nextjs.org/docs/app/building-your-application/deploying#docker-image" rel="noopener noreferrer"&gt;NEXT.JS tutorial for docker container&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 4:&lt;/strong&gt; Open the docker-compose.yml file and copy this yaml script in the file.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Use Docker Compose file format version 3.9
version: '3.9'

services:
  # Define the service named 'app'
  app:
    # Build the Docker image using the Dockerfile in the current directory
    build:
      context: .
      dockerfile: Dockerfile
    # Name the container 'nextjs14-container'
    container_name: nextjs14-container
    # Map port 3000 on the host to port 3000 in the container
    ports:
      - '3000:3000'
    # Avoid mounting node_modules from the host to the container
    volumes:
      - /app/node_modules

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 5:&lt;/strong&gt; Now, our NEXTJS App is dockerized and we can run the &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;docker-compose up --build -d&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%2Fd22xqtelue0np978swbl.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%2Fd22xqtelue0np978swbl.png" alt="docker-compose finished building the docker container" width="800" height="140"&gt;&lt;/a&gt;&lt;br&gt;
Once finished our image now becomes a container. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;docker ps&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Using docker ps we can check if our container is 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%2Fbtbcygs7q3fwcd36bs18.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%2Fbtbcygs7q3fwcd36bs18.png" alt="running docker container" width="800" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note: Running containers are also shown and can be managed via the docker desktop application instead of CLI.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Now that NextJS is dockerized we can start to implement the postgres database.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 1&lt;/strong&gt;: Create a .env file in the root folder and also add it in the .gitignore file.
Contents of .env file:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASE_URL="postgresql://postgres:example@localhost:5432/postgres"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 2&lt;/strong&gt;: Now we have to install the drizzle adapters
You can learn more about drizzle &lt;a href="https://orm.drizzle.team/docs/overview" rel="noopener noreferrer"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i drizzle-orm postgres
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i -D drizzle-kit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Also install ZOD&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i zod
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 3&lt;/strong&gt;: Create folder named lib in src directory and &lt;code&gt;env.ts&lt;/code&gt; file in the lib folder.
Contents of env.ts zod environment variable validation
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {z} from 'zod'

const envSchema = z.object({
    DATABASE_URL:z.string().url()
})

export const env = envSchema.parse(process.env)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 4&lt;/strong&gt;: Create a folder named db in src directory and &lt;code&gt;index.ts&lt;/code&gt; &amp;amp; &lt;code&gt;schema.ts&lt;/code&gt; files in it.
Contents of index.ts with zod schema imported from the env.ts file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';
import {env} from '@/lib/env'
import * as schema from './schema'

const pg = postgres(env.DATABASE_URL)
const db = drizzle(pg,{schema})
export {db,pg}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Contents of schema.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import { pgTable,serial,text, timestamp } from "drizzle-orm/pg-core";

export const Posts=pgTable('posts',{
    id:serial("id").primaryKey().notNull(),
    title:text('title').notNull(),
    body:text('body').notNull(),
    createdAt:timestamp('createdAt',{
        withTimezone:true,
        mode:"date"
    }).notNull().defaultNow(),
    updatedAt:timestamp('updatedAt',{
        withTimezone:true,
        mode:"date"
    }).notNull().defaultNow()
})

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note: Make your own required schema here this is just for demo.&lt;/strong&gt;&lt;br&gt;
The resulting file structure will 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%2Fz63kruf9i93qhkpfam7s.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%2Fz63kruf9i93qhkpfam7s.png" alt="Folder creation for environment variables and drizzle setup" width="295" height="774"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 5&lt;/strong&gt;: Go into package.json and add this under scripts:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"db:studio": "npx drizzle-kit studio --config=drizzle.config.ts",
"db:push": "npx drizzle-kit push --config=drizzle.config.ts"
&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%2Fe0pfi71vtk5qj3q7vfja.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%2Fe0pfi71vtk5qj3q7vfja.png" alt="Package.json" width="714" height="242"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Now we locally host this db using docker
&lt;/h1&gt;

&lt;p&gt;for that we just need to add this extra setup in the docker compose file which takes an already built postgres image which will be our container.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  drizzle-db:
    image: postgres
    restart: always
    container_name: drizzle-db
    ports:
      - 5432:5432
    environment:
      POSTGRES_PASSWORD: example
      PGDATA: /data/postgres
    volumes:
      - postgres:/data/postgres
volumes:
  postgres:   
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your docker-compose file will look like this now:&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%2Fy7ozqzf7d91lx9elwq5j.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%2Fy7ozqzf7d91lx9elwq5j.png" alt="docker compose" width="566" height="606"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Last few steps&lt;/strong&gt;:
Create the docker container make sure docker desktop is running:
&amp;gt; docker compose up --build -d&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Migrate the database schema:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;npm run db:push&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now you can interact with your database using drizzle orm&lt;/p&gt;

&lt;p&gt;If you guys liked the blog connect with me on &lt;a href="https://www.linkedin.com/in/nishidev/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>postgres</category>
      <category>docker</category>
      <category>nextjs</category>
    </item>
  </channel>
</rss>
