mirror of
https://github.com/docker/awesome-compose.git
synced 2025-04-19 15:28:06 +02:00
Merge c62ffcdc52
into 18f59bdb09
This commit is contained in:
commit
5ca21c1881
6 changed files with 86 additions and 0 deletions
26
nodejs-postgres/README.md
Normal file
26
nodejs-postgres/README.md
Normal file
|
@ -0,0 +1,26 @@
|
|||
# NodeJS with Postgres Connection
|
||||
|
||||
This is a sample project that demonstrates how to connect a NodeJS app using Express to a Postgres database using Docker Compose. The project includes the following components:
|
||||
|
||||
1. NodeJS app using Express
|
||||
2. Postgres database
|
||||
3. Docker Compose file to set up the environment
|
||||
|
||||
## Project Structure
|
||||
The project consists of the following files:
|
||||
|
||||
1. _docker-compose.yml_: The Docker Compose file that defines the services for the NodeJS app and Postgres database, and the network connections between them.
|
||||
2. _nodejs directory_: Contains the NodeJS code, including index.js and Dockerfile for building the NodeJS app image.
|
||||
3. _postgres directory_: Contains the Dockerfile for building the Postgres database image.
|
||||
|
||||
## Getting Started
|
||||
To run this project, follow these steps:
|
||||
|
||||
1. Clone the repository.
|
||||
2. From the root of the directory, run docker-compose up to start the environment.
|
||||
3. The NodeJS app will be available at http://localhost:3000 and the Postgres database can be accessed at postgres://myuser:mypassword@localhost:5432/mydb.
|
||||
Expected Results
|
||||
When you access the NodeJS app at http://localhost:3000, you should see the message "Hello World!" displayed in the browser.
|
||||
|
||||
## Conclusion
|
||||
This project provides a simple example of how to connect a NodeJS app to a Postgres database using Docker Compose. The project structure follows a standard naming convention and separates the code for the two services into separate directories. By using Docker Compose, it's easy to set up a development environment that can be used to test and debug your NodeJS and Postgres applications.
|
19
nodejs-postgres/docker-compose.yml
Normal file
19
nodejs-postgres/docker-compose.yml
Normal file
|
@ -0,0 +1,19 @@
|
|||
version: '3'
|
||||
services:
|
||||
db:
|
||||
build:
|
||||
context: ./postgres
|
||||
ports:
|
||||
- "5432:5432"
|
||||
app:
|
||||
build:
|
||||
context: ./nodejs
|
||||
ports:
|
||||
- "3000:3000"
|
||||
depends_on:
|
||||
- db
|
||||
networks:
|
||||
- backend
|
||||
|
||||
networks:
|
||||
backend:
|
13
nodejs-postgres/nodejs/Dockerfile
Normal file
13
nodejs-postgres/nodejs/Dockerfile
Normal file
|
@ -0,0 +1,13 @@
|
|||
FROM node:14
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package*.json ./
|
||||
|
||||
RUN npm install
|
||||
|
||||
COPY . .
|
||||
|
||||
EXPOSE 3000
|
||||
|
||||
CMD [ "npm", "start" ]
|
11
nodejs-postgres/nodejs/index.js
Normal file
11
nodejs-postgres/nodejs/index.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
const express = require('express');
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
|
||||
app.get('/', (req, res) => {
|
||||
res.send('Hello World!');
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening at http://localhost:${port}`);
|
||||
});
|
12
nodejs-postgres/nodejs/package.json
Normal file
12
nodejs-postgres/nodejs/package.json
Normal file
|
@ -0,0 +1,12 @@
|
|||
{
|
||||
"name": "nodejs-express-example",
|
||||
"version": "1.0.0",
|
||||
"description": "A simple NodeJS app using Express",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"start": "node index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.17.1"
|
||||
}
|
||||
}
|
5
nodejs-postgres/postgres/Dockerfile
Normal file
5
nodejs-postgres/postgres/Dockerfile
Normal file
|
@ -0,0 +1,5 @@
|
|||
FROM postgres:12
|
||||
|
||||
ENV POSTGRES_DB mydb
|
||||
ENV POSTGRES_USER myuser
|
||||
ENV POSTGRES_PASSWORD mypassword
|
Loading…
Add table
Add a link
Reference in a new issue