Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Dockerize Nodejs and Postgres example

Tags:

Posted on Oct 19 Docker provides lightweight containers to run services in isolation from our infrastructure so we can deliver software quickly. In this tutorial, I will show you how to dockerize Nodejs Express and Postgres example using Docker Compose.Related Posts:Assume that we have a Nodejs Application working with Postgres database.The problem is to containerize a system that requires more than one Docker container:Docker Compose helps us setup the system more easily and efficiently than with only Docker. We're gonna following these steps:Directory Structure:You can read and get Github source code from one of following tutorials:Using the code base above, we put the Nodejs project in bezkoder-app folder and modify some files to work with environment variables.Firstly, let's add dotenv module into package.json.Next we import dotenv in server.js and use process.env for setting port.Then we change modify database configuration and initialization.app/config/db.config.jsapp/models/index.jsWe also need to make a .env sample file that shows all necessary arguments.bezkoder-app/.env.sampleDockerfile defines a list of commands that Docker uses for setting up the Node.js application environment. So we put the file in bezkoder-app folder.Because we will use Docker Compose, we won't define all the configuration commands in this Dockerfile.bezkoder-app/DockerfileLet me explain some points:Let's make Nodejs connect with PostgreSQL using Docker.On the root of the project directory, we're gonna create the docker-compose.yml file. Follow version 3 syntax defined by Docker:Let's implement the details.docker-compose.ymlpostgresdb:app:You should note that the host port (LOCAL_PORT) and the container port (DOCKER_PORT) is different. Networked service-to-service communication uses the container port, and the outside uses the host port.In the service configuration, we used environmental variables defined inside the .env file. Now we start writing it..envWe can easily run the whole with only a single command:docker compose upDocker will pull the PostgreSQL and Node.js images (if our machine does not have it before).The services can be run on the background with command:docker compose up -dNow you can check the current working containers:And Docker images:Send a HTTP request to the Nodejs - Postgres application:Check Postgres Database:Stopping all the running containers is also simple with a single command:docker compose downIf you need to stop and remove all containers, networks, and all images used by any service in docker-compose.yml file, use the command:docker compose down --rmi allToday we've successfully created Docker Compose file for Postgres and Nodejs application. Now we can deploy Nodejs Express and Postgres with Docker on a very simple way: docker-compose.yml.You can apply this way to one of following project:Happy Learning! See you again.The source code for this tutorial can be found at Github.If you want to add Comments for each Tutorial. It is the One-to-Many Relationship there is a tutorial:Node.js Sequelize Associations: One-to-Many example(the tutorial uses MySQL but you can easily change the configuration for PostgreSQL)Or you can add Tags for each Tutorial and add Tutorials to Tag (Many-to-Many Relationship):Node.js Sequelize Associations: Many-to-Many exampleTemplates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Alejandro Velez - Oct 15 Zafar Urakov - Sep 25 Varun Gujarathi - Oct 16 Cybermaxi7 - Sep 24 Once suspended, tienbku will not be able to comment or publish posts until their suspension is removed. Once unsuspended, tienbku will be able to comment and publish posts again. Once unpublished, all posts by tienbku will become hidden and only accessible to themselves. If tienbku is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Tien Nguyen. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag tienbku: tienbku consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging tienbku will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Dockerize Nodejs and Postgres example

×