How to Set Up Symfony with Docker for Local Development - 149 reads
Prerequisites:
- Basic knowledge of Docker and Symfony.
- Docker installed on your machine.
- Docker Compose installed.
1.Create a New Symfony Project
First, we need to create a new Symfony project. If you already have a Symfony project, you can skip this step.
2.Set Up a Dockerfile
Create a Dockerfile in the root of your Symfony project. This file will define the environment in which your Symfony application will run.
2.Set Up "docker-compose.yml"
Docker Compose is used to define and run multi-container Docker applications. We'll create a docker-compose.yml file to configure our Symfony application, database, and other services.
3.Configure Nginx
Create an nginx directory in your project root and add an nginx.conf file to configure Nginx for serving the Symfony application.
4.Build and Run the Containers
Now that all configurations are in place, it's time to build and start the Docker containers. To build the containers, run the following command (this has to be done only once):
To stop the containers, you can run:
And to start them, you can run:
6.Accessing the Symfony Application
Once the containers are up and running, you can access your Symfony application in the browser at http://localhost:8080
. If everything is set up correctly, you should see the Symfony welcome page.
7.Managing Your Database
The MySQL database is running in a Docker container and is accessible in the browser at http://localhost:8081
. You can also connect third-party software such as your IDE to it by using the credentials configured in docker-compose.yml (MYSQL_USER, MYSQL_PASSWORD, at port 33066).
Conclusion
With Docker, you can easily create a consistent development environment for your Symfony projects. This setup ensures that all dependencies are isolated and version-controlled, making it easier to collaborate with other developers and deploy your application. As you become more familiar with Docker, you can extend this setup to include additional services like Redis, Elasticsearch, or RabbitMQ.