Created
January 25, 2024 21:28
-
-
Save willgarcia/84935eae58485b6d3601458bcc7cf662 to your computer and use it in GitHub Desktop.
Example docker network beginner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
docker rm -f $(docker ps -a) | |
docker network delete postgres-network | |
docker network create postgres-network | |
docker rm -f postgres-container | |
docker run --name postgres-container \ | |
--network postgres-network \ | |
-e POSTGRES_PASSWORD=mysecretpassword \ | |
-e POSTGRES_USER=test \ | |
-p 5432:5432 \ | |
-d postgres | |
docker build -t myapp-image . | |
docker rm -f myapp-container | |
docker run --name myapp-container \ | |
--network postgres-network myapp-image |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use an official Node.js runtime as a parent image | |
FROM node:14 | |
# Set the working directory in the container | |
WORKDIR /usr/src/app | |
# Copy package.json and package-lock.json to the working directory | |
COPY package*.json ./ | |
# Install app dependencies | |
RUN npm install | |
# Bundle app source | |
COPY . . | |
# Expose the port the app runs on | |
EXPOSE 3000 | |
# Define environment variable | |
ENV NODE_ENV=production | |
# Command to run the application | |
CMD ["npm", "start"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Sequelize } = require('sequelize') | |
// Initialize Sequelize with your PostgreSQL credentials | |
const sequelize = new Sequelize( | |
'postgres://test:mysecretpassword@postgres-container:5432/database', | |
{ | |
dialect: 'postgres', | |
} | |
) | |
// Test the connection | |
async function testConnection() { | |
await sequelize.authenticate() | |
console.log('Connection has been established successfully.') | |
} | |
// Call the testConnection function | |
testConnection() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "test", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "node index.js" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"pg": "^8.11.3", | |
"sequelize": "^6.35.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment