DEV Community

Imam Ali Mustofa for Street Community Programmer

Posted on • Edited on

Adding Container IP to Node.js Express + Prisma ORM on Docker Container - Automagically

Hello Punk!

Welcome to the wild world of Software Freestyle Engineering, where we blend code and creativity to create magical software experiences. Today, we're going to embark on an adventure in Docker, Prisma, Node.js, and Express to solve a crucial problem: how to add the Container IP to the environment variables of your Node.js Express + Prisma ORM project when the container finishes booting. Get ready for a rollercoaster ride of code and character-driven development!

The Picture

You have your Node.js Express + Prisma ORM project wrapped in a cozy Docker container. It's like a little universe of its own, complete with its IP address and secrets stored in environment variables. But when the container starts, how do you make sure that IP address becomes a part of your project's environment? Fear not, my friend, for I shall guide you through this process with a pinch of humor and a dash of enthusiasm!

Grab your favorite beverage, fire up your code editor, and let's dive into the script that will bring our container to life:

#!/bin/bash
# Filename: .docker/entrypoint.sh

# Time to rock and roll! Run your startup commands or scripts here
echo "Container has started!"

# Ahoy, me matey! Retrieve the container's IP address
CONTAINER_IP=$(hostname -i)
echo "Container IP Address: $CONTAINER_IP"

# Let's play detective! Check if the .env file exists
if [ -f "/app/.env" ]; then
  echo ".env file already exists"

  # Hold your horses! Check if the DATABASE_URL variable exists in the .env file
  if grep -q "DATABASE_URL" "/app/.env"; then
    echo "DATABASE_URL variable already exists in .env file"

    # It's makeover time! Replace the IP address in DATABASE_URL with the container IP
    sed -i "s/@[^:]*:/@$CONTAINER_IP:/" /app/.env
    echo "DATABASE_URL updated with container IP: $CONTAINER_IP"
  else
    # Avast, me hearties! Append the CONTAINER_IP variable to the .env file
    echo "DATABASE_URL=postgresql://user:password@$CONTAINER_IP:5432/db" >> /app/.env
    echo "DATABASE_URL added to .env file with container IP: $CONTAINER_IP"
  fi
else
  # Let's create something new! A shiny .env file with the CONTAINER_IP variable
  echo "DATABASE_URL=postgresql://user:password@$CONTAINER_IP:5432/db" > /app/.env
  echo "New .env file created with container IP: $CONTAINER_IP"
fi

# It's showtime! Start the main process (e.g., start your application)
exec "$@"
Enter fullscreen mode Exit fullscreen mode

Now, let's break this script down, step by step, and inject some playful explanations along the way. We're going to have more fun than a barrel of monkeys, so hold on tight!

The Boring

First things first, we announce with pride that our container has started. It's like the opening act of a rock concert, where we set the stage for all the action to come. Cue the applause!

Next, we transform into a detective, sniffing out the container's IP address. It's time to unveil the secret identity of our little container friend. We store this precious IP address in the CONTAINER_IP variable. It's like discovering buried treasure!

The Sherlock Holmes

Now, let's play Sherlock Holmes and investigate the existence of the elusive .env file. We need to know if it's already lurking around. If it is, we let out an "Ah ha!" and move on to the next clue. But if it's not there, well, we take matters into our own hands and create a brand-new .env file. It's like forging our own destiny!

Inside our .env file, there's a particular variable we're after: the cunning DATABASE_URL. We need to know if it's already hiding inside. If it is, we pat ourselves on the back and exclaim, "Well, well, well, what have we here?" But if it's missing, we take control and append it to the .env file. It's like making sure our treasure map is complete!

The Fun

Now comes the fun part: the makeover! We use a sed command to replace the IP address portion of the DATABASE_URL with our container's IP. It's like giving our database connection a new look, complete with the latest fashion trends!

But what if the DATABASE_URL is already present in the .env file? Well, it's time for a little update! We use the same sed command to ensure that the DATABASE_URL stays fresh and up to date. It's like getting a software makeover, but without the pricey salon bill!

Don't Bungling

But wait, what if the .env file doesn't exist at all? Fear not, my adventurous friend! We don our creator's hat and bring a brand-new .env file into existence. We fill it with the glorious DATABASE_URL containing our container IP. It's like breathing life into a new creation, just like Dr. Frankenstein!

And now, the stage is set, the actors are in position, and it's showtime! We start the main process of our container, whether it's launching our application or performing some code wizardry. It's like hitting the play button and letting the magic unfold!

🎉 TaDa Emoji (This is not generated by ChatGPT!)

Congratulations, my intrepid coder! You've made it through this wild journey of development journaling and code exploration. You've learned how to add the Container IP to the environment variables of your Node.js Express + Prisma ORM project in a Docker container. The world is your oyster, and you're the one wielding the code sword!

Remember, Punk, in the world of software freestyling, there are no limits. Code with passion, unleash your creativity, and have a blast along the way. Until next time, keep rocking those keyboards and coding with character!

🛹 Stay freestyle, my friend!

DISCLAIMER: This content was made via Google Translate (an AI tool used to translate languages, you may have forgotten so I reminded it). If the language I use seems odd, assume I'm digressing and trying to say something. Why should there be this disclaimer? Because I always considered ChatGPT!!!

Top comments (0)