Post

Docker file 작성하기, SpringBoot, Next.js, HTML올리기

☑️ Docker file

  • docker file: file to create docker image
  • docker image is created using docker file

✅ FROM

  • create base image
  • ubuntu, JDK, Node
1
2
3
4
FROM ubuntu:latest

ENTRYPOINT ["/bin/bash", "-c", "sleep 500"]
# 500초동안 시스템을 일시정지시키는 명령어
1
2
3
4
FROM openjdk:17-jdk

ENTRYPOINT ["/bin/bash", "-c", "sleep 500"]
# 500초동안 시스템을 일시정지시키는 명령어
  • run in terminal
  • inside package with docker file
1
2
3
4
5
6
7
8
9
10
11
12
-- inside package with docker file

-- build
docker build -t 
docker build -t my-node-server .

-- check image
docker image ls


-- run container
 docker run -d my-node-server

✅ COPY

  • copy file in host computer to container
  • COPY 호스트 상대경로 /container절대경로
1
2
3
4
5
6
7
8
9
10
11
12
13
FROM ubuntu

# host computer의 app.txt를 container의 /app.txt에 복사
COPY app.txt /app.txt

# host computer의 my-app폴더를 container의 /my-app/에 복사
COPY my-app /my-app/

# host computer의 모든 txt파일을 container의 /text-files/라는 파일에 복사
COPY *.txt /text-files/


ENTRYPOINT ["/bin/bash", "-c", "sleep 500"]
  • ❓ if there is a file I do not want to copy?
  • add to .dockerignore

✅ ENTRYPOINT

  • command to start when container is run for the first time
1
ENTRYPOINT ["/bin/bash", "-c", "sleep 500"]

☑️ Upload Springboot on Docker

  • create docker file
1
2
3
4
5
6
FROM openjdk:17-jdk
LABEL authors="soheeparklee"

COPY build/libs/*SNAPSHOT.jar app.jar

ENTRYPOINT ["java", "-jar", "/app.jar"]
  • run build, run commands
1
2
3
4
5
6
7
8
9
10
11
-- clean build
./gradlew clean build

-- build
docker build -t java-server .

-- run
docker run -d -p 8080:8080 java-server

-- check
docker ps

✅ RUN

  • run command while creating image
  • used for install, set environment

  • RUN 🆚 ENTRYPOINT
  • RUN: run command while creating image
  • ENTRYPOINT: run command after creating container
1
2
3
4
5
6
FROM ubuntu

#download git on this container
RUN apt update && apt install -y git

ENTRYPOINT ["/bin/bash", "-c", "sleep 500"]
  • create ubuntu container
  • install git with RUN command
  • after container is created, run ENTRYPOINT

✅ WORKDIR

  • set work directory to save the files
  • to maintain packages in container in an organized way
1
2
3
4
5
6
7
FROM ubuntu

WORKDIR /my-dir

COPY ./ ./

ENTRYPOINT ["/bin/bash", "-c", "sleep 500"]
  • now all dockerfiles will be created in /my-dir folder

✅ EXPOSE

  • document the port that the programming is running
1
EXPOSE 3000

☑️ Upload Nest.js

  • install npm, create project
1
2
3
4
5
# install
sudo npm i -g @nestjs/cli

# create
nest new my-server
  • dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
FROM node
WORKDIR /app

COPY . .

RUN npm install
RUN npm run build

EXPOSE 3000


ENTRYPOINT ["node", "dist/main.js"]

☑️ Upload Next.js

  • create project
1
npx create-next-app@latest
  • dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
FROM node:20-alpine #simplified version of node:20

WORKDIR /app

COPY . .

RUN npm install
RUN npm run build

EXPOSE 3000

ENTRYPOINT ["npm", "run", "start"]

  • run container
1
2
3
4
5
npm run dev

docker build -t my-server .

docker run -d -p 80:3000 my-server

☑️ Upload HTML

  • create index.html, style.css
  • create dockerfile
1
2
3
FROM nginx

COPY ./ /usr/share/nginx/html
  • build and run
1
2
3
docker build -t my-server .

docker run -d -p 80:80 my-server

This post is licensed under CC BY 4.0 by the author.