Created
January 22, 2025 10:58
-
-
Save tarasowski/e2a47bef2acf7c049f9f0b8364364d9e to your computer and use it in GitHub Desktop.
docker example notes-api
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
#1 wir erstelen das Volumen und Network | |
docker volume create notes-db-data | |
docker network create notes-api-network | |
#2 wir starten den db container | |
docker container run \ | |
--detach \ | |
--name=notes-db \ | |
--volume notes-db-data:/var/lib/postgresql/data \ | |
--env POSTGRES_DB=notesdb \ | |
--env POSTGRES_PASSWORD=secret \ | |
--network=notes-api-network \ | |
postgres:12 | |
#3 müssen ein Dockerfile erstellen im Folder /notes-api/api | |
# stage one | |
FROM node:lts-alpine AS builder | |
# install dependencies for node-gyp | |
RUN apk add --no-cache python3 make g++ | |
WORKDIR /app | |
COPY ./package.json . | |
RUN npm install --only=prod | |
# stage two | |
FROM node:lts-alpine | |
EXPOSE 3000 | |
ENV NODE_ENV=production | |
USER node | |
RUN mkdir -p /home/node/app | |
WORKDIR /home/node/app | |
COPY . . | |
COPY --from=builder /app/node_modules | |
/home/node/app/node_modules | |
CMD [ "node", "bin/www" ] | |
#4 docker container für die starten | |
docker container run \ | |
--detach \ | |
--name=notes-api \ | |
--env DB_HOST=notes-db \ | |
--env DB_DATABASE=notesdb \ | |
--env DB_PASSWORD=secret \ | |
--publish=3000:3000 \ | |
--network=notes-api-network \ | |
notes-api | |
#5 hier muss die migration im container stattfinden damit die tabellen in der db angelegt sind | |
docker container exec notes-api npm run db:migrate | |
#6 hier ein curl laufen lassen auf localhost:3000/notes -> data: [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment