본문 바로가기

Docker

Dockerfile로 이미지 만들기

Dockerfile 이란?

  • Dockerfile은 Docker 이미지를 생성하기 위한 스크립트입니다. 이 파일에는 이미지를 빌드하는 데 필요한 명령어들이 포함되어 있습니다.
  • Dockerfile을 사용하면 이미지 생성 과정을 자동화하고 일관되게 만들 수 있습니다.

 

 

Dockerfile 샘플 코드

# Dockerfile
FROM ubuntu:latest

# MAINTAINER Your Name <your-email@example.com>
# 위 명령어는 현재 사용되고 있지 않아 아래 명령어로 사용하기를 권장
LABEL maintainer="Your Name <your-email@example.com>"

# Docker 이미지에 추가 메타데이터 설정
LABEL purpose=Web Server

# 패키지 목록 업데이트 후 Nginx 설치
RUN apt-get update && apt-get install -y nginx

# 호스트의 nginx.conf 파일을 컨테이너의 /etc/nginx/nginx.conf 디렉토리에 복사
COPY nginx.conf /etc/nginx/nginx.conf

# Docker 컨테이너에서 사용할 네트워크 포트 지정. 단순히 문서화 목적이며, 실제 포트와 연결하려면
# docker run 사용해야 한다.
EXPOSE 80

# 컨테이너가 시작될 때 기본적으로 실행될 명령어 지정
# Dockerfile에서 한 번만 사용가능하며, 여러 번 사용하면 마지막 CMD가 적용된다.
CMD ["nginx", "-g", "daemon off;"]
  • FROM : Docker 이미지를 만들 때 기반이 되는 베이스 이미지를 지정합니다.
  • LABEL : 메타데이터를 설정하는 명령어. 이미지의 유지 관리자를 나타내며, key-value 쌍으로 표현된다.

nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip  on;
    gzip_disable "msie6";

    include /etc/nginx/conf.d/*.conf;
}

'Docker' 카테고리의 다른 글

Ubuntu 22.04 Docker 설치  (0) 2024.08.11
Github Actions CI/CD 스크립트 코드  (1) 2024.08.11
Docker 명령어  (0) 2024.08.10
Docker 설치 및 삭제 - Windows 버전  (0) 2024.08.09
Docker 란?  (0) 2024.08.08