title: How to set 'spring.datasource.url' inside a Docker Container
body: <p>I created a Spring Boot application which uses a MySQL database. I use a docker-compose to launch the database.</p>
  <pre><code>
  services:
    adminer:
      image: adminer
      restart: always
      ports:
        - 8888:8080
    db:
      image: mysql:latest
      restart: always
      environment:
        MYSQL_ROOT_PASSWORD: 'example' # TODO: Change this      
      volumes:
        - &quot;./config/my.conf:/etc/mysql/conf.d/config-file.cnf&quot;
        - &quot;./data:/var/lib/mysql:rw&quot;
  </code></pre>
  <p>The Spring Boot Application (Backend) currently does not use Docker, I run it inside Eclipse. Before launching the Backend I have to grep the Docker Container for IPAddress:</p>
  <pre><code>docker inspect mysql_ex_db_1 | grep 'IPAddress'
  </code></pre>
  <p>which results something like this (this exact address changes time-to time)</p>
  <pre><code>                &quot;IPAddress&quot;: &quot;&quot;,
                      &quot;IPAddress&quot;: &quot;172.21.0.2&quot;, 
  </code></pre>
  <p>Then I take this value and I set <code>spring.datasource.url</code> inside Eclipse in the file <code>Application.properties</code> with it.</p>
  <pre><code>spring.datasource.url=jdbc:mysql://172.21.0.2:3306/employee_management_system?allowPublicKeyRetrieval=true&amp;useSSL=false&amp;createDatabaseIfNotExist=true
  
  </code></pre>
  <p>After this I can launch the Backend in Eclipse the Connection to database is there, everything works.</p>
  <p>Now I want to move the launching of Backend from Eclipse to the same docker-compose file I use to launch the database. Therefore I built an image, and appended the docker-compose file:</p>
  <pre><code>version: '3.1'
  
  services:
    adminer:
      image: adminer
      restart: always
      ports:
        - 8888:8080
    db:
      image: mysql:latest
      restart: always
      environment:
        MYSQL_ROOT_PASSWORD: 'example' # TODO: Change this      
      volumes:
        - &quot;./config/my.conf:/etc/mysql/conf.d/config-file.cnf&quot;
        - &quot;./data:/var/lib/mysql:rw&quot;
    backend:
      image: backend:latest
      restart: always
      ports:
        - 8090:8080
  </code></pre>
  <p>In this case how can I configure the IPAddress in <code>spring.datasource.url</code>?
  The exact IPAddress changes whenever I re-launch the mysql containers.</p>
  <pre><code>spring.datasource.url=jdbc:mysql://172.21.0.2:3306/employee_management_system?allowPublicKeyRetrieval=true&amp;useSSL=false&amp;createDatabaseIfNotExist=true
  
  </code></pre>
  <p>So what should I write instead of '172.21.0.2' ?</p>
  <p>I tried <strong>localhost</strong> here but it
  does not seem to work.</p>