Docker Installation Guide for Symfony Framework

WBOY
Release: 2023-10-20 13:16:42
Original
935 people have browsed it

Docker Installation Guide for Symfony Framework

Docker Installation Guide for Symfony Framework

Overview
Docker is an open source containerization platform that can be used to quickly build, package and deploy applications. Symfony framework is an excellent PHP framework for rapid development and maintenance of maintainable web applications. Combining the Docker and Symfony frameworks, you can efficiently build and manage application development environments.

This article will provide you with a detailed installation guide for the Symfony framework in Docker, including specific code examples.

Step 1: Install Docker and Docker Compose
First, you need to install Docker and Docker Compose on your local machine. The installation method of Docker varies depending on the operating system, so I won’t go into details here. Docker Compose is a tool for defining and running multi-container Docker applications. You can install Docker Compose through the following command:

$ sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
Copy after login

After completing the installation, you can verify whether the Docker Compose installation is successful through the docker-compose --version command.

Step 2: Create a Symfony project
Next, we will use the Symfony CLI to create a new Symfony project. Please make sure you have installed the Symfony CLI on your local machine. You can use the following command to install:

$ curl -sS https://get.symfony.com/cli/installer | bash
$ mv ~/.symfony/bin/symfony /usr/local/bin/symfony
Copy after login

After the installation is successful, you can use the symfony --version command to verify whether the Symfony CLI installation is successful.

Now, we can use the Symfony CLI to create a new Symfony project. Run the following command:

$ symfony new my_project_name --full
Copy after login

This command will create a Symfony project named my_project_name in the current directory and use the complete Symfony application template. This process may take some time, please be patient.

Step 3: Create a Docker configuration file
In order to run the Symfony project in a Docker container, we need to create a Docker configuration file. Create a file named docker-compose.yaml in the root directory of the Symfony project and add the following content:

version: '3'
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8000:8000
    volumes:
      - .:/app
    depends_on:
      - db
  db:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=symfony
      - MYSQL_USER=symfony
      - MYSQL_PASSWORD=symfony
    volumes:
      - db_data:/var/lib/mysql
volumes:
  db_data:
Copy after login

This configuration file defines two services, namely web and db. web The service is used to run the web service of the Symfony application and is mapped to port 8000 of the local machine. dbThe service uses the MySQL 5.7 image and is configured with some environment variables and volumes.

Step 4: Create Dockerfile
Next, we need to create a file named Dockerfile to build the Docker image of the Symfony project. Create a file named Dockerfile in the root directory of the Symfony project, and add the following content:

FROM php:7.4-apache

RUN docker-php-ext-install pdo pdo_mysql
RUN a2enmod rewrite

WORKDIR /app
COPY . /app

RUN chown -R www-data:www-data /app

CMD ["apache2-foreground"]
Copy after login

This Dockerfile uses the official PHP 7.4 and Apache image, and installs PHP pdo and pdo_mysql extensions. Apache's rewrite rules are also enabled, and all files of the Symfony project are copied to the /app directory of the container. Finally, start the Apache service.

Step 5: Build and run the Docker container
After completing the above steps, we can use Docker Compose to build and run the Docker container of the Symfony project. Run the following command in the root directory of the Symfony project:

$ docker-compose up -d
Copy after login

This command will automatically build and run the project's Docker container. The -d parameter is used to run the container in the background.

Now, you can visit http://localhost:8000 through your browser to view the running status of the Symfony project.

Conclusion
Through the guide in this article, you can easily combine the Symfony framework with Docker and quickly set up and manage the development environment for Symfony applications. Using Docker's containerization technology can improve the portability and scalability of applications and simplify environment configuration during the development process. I hope this guide will be helpful to your Symfony project development work.

The above is the detailed content of Docker Installation Guide for Symfony Framework. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!