Skip to content

Instantly share code, notes, and snippets.

@Klerith
Last active December 15, 2024 02:33
Show Gist options
  • Save Klerith/eb30ce8ca7e0b70f19fc5efee3bd9b9b to your computer and use it in GitHub Desktop.
Save Klerith/eb30ce8ca7e0b70f19fc5efee3bd9b9b to your computer and use it in GitHub Desktop.
Preparar imagen de Docker - Node App

Build

docker-compose -f docker-compose.prod.yml --env-file .env.prod up --build

Run

docker-compose -f docker-compose.prod.yml --env-file .env.prod up

Nota

Por defecto, docker-compose usa el archivo .env, por lo que si tienen el archivo .env y lo configuran con sus variables de entorno de producción, bastaría con

docker-compose -f docker-compose.prod.yml up --build

Cambiar nombre

docker tag <nombre app> <usuario docker hub>/<nombre repositorio>

Ingresar a Docker Hub

docker login

Subir imagen

docker push <usuario docker hub>/<nombre repositorio>
version: '3'
services:
db:
image: postgres:14.4
restart: always
ports:
- "${DB_PORT}:${DB_PORT}"
environment:
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
container_name: anylistDB
volumes:
- ./postgres:/var/lib/postgresql/data
anylistapp:
depends_on:
- db
build:
context: .
dockerfile: Dockerfile
image: nest-graphql
container_name: AnylistApp
restart: always # reiniciar el contenedor si se detiene
ports:
- "${PORT}:${PORT}"
environment:
DB_PASSWORD: ${DB_PASSWORD}
DB_NAME: ${DB_NAME}
DB_HOST: ${DB_HOST}
DB_PORT: ${DB_PORT}
DB_USERNAME: ${DB_USERNAME}
JWT_SECRET: ${JWT_SECRET}
PORT: ${PORT}
# Install dependencies only when needed
FROM node:18-alpine3.15 AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
# Build the app with cache dependencies
FROM node:18-alpine3.15 AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN yarn build
# Production image, copy all the files and run next
FROM node:18-alpine3.15 AS runner
# Set working directory
WORKDIR /usr/src/app
COPY package.json yarn.lock ./
RUN yarn install --prod
COPY --from=builder /app/dist ./dist
CMD [ "node","dist/main" ]
@CHB94git
Copy link

CHB94git commented Jun 1, 2024

Excelente Fernando! Gracias por el curso

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment