Dockerizing a Symfony Application

I’ve been working on getting a Symfony API Dockerized for what seems like entirely too long. Some of the issues I came across were with PHP 7.0 not being fully supported with Symfony so I had to fall back to PHP 5.6; however I am proud to say I have completed this task without using docker-compose which seemed to be a layer that most people advised to use. I however wanted to use a more standard Dockerfile for setting up this environment.


Below is a Dockerfile for setting up your Symfony Application on a centos7 image.

# Set Base Image to Centos
FROM centos
# Maintainer
MAINTAINER Matthew Harris
# Set Env
ENV container docker
# Install Tools, Nginx, PHP
RUN rpm -Uvh
RUN rpm -Uvh
RUN yum install -y \
    git \
    php56w-fpm \
    php56w-opcache \
    php56w-common \
    php56w-xml \
    php56w-cli \
    php56w-pdo \
    php56w-mbstring \
    php56w-gd \
    php56w-pecl-imagick \
    php56w-soap \
    nginx \
# Install Composer
RUN curl -sS | php
RUN mv composer.phar /usr/local/bin/composer
# Configure Nginx
ADD config-files/nginx.conf /etc/nginx/
ADD config-files/site.conf /etc/nginx/conf.d/
# Configure PHP
ADD config-files/php.ini /etc/php.ini
ADD config-files/php-fpm.conf /etc/php-fpm.d/www.conf
# Ensure Directory Structure Exists and Mount Backend
RUN mkdir -p /var/www/api
COPY . /var/www/api
RUN mkdir -p /var/www/api/app/cache/prod

# Composer Install
RUN cd /var/www/api && composer install --no-interaction
RUN chown -R nobody:nobody /var/www/api

# Open Ports

# Because Systemd is Systemd
RUN (cd /lib/systemd/system/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/*; \
rm -f /lib/systemd/system/*udev*; \
rm -f /lib/systemd/system/*initctl*; \
rm -f /lib/systemd/system/*;\
rm -f /lib/systemd/system/*;
# Without this, init won't start the enabled services and exec'ing and starting
# them reports "Failed to get D-Bus connection: Operation not permitted".
VOLUME /run /tmp
VOLUME [ “/sys/fs/cgroup” ]
# Enable Services
RUN systemctl enable nginx.service
RUN systemctl enable php-fpm.service
CMD /usr/sbin/init

The biggest hurdles to get past were systemd being systemd but for more information on getting around this see:

Build your image

docker build -t my/api .

Run your container

docker run --privileged --name myapi1 -p 8080:80 -i -it my/api
Detected virtualization docker.
Detected architecture x86-64.
Welcome to CentOS Linux 7 (Core)!
Set hostname to <190e8f15457e>.
Initializing machine ID from random generator.
Failed to install release agent, ignoring: No such file or directory
[  OK  ] Reached target Local File Systems.
[  OK  ] Reached target Paths.
[  OK  ] Reached target Swap.
[  OK  ] Created slice Root Slice.
[  OK  ] Listening on Delayed Shutdown Socket.
[  OK  ] Created slice System Slice.
[  OK  ] Reached target Slices.
[  OK  ] Listening on Journal Socket.
         Starting Create Volatile Files and Directories...
         Starting Journal Service...
[  OK  ] Started Create Volatile Files and Directories.
[ INFO ] Update UTMP about System Boot/Shutdown is not active.
[DEPEND] Dependency failed for Update UTMP about System Runlevel Changes.
Job systemd-update-utmp-runlevel.service/start failed with result 'dependency'.
[  OK  ] Started Journal Service.
[  OK  ] Reached target System Initialization.
[  OK  ] Reached target Timers.
[  OK  ] Listening on D-Bus System Message Bus Socket.
[  OK  ] Reached target Sockets.
[  OK  ] Reached target Basic System.
         Starting The nginx HTTP and reverse proxy server...
         Starting The PHP FastCGI Process Manager...
         Starting Cleanup of Temporary Directories...
[  OK  ] Started Cleanup of Temporary Directories.
[  OK  ] Started The PHP FastCGI Process Manager.
[  OK  ] Started The nginx HTTP and reverse proxy server.
[  OK  ] Reached target Multi-User System.

And done.

Write a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.