DevOps/Docker

Dockerfile

잔망루피 2022. 4. 25. 22:02
반응형

Dockerfile 예제

FROM openjdk:11

ARG JAR_FILE=./build/libs/*.jar
COPY ${JAR_FILE} app.jar

EXPOSE 8080
ENTRYPOINT ["java", "-jar","/app.jar"]

FROM 도커 이미지에서 layer를 생성

ARG 변수 생성

COPY 도커 클라이언트의 현재 폴더에서 파일을 추가

EXPOSE 컨테이너가 연결을 수신하는 포트 지정

ENTRYPOINT 이미지의 주요 명령어 설정해서 명령어를 통해 이미지가 실행될 수 있게 한다.

 

위의 코드를 해석하면,

  1. openjdk:11 이미지를 가져온다.
  2. JAR_FILE 변수에 jar 파일을 넣는다.
  3. 해당 변수를 app.jar로 복사한다.
  4. 8080 포트를 연다.
  5. java -jar /app.jar 명령어가 실행된다.

 

그 외

  • CMD 👉 컨테이너 내에서 실행할 명령어
  • RUN 👉 애플리케이션을 빌드
  • --from 👉 별칭으로 해당 stage를 불러옴

 

# https://github.com/docker/awesome-compose/blob/master/react-nginx/Dockerfile

# syntax=docker/dockerfile:1.4

# 1. For build React app
FROM node:lts AS development

# Set working directory
WORKDIR /app

# 
COPY package.json /app/package.json
COPY package-lock.json /app/package-lock.json

# Same as npm install
RUN npm ci

COPY . /app

ENV CI=true
ENV PORT=3000

CMD [ "npm", "start" ]

FROM development AS build

RUN npm run build


FROM development as dev-envs
RUN <<EOF
apt-get update
apt-get install -y --no-install-recommends git
EOF

RUN <<EOF
useradd -s /bin/bash -m vscode
groupadd docker
usermod -aG docker vscode
EOF
# install Docker tools (cli, buildx, compose)
COPY --from=gloursdocker/docker / /
CMD [ "npm", "start" ]

# 2. For Nginx setup
FROM nginx:alpine

# Copy config nginx
COPY --from=build /app/.nginx/nginx.conf /etc/nginx/conf.d/default.conf

WORKDIR /usr/share/nginx/html

# Remove default nginx static assets
RUN rm -rf ./*

# Copy static assets from builder stage
COPY --from=build /app/build .

# Containers run nginx with global directives and daemon off
ENTRYPOINT ["nginx", "-g", "daemon off;"]

as로 stage에 별명을 붙여주고 --from으로 부른다.

 

 

 


참고 👇

https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

 

Best practices for writing Dockerfiles

 

docs.docker.com

 

https://docs.docker.com/build/building/multi-stage/

 

Multi-stage builds

Learn about multi-stage builds and how you can use them to improve your builds and get smaller images

docs.docker.com

 

반응형