<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nicolas Mugnier on Medium]]></title>
        <description><![CDATA[Stories by Nicolas Mugnier on Medium]]></description>
        <link>https://medium.com/@nicolas.mugnier?source=rss-d1ce77ed15cc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*40mAbIioISNkpOWyyRs7Kw.jpeg</url>
            <title>Stories by Nicolas Mugnier on Medium</title>
            <link>https://medium.com/@nicolas.mugnier?source=rss-d1ce77ed15cc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 05:04:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nicolas.mugnier/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Docker guide, build a PHP Application]]></title>
            <link>https://medium.com/@nicolas.mugnier/docker-guide-build-a-php-application-96005a41bacb?source=rss-d1ce77ed15cc------2</link>
            <guid isPermaLink="false">https://medium.com/p/96005a41bacb</guid>
            <category><![CDATA[docker-compose]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Nicolas Mugnier]]></dc:creator>
            <pubDate>Mon, 27 Jan 2025 00:00:53 GMT</pubDate>
            <atom:updated>2026-03-04T21:10:08.638Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cFnr-z85DHHdmnB-.jpg" /></figure><p>Step by step guide showing how to build a simple application using Docker.</p><h3>Prerequisites</h3><p>In this guide we will assume the following statements :</p><ul><li><strong>docker cli</strong> is installed (required)</li><li>you have a basic understanding of <strong>php</strong> (should have)</li><li>you’re familiar with dependency manager tool like <strong>composer</strong> (should have)</li></ul><h3>Step 1 — Run php-fpm &amp; nginx containers separately</h3><p>A PHP application is running over a web server. So first we have to select which <strong>php</strong> version to use.</p><p>In this guide we will use <strong>php 8.4</strong> with <strong>nginx</strong> as web server.</p><p>Docker images can be pulled from Docker public registry and we can search for images using <strong>docker cli</strong></p><pre>docker search php</pre><p>Many images are listed, but we will use the official docker image :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1013/0*blqX8JMbdUGc_20q.png" /></figure><p>As we can see the name of the image is <strong>php</strong> without prefix. So we can access to the image using the url <a href="https://hub.docker.com/_/php">https://hub.docker.com/_/<em>php</em></a>.</p><p>All available releases are listed in the <strong>tags</strong> tab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fxFgrcqNtnPiHtsa.png" /></figure><p>For this guide we will use <strong>8.4.3-fpm-alpine3.20</strong>, so let’s go and start the container!</p><pre>docker run php:8.4.3-fpm-alpine3.20</pre><p>Docker will first pull the image from the registry and next start <strong>php-fpm</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/934/0*3PQU5QqyITv18UvN.png" /></figure><p>The container is now ready to handle connections, it can be stopped by using Ctrl C.</p><p>We can reproduce the same approach in order to find <strong>nginx</strong> image, we will use the latest available release :</p><pre>docker run nginx</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FYwPhKoRl3D5wiag.png" /></figure><p>The container start the server with multiple workers and it is ready to use. However at this point if you open your browser to <a href="http://localhost/">http://localhost</a> nothing will be displayed. In order to access the <strong>nginx</strong> welcome page, we have to forward the port 80, so first we have to stop the container using Ctrl C and next run the following command</p><pre>docker run -p 80:80 nginx</pre><p>we can now see the <strong>nginx</strong> welcome page when browsing to localhost</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*brmWEqMHPSPeIZht.png" /></figure><h3>Step 2 — Stronger togethers, display phpinfo()</h3><p>We are now able to start <strong>php</strong> and <strong>nginx</strong> containers, the next step will be to use them together and display a very simple index.php file. Let’s go!</p><p>Create index.php file</p><pre>mkdir src<br>touch src/index.php<br>echo -e &quot;&lt;?php\n\nphpinfo();&quot; &gt; src/index.php</pre><p>Add nginx configuration</p><pre>mkdir -p docker/nginx<br>touch docker/nginx/nginx.conf</pre><p>nginx.conf file content</p><pre>user www-data;<br>worker_processes 5;<br>events { worker_connections 1024; }<br>http {    <br>  default_type application/octet-stream;    <br>  charset utf-8;    <br>  server_tokens off;    <br>  tcp_nopush on;<br>  tcp_nodelay off;<br>  server {        <br>    root /usr/share/nginx/html;<br>    location / {            <br>      try_files $uri /index.php$is_args$args;        <br>    }        <br>    location ~ ^/(index)\.php(/|$) {            <br>      fastcgi_pass php:9000;            <br>      fastcgi_split_path_info ^(.+\.php)(/.*)$;            <br>      include fastcgi_params;            <br>      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<br>      fastcgi_param DOCUMENT_ROOT $document_root;        <br>    }<br>  }<br>}</pre><p>Create a new docker network in order to allow communication between containers</p><pre>docker network create app-network</pre><p>Start <strong>php-fpm</strong></p><pre>docker run --rm --network app-network --name php \  <br>  -v $PWD/src:/usr/share/nginx/html \  <br>  php:8.4.3-fpm-alpine3.20</pre><p>Start <strong>nginx</strong></p><pre>docker run --rm --network app-network --name nginx -p 80:80 \  <br>  -v $PWD/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \  <br>  -v $PWD/src:/usr/share/nginx/html \  <br>  nginx</pre><p>Result on <a href="http://localhost/">http://localhost</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qsnK2Mlkvo05zy7J.png" /></figure><h3>Step 3 — Display data using PostgreSQL &amp; Doctrine</h3><p>In this step we will add a <strong>PostgreSQL</strong> database and use the Doctrine orm in order to manipulate entities.</p><p>Doctrine can be installed by using composer, so we will init a new project and require dependencies :</p><pre>docker run --rm --interactive --tty --volume $PWD:/app composer init</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/909/0*bdw08WxgIP8vSDI1.png" /></figure><ul><li>Package name: docker/app</li><li>Description: leave empty</li><li>Author: leave empty</li><li>Minimum Stability: leave empty</li><li>Package Type: leave empty</li><li>License: leave empty</li><li>Would you like to define dependencies: yes</li><li>Search for a package: doctrine/orm</li><li>Enter the version constraint to require: leave empty</li><li>Search for a package: doctrine/dbal</li><li>Enter the version constraint to require: leave empty</li><li>Search for a package: symfony/cache</li><li>Enter the version constraint to require: leave empty</li><li>Search for a package: leave empty</li><li>Would you like to define your dev dependencies: n</li><li>Add PSR-4 autoload mapping? Maps namespace “Docker\App” to the entered relative path. [src/, n to skip]: tap enter</li><li>Do you confirm generation: yes</li><li>Would you like to install dependencies now: yes</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qrh21EOREb3J50aA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*E-SU79OKbVJBHvwY.png" /></figure><p>Composer installation is done and we can now see the vendor directory, composer.json and composer.lock files</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/0*Shtg2WNja-PRPQHy.png" /></figure><p>Now we can configure Doctrine.</p><p>Create bootstrap.php file</p><pre>touch bootstrap.php</pre><pre>&lt;?php<br><br>use Doctrine\DBAL\DriverManager;<br>use Doctrine\ORM\EntityManager;<br>use Doctrine\ORM\ORMSetup;<br><br>require_once &quot;vendor/autoload.php&quot;;<br>$config = ORMSetup::createAttributeMetadataConfiguration(<br>  paths: [__DIR__ . &#39;/src&#39;],<br>  isDevMode: true,<br>);<br>$connection = DriverManager::getConnection(<br>  [    <br>    &#39;driver&#39; =&gt; &#39;pdo_pgsql&#39;,<br>    &#39;user&#39; =&gt; &#39;postgres&#39;,<br>    &#39;password&#39; =&gt; &#39;secret&#39;,<br>    &#39;dbname&#39; =&gt; &#39;postgres&#39;,<br>    &#39;host&#39; =&gt; &#39;postgres&#39;,<br>    &#39;port&#39; =&gt; 5432<br>  ],<br>  $config<br>);<br>$entityManager = new EntityManager($connection, $config);</pre><p>Create the bin/doctrine file</p><pre>mkdir bin<br>touch bin/doctrine</pre><pre>#!/usr/bin/env php<br>&lt;?php<br><br>use Doctrine\ORM\Tools\Console\ConsoleRunner;<br>use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;<br><br>require __DIR__ . &#39;/../bootstrap.php&#39;;<br>ConsoleRunner::run(<br>  new SingleManagerProvider($entityManager)<br>);</pre><p>Try the command</p><pre>docker run --rm --volume $PWD:/user/src/app php:8.4-cli php /user/src/app/bin/doctrine</pre><p>Result</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gZ_g9DKZxQgz9dmW.png" /></figure><p>Create a new Entity</p><pre>mkdir -p src/BusinessRules/Entities<br>touch src/BusinessRules/Entities/Album.php</pre><pre>&lt;?php<br><br>namespace Docker\App\BusinessRules\Entities;<br><br>use Doctrine\ORM\Mapping as ORM;<br><br>#[ORM\Entity]<br>#[ORM\Table(name: &#39;albums&#39;)]<br>class Album<br>{<br>    #[ORM\Id]<br>    #[ORM\Column(type: &#39;integer&#39;)]<br>    #[ORM\GeneratedValue]<br>    public readonly ?int $id;<br><br>    public function __construct(<br>        #[ORM\Column(type: &#39;string&#39;)]<br>        public string $title,<br>        #[ORM\Column(type: &#39;string&#39;)]<br>        public string $artist,<br>    ) {}<br><br>    public function getId(): ?int<br>    {<br>        return $this-&gt;id;<br>    }<br>}</pre><p>We can now generate the schema with the following command</p><pre>docker run --rm --volume $PWD:/user/src/app php:8.4-cli \  <br>  php /user/src/app/bin/doctrine orm:schema-tool:update --force --dump-sql</pre><p>The following error should be displayed</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/0*1ZNIbjlq66HZd1re.png" /></figure><p>This errors appears because pgsql driver is not installed in php:8.4-cli. In order to fix this error we will use a Dockerfile and add the extension, let&#39;s go!</p><pre>mkdir -p docker/php-cli<br>touch docker/php-cli/Dockerfile</pre><pre>FROM php:8.4-cli<br><br>RUN apt-get update &amp;&amp; apt-get install -y \<br>    libpq-dev \<br>    &amp;&amp; docker-php-ext-install pdo_pgsql pgsql \<br>    &amp;&amp; apt-get clean &amp;&amp; rm -rf /var/lib/apt/lists/*</pre><p>Build a new image called my-php-cli</p><pre>cd docker/php-cli<br>docker build -t my-php:8.4-cli .</pre><p>We can now view the new image</p><pre>docker images | grep php</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AvH8zLMxN0JEpBJ8.png" /></figure><p>So now we can try to create entities using our newest image my-php:8.4-cli</p><pre>docker run --rm --volume $PWD:/user/src/app my-php:8.4-cli \  <br>  php /user/src/app/bin/doctrine orm:schema-tool:update --force --dump-sql</pre><p>Ooops, we’ve got another error</p><pre>In ExceptionConverter.php line 77:<br><br>  An exception occurred in the driver: SQLSTATE[08006] [7] could not translat<br>  e host name &quot;postgres&quot; to address: Name or service not known<br><br>orm:schema-tool:update [--em EM] [--complete] [--dump-sql] [-f|--force]</pre><p>This error is about unkown host <strong>postgres</strong>, we have now to start the <strong>postgres</strong> container</p><p>Open a new terminal and run the following command</p><pre>docker run --rm --name postgres --network app-network \  <br>  -e POSTGRES_PASSWORD=secret postgres</pre><p>Next in another terminal</p><pre>docker run --rm --network app-network --volume $PWD:/user/src/app my-php:8.4-cli \  <br>  php /user/src/app/bin/doctrine orm:schema-tool:update --force --dump-sql</pre><p>Success!</p><pre>Updating database schema...<br><br>CREATE TABLE albums (  <br>  id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL,  <br>  title VARCHAR(255) NOT NULL,  <br>  artist VARCHAR(255) NOT NULL,  <br>  PRIMARY KEY(id)<br>);<br><br>1 query was executed<br>[OK] Database schema updated successfully!</pre><p>So here we go, we can now add some records in our new table ‘albums’, for that, we will use a simple <strong>php</strong> script.</p><pre>touch createAlbums.php</pre><pre>&lt;?php<br><br>require_once &quot;bootstrap.php&quot;;<br><br>use Docker\App\BusinessRules\Entities\Album;<br><br>$fixtures = [<br>  [<br>    &#39;title&#39; =&gt; &#39;Burn My Eyes&#39;,<br>    &#39;artist&#39; =&gt; &#39;Machine Head&#39;<br>  ],<br>  [<br>    &#39;title&#39; =&gt; &#39;Aggression Continuum&#39;,<br>    &#39;artist&#39; =&gt; &#39;Fear Factory&#39;<br>  ],<br>  [<br>    &#39;title&#39; =&gt; &#39;Black Album&#39;,<br>    &#39;artist&#39; =&gt; &#39;Metallica&#39;<br>  ]<br>];<br><br>foreach ($fixtures as $fixture) {<br>    $album = new Album(<br>        title: $fixture[&#39;title&#39;],<br>        artist: $fixture[&#39;artist&#39;]<br>    );<br><br>    $entityManager-&gt;persist($album);<br>    $entityManager-&gt;flush();<br><br>    echo &quot;Created album with ID &quot; . $album-&gt;getId() . &quot;\n&quot;;<br>}</pre><pre>docker run --rm --network app-network --volume $PWD:/user/src/app my-php:8.4-cli \<br>  php /user/src/app/createAlbums.php</pre><p>Result</p><pre>Created album ID 1<br>Created album ID 2<br>Created album ID 3</pre><p>Last but not least, let’s display the list!</p><p>Create a new index.php file in the project root directory</p><pre>touch index.php</pre><pre>&lt;?php<br><br>require_once __DIR__.&#39;/bootstrap.php&#39;;<br><br>$albumRepository = $entityManager-&gt;getRepository(<br>    Docker\App\BusinessRules\Entities\Album::class<br>);<br>$albums = $albumRepository-&gt;findAll();<br><br>$str = &#39;&lt;h1&gt;Albums&lt;/h1&gt;&#39;;<br>$str .= &#39;&lt;ul&gt;&#39;;<br>foreach ($albums as $album) {<br>    $str .= &#39;&lt;li&gt;&#39;.$album-&gt;title.&#39; (&#39;.$album-&gt;artist.&#39;)&lt;/li&gt;&#39;;<br>}<br>$str .= &#39;&lt;/ul&gt;&#39;;<br><br>echo $str;</pre><p>Add pgsql extension in php-fpm container</p><pre>mkdir -p docker/php-fpm<br>touch docker/php-fpm/Dockerfile</pre><pre>FROM php:8.4.3-fpm-alpine3.20<br><br>RUN apk add --no-cache \    <br>postgresql-dev \    <br>&amp;&amp; docker-php-ext-install pdo_pgsql pgsql</pre><p>Build the image</p><pre>cd docker/php-fpm<br>docker build -t my-php:8.4.3-fpm-alpine3.20 .</pre><p>Start containers (in separate terminals)</p><p>Start postgres</p><pre>docker run --rm --name postgres --network app-network \<br>  -e POSTGRES_PASSWORD=secret postgres</pre><p>Start php-fpm</p><pre>docker run --rm --network app-network --name php \<br>  -v $PWD:/usr/share/nginx/html \<br>  my-php:8.4.3-fpm-alpine3.20</pre><p>Start nginx</p><pre>docker run --rm --network app-network --name nginx -p 80:80 \<br>  -v $PWD/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \<br>  -v $PWD:/usr/share/nginx/html \<br>  nginx</pre><p>Result</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/0*KcI9u_GijA0JdGee.png" /></figure><p>Congratulations! This simple page is displayed using 3 containers, we can see them with this command</p><pre>docker ps</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*neVzhoZ9Hzt6kgJc.png" /></figure><p>We can now stop all containers by using Ctrl C in each terminal.</p><h3>Step 4 — Docker Compose</h3><p>In this last step we will introduce docker-compose in order to make our life easier. Instead of running each container one by one we will create a special file in order to manage them all in one place. Let’s go!</p><pre>touch docker-compose.yml</pre><pre>networks:<br>  app-network:<br><br>services:<br><br>  postgres:<br>    image: postgres:latest<br>    container_name: postgres<br>    environment:<br>      POSTGRES_PASSWORD: secret<br>    networks:<br>      - app-network<br><br>  php-fpm:<br>    build: ./docker/php-fpm<br>    image: my-php:8.4.3-fpm-alpine3.20<br>    container_name: php<br>    working_dir: /usr/share/nginx/html<br>    volumes:<br>      - .:/usr/share/nginx/html<br>    networks:<br>      - app-network<br><br>  nginx:<br>    image: nginx:latest<br>    container_name: nginx<br>    working_dir: /usr/share/nginx/html<br>    volumes:<br>      - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro<br>      - .:/usr/share/nginx/html<br>    ports:<br>      - 80:80<br>    networks:<br>      - app-network<br><br>  php-cli:<br>    build: ./docker/php-cli<br>    image: my-php:8.4-cli<br>    container_name: php-cli<br>    volumes:<br>      - .:/user/src/app<br>    networks:<br>      - app-network<br>    working_dir: /user/src/app<br><br>  composer:<br>    image: composer:latest<br>    container_name: composer<br>    volumes:<br>      - .:/app</pre><p>Init database</p><pre>docker-compose run --rm php-cli bin/doctrine orm:schema-tool:update --force --dump-sql</pre><p>Add records</p><pre>docker-compose run --rm php-cli php createAlbums.php</pre><p>Start containers</p><pre>docker-compose up -d</pre><p>Add a new dev dependency using composer</p><pre>docker-compose run --rm composer require --dev phpunit/phpunit</pre><p>Stop containers and remove all volumes</p><pre>docker-compose down -v</pre><p>That’s it! As we can see all settings have been moved into docker-compose.yml, commands are becoming more simple and it allows to start/stop containers in a breeze!</p><p><strong>Resources</strong></p><ul><li><a href="https://hub.docker.com/">https://hub.docker.com</a></li><li><a href="https://docker.com/">https://docker.com</a></li><li><a href="https://getcomposer.org/">https://getcomposer.org/</a></li><li><a href="https://www.doctrine-project.org/projects/doctrine-orm/en/current/tutorials/getting-started.html">https://www.doctrine-project.org/projects/doctrine-orm/en/current/tutorials/getting-started.html</a></li><li><a href="https://docs.docker.com/compose/">https://docs.docker.com/compose/</a></li></ul><p>Thank you for taking the time to read this article and congratulations for running this sample application! Feel free to share your thoughts, experiences, or questions in the comments.</p><p><em>Originally published at </em><a href="https://blog.anyvoid.dev/docker/2025/01/27/docker-guide.html"><em>https://blog.anyvoid.dev</em></a><em> on January 27, 2025.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96005a41bacb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>