<?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: Bruno do Nascimento Maciel</title>
    <description>The latest articles on DEV Community by Bruno do Nascimento Maciel (@macielti).</description>
    <link>https://dev.to/macielti</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%2F354161%2Fab2e74c2-7862-448b-9947-184919c8fa07.jpeg</url>
      <title>DEV Community: Bruno do Nascimento Maciel</title>
      <link>https://dev.to/macielti</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/macielti"/>
    <language>en</language>
    <item>
      <title>Custom Subnet IP for docker0 Interface</title>
      <dc:creator>Bruno do Nascimento Maciel</dc:creator>
      <pubDate>Mon, 23 Mar 2020 20:30:03 +0000</pubDate>
      <link>https://dev.to/macielti/custom-subnet-ip-for-docker0-interface-1jod</link>
      <guid>https://dev.to/macielti/custom-subnet-ip-for-docker0-interface-1jod</guid>
      <description>&lt;p&gt;Here is the problem, I want to deploy an API in my server but the server runs other services that conflict with the default IP that was given by default for &lt;code&gt;docker0&lt;/code&gt; interface. So I need to set up a custom subnet IP for that interface and put up some docker containers without creating new interfaces with the same subnet that is conflicting with the aforementioned services.&lt;/p&gt;

&lt;h2&gt;
  
  
  Docker Service Config
&lt;/h2&gt;

&lt;p&gt;First, we need to set the custom subnet for the &lt;code&gt;docker0&lt;/code&gt; default interface. To do this we need to edit or create if it does not exist a configuration file for the docker service.&lt;br&gt;
&lt;code&gt;# vim /etc/docker/daemon.json&lt;/code&gt;&lt;br&gt;
And put this as content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"bip"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"10.10.0.1/24"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that we need to restart the docker service:&lt;br&gt;
&lt;code&gt;# service docker restart&lt;/code&gt;&lt;br&gt;
After restart the docker service we can run &lt;code&gt;ifconfig&lt;/code&gt; to see if the changes were applied:&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvw4h865k4xbjx1bi8got.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvw4h865k4xbjx1bi8got.png" alt="Alt Text" width="622" height="53"&gt;&lt;/a&gt;&lt;br&gt;
As we can see in the image, the &lt;code&gt;docker0&lt;/code&gt; interface is set with the subnet that we have inserted in the &lt;code&gt;daemon.json&lt;/code&gt; file.&lt;/p&gt;
&lt;h2&gt;
  
  
  Docker Compose
&lt;/h2&gt;

&lt;p&gt;Now we have to set in our &lt;code&gt;docker-compose.yml&lt;/code&gt; file, that we want to use the &lt;code&gt;docker0&lt;/code&gt; interface and not create some random new one. To do this you only need to add &lt;code&gt;network_mode: bridge&lt;/code&gt; option to all your services inside  &lt;code&gt;docker-compose.yml&lt;/code&gt; file, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3.3'&lt;/span&gt;

&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;db&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;mysql:5.7&lt;/span&gt;
        &lt;span class="na"&gt;restart&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;always&lt;/span&gt;
        &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="na"&gt;MYSQL_ROOT_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Lorem-Diem'&lt;/span&gt;
            &lt;span class="na"&gt;MYSQL_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;lorem'&lt;/span&gt;
            &lt;span class="na"&gt;MYSQL_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Lorem-Ipsum'&lt;/span&gt;
        &lt;span class="na"&gt;network_mode&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;bridge&lt;/span&gt;
        &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;3306:3306'&lt;/span&gt;
        &lt;span class="na"&gt;volumes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
            &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;./mysql_data:/var/lib/mysql&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And after that you can just build and put up your containers:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker-compose up -d --build&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, if you run &lt;code&gt;ifconfig&lt;/code&gt; again you will see that none new interfaces were created.&lt;/p&gt;

&lt;p&gt;If you want to see your container IP, get the container id using &lt;code&gt;$ docker ps&lt;/code&gt; and run &lt;code&gt;$ docker inspect &amp;lt;your container id&amp;gt; | grep -i ip&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Single Docker
&lt;/h2&gt;

&lt;p&gt;If you want to start a single docker using the &lt;code&gt;docker0&lt;/code&gt; interface, you can add the &lt;code&gt;--net bridge&lt;/code&gt; to your &lt;code&gt;docker run&lt;/code&gt; command, like:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ docker run -it -d --net bridge ubuntu:18.04 /bin/bash&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The above command brings up a detached container running Ubuntu version 18.04 and calls the &lt;code&gt;/bin/bash&lt;/code&gt; so that you can run &lt;code&gt;ifconfig&lt;/code&gt; again in your host machine to see that the container is really using the default &lt;code&gt;docker0&lt;/code&gt; interface.&lt;/p&gt;

&lt;p&gt;All the containers mentioned in this post will receive an IP address that fits the &lt;code&gt;docker0&lt;/code&gt; subnet.&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
      <category>linux</category>
    </item>
  </channel>
</rss>
