아래 유튜브를 보면서 실습해보자.
얄팍한 코딩사전 도커
https://www.yalco.kr/36_docker/
서버생성까지만 확인
https://ondolroom.tistory.com/891
우분투 20.04 도커 설치 방법
https://shanepark.tistory.com/237
html, react, 서버 등등
아래 도커파일 예제 참고
docker build -t 이미지이름 도커파일경로
docker build —platform linux/amd64 -t 이미지이름 도커파일경로
-t 옵션 : 이미지 태그
docker run -it --restart=always -d -p 포트:포트(인바운드 설정) --name 컨테이너이름 이미지이름
-it : 컨테이너 접속용
--restart=always : 도커 재구동시 자동 실행
-d : 백그라운드 실행
-p : 포트설정 ex) 80:8080 80으로 접속시 8080으로 인바운드
--name : 컨테이너 이름 설정
docker volume create 볼륨이름
docker volume ls 으로 볼륨 확인가능
docker run -it --restart=always -d -p 3306:3306 -v test_volume:/var/lib/mysql --name 컨테이너이름 이미지이름
-v : 볼륨 설정
정적 html 웹 서버
FROM nginx
# 컨테이너 실행 전 작동할 명령
# RUN (명령)
# 타임존 설정 (설정을 하지 않으면 시간 저장시 다른 시간대로 저장됨)
RUN ln -snf /usr/share/zoneinfo/Asia/Seoul /etc/localtime
RUN echo Asia/Seoul > /etc/timezone
# 엔진엑스는 해당 경로에 index파일을 만들어줘야함
COPY . /usr/share/nginx/html
mariadb
# 도커 허브에서 이미지를 가져와서 이미지를 작업한다
# FROM (이미지 이름:버전)
FROM mariadb
# 컨테이너 실행 전 작동할 명령
# RUN (명령)
# 타임존 설정 (설정을 하지 않으면 시간 저장시 다른 시간대로 저장됨)
RUN ln -snf /usr/share/zoneinfo/Asia/Seoul /etc/localtime
RUN echo Asia/Seoul > /etc/timezone
# 이미지 내부 환경 설정
# ENV (타겟) (내용)
ENV MYSQL_USER testuser
ENV MYSQL_PASSWORD password
ENV MYSQL_ROOT_PASSWORD password
fastapi
# 도커 허브에서 이미지를 가져와서 이미지를 작업한다
# FROM (이미지 이름:버전)
FROM python:3.10.7
# 컨테이너 실행 전 작동할 명령
# RUN (명령)
# 타임존 설정 (설정을 하지 않으면 시간 저장시 다른 시간대로 저장됨)
RUN ln -snf /usr/share/zoneinfo/Asia/Seoul /etc/localtime
RUN echo Asia/Seoul > /etc/timezone
# 컨테이너 내 작업 경로
# WORKDIR (경로)
WORKDIR /app
# 파일 복사
COPY . .
# 파이썬 라이브러리 설치
RUN pip install -r requirements.txt
# 서버 실행
CMD python src/main.py
spring boot
# 도커 허브에서 이미지를 가져와서 이미지를 작업한다
# FROM (이미지 이름:버전)
FROM openjdk:11.0.8-jre-slim
# 컨테이너 실행 전 작동할 명령
# RUN (명령)
# 타임존 설정 (설정을 하지 않으면 시간 저장시 다른 시간대로 저장됨)
RUN ln -snf /usr/share/zoneinfo/Asia/Seoul /etc/localtime
RUN echo Asia/Seoul > /etc/timezone
# 컨테이너 내 작업 경로
# WORKDIR (경로)
WORKDIR /app
# 작업 파일을 변수화 하기
# ARG (변수명)=(파일명)
ARG JAR_FILE=demo*.jar
# 작업 파일을 컨테이너로 복사
# COPY (파일명 또는 ${변수명}) (복사할 파일명)
COPY ${JAR_FILE} application.jar
# 컨테이너 시작 시 내릴 명령 (CMD와 ENTRYPOINT 차이 확인)
# ENTRYPOINT [(명령),(매개변수),(매개변수),(...)]
ENTRYPOINT ["java","-jar","application.jar"]
nextjs
# 도커 허브에서 이미지를 가져와서 이미지를 작업한다
# FROM (이미지 이름:버전)
# FROM node:14.15.4
FROM node
# 컨테이너 실행 전 작동할 명령
# RUN (명령)
# 타임존 설정 (설정을 하지 않으면 시간 저장시 다른 시간대로 저장됨)
RUN ln -snf /usr/share/zoneinfo/Asia/Seoul /etc/localtime
RUN echo Asia/Seoul > /etc/timezone
# 컨테이너 내 작업 경로
# WORKDIR (경로)
WORKDIR /app
# 작업 파일을 컨테이너로 복사
# COPY (파일명 또는 ${변수명}) (복사할 파일명)
# app dependencies, install 및 caching
COPY . .
# 컨테이너 실행 전 작동할 명령
# RUN (명령)
RUN yarn install
# 이미지 내부 환경 설정
# ENV (타겟) (내용)
# `/app/node_modules/.bin`을 $PATH 에 추가
# (bin폴더를 찾기 어려울 수 있으므로 환경변수에 추가해줌)
ENV PATH /app/node_modules/.bin:$PATH
RUN yarn build
# 컨테이너 시작 시 내릴 명령 (CMD와 ENTRYPOINT 차이 확인)
# ENTRYPOINT [(명령),(매개변수),(매개변수),(...)]
CMD ["yarn", "start"]
nextjs - 다른버전
FROM node:20-alpine AS base
# Install dependencies only when needed
FROM base 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
# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV GENERATE_SOURCEMAP=false
RUN NODE_OPTIONS="--max-old-space-size=2048"
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN yarn build
# If using npm comment out above and use below instead
# RUN npm run build
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app
ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 80
ENV PORT 80
# set hostname to localhost
ENV HOSTNAME "0.0.0.0"
# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
CMD ["node", "server.js"]
docker exec -it 컨테이너명 /bin/bash