docker 간단한 예제

JangUT·2025년 3월 7일

📌 컨테이너 기본 실행

1. 도커 버전 확인

	docker --version

2. Hello World 컨테이너 실행

	docker run hello-world
  • 의미 파악
    • docker run 명령어는 이미지를 기반으로 컨테이너를 실행합니다.
    • hello-world 이미지는 Docker Hub 에서 자동으로 다운로드(pull)됩니다.

3. 출력 확인

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

📌 docker 명령어

Ubuntu 컨테이너 생성 및 접속

	docker run -it ubuntu bash
  • it 옵션 : 인터랙티브 터미널(interactive + TTY)모드
  • ubuntu : 사용할 이미지 이름
  • bash : 컨테이너 시작 시 bash 셸을 실행시킨다는 의미

컨테이너 내부에서 명령어 실행

  • 컨테이너 내부로 들어갔으므로, 이제 리눅스 환경과 유사하게 명령어를 사용할 수 있습니다.
	# 패키지 목록 업데이트
	apt-get update
    
    # 텍스트 에디터 nano 설치(예시)
	apt-get install nano -y

	# 테스트 용도로 파일 작성
	echo "Hello Docker" > hello.txt
	cat hello.txt
	
	# bash 셸 종료(컨테이너도 함께 종료)
	exit
		

컨테이너 확인

# 모든 컨테이너 목록 확인(종료된 컨테이너 포함)
docker ps -a

# 실행 중인 컨테이너만 보려면
docker ps

컨테이너 재실행 및 제거

# 재실행 (컨테이너 ID는 전부 입력할 팔요없음)
docker start <컨테이너ID or 컨테이너이름>

docker exec -it <컨테이너ID or 컨테이너이름> bash

# 컨테이너 삭제
docker rm <컨테이너ID or 컨테이너이름>

# 이미지 목록 확인
docker images

# 이미지 삭제
docker rmi <컨테이너ID or 컨테이너이름> , docker image rm <컨테이너ID or 컨테이너이름>

Dockerfile 작성

1. 프로젝트 최상위 폴더에 Dockerfile을 작성합니다.


# 1. 사용할 베이스 이미지 지정 (OpenJDK 17)
FROM openjdk:17-jdk-slim

# 2. JAR 파일을 /app 디렉토리 복사
ARG JAR_FILE=build/libs/spring-petclinic-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} /app/springboot-app.jar

# 3. 컨테이너 시작 시 실행할 명령어
ENTRYPOINT ["java", "-jar", "/app/spring-app.jar"]

2. 도커 이미지 빌드

#Dockerfile 최상위 폴더에서 실행
docker build -t springboot-docker-demo:1.0 .
  • t 옵션으로 이미지 이름 및 태그 지정 : springboot-docker-demo:1.0
  • . 은 현재 디렉토리 (=Dockerfile이 있슨 위치)

3. 빌드된 이미지 실행 확인

docker run -p 8080:8080 springboot-docker-demo:1.0
  • p 8080:8080 : 호스트의 8080 포트와 컨테이너의 8080 포트를 연결 (포트 매핑)
  • 브라우저에서 http://localhost:8080 접속 시, 스프링 부트 애플리케이션의 기본 페이지 or "Welcome" 메시지가 나타나는지 확인

Docker Compose

  • Docker Compose두 개 이상의 컨테이너 를 한 번에 실행 및 관리할 수 있습니다.

docker-compose.yml

  • 프로젝르 루트에 docker-compose.yml 파일을 생성합니다.
services:
  app:
    image: springboot-docker-demo:1.0
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    enviroment:
      SPRING_DATASOURSE_URL: jdbc:mysql://db:3306/my_database?useSSL=false
      SPRING_DATASOURSE_USERNAME: root
      SPRING_DATASOURSE_PASSWORD: root
    depends_on:
      - db
  
  db:
    image: mysql:8.0
    container_name: my-mysql
    enviroment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: my_database
      MYSQL_USER: demo
      MYSQL_PASSWORD: demo
    ports:
      - "3306:3306"
    volumes:
      - db_data:/var/lib/mysql
      
volumes:
  db_data:

주요 설정 설명

1. app(Spring Boot)

  • image : 이미 빌드된 이미지나 build 지시자를 통해 현재 디렉트로의 Dockerfile로 이미지를 빌드할 수도 있음
  • ports : 호스트 8080 <--> 컨테이너 8080 매핑
  • enviroment : Spring Application에서 DB 연결 정보를 환경변수로 주입
  • depends_on : db 서비스가 먼저 실행된 후 app를 실행

2. db(MySQL)

  • image: mysql:8.0
  • MYSQL_ROOT_PASSWORD , MYSQL_DATABASE 등 환경변수로 DB초기 설정
  • volumes : DB데이터를 영구 저장하기 위한 볼륨 마운트

3. volumes

  • db_data : MySQL 데이터가 저장되는 도커 볼륨

Docker Compose 실행

docker-compose up -d
  • d 옵션: 백그라운드(detached) 모드로 실행
  • app, db 두 개의 컨테이너가 동시에 실행됨

Compose 종료 및 정리

# 컨테이너와 네트워크를 죵료합니다.
docker-compose down
profile
평범한 개발자

0개의 댓글