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.

Dockerfile

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 https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
RUN rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
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 \
    systemd
 
# Install Composer
RUN curl -sS https://getcomposer.org/installer | 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
EXPOSE 80

# Because Systemd is Systemd
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
 
# 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:
https://hub.docker.com/_/centos/

Build your image

docker build -t my/api .

Run your container

docker run --privileged --name myapi1 -p 8080:80 -i -it my/api
systemd 219 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ -LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN)
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.