Symfony with Docker — slim-fit tutorial

Intro

In my experience of over 10 years as an Engineer, I have never encountered a piece of technology with a bigger impact than Docker. It has increased my productivity, helped me debug and fix bugs faster and deploy almost instantly. I remember the days when I used to replicate the production environment by using a separate repository of scripts, by making sure that the versions of all libraries and servers aligned and that my development environment mirrors production perfectly. This used to consume many hours of development time and a lot of mental energy dedicated to little details. Ever since I started using Docker 3 years ago, I have saved invaluable mental energy and hundreds of hours for setting up environments, deploying and maintaining applications. I have written a very pragmatic tutorial, focused on how to reap the benefits of Docker as fast as possible. In this tutorial, I will focus mainly on deploying a slim Symfony app for development.

Step 1 — Installing Docker

Docker runs on any platform, however, I recommend that you run it inside an Ubuntu virtual machine for macOS or Windows. These platforms don’t support Docker virtualization very well and you might run into issues hard to debug. I’m running Docker inside an Ubuntu Server machine inside VirtualBox on my Windows laptop and it operates super smoothly. I allocated 2 GB of RAM to the machine and 4 CPUs. Below you can find the instructions for installing VirtualBox, Ubuntu, and Docker

Step 2 — The Dockerfile

To create a Docker container, Docker reads the instructions from the Dockerfile. This file is the most popular way of building docker containers and is very easy to understand and use. Let’s build a container from the alpine image, a stripped-down Linux distribution. Create a Dockerfile with the following contents

FROM alpine
RUN echo “Hello World!”
sudo docker build -t simple-alpine .
sudo docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
simple-alpine latest 5906f9bc045e 2 seconds ago 5.59MB
sudo docker run -it simple-alpine /bin/sh

Step 3 — The docker-compose

Besides the Dockerfile, there is another way you can run containers, and that is by using a docker-compose YAML file. In this file, you can define multiple containers with different images and you can configure how these containers interact with the host machine and between themselves. For each image, Docker will run a container that will read the Dockerfile and execute the instructions in that file. Install docker-compose by running

sudo apt-get install docker-compose
version: “3”services:
webapp:
container_name: webapp
image: “nginx:latest”
ports:
- “50080:80”
sudo docker-compose up -d
version: “3”services:
webapp:
container_name: webapp
image: “nginx:latest”
ports:
- “50080:80”
php-fpm:
container_name: php_fpm
image: akeneo/fpm
user: docker
ports:
- “59000:9000”
sudo docker-compose down
sudo docker-compose up -d
sudo docker exec -it webapp bash
sudo docker exec -it php_fpm bash
server {
listen 80;
server_name localhost;
location / {
# try to serve file directly, fallback to index.php and let Symfony handle it
try_files $uri /index.php$is_args$args;
}
# pass the PHP scripts to FastCGI docker image
location ~ ^/index\.php(/|$) {
root html;
fastcgi_pass php-fpm:9001;
fastcgi_index index.php;
# this is the absolute path on the docker Image
fastcgi_param SCRIPT_FILENAME /home/docker/public$fastcgi_script_name;
include fastcgi_params;
}
}
version: “3”

services:
webapp:
volumes:
- ./config/nginx:/etc/nginx/conf.d
container_name: webapp
image: “nginx:latest” # official image
ports:
- “50080:80”
php-fpm:
volumes:
- ./:/home/docker # mounting our application on a specific path
container_name: php_fpm
image: akeneo/fpm
user: docker
ports:
- “59000:9000”
- ./var/log:/var/log/nginx
File not found.
sudo docker exec -it php_fpm composer create-project symfony/skeleton symfony-app
  • The symfony app in our php-fpm container
version: “3”

services:
webapp:
volumes:
- ./config/nginx:/etc/nginx/conf.d
- ./symfony-app/public:/usr/share/nginx/html
container_name: webapp
image: “nginx:latest” # official image
ports:
- “50080:80”
php-fpm:
volumes:
- ./symfony-app:/home/docker # mounting our application on a specific path
container_name: php_fpm
image: akeneo/fpm
user: docker
ports:
- “59000:9000”

Final steps

Now that we have the docker-compose ready it’s just a matter of tinkering around with configurations, committing and deploying on our production environment. Deployment can be done manually, but it’s highly recommended to use Kubernetes and integrate everything in a CI/CD pipeline.

Software Engineer and Technology Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store