Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker node app #311

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions nodejs/docker-app/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.git
.gitignore
node_modules/
7 changes: 7 additions & 0 deletions nodejs/docker-app/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
NODE_ENV=production
DB_SCHEMA= postgres
DB_USER= postgres
DB_PASSWORD= postgres
DB_HOST= localhost
DB_HOST= postgres
DB_PORT= 5432
7 changes: 7 additions & 0 deletions nodejs/docker-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM node:18.16.0-alpine3.17
WORKDIR /opt/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3333
CMD [ "npm", "start"]
15 changes: 15 additions & 0 deletions nodejs/docker-app/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
So what the above command will check our current directory for our docker-compose yaml file, and it is going to start all the services in detach mode `-d`.

If we want it to build first before start we can insert `--build` tag.
# Image

As we can see theres a much of stuff happening, we can see our docker node server is built, it is creeated and runned.

We can run `docker container ls` we can check a list of all running containers
# Image.****

We can see that it expose our `docker-node-server` on port `3333:3333`. If we go the the browser/test-tool then `localhost:3333` we can see our server welcome response message. This shows our Docker compose is working properly.

# Image

You can go ahead and try creating and quering for User data using the `/users` endpoint.****
18 changes: 18 additions & 0 deletions nodejs/docker-app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const express = require("express");
const dotenv = require("dotenv");
dotenv.config();
const UserRouter = require("./routes/users");
const app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: false }));

app.get("/", (req, res) => {
res.send({ data: { message: "Welcome to Docker Crash Course!!!" } });
});

app.use("/users", UserRouter);

app.listen(3333, () => {
console.log("Server is running on port 3333");
});
46 changes: 46 additions & 0 deletions nodejs/docker-app/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const Sequelize = require("sequelize");
const sequelize = new Sequelize(
process.env.DB_SCHEMA || "postgres",
process.env.DB_USER || "postgres",
process.env.DB_PASSWORD || "123456",
{
host: process.env.DB_HOST || "localhost",
port: process.env.DB_PORT || 5432,
dialect: "postgres",
dialectOptions: {
ssl: process.env.DB_SSL == "true",
},
logging: false, // Disable logging
}
);

sequelize
.authenticate()
.then(() => {
console.log("Connection has been established successfully.");
})
.catch(err => {
console.error("Unable to connect to the database:", err);
});

const Users = sequelize.define("Users", {
name: {
type: Sequelize.STRING,
allowNull: false,
},
age: {
type: Sequelize.INTEGER,
allowNull: true,
},
});

sequelize
.sync({ force: false }) // `force: true` will drop the table if it already exists
.then(() => {
console.log("Database & tables created!");
})
.catch(error => {
console.error("Unable to create tables:", error);
});

module.exports = { sequelize, Users };
37 changes: 37 additions & 0 deletions nodejs/docker-app/database.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const db = require("./database");

beforeAll(async () => {
await db.sequelize.sync({ force: false });
});

test("Add new User", async () => {
expect.assertions(1);
const user = await db.Users.create({
id: 1234,
name: "Denver Mike",
age: 18,
});
expect(user.id).toEqual(1234);
});

test("Get a User", async () => {
expect.assertions(2);
const user = await db.Users.findByPk(1234);
expect(user.name).toEqual("Denver Mike");
expect(user.age).toEqual(18);
});

test("Delete state", async () => {
expect.assertions(1);
await db.Users.destroy({
where: {
id: 1234,
},
});
const user = await db.Users.findByPk(1234);
expect(user).toBeNull();
});

afterAll(async () => {
await db.sequelize.close();
});
24 changes: 24 additions & 0 deletions nodejs/docker-app/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: "3.9"
services:
postgres:
image: postgres:latest
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
volumes:
- server-db:/var/lib/postgresql/data

server:
build:
context: .
env_file:
- .env
depends_on:
- postgres
ports:
- "3333:3333"

volumes:
server-db:
Loading