DEV Community

Pradipta
Pradipta

Posted on

Seamless communication between containers - docker compose

Let's consider an example of a Spring Boot application which interacts with the MySQL database. Couple of images have been created for MySQL and Spring Boot respectively. The images can be run as containers. But how they going to interact with each other?

One way is to create a docker network and spin up the containers within that network. This will work but you have to deal with long commands which is difficult to maintain in bigger projects. This is the problem docker compose solves by making the container communication easy to achieve and maintain.

  • Let's consider the following Spring Boot Application which communicates with a MySQL database and provides a basic JWT implementation for securing Rest service endpoints.
    https://github.com/pradz13/spring-security-jwt.git

  • Spring Boot Application needs to define a Dockerfile as follows -

FROM maven:3.9.3-sapmachine-17
WORKDIR .
COPY . .
RUN mvn clean install -DskipTests
CMD mvn spring-boot:run
Enter fullscreen mode Exit fullscreen mode
  • Define the docker compose configuration file as follows -
version: "3.8"

services:
  mysqldb:
    image: mysql:5.7
    restart: unless-stopped
    env_file: ./.env
    environment:
      - MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
      - MYSQL_DATABASE=$MYSQLDB_DATABASE
    ports:
      - $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT
    volumes:
      - db:/var/lib/mysql

  app:
    depends_on:
      - mysqldb
    build: .
    restart: on-failure
    env_file: ./.env
    ports:
      - $SPRING_LOCAL_PORT:$SPRING_DOCKER_PORT
    environment:
      SPRING_APPLICATION_JSON: '{
            "spring.datasource.url"  : "jdbc:mysql://mysqldb:$MYSQLDB_DOCKER_PORT/$MYSQLDB_DATABASE?useSSL=false",
            "spring.datasource.username" : "$MYSQLDB_USER",
            "spring.datasource.password" : "$MYSQLDB_ROOT_PASSWORD",
            "spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.MySQLDialect",
            "spring.jpa.hibernate.ddl-auto" : "update"
          }'
    volumes:
      - .m2:/root/.m2
    stdin_open: true
    tty: true

volumes:
  db:
Enter fullscreen mode Exit fullscreen mode
  • Create the .env file which will have the configurations.
MYSQLDB_USER=root
MYSQLDB_ROOT_PASSWORD=123456
MYSQLDB_DATABASE=bezkoder_db
MYSQLDB_LOCAL_PORT=3307
MYSQLDB_DOCKER_PORT=3306

SPRING_LOCAL_PORT=6868
SPRING_DOCKER_PORT=8080
Enter fullscreen mode Exit fullscreen mode
  • Run the containers with the following command -
docker-compose up -d
Enter fullscreen mode Exit fullscreen mode

-d will run it in detached mode.

  • To stop the containers following command can be used -
docker-compose down
Enter fullscreen mode Exit fullscreen mode

To stop the containers and delete the images, use the following command -

docker-compose down --rmi all
Enter fullscreen mode Exit fullscreen mode

Please note, when docker compose is used, no need to create docker network explicitly, it is taken care by docker compose internally.

Top comments (0)