<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vytenis</title>
    <description>The latest articles on DEV Community by Vytenis (@gvytenis).</description>
    <link>https://dev.to/gvytenis</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F870079%2F7282a97b-b357-4cde-bbdc-b6da750b458d.jpeg</url>
      <title>DEV Community: Vytenis</title>
      <link>https://dev.to/gvytenis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gvytenis"/>
    <language>en</language>
    <item>
      <title>Streamlining Dockerfile configuration in PHP</title>
      <dc:creator>Vytenis</dc:creator>
      <pubDate>Wed, 15 Jun 2022 04:16:54 +0000</pubDate>
      <link>https://dev.to/gvytenis/streamlining-dockerfile-configuration-in-php-1lj4</link>
      <guid>https://dev.to/gvytenis/streamlining-dockerfile-configuration-in-php-1lj4</guid>
      <description>&lt;p&gt;Nowadays, everything works better when using Docker for setting up local environments rather than installing PHP and other dependencies locally. When starting new PHP projects, Dockerfiles are copied from one codebase to another because each project requires similar basic dependencies to run. That makes it very difficult to maintain changes to containers.&lt;/p&gt;

&lt;p&gt;To avoid this at &lt;a href="https://telesoftas.com"&gt;TeleSoftas&lt;/a&gt; we came up with an idea to have a simple base image, which would simplify the development and maintainability of our projects. This image is published on DockerHub, which makes starting a new project a breeze by reducing the amount of code required.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;Let’s take a look at the basic PHP image &lt;strong&gt;Dockerfile&lt;/strong&gt; required to run a Symfony application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM php:8.1-fpm-alpine  

RUN addgroup -g 1000 -S app &amp;amp;&amp;amp; \  
    adduser -u 1000 -S app -G app  

RUN apk add \  
    autoconf \  
    build-base \  
    git  

RUN docker-php-ext-install pdo_mysql  

RUN pecl install apcu-5.1.21  
RUN docker-php-ext-enable apcu opcache  

COPY --from=composer:2.1 /usr/bin/composer /usr/local/bin/composer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not only that, but this simple configuration also works for plain PHP projects as well as Laravel. Copying the same Dockerfile and maintaining it through all the projects is getting a bit hard and messy, so why not make it simpler?&lt;/p&gt;

&lt;h2&gt;
  
  
  Publishing the image to Dockerhub
&lt;/h2&gt;

&lt;p&gt;To avoid creating a new Dockerfile for each project, you can publish it to DockerHub and pull it via &lt;strong&gt;docker-compose.yml&lt;/strong&gt; directly from it. And building the image is as simple as a few commands - log in to your &lt;strong&gt;DockerHub&lt;/strong&gt; account, build the image and push it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker login -u &amp;lt;YOUR_DOCKERHUB_USERNAME&amp;gt; -p &amp;lt;YOUR_DOCKERHUB_TOKEN&amp;gt; docker.io
docker build -t index.docker.io/&amp;lt;YOUR_DOCKERHUB_USERNAME&amp;gt;/php:8.1 .
docker push index.docker.io/&amp;lt;YOUR_DOCKERHUB_USERNAME&amp;gt;/php:8.1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using the newly built image
&lt;/h2&gt;

&lt;p&gt;First, create a &lt;strong&gt;docker-compose.yml&lt;/strong&gt; file with the following service configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3.7'  

services:  
    php:  
        image: &amp;lt;YOUR_DOCKERHUB_USERNAME&amp;gt;/php:8.1
        user: app  
        volumes:  
            - ./app:/srv/app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can simply ignore building the image locally, and it will be pulled from DockerHub. This way you avoid the Dockerfile duplication across projects and make it easy to maintain.&lt;/p&gt;

&lt;p&gt;This is useful when creating a new Symfony/Laravel project from scratch or, for example, executing some PHP code locally, because Docker is more popular nowadays than having all the libraries on your host machine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extending Dockerfile per project
&lt;/h2&gt;

&lt;p&gt;Sometimes you need some dependencies in one project and don’t need them in another. That is also easy to do using the newly published image. For example, on some projects, you might need XDebug or some other additional libraries when developing locally. To solve this, you can create a small &lt;strong&gt;Dockerfile&lt;/strong&gt;, which will pull the base image and add the necessary libraries on top of it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM &amp;lt;YOUR_DOCKERHUB_USERNAME&amp;gt;/php:8.1  

RUN pecl install xdebug-3.1.1
RUN docker-php-ext-enable xdebug
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, expand your &lt;strong&gt;docker-compose.yml&lt;/strong&gt; configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3.7'  

services:  
    php:  
        build: .  
        user: app  
        volumes:  
            - ./app:/srv/app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And it is as simple as this. There is no code duplication and basic dependencies are easy to maintain across all the projects. You can add as many or as few customizations to this local &lt;strong&gt;Dockerfile&lt;/strong&gt; as you need.&lt;/p&gt;

&lt;h2&gt;
  
  
  Automating publishing to Dockerhub
&lt;/h2&gt;

&lt;p&gt;Let's say you need to bump Composer to a newer version in your base image. To do this, you have to update our Dockerfile, log in to DockerHub, build the image and publish it. You can simplify this by automating your pipeline's building and publishing stages.&lt;/p&gt;

&lt;p&gt;For this tutorial, I've chosen GitLab as a source code repository and created a .gitlab-ci.yml file with the following configuration to automate the whole process&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;stages:  
    - build  

variables:  
    IMAGE_VERSION: '8.1'  

build:  
    stage: build  
    image: docker:latest  
    before_script:  
        - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD"  "$CI_REGISTRY"  
    script:  
        - docker build --pull -t $CI_REGISTRY_IMAGE:$IMAGE_VERSION .  
        - docker push $CI_REGISTRY_IMAGE:$IMAGE_VERSION
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only one step is left - define the variables in the project’s CI/CD settings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CI_REGISTRY=docker.io  
CI_REGISTRY_IMAGE=index.docker.io/&amp;lt;YOUR_DOCKERHUB_USERNAME&amp;gt;/php
CI_REGISTRY_USER=&amp;lt;YOUR_DOCKERHUB_USERNAME&amp;gt;  
CI_REGISTRY_PASSWORD=&amp;lt;YOUR_DOCKERHUB_TOKEN&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you push the updated image to GitLab, the image will be built and pushed to DockerHub once the pipeline finishes. This configuration can be easily adapted to Bitbucket or Github by following their pipeline setup guides.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;There is only one big step to take - decide what is required in your base image, build it, and publish it to DockerHub. And that’s what can be done:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Create two repositories for PHP 8.0 and 8.1 Docker images.&lt;/li&gt;
&lt;li&gt; Create the Dockerfile and add CI configuration to automate the whole process.&lt;/li&gt;
&lt;li&gt; Tag the image and push it to the DockerHub image registry once the pipeline finishes. And that’s it. That’s how you can simplify your development quite a bit.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This benefits not only existing projects but new ones as well. It makes it easy to maintain all of the applications - if a new library or package is required for the latest version of Symfony to work, just update the base image one time and already have it available in all projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Going further
&lt;/h2&gt;

&lt;p&gt;On top of the previously mentioned improvements, it is possible to simplify the whole development stack configuration even more. This example is for PHP, but the same process can be adapted to other containers, such as Nginx. For example, there might be a requirement to reduce logging in most projects. To accomplish this you can create a new Nginx base image with log exclusion and reuse it.&lt;/p&gt;

</description>
      <category>php</category>
      <category>docker</category>
      <category>symfony</category>
      <category>dockerfile</category>
    </item>
  </channel>
</rss>
