이 기사는 Dockerfile 생성, 이미지 빌딩 및 컨테이너 런타임을 다루는 PHP 7과 함께 Docker를 사용하여 설명합니다. 보안 모범 사례 (뿌리가 아닌 사용자, 종속성 업데이트, 입력 유효성 검증), Docker Comp와의 멀티 서비스 관리에 대해 자세히 설명합니다.
PHP 7을 사용하여 Docker를 사용하려면 PHP 응용 프로그램 (PHP 자체), 웹 서버 (Apache 또는 Nginx와 같은), 필요한 확장 및 응용 프로그램 코드가 포함 된 Docker 이미지를 작성하는 것이 포함됩니다. 다음은 프로세스의 고장입니다.
1. Create a Dockerfile
: This file contains instructions for building your Docker image. Apache를 사용하는 기본 예는 다음과 같습니다.
<code class="dockerfile">FROM php:7.4-apache # Install necessary PHP extensions RUN docker-php-ext-install pdo_mysql # Copy your application code COPY . /var/www/html # Expose the port Apache listens on EXPOSE 80</code>
This Dockerfile
starts with a base PHP 7.4 image including Apache. It then installs the pdo_mysql
extension (essential for database interaction) and copies your application code into the correct directory. 마지막으로 포트 80을 노출시켜 컨테이너 외부에서 응용 프로그램에 액세스 할 수 있도록합니다.
2. Build the Docker Image: Navigate to the directory containing your Dockerfile
and run:
<code class="bash">docker build -t my-php-app .</code>
This command builds the image and tags it as my-php-app
.
3. Run the Docker Container: After building, run the container:
<code class="bash">docker run -p 8080:80 -d my-php-app</code>
This command runs the container in detached mode ( -d
), mapping port 8080 on your host machine to port 80 inside the container. You can now access your application at http://localhost:8080
. Remember to replace 8080
with your preferred port if necessary. 특정 설정 (예 : Apache 대신 Nginx 사용)을 기반으로이를 조정해야 할 수도 있습니다.
Docker에서 PHP 7 응용 프로그램을 확보하려면 다층 접근 방식이 포함됩니다.
Dockerfile
should create and switch to a non-root user.예, Docker Compose는 단일 응용 프로그램 내에서 여러 서비스를 관리하는 데 이상적입니다. 예를 들어, PHP 응용 프로그램에 대한 별도의 컨테이너, 데이터베이스 (MySQL 또는 PostgreSQL과 같은), 메시지 큐 (RabbitMQ와 같은) 및 Redis 캐시가있을 수 있습니다.
A docker-compose.yml
file would define each service:
<code class="yaml">version: "3.9" services: web: build: ./web ports: - "8080:80" depends_on: - db db: image: mysql:8 environment: MYSQL_ROOT_PASSWORD: my-secret-password MYSQL_DATABASE: mydatabase MYSQL_USER: myuser MYSQL_PASSWORD: mypassword</code>
This example shows a web
service (your PHP application) and a db
service (MySQL). The depends_on
directive ensures the database starts before the web application. You would have separate Dockerfile
s for each service. Docker Compose는 이러한 상호 연결된 서비스의 관리를 단순화하여 시작, 중지 및 조정을 보장합니다.
Docker의 PHP 7 응용 프로그램 문제 해결 종종 여러 영역을 확인하는 것이 포함됩니다.
docker logs <container_id></container_id>
to view the logs.docker ps
to check if the container is running and docker inspect <container_id></container_id>
to get more detailed information about the container's state and configuration.Dockerfile
correctly installs necessary extensions, sets the correct working directory, and copies all required files.docker network inspect bridge
(or the name of your network) to check connectivity.Dockerfile
, rebuild the image to reflect those changes.보다 자세한 문제 해결 정보는 공식 Docker 및 PHP 문서에 문의하십시오. 이러한 단계를 특정 설정 및 오류 메시지에 맞게 조정하면 문제를 효율적으로 해결하는 데 도움이됩니다.
위 내용은 PHP 7과 함께 Docker를 사용하는 방법?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!