Docker installation of Symfony: a simple and fast development environment
Overview:
Symfony is a PHP development framework for building modern web applications. In order to facilitate developers to quickly set up a Symfony development environment, we can use Docker containers for installation and configuration. This article will show you how to use Docker to install Symfony and provide specific code examples.
Step 1: Install Docker
First, you need to install Docker on your machine. Docker is an open source containerization platform that helps us build and manage containerized applications. You can visit the Docker official website (https://www.docker.com/) to download and install the version suitable for your operating system.
Step 2: Create the Symfony project
After installing Docker, we can start creating the Symfony project. Execute the following command in the command line:
$ docker run --rm -v $(pwd):/opt -w /opt composer create-project symfony/skeleton my_project
The above command will use Composer to create a Symfony project named "my_project" in the current directory.
Step 3: Create Dockerfile
Next, we need to create a file called Dockerfile, which will define the Docker image of the Symfony project. Create a file named Dockerfile in the project root directory and copy the following content into it:
# 使用PHP官方镜像作为基础镜像 FROM php:7.4-apache # 安装Symfony所需的扩展 RUN apt-get update && apt-get install -y libicu-dev zip unzip git RUN docker-php-ext-configure intl RUN docker-php-ext-install intl RUN docker-php-ext-install pdo pdo_mysql # 复制Symfony项目到容器中 COPY . /var/www/html # 设置Apache的DocumentRoot WORKDIR /var/www/html/public RUN sed -ri -e 's!/var/www/html!/var/www/html/public!g' /etc/apache2/sites-available/*.conf RUN sed -ri -e 's!/var/www/!/var/www/html/public!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf # 启用Apache的rewrite模块 RUN a2enmod rewrite
The above Dockerfile will build an image based on PHP 7.4 and Apache, and install the extensions required by Symfony. It will also copy the Symfony project into the container and configure Apache's DocumentRoot to be the project's public directory to ensure proper routing and access.
Step 4: Build the Docker image
Enter the root directory of the Symfony project in the command line and execute the following command to build the Docker image:
$ docker build -t my_symfony_app .
The above command will use the Dockerfile and the current directory to build a Docker image named "my_symfony_app".
Step 5: Run the Symfony application
After building the Docker image, we can use the following command to run the Symfony application:
$ docker run -p 8080:80 my_symfony_app
The above command will start a container and change the container's Port 80 is mapped to port 8080 of the local machine. Therefore, you can view the running Symfony application by visiting "http://localhost:8080" in your browser.
Summary:
Using Docker to install Symfony can help us quickly build a development environment and ensure the consistency and portability of the environment. This article shows you how to use Docker to install and configure Symfony by giving specific code examples. Hopefully this article will help you easily start using Symfony for web application development.
The above is the detailed content of Docker installs Symfony: a simple and fast development environment. For more information, please follow other related articles on the PHP Chinese website!