Home  >  Article  >  Operation and Maintenance  >  Docker practice: install Symfony and build a complete development environment

Docker practice: install Symfony and build a complete development environment

王林
王林Original
2023-10-20 10:09:34860browse

Docker practice: install Symfony and build a complete development environment

Docker Practice: Install Symfony and build a complete development environment

Introduction:
Docker is a lightweight and portable containerization platform that allows development People quickly build, deploy, and run applications in containers. In this article, we will introduce how to use Docker to install Symfony and build a complete development environment. We'll provide specific code examples to help you get started quickly.

1. Install Docker and Docker Compose
Before we begin, we first need to install Docker and Docker Compose. You can go to the Docker official website https://www.docker.com/ to download and install the version suitable for your operating system.

2. Create a Symfony project
Next, we will use Docker to create a Symfony project. First, open a terminal or command prompt and go into the directory where you want to create the project. Then run the following command:

$ docker run --rm -v $(pwd):/app composer create-project symfony/website-skeleton myproject

The above command will create a Symfony project named "myproject" in the current directory. You can also replace "myproject" with your own project name.

3. Configure the Docker Compose file
Create a file named "docker-compose.yml" in the root directory of the project, and configure it according to the following content:

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

Above The configuration file defines two services: web and db. The web service is used to run Symfony applications, and the db service is used to run the MySQL database. We also define a shared network app_net and connect the Symfony application and database to this network.

4. Create a Dockerfile
Create a file named "Dockerfile" in the root directory of the project and configure it as follows:

FROM php:7.4-apache

WORKDIR /app

RUN docker-php-ext-install pdo pdo_mysql

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

The above Dockerfile defines a file based on php:7.4-A new mirror for the apache mirror. We installed the necessary PHP extensions and Composer in it.

5. Build and run the container
We have completed all the necessary configuration and can now start building and running the container. In a terminal or command prompt, go to the root directory of your project and run the following command:

$ docker-compose up -d

The above command will create and run two containers based on the configuration file: one for running the Symfony application, and another for running the Symfony application. A container for running a MySQL database.

6. Access the Symfony application
Everything is ready, you can now access the Symfony application through the browser. Open a browser and enter "http://localhost:8000". You will see the Symfony welcome page, proving that your application has run successfully.

7. Additional configuration and use of other services
In addition to the above basic configuration, you can also perform additional configuration and use other services according to your own needs. For example, you can configure an SMTP server for sending emails, use Redis or Elasticsearch, etc.

Summary:
This article introduces how to use Docker to install Symfony and build a complete development environment. We provide specific code examples to help you get started quickly. Using Docker can provide a lightweight, portable development environment that allows developers to build and deploy applications more efficiently. I hope this article is helpful to you, and I wish you success in Symfony development!

The above is the detailed content of Docker practice: install Symfony and build a complete 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