<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>docker-compose | Nerdpress.org</title>
	<atom:link href="https://nerdpress.org/tag/docker-compose/feed/" rel="self" type="application/rss+xml" />
	<link>https://nerdpress.org</link>
	<description>...dev, tech problems and solutions.</description>
	<lastBuildDate>Fri, 24 Feb 2023 10:04:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Import data into MySql with docker-compose</title>
		<link>https://nerdpress.org/2023/02/24/import-data-into-mysql-with-docker-compose/</link>
		
		<dc:creator><![CDATA[Ivo Bathke]]></dc:creator>
		<pubDate>Fri, 24 Feb 2023 10:04:31 +0000</pubDate>
				<category><![CDATA[docker-compose]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[PHPStorm]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=3221</guid>

					<description><![CDATA[<p>Having a docker-compose setup which involves a Database like Mysql or MariaDB, then at some point you might want to import data into those Databases. There are several ways to import the data in your docker-compose setup. So let&#8217;s see how we can do this: 1. Using a volume for import data This applies to &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2023/02/24/import-data-into-mysql-with-docker-compose/" class="more-link">Continue reading<span class="screen-reader-text"> "Import data into MySql with docker-compose"</span></a></p>
The post <a href="https://nerdpress.org/2023/02/24/import-data-into-mysql-with-docker-compose/">Import data into MySql with docker-compose</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>Having a docker-compose setup which involves a Database like Mysql or MariaDB, then at some point you might want to import data into those Databases.</p>



<p>There are several ways to import the data in your docker-compose setup.</p>



<ol class="wp-block-list">
<li>Using a volume for import data</li>



<li>Using mysql client from commandline with docker-compose exec</li>



<li>Using phpmyadmin in docker-compose setup</li>



<li>Using a mysql GUI client on the host and connect to the DB in the Docker container</li>
</ol>



<p>So let&#8217;s see how we can do this:</p>



<span id="more-3221"></span>



<h2 class="wp-block-heading">1. Using a volume for import data</h2>



<p>This applies to both official <a href="https://hub.docker.com/_/mysql/" target="_blank" rel="noopener" title="">Mysql</a><em> and <a href="https://hub.docker.com/_/mariadb/" target="_blank" rel="noopener" title="">MariaDB</a></em> official images.<br />These have builtin import mechanism to import data.<br />To quote from the docs:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directory</p>
</blockquote>



<p>So all dumps that are found in the <em>/docker-entrypoint-initdb.d</em> directory of the image will be imported unless the database already contains data.</p>



<p>So let&#8217;s add this volume to our docker-compose.yaml</p>



<p><code>volumes: - ./database_dump.sql:/docker-entrypoint-initdb.d/datadump.sql</code></p>



<p>On next <code>docker-compose up</code> the data will be imported in your empty database.<br />You will see something like this in your docker-compose logs:</p>



<p>2023-02-06 15:40:16+00:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/database_dump.sql</p>



<p>Note: if there is already data in the database you need to clear the data first by f.e. clearing the volume, otherwise the import will not start:<br />You can clear all volumes of your docker-compose setup with (caution: this will clear <em>all</em> volumes, not just the database volume):</p>



<p><code>docker-compose down -v</code></p>



<h2 class="wp-block-heading">2. Using mysql client from commandline with docker-compose exec</h2>



<p>With this one liner you can import a SQL dump from a docker image that has a MySql/MariaDB client installed and are linked to the DB container:</p>


<pre class="wp-block-code"><span><code class="hljs language-javascript">docker-compose exec my-app bash -c <span class="hljs-string">"mysql -u root -h mysql --password=root database &lt; database_dump.sql"</span></code></span></pre>


<p>This will execute the application image and import the data with the mysql client which connects to the host <code>mysql</code> which is the linked mysql container.</p>



<h2 class="wp-block-heading">3. Use phpmyadmin in docker-compose setup</h2>



<p>If the above is too consolish then you also can just add a phpmyaadmin container in your docker-compose setup and administer the database with a GUI from the browser.</p>


<pre class="wp-block-code"><span><code class="hljs">  myproject_phpmyadmin:
	image: phpmyadmin/phpmyadmin:latest
	ports:
	  - 8080:80
	environment:
	  PMA_HOST: myproject_mysql</code></span></pre>


<p>Then you can just open <em>http://localhost:8080</em> in the browser and log into to Phpmyadmin.<br />To import data go to the import tab and upload the dump file.</p>



<figure class="wp-block-image size-full"><a href="https://nerdpress.org/wp-content/uploads/2023/02/phpmyadmin_import_tab.png"><img decoding="async" width="481" height="51" src="https://nerdpress.org/wp-content/uploads/2023/02/phpmyadmin_import_tab.png" alt="" class="wp-image-3222" srcset="https://nerdpress.org/wp-content/uploads/2023/02/phpmyadmin_import_tab.png 481w, https://nerdpress.org/wp-content/uploads/2023/02/phpmyadmin_import_tab-300x32.png 300w" sizes="(max-width: 481px) 100vw, 481px" /></a><figcaption class="wp-element-caption">PhpMyAdmin Import tab</figcaption></figure>



<h2 class="wp-block-heading">4. Using a mysql GUI client on the host and connect to the DB in the Docker container</h2>



<p>For this approach you just need to open a port in the database container to the outside and use this port with localhost in the settings of your database client.</p>


<pre class="wp-block-code"><span><code class="hljs language-php">  myproject_mysql:
    image: mysql:<span class="hljs-number">5.7</span>
    hostname: mysql.${DOMAIN}
    container_name: ${CONTAINER_NAME}.mysql
      volumes:
	- ${PWD}/data/mysql/:/<span class="hljs-keyword">var</span>/lib/mysql
	- ./container/mysql/my.cnf:/etc/mysql/conf.d/z_my.cnf
    ports: <span class="hljs-comment"># open the mysql port to the host</span>
      - <span class="hljs-string">"3306:3306"</span>
    environment:
  ...</code></span></pre>


<p>See this screenshot from IntellIj&#8217;s PHPStorm Database tool:</p>



<figure class="wp-block-image size-full"><a href="https://nerdpress.org/wp-content/uploads/2023/02/PHPStorm_Database_property.png"><img fetchpriority="high" decoding="async" width="807" height="491" src="https://nerdpress.org/wp-content/uploads/2023/02/PHPStorm_Database_property.png" alt="" class="wp-image-3223" srcset="https://nerdpress.org/wp-content/uploads/2023/02/PHPStorm_Database_property.png 807w, https://nerdpress.org/wp-content/uploads/2023/02/PHPStorm_Database_property-300x183.png 300w, https://nerdpress.org/wp-content/uploads/2023/02/PHPStorm_Database_property-768x467.png 768w" sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" /></a><figcaption class="wp-element-caption">PHPStorm DB settings</figcaption></figure>



<p>From my experience these clients are very good to check data but not so good for import, at least I had rather poor experience in the PHPStorm DB Tool.<br />So going consolish on imports is recommended :)</p>The post <a href="https://nerdpress.org/2023/02/24/import-data-into-mysql-with-docker-compose/">Import data into MySql with docker-compose</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Recreate pruned docker networks with docker-compose</title>
		<link>https://nerdpress.org/2021/09/27/recreate-pruned-docker-networks-with-docker-compose/</link>
		
		<dc:creator><![CDATA[Ivo Bathke]]></dc:creator>
		<pubDate>Mon, 27 Sep 2021 08:15:16 +0000</pubDate>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[docker]]></category>
		<category><![CDATA[docker-compose]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=3078</guid>

					<description><![CDATA[<p>So here is nice chain of docker errors and its solution. 1. Docker has reached its maximum of created networks and errors like this: ERROR: could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network The easiest (and most brute though) solution to this is to prune all &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2021/09/27/recreate-pruned-docker-networks-with-docker-compose/" class="more-link">Continue reading<span class="screen-reader-text"> "Recreate pruned docker networks with docker-compose"</span></a></p>
The post <a href="https://nerdpress.org/2021/09/27/recreate-pruned-docker-networks-with-docker-compose/">Recreate pruned docker networks with docker-compose</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>So here is nice chain of docker errors and its solution.<br /><br />1. Docker has reached its maximum of created networks and errors like this:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><code>ERROR: could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network</code></p></blockquote>



<p>The easiest (and most brute though) solution to this is to prune all networks, as suggested <a href="https://stackoverflow.com/a/45377351/541949" target="_blank" rel="noreferrer noopener">here on SO</a>.</p>



<p><code>docker network prune</code></p>



<p>This will delete all networks that are not connected to currently active containers.<br />But this also means it will delete all networks of your not runnning dev projects.<br />(Which might be numerous as in my case.)</p>



<span id="more-3078"></span>



<p>2. So next time you spin up another dev project the network will be missing, as it has been pruned and you will see this error:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><code>Could not attach to network 726...: rpc error: code = NotFound desc = network 726... not found</code></p></blockquote>



<p>Ok sure, the network is gone, but no problem let&#8217;s recreate it.<br />As described in this <a href="https://nerdpress.org/2020/01/27/docker-compose-network-not-found/" target="_blank" rel="noreferrer noopener">blog post</a>.<br />We can simply add the &#8212;<code>force-recreate</code> option to docker-compose up and run it once then remove the option again to continue as usual.</p>


<pre class="wp-block-code"><span><code class="hljs">docker-compose up --force-recreate</code></span></pre>


<p>Also described <a href="https://stackoverflow.com/a/53571715/541949" target="_blank" rel="noreferrer noopener">here on SO</a>.</p>



<p>Then we are back to normal in our dev docker environments.</p>The post <a href="https://nerdpress.org/2021/09/27/recreate-pruned-docker-networks-with-docker-compose/">Recreate pruned docker networks with docker-compose</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Docker-compose and Unknown MySQL server host</title>
		<link>https://nerdpress.org/2020/12/08/docker-compose-and-unknown-mysql-server-host/</link>
		
		<dc:creator><![CDATA[Ivo Bathke]]></dc:creator>
		<pubDate>Tue, 08 Dec 2020 10:06:25 +0000</pubDate>
				<category><![CDATA[docker]]></category>
		<category><![CDATA[docker-compose]]></category>
		<category><![CDATA[MySQL]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=2962</guid>

					<description><![CDATA[<p>In case you encounter this error with mysql and your docker-compose setup: &#8230; and you dont know why because everything seems to be correct. Then you might have an upgrade problem with mysql because you are reusing an old volume that was created for another mysql version.This can happen when you use a unspecified tag &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2020/12/08/docker-compose-and-unknown-mysql-server-host/" class="more-link">Continue reading<span class="screen-reader-text"> "Docker-compose and Unknown MySQL server host"</span></a></p>
The post <a href="https://nerdpress.org/2020/12/08/docker-compose-and-unknown-mysql-server-host/">Docker-compose and Unknown MySQL server host</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>In case you encounter this error with mysql and your docker-compose setup:</p>


<pre class="wp-block-code"><span><code class="hljs language-xml">Unknown MySQL server host <span class="hljs-tag">&lt;<span class="hljs-name">mysql-service-name</span>&gt;</span></code></span></pre>


<p>&#8230; and you dont know why because everything seems to be correct.</p>



<p>Then you might have an upgrade problem with mysql because you are reusing an old volume that was created for another mysql version.<br>This can happen when you use a unspecified tag as <code>mysql:latest</code> (not recommended anyway) and there was a version bump in the official mysql image. <br>Or you upgraded the mysql container yourself, f.e. from <code>mysql:5.6</code> to <code>mysql:5.7</code> and you are reusing the data volume with the mysql files.</p>



<span id="more-2962"></span>



<p>When you check the startup logs you might see that mysql container is shutting down after startup because of an error like this:</p>


<pre class="wp-block-code"><span><code class="hljs">Can't open the mysql.plugin table. Please run mysql_upgrade to create it.</code></span></pre>


<p>You can run the upgrade command in the container:</p>


<pre class="wp-block-code"><span><code class="hljs language-javascript">docker exec -it mysql_container_name bash -c <span class="hljs-string">"mysql_upgrade -uroot -proot"</span></code></span></pre>


<p>Or simply delete the volume and recreate it.<br><em>Note: the data will unfortunatly be lost. So better dump the data before deleting the volume and reimport it.</em><br>In my case its no problem because im starting a new project anyway.</p>



<p>To delete the volume you can:</p>


<pre class="wp-block-code"><span><code class="hljs language-xml">docker volume rm <span class="hljs-tag">&lt;<span class="hljs-name">volume</span> <span class="hljs-attr">id</span>&gt;</span></code></span></pre>


<p>Or just delete the data dir:</p>


<pre class="wp-block-code"><span><code class="hljs">rm -rf ./data/mysql</code></span></pre>


<p>Then restart your setup so the mysql container recreates the databases:</p>


<pre class="wp-block-code"><span><code class="hljs">docker-compose up</code></span></pre>


<p>and mysql is working again and the application is able to connect. :)</p>The post <a href="https://nerdpress.org/2020/12/08/docker-compose-and-unknown-mysql-server-host/">Docker-compose and Unknown MySQL server host</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>docker-compose network not found</title>
		<link>https://nerdpress.org/2020/01/27/docker-compose-network-not-found/</link>
		
		<dc:creator><![CDATA[Ivo Bathke]]></dc:creator>
		<pubDate>Mon, 27 Jan 2020 10:22:17 +0000</pubDate>
				<category><![CDATA[docker]]></category>
		<category><![CDATA[docker-compose]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=2903</guid>

					<description><![CDATA[<p>Recently i more often encounter this strange error in my docker-compose setups while bringing up the containers with docker-compose up: ERROR: for docker_myproject_phpmyadmin_1 Cannot start service mg_myproject_phpmyadmin: network 15dff95a23808fe9d14b3dae71c0daeaa8b274c90566baa5beff37f3738033b6 not found For some reason the networks were deleted and docker-compose doesnt automatically recreate networks unless you tell so by forcing docker to recreate containers: docker-compose &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2020/01/27/docker-compose-network-not-found/" class="more-link">Continue reading<span class="screen-reader-text"> "docker-compose network not found"</span></a></p>
The post <a href="https://nerdpress.org/2020/01/27/docker-compose-network-not-found/">docker-compose network not found</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>Recently i more often encounter this strange error in my docker-compose setups while bringing up the containers with <code>docker-compose up</code>:</p>



<p><code>ERROR: for docker_myproject_phpmyadmin_1  Cannot start service mg_myproject_phpmyadmin: network 15dff95a23808fe9d14b3dae71c0daeaa8b274c90566baa5beff37f3738033b6 not found</code></p>



<span id="more-2903"></span>



<p>For some reason the networks were deleted and docker-compose doesnt automatically recreate networks unless you tell so by forcing docker to recreate containers:</p>



<p><code>docker-compose up --force-recreate</code></p>



<p>Solution came from this issue:<br /> <a href="https://github.com/docker/compose/issues/5745#issuecomment-370031631">https://github.com/docker/compose/issues/5745#issuecomment-370031631</a></p>



<p>Probably i have to explicitly declare the networks to prevent mixups between projects.<br />Though i thought this would not be necessary&#8230;</p>The post <a href="https://nerdpress.org/2020/01/27/docker-compose-network-not-found/">docker-compose network not found</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Adminer for Sqlite in Docker</title>
		<link>https://nerdpress.org/2019/10/23/adminer-for-sqlite-in-docker/</link>
		
		<dc:creator><![CDATA[Ivo Bathke]]></dc:creator>
		<pubDate>Wed, 23 Oct 2019 08:02:47 +0000</pubDate>
				<category><![CDATA[docker]]></category>
		<category><![CDATA[Sqlite]]></category>
		<category><![CDATA[adminer]]></category>
		<category><![CDATA[docker-compose]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=2889</guid>

					<description><![CDATA[<p>Recently i wanted to use Sqlite with Adminer in Docker and it turned out to be not so easy. I actually thought i could just declare Adminer in a docker-compose.yml file with a volume mounted, similar as i would do for adminer with mysql. Update: Today i would rather use the IntelliJ Database Tool for &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2019/10/23/adminer-for-sqlite-in-docker/" class="more-link">Continue reading<span class="screen-reader-text"> "Adminer for Sqlite in Docker"</span></a></p>
The post <a href="https://nerdpress.org/2019/10/23/adminer-for-sqlite-in-docker/">Adminer for Sqlite in Docker</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>Recently i wanted to use Sqlite with <a aria-label="Adminer (opens in a new tab)" href="https://www.adminer.org/" target="_blank" rel="noreferrer noopener">Adminer</a> in Docker and it turned out to be not so easy.<br /> I actually thought i could just declare Adminer in a docker-compose.yml file with a volume mounted, similar as i would do for adminer with mysql.</p>



<p><strong>Update</strong>: Today i would rather use the <a href="https://nerdpress.org/2021/05/17/sqlite-administration-in-intellij-ide/">IntelliJ Database Tool for Sqlite administration</a>.</p>



<p>But since Adminer is a popular hacking target they introduced a <a href="https://www.adminer.org/en/password/" target="_blank" rel="noreferrer noopener" aria-label="feature (opens in a new tab)">feature</a> that does not allow to run adminer without a password, out of the box.<br /> Sqlite database usually runs without password and dang, workaround needed!</p>



<span id="more-2889"></span>



<p>So we need to add a plugin to adminer that allows password-less login and extend the official Adminer Docker image to include the plugin.</p>



<p>We add a script that loads the official password-less-login plugin and copy it to the plugins-enabled folder of the Adminer image</p>



<p>login-password-less.php:</p>



<div class="wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex">
<div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow" style="flex-basis:100%"><div class="wp-block-syntaxhighlighter-code "><pre class="brush: php; title: ; notranslate">
&lt;?php
require_once(&#039;plugins/login-password-less.php&#039;);

/** Set allowed password
 * @param string result of password_hash
 */
return new AdminerLoginPasswordLess(
    $password_hash = password_hash(&quot;admin&quot;, PASSWORD_DEFAULT)
);
</pre></div></div>
</div>



<p>Dockerfile:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
FROM adminer
USER root
COPY login-password-less.php /var/www/html/plugins-enabled/login-password-less.php
#USER adminer # we run as root because of permissions problems on db file with the volume
</pre></div>


<p>In docker-compose.yml:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: yaml; title: ; notranslate">
version: &quot;3&quot;
services:
  app:
    build: ./php
      - mailcatcher
    volumes:
      - &quot;../:/app:rw&quot;
      - &quot;./php/cli/php.ini:/etc/php/7.3/cli/php.ini:ro&quot;
      - &quot;./php/fpm/php.ini:/etc/php/7.3/fpm/php.ini:ro&quot;

  nginx:
    build: ./nginx
    depends_on:
      - app
    command: /bin/sh -c &quot;nginx -g &#039;daemon off;&#039;&quot;
    volumes:
      - &quot;..:/app:rw&quot;
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
    ports:
      - &#039;8088:80&#039;

  adminer:
    build: ./adminer
    restart: unless-stopped
    volumes:
      - &quot;..:/app:rw&quot;
    ports:
      - 8080:8080
</pre></div>


<p>File tree:</p>



<ul class="wp-block-list"><li>adminer<br />&#8211; Dockerfile<br />&#8211; login-password-less.php</li><li>nginx<br />&#8230;</li><li>php<br />&#8230;</li><li>docker-compose.yml</li></ul>



<p><em>Note that since docker-compose.yml version 3 the volumes_from directive was removed in favor of top level volumes to share volumes across images. However this implementation is imho rather weird, so i jut duplicated the volumes declaration in the App container and Adminer container.<br /> Feels rather wrong but thats all i could come up with. Any advice here is welcome.</em></p>



<p>Now that this is done we can open the Sqlite3 database with the pseudo-password &#8220;admin&#8221; and manage the database.</p>



<p>After all you could also just copy the one-file Adminer with plugin to the php container and run it from within the php container. Just adjust your Nginx config to allow calling the file directly and take care to not deploy Adminer to live.<br /> There is also a project that bundles the plugin with Adminer to one file: <a href="https://github.com/FrancoisCapon/LoginToASqlite3DatabaseWithoutCredentialsWithAdminer" target="_blank" rel="noreferrer noopener">https://github.com/FrancoisCapon/LoginToASqlite3DatabaseWithoutCredentialsWithAdminer/blob/master/build-adminer-4-sqlite3-into-one-file.sh</a></p>The post <a href="https://nerdpress.org/2019/10/23/adminer-for-sqlite-in-docker/">Adminer for Sqlite in Docker</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
