[Docker] 6. Dockerfile

문정준's avatar
Jul 30, 2025
[Docker] 6. Dockerfile

스크립트를 통한 이미지 사전 설정

  • 내 컴퓨터에서 미리 파일을 설정하고 내가 원하는 이미지로 만들기 위한 스크립트 설정
  • 이를 Dockerfile이라고 함
  • 복사 후 덮어쓰기의 방식을 사용 : 마운트 기법이 아님
    • 마운트는 현재 설정을 건드리지 않으면서 작업을 진행해야 할 때 주로 사용
 

Dockerfile 생성

  • httpd의 index.html을 바꾸기 위해 위치 확인 필요
  • httpd의 index.html은 /usr/local/apache2/htdocs에 있음
  • 다음과 같이 Dockerfile 작성
    • FROM : 사용할 이미지 이름
    • COPY : 파일 또는 폴더 복사
    • CMD : 이미지 구동 시 사용할 명령어
FROM httpd COPY ./htdocs /usr/local/apache2/htdocs CMD ["httpd-foreground"]
 
  • Dockerfile을 통해 이미지를 build
    • 명령어 입력
docker build -t 'image name' . # .은 ./이 생략된 형태 # 스크립트 이름이 Dockerfile이 아닐 경우 상대 경로를 통해 명시해주어야 함 # docker build -t 'image name' ./'customfile name'
notion image
 
  • 이미지가 새롭게 생성된 것을 확인 가능
notion image
 
  • 이미지 실행 시 의도한 화면 출력 확인 가능
docker run -d -p 8080:80 webserver
notion image
 

실습 : nginx의 포트 번호 및 index.html 수정

  • nginx의 포트 번호를 확인하기 위해 conf 확인
    • /etc/nginx/nginx.conf 확인
      • include를 통해 conf.d 폴더 내부에 추가 설정이 있는 것을 확인 가능
user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /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; include /etc/nginx/conf.d/*.conf; }
 
  • conf.d 폴더 내부의 default.conf 확인
    • listen → 내 포트 번호
    • location / → 기본 주소로 접속 시 보여줄 파일
      • root 폴더 : /usr/share/nginx/html
      • index : index.html or index.htm
server { listen 80; listen [::]:80; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
 
  • 설정을 다른 파일명으로 만들면 설정 파일이 서로 충돌할 수 있음 : 이름을 같게 해서 덮어쓰기
    • 포트를 5000번으로 변경한 default.conf 파일을 conf.d 폴더 안에 생성
notion image
server { listen 5000; listen [::]:5000; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
 
  • 새로 만들 html 파일도 html 폴더 안에 생성
 
  • Dockerfile 작성
FROM nginx COPY ./conf.d /etc/nginx/conf.d COPY ./html /usr/share/nginx/html CMD ["nginx", "-g", "daemon off;"]
 
  • build : 성공적으로 작동
notion image
 
  • 접속 원활
notion image
 
Share article

sxias