Sand Fox (Posts about docker)
https://sandfox.me/tags/docker.xml
2024-03-25T17:50:26Z
Anton Smirnov
Nikola
Installing Composer in Docker the 2020-s Way
https://sandfox.me/php/install-composer-in-docker-2.html
2023-01-11T10:14:00+02:00
2023-01-11T10:14:00+02:00
Anton Smirnov
<p>While <a class="reference external" href="https://sandfox.me/php/install-composer-in-docker.html">the original way</a> is still valid, in Docker it's now easier to copy the Composer executable from its Docker image.</p>
<div class="code"><pre class="code docker"><a id="rest_code_db28e163bd9f424d955ae8b85d8794a8-1" name="rest_code_db28e163bd9f424d955ae8b85d8794a8-1" href="https://sandfox.me/php/install-composer-in-docker-2.html#rest_code_db28e163bd9f424d955ae8b85d8794a8-1"></a><span class="k">COPY</span><span class="w"> </span>--from<span class="o">=</span>composer:latest<span class="w"> </span>/usr/bin/composer<span class="w"> </span>/usr/local/bin/composer
</pre></div>
<p>or for PHP < 7.2.5</p>
<div class="code"><pre class="code docker"><a id="rest_code_f7347ac4431e47f4ae9d679e8c748f3a-1" name="rest_code_f7347ac4431e47f4ae9d679e8c748f3a-1" href="https://sandfox.me/php/install-composer-in-docker-2.html#rest_code_f7347ac4431e47f4ae9d679e8c748f3a-1"></a><span class="k">COPY</span><span class="w"> </span>--from<span class="o">=</span>composer:2.2<span class="w"> </span>/usr/bin/composer<span class="w"> </span>/usr/local/bin/composer
</pre></div>
<p>Please note that without the installer, dependencies won't be checked, so you also need to install git and/or zip,
and preferably optional dependencies like mbstring too.</p>
<p>Found accidentally <a class="reference external" href="https://stackoverflow.com/a/58694421">on the Stack Overflow</a>.
There is also <a class="reference external" href="https://stackoverflow.com/a/68600278">another interesting and underliked method</a> for the case
when you don't want to keep the composer executable and cache files in your image.</p>
Install Composer in Docker
https://sandfox.me/php/install-composer-in-docker.html
2023-01-11T10:14:00+02:00
2020-04-02T11:26:00+03:00
Anton Smirnov
<div><aside class="admonition note">
<p class="admonition-title">Note</p>
<p>You can still use it to install Composer programmatically but for Docker there is <a class="reference external" href="https://sandfox.me/php/install-composer-in-docker-2.html">an easier way now</a></p>
</aside>
<p>Here is a small snippet adapted from the <a class="reference external" href="https://getcomposer.org/doc/faqs/how-to-install-composer-programmatically.md">official instruction</a>:</p>
<div class="code"><pre class="code docker"><a id="rest_code_fd18ad11a27f41e781f79207354497c7-1" name="rest_code_fd18ad11a27f41e781f79207354497c7-1" href="https://sandfox.me/php/install-composer-in-docker.html#rest_code_fd18ad11a27f41e781f79207354497c7-1"></a><span class="k">RUN</span><span class="w"> </span>php<span class="w"> </span>-r<span class="w"> </span><span class="s2">"copy('https://composer.github.io/installer.sig', '/tmp/composer.sig');"</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="se">\</span>
<a id="rest_code_fd18ad11a27f41e781f79207354497c7-2" name="rest_code_fd18ad11a27f41e781f79207354497c7-2" href="https://sandfox.me/php/install-composer-in-docker.html#rest_code_fd18ad11a27f41e781f79207354497c7-2"></a><span class="w"> </span>php<span class="w"> </span>-r<span class="w"> </span><span class="s2">"copy('https://getcomposer.org/installer', '/tmp/composer-setup.php');"</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="se">\</span>
<a id="rest_code_fd18ad11a27f41e781f79207354497c7-3" name="rest_code_fd18ad11a27f41e781f79207354497c7-3" href="https://sandfox.me/php/install-composer-in-docker.html#rest_code_fd18ad11a27f41e781f79207354497c7-3"></a><span class="w"> </span>php<span class="w"> </span>-r<span class="w"> </span><span class="s1">'$expected = file_get_contents("/tmp/composer.sig"); $actual = hash_file("sha384", "/tmp/composer-setup.php"); exit(intval(!hash_equals($expected, $actual)));'</span><span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="se">\</span>
<a id="rest_code_fd18ad11a27f41e781f79207354497c7-4" name="rest_code_fd18ad11a27f41e781f79207354497c7-4" href="https://sandfox.me/php/install-composer-in-docker.html#rest_code_fd18ad11a27f41e781f79207354497c7-4"></a><span class="w"> </span>php<span class="w"> </span>/tmp/composer-setup.php<span class="w"> </span>--install-dir<span class="o">=</span>/usr/local/bin<span class="w"> </span>--filename<span class="o">=</span>composer<span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="se">\</span>
<a id="rest_code_fd18ad11a27f41e781f79207354497c7-5" name="rest_code_fd18ad11a27f41e781f79207354497c7-5" href="https://sandfox.me/php/install-composer-in-docker.html#rest_code_fd18ad11a27f41e781f79207354497c7-5"></a><span class="w"> </span>chmod<span class="w"> </span>+x<span class="w"> </span>/usr/local/bin/composer<span class="w"> </span><span class="o">&&</span><span class="w"> </span><span class="se">\</span>
<a id="rest_code_fd18ad11a27f41e781f79207354497c7-6" name="rest_code_fd18ad11a27f41e781f79207354497c7-6" href="https://sandfox.me/php/install-composer-in-docker.html#rest_code_fd18ad11a27f41e781f79207354497c7-6"></a><span class="w"> </span>rm<span class="w"> </span>/tmp/composer-setup.php<span class="w"> </span>/tmp/composer.sig
</pre></div>
<p>It doesn't require a script file, doesn't deal with environment variables, and doesn't depend on the shell.</p>
<p><a href="https://sandfox.me/php/install-composer-in-docker.html">Read moreā¦</a> (1 min remaining to read)</p></div>