Docker practice: quickly install Symfony and configure the development environment
Introduction:
With the rapid development of cloud computing and containerization technology, Docker has become a modern application One of the important tools for program development and deployment. Symfony, as a popular PHP framework, can also quickly install and configure the development environment through Docker. This article will introduce how to use Docker to install Symfony, detail the configuration steps of the development environment, and provide corresponding code examples.
Part One: Installing Docker
To start using Docker, you first need to install Docker on your local machine. Please follow the guidelines provided by the official Docker documentation to download and install the Docker version appropriate for your operating system. After the installation is complete, you can verify that Docker has been installed successfully by entering "docker --version" in the terminal or command prompt.
Part 2: Install Symfony using Docker
docker run -it --rm -v $(pwd):/app composer create-project symfony/website-skeleton myproject cd myproject
This command will create a new Symfony project named "myproject" and store it in the current directory.
FROM php:7.4.2-apache RUN apt-get update && apt-get install -y libicu-dev && docker-php-ext-install -j$(nproc) intl RUN a2enmod rewrite COPY . /var/www/html/
This Dockerfile Used PHP 7.4.2 and Apache as the base image, and installed the Intl extension required by Symfony. Additionally, we enabled Apache’s rewrite module and copied all of the Symfony project’s files into the container’s /var/www/html/ directory.
docker build -t symfony-app .
This command will be based on The Dockerfile in the current directory builds a new image named "symfony-app".
docker run -d -p 8000:80 --name symfony-app symfony-app
This command will start a container named "symfony-app", And map it to port 8000 of the host.
Part 3: Configuring the Symfony development environment
The Symfony application has successfully run in the Docker container, now we need to make some common configurations for the development environment.
DATABASE_URL=mysql://db_user:db_password@database_host:database_port/db_name
Replace "db_user" with your database username, "db_password" with your database password, "database_host" with your database host, "database_port" with your database port, and "db_name" is your database name.
docker exec -it symfony-app bin/console doctrine:migrations:migrate
This command Any pending database migrations will be applied.
docker exec -it symfony-app composer install
This command will install all required Symfony runtime components.
In this way, you You can edit and debug code directly in the Symfony container through development tools.
Conclusion:
Through Docker, we can quickly and easily install and configure the Symfony development environment. This article provides detailed steps and code examples to help readers quickly get started and start developing with Symfony. Using Docker, we can easily set up and switch development environments, improving development efficiency and code quality. I wish you success in your Symfony development journey!
(Note: The above code examples are for reference only, please adjust and modify according to the actual situation.)
The above is the detailed content of Docker combat: quickly install Symfony and configure the development environment. For more information, please follow other related articles on the PHP Chinese website!