Skip to content

Instantly share code, notes, and snippets.

@kennethnwc
Last active July 28, 2024 08:09
Show Gist options
  • Save kennethnwc/efc81d448a6381f07fd42b4305f12f68 to your computer and use it in GitHub Desktop.
Save kennethnwc/efc81d448a6381f07fd42b4305f12f68 to your computer and use it in GitHub Desktop.
My docker-compose with nextjs and nginx
.next/
node_modules/
Dockerfile
yarn-error.log
.dockerignore
.git
.gitignore
image: docker
services:
- docker:dind
stages:
- test
- deploy
test:
stage: test
only:
- staging
script:
- echo It should be running some test
tags:
- staging
step-deploy-staging:
stage: deploy
only:
- staging
variables:
API_URL: ""
GOOGLE_KEY: "ABCD"
NEXT_APP_VERSION: "0.0.13"
script:
# - sudo docker image prune -f
# - docker volume prune -f
- docker-compose build
- docker-compose down
- docker-compose up -d
tags:
- staging
environment: staging
step-deploy-prod:
stage: deploy
only:
- production
variables:
API_URL: ""
script:
- sudo docker image prune -f
- docker-compose build --no-cache
- docker-compose up -d
environment: production
version: "3.8"
services:
nextjs:
ports:
- "3000:3000"
build:
context: nextapp
dockerfile: Dockerfile.dev
volumes:
- ./nextapp:/app
- nextjs-dev-node_modules:/app/node_modules
- nextjs-dev-next:/app/.next
restart: always
frontend:
image: nginx:latest
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf.d:/etc/nginx/conf.d
ports:
- "80:80"
depends_on:
- nextjs
restart: always
volumes:
nextjs-dev-node_modules:
nextjs-dev-next:
version: "3.8"
services:
nextjs:
image: kennethnwc/nextapp:${NEXT_APP_VERSION}
ports:
- "3000:3000"
build:
context: nextapp
dockerfile: Dockerfile
target: prod
args:
NEXT_PUBLIC_GOOGLE_KEY: ${GOOGLE_KEY}
NEXT_PUBLIC_APP_VERSION: ${NEXT_APP_VERSION}
volumes:
- /app/node_modules
- /app/.next
restart: always
environment:
- API_URL=${API_URL}
- GOOGLE_KEY=${GOOGLE_KEY}
frontend:
image: nginx:latest
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf.d:/etc/nginx/conf.d
ports:
- "80:80"
depends_on:
- nextjs
restart: always
FROM node:14.15-alpine as base
RUN mkdir /app
WORKDIR /app
COPY package*.json ./
COPY yarn.lock ./
FROM base as pre-prod
COPY . .
RUN yarn install --frozen-lockfile
ARG NEXT_PUBLIC_GOOGLE_KEY
ARG NEXT_PUBLIC_APP_VERSION
ENV NEXT_PUBLIC_GOOGLE_KEY=${NEXT_PUBLIC_GOOGLE_KEY}
ENV NEXT_PUBLIC_APP_VERSION=${NEXT_PUBLIC_APP_VERSION}
RUN yarn build
FROM node:14.15-alpine as prod
RUN mkdir /app
WORKDIR /app
COPY --from=pre-prod /app/public ./public
COPY --from=pre-prod /app/.next ./.next
COPY --from=pre-prod /app/node_modules ./node_modules
EXPOSE 3000
CMD ["node_modules/.bin/next", "start"]
FROM node:14.5.0 as dev
RUN mkdir /app
WORKDIR /app
COPY package*.json ./
COPY yarn.lock ./
RUN yarn
EXPOSE 3000
CMD ["yarn" , "dev"]
@nikelborm
Copy link

Hi friend! You have a little mistake in Dockerfile on line 9 forzen-lockfile. It should be frozen

@kennethnwc
Copy link
Author

kennethnwc commented Mar 30, 2023

thank you. Just wonder if this still related. Its been a long time.

@verdverm
Copy link

Do you have the nginx files? That's where I'm getting weird issues

@kennethnwc
Copy link
Author

kennethnwc commented Feb 28, 2024

Do you have the nginx files? That's where I'm getting weird issues

This setup i used was long long time ago. I am not sure it helps. Here the nginx conf i got.

`resolver 10.xxx.x.x;
server {
listen 80;
# server_name _;

location /_next/static {
    proxy_cache STATIC;
    proxy_pass http://frontend_upstream;
    # For testing cache - remove before deploying to production
    # add_header X-Cache-Status $upstream_cache_status;
}

location / {
    proxy_pass http://frontend_upstream;
    # expires 60s;
}  

location /service/api {
proxy_set_header Host $host;
# development
proxy_pass http://10.xxx.xxx.1:4000;
# staging
#proxy_pass http://10.xxx.xxx.11:4000;
# production
# proxy_pass http://10.xxx.xxx.12:4000;

}

}`

I have no idea how this works right now. lol

@zeronumbers
Copy link

RUN mkdir /app
WORKDIR /app

why mkdir when workdir will create it?

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