Dockerfile
아래는 Dockerfile의 주요 구성 내용입니다.
1. 베이스 이미지 및 메인테이너 정보
FROM ubuntu:20.04
MAINTAINER evan <test@gmail.com>
- 베이스 이미지로 ubuntu:20.04 사용
- 작성자 정보 추가
2. 환경 변수 설정
ENV DEBIAN_FRONTEND noninteractive
- DEBIAN_FRONTEND를 noninteractive로 설정하여 설치 중 사용자 입력을 방지
3. 기본 패키지 설치 및 업데이트
RUN apt-get update
RUN apt-get -y upgrade
RUN apt-get -y install software-properties-common apt-utils
- 시스템 패키지 업데이트 및 필수 패키지 설치
4. PHP 7.3 및 확장 프로그램 설치
RUN LC_ALL=C.UTF-8 add-apt-repository -y ppa:ondrej/php
RUN apt-get -y update
RUN apt-get -y install openssl curl nginx rdate sudo locales imagemagick php-imagick composer vim git unzip
RUN apt-get -y install php7.3 php7.3-common php7.3-fpm php7.3-mysql php7.3-zip php7.3-pdo php7.3-cli php7.3-bcmath php7.3-curl php7.3-gd php7.3-opcache php7.3-mbstring php7.3-xml php7.3-xdebug
- ppa:ondrej/php 저장소 추가
- PHP 7.3과 함께 다양한 확장 프로그램 설치 (MySQL, GD, Xdebug 등)
5. Nginx 설정
ADD php.ini /etc/php/7.3/fpm/php.ini
ADD nginx/fastcgi_params /etc/nginx/fastcgi_params
- PHP 설정 파일(php.ini)과 Nginx의 FastCGI 파라미터를 복사
6. Node.js 및 Yarn 설치
RUN apt-get update && apt-get install -y nodejs yarn
- Node.js와 Yarn 설치. 이는 JavaScript 기반 프로젝트나 의존성 관리에 유용
7. 포트 설정
EXPOSE 80
- 기본적으로 Nginx가 사용하는 80번 포트를 노출
8. 프로젝트 복사
ADD html /var/www/html
- html 디렉토리 내부의 프로젝트를 컨테이너의 /var/www/html로 복사
9. 로케일 및 타임존 설정
RUN /usr/sbin/locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
RUN ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/local-time
- UTF-8 로케일과 Asia/Seoul 타임존 설정
10. Xdebug 설정
RUN echo "xdebug.remote_host=host.docker.internal" >> /etc/php/7.3/fpm/conf.d/20-xdebug.ini \
&& echo "xdebug.idekey=PHPSTORM" >> /etc/php/7.3/fpm/conf.d/20-xdebug.ini \
&& echo "xdebug.remote_port=9001" >> /etc/php/7.3/fpm/conf.d/20-xdebug.ini \
&& echo "xdebug.remote_handler=dbgp" >> /etc/php/7.3/fpm/conf.d/20-xdebug.ini \
&& echo "xdebug.remote_autostart=1" >> /etc/php/7.3/fpm/conf.d/20-xdebug.ini \
&& echo "xdebug.remote_mode=req" >> /etc/php/7.3/fpm/conf.d/20-xdebug.ini \
&& echo "xdebug.remote_enable=1" >> /etc/php/7.3/fpm/conf.d/20-xdebug.ini
- Xdebug 설정 파일(20-xdebug.ini)에 원격 디버깅을 위한 환경 추가
11. PHP-FPM 및 Nginx 실행 설정
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
CMD service php7.3-fpm start && nginx
- Nginx 데몬을 비활성화하여 Docker 컨테이너에서 프로세스를 관리
- 컨테이너 실행 시 PHP-FPM과 Nginx를 함께 시작
전체 코드
# Base image
FROM ubuntu:20.04
# Maintainer information
MAINTAINER Wonseok.Lee <wonsuc01@gmail.com>
# Prevent interactive prompts during installation
ENV DEBIAN_FRONTEND noninteractive
# System updates and essential tools
RUN apt-get update && apt-get -y upgrade
RUN apt-get -y install software-properties-common apt-utils
# Add PHP repository and update
RUN LC_ALL=C.UTF-8 add-apt-repository -y ppa:ondrej/php
RUN apt-get -y update
# Install PHP, extensions, and additional tools
RUN apt-get -y install openssl curl nginx rdate sudo locales imagemagick php-imagick composer vim git unzip
RUN apt-get -y install php7.3 php7.3-common php7.3-fpm php7.3-mysql php7.3-zip php7.3-pdo php7.3-cli php7.3-bcmath php7.3-curl php7.3-gd php7.3-opcache php7.3-mbstring php7.3-xml php7.3-xdebug
# Add PHP and Nginx configuration files
ADD php.ini /etc/php/7.3/fpm/php.ini
ADD nginx/fastcgi_params /etc/nginx/fastcgi_params
# Install Node.js and Yarn
RUN apt-get update && apt-get install -y nodejs yarn
# Expose port for Nginx
EXPOSE 80
# Copy project files to the container
ADD html /var/www/html
# Locale and timezone settings
RUN /usr/sbin/locale-gen en_US.UTF-8
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
RUN ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/local-time
# Nginx configuration
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
# Xdebug configuration
RUN echo "xdebug.remote_host=host.docker.internal" >> /etc/php/7.3/fpm/conf.d/20-xdebug.ini \
&& echo "xdebug.idekey=PHPSTORM" >> /etc/php/7.3/fpm/conf.d/20-xdebug.ini \
&& echo "xdebug.remote_port=9001" >> /etc/php/7.3/fpm/conf.d/20-xdebug.ini \
&& echo "xdebug.remote_handler=dbgp" >> /etc/php/7.3/fpm/conf.d/20-xdebug.ini \
&& echo "xdebug.remote_autostart=1" >> /etc/php/7.3/fpm/conf.d/20-xdebug.ini \
&& echo "xdebug.remote_mode=req" >> /etc/php/7.3/fpm/conf.d/20-xdebug.ini \
&& echo "xdebug.remote_enable=1" >> /etc/php/7.3/fpm/conf.d/20-xdebug.ini
# Start PHP-FPM and Nginx when the container runs
CMD service php7.3-fpm start && nginx
사용 방법
- Docker 이미지 빌드
$ docker build -t my-php-nginx .
- Docker 컨테이너 실행
$ docker run -d -p 8080:80 --name php-nginx-container my-php-nginx
브라우저에서 http://localhost:8080로 접속하여 확인 가능합니다.
- 컨테이너 관리 명령어
- 작업명령어
컨테이너 시작 $ docker start php-nginx-container 컨테이너 중지 $ docker stop php-nginx-container 컨테이너 삭제 $ docker rm php-nginx-container
결론
이 Dockerfile은 Ubuntu 20.04 기반 PHP 7.3 및 Nginx 환경을 효율적으로 구성합니다. Xdebug와 함께 개발 및 디버깅 작업을 손쉽게 수행할 수 있습니다. 추가적으로 Node.js와 Yarn을 포함하여 JavaScript 기반 프로젝트와의 통합도 가능합니다.