Home  >  Article  >  Operation and Maintenance  >  Docker installation of Symfony: Quick guide to setting up a development environment

Docker installation of Symfony: Quick guide to setting up a development environment

王林
王林Original
2023-10-20 14:25:501388browse

Docker installation of Symfony: Quick guide to setting up a development environment

Docker Installation of Symfony: Quick Guide to Setting Up a Development Environment

Symfony is a popular PHP framework that provides a fast, reliable and scalable way to Build web applications. With the popularity of Symfony, many developers hope to quickly build a Symfony development environment. In this article, we will introduce how to use Docker to quickly install Symfony and provide specific code examples.

Prerequisites:
Before you begin, you need to install Docker and Docker Compose. You can download and install them from the official website. Please refer to the official documentation for the specific installation process.

  1. Create a Symfony project
    First, open a terminal or command line tool in the directory where you want to create a Symfony project. Then, run the following command to create a new Symfony project:
docker run --rm -v $(pwd):/app composer create-project symfony/website-skeleton myproject

This command will use Composer to create a Symfony project named "myproject".

  1. Create Docker configuration file
    Create a new file in the root directory of the Symfony project and name it docker-compose.yml. Then, copy the following code into this file:
version: '3.7'

services:
  webserver:
    image: nginx:alpine
    ports:
      - 80:80
    volumes:
      - ./myproject/public:/var/www/html
    depends_on:
      - php
  php:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./myproject:/var/www/html

This configuration file defines two Docker services: webserver and php. The webserver service uses the Nginx image, maps port 80 of the host to port 80 of the container, and mounts the public folder of the Symfony project to the /var/www/html directory of the container. The php service will use the Dockerfile we will create later.

  1. Create Dockerfile
    Create a new file in the root directory of the Symfony project and name it Dockerfile. Then, copy the following code into this file:
FROM php:7.4-fpm

RUN apt-get update && apt-get install -y 
    git 
    unzip 
    libpq-dev

RUN docker-php-ext-install pdo pdo_pgsql

WORKDIR /var/www/html

CMD ["php-fpm"]

EXPOSE 9000

This Dockerfile defines the image required for our php service and installs some commonly used software packages and extensions.

  1. Build Docker Container
    In the root directory of the Symfony project, run the following command to build and start the Docker container:
docker-compose up -d

This command will be based on docker-compose Configuration in .yml file to build and start Docker containers. Please make sure you have run this command in the root directory of your Symfony project.

  1. Accessing the Symfony application
    When the Docker container is successfully started, you can visit http://localhost in the browser to view the Symfony application. You should be able to see Symfony's welcome page.
  2. Additional required components
    Depending on your specific project needs, you may need to add some components and libraries. For example, if you need to use a database, run the following command to install Doctrine ORM and database-related extensions:
docker exec -it myproject_php_1 composer require symfony/orm-pack
docker exec -it myproject_php_1 composer require symfony/maker-bundle --dev

This command will install these components and extensions in the running php container.

  1. Further customization
    You can further customize the Symfony project according to your needs. You can modify the docker-compose.yml file to add more services, such as database services. The Dockerfile can be modified to install other required extensions.

Summary:
By using Docker, you can quickly and easily build a Symfony development environment. Through the above steps, we created a Docker container containing the Symfony project and related services. You can further customize this environment according to your project needs and easily share it with team members.

I hope this article can help you quickly set up a Symfony development environment and speed up your development process. I wish you success in developing web applications using the Symfony framework!

The above is the detailed content of Docker installation of Symfony: Quick guide to setting up a development environment. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn