A simple guide to setup Symfony PHP framework in a local environment using Docker.
This tutorial assumes that you have already install docker and docker compose.
*For this tutorial I’ve used Ubuntu Linux.
Checking if you have installed Docker in your machine:
docker -v && docker-compose -v
In case you haven’t installed Docker and Docker Compose follow this instructions for your operating system (https://docs.docker.com/engine/install/)
1. Let’s install a Symfony project using docker
Run this command in where you want to install Symfony, you have two options here, website or microservice/console/API…
Website skeleton:
sudo docker run --rm -v $(pwd):/app composer create-project symfony/website-skeleton project_name
Microservice, console application or API
sudo docker run --rm -v $(pwd):/app composer create-project symfony/skeleton project_name
At the end of the installation you’ll get something like this:
2. Creating our Dockerfile
In you root folder (where you executed the create-project command) create a file called Dockerfile and paste the follow:
FROM php:8.0.3-fpm-alpine# Set working directory
WORKDIR /var/www/html# Install dependencies
RUN apk update \
&& apk upgrade \
&& apk add --no-cache \
zlib-dev \
libzip-dev \
bash \
unzip \
curl# Install extensions
RUN docker-php-ext-install mysqli pdo pdo_mysql zip exifRUN wget https://get.symfony.com/cli/installer -O - | bash \
&& mv /root/.symfony/bin/symfony /usr/local/bin/symfony# Clean Cache
RUN rm -rf /var/cache/apk/*CMD ["symfony", "serve", "--no-tls"]EXPOSE 80
3. Creating our docker-compose.yml
In the same folder create a file called docker-compose.yml and paste the follow:
version: "3.3"
services:
# we named our first service 'web'
web:
# build a custom image
build:
context: .
dockerfile: Dockerfile#restart: on-failure# map host port 8001 to container port 8000
ports:
- 8001:8000# volumes are like shared folders
# container will see your local code changes
volumes:
- ./project_name:/var/www/html# first load the 'db' service
depends_on:
- db# make 'db' a known service/host inside of 'web'
# use this to make a mysql connection to host 'db'
links:
- dbnetworks:
- defaultdb:
# use a default image
image: mysql:8.0#restart: on-failurevolumes:
- ./.mysql:/var/lib/mysql:rw
- ./mysql-dev-init.sql:/docker-entrypoint-initdb.d/init.sql:ro# again, port mapping
# e.g. to use Sequel Pro on our mac
ports:
- 3306:3306# the mysql image uses these to create database and users
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: dbnetworks:
- defaultnetworks:
default:
driver: bridge
4. Let’s build it!
Run the following command:
sudo docker-compose -f docker-compose.yml up --build
If everything went well you’ll see something like this:
Let’s check the dockers running:
docker ps
You’ll see something like this:
5. Surf this beautiful dockerized Symfony
Open your browser using the next url: (http://localhost:8001/) you’ll see the welcome page:
One more thing, in case you need to access the docker use this commands:
sudo docker exec -it [container_name] bash
You’ll see something like this:
That’s all folks! Happy coding!