Manage Docker Containers with Docker Compose

Manage Docker Containers with Docker Compose

1 Comment

Docker Compose defines and runs multi-container Docker applications. It lets you create and start all services from a configuration file with a single docker-compose command.

In the previous posts on the Docker series, I discussed how to run Spring Boot on Docker,

In this post, I will discuss how to dockerize multiple containers that depend on each other. You’ll also see how these containers can be managed together with single commands.

Docker Compose Installation

We will start by installing Docker Compose on Ubuntu.

1. Run this command to download the current stable release of Docker Compose:

$ sudo curl -L "$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

2. Apply executable permissions to the binary:

$ sudo chmod +x /usr/local/bin/docker-compose

3. Test the installation.

$ docker-compose --version

The figure shows the docker-compose version

Docker-Compose versionTo install Docker-Compose on Windows/Mac click here.

Docker Compose

I’ll create two containers:

    1. A simple user-service application that will expose one REST endpoint to manage user information.
    2. The second one is a MongoDB container that will store the information of users.

    The Application

    Add the spring-boot-starter-data-mongodb dependency in your pom.xml.

    The pom.xml file is this.

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns=""
            <relativePath/> <!-- lookup parent from repository -->

    The example REST service has a POST method in the controller. The code of the controller is this.

    package guru.springframework.dockercompose.controller;
    import guru.springframework.dockercompose.domain.User;
    import guru.springframework.dockercompose.exception.UserAlreadyExistsException;
    import guru.springframework.dockercompose.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.*;
    * RestController annotation is used to create
    * Restful web services using Spring MVC
    * RequestMapping annotation maps
    * HTTP requests to handler methods
    @RequestMapping(value = "/api/v1/")
    public class UserController {
      private UserService userService;
      ResponseEntity responseEntity;
      public UserController(UserService userService) {
        this.userService = userService;
      * PostMapping Annotation for mapping HTTP POST requests onto
      * specific handler methods.
      public ResponseEntity<?> saveUser(@RequestBody User user) throws UserAlreadyExistsException, Exception {
        User savedUser = userService.saveUser(user);
        responseEntity = new ResponseEntity(savedUser, HttpStatus.CREATED);
        return responseEntity;

    The preceding code has one method to save user details. It calls the saveUser() method of UserService.

    The Dockerfile

    The Dockerfile for the user-service is this.

    FROM openjdk:11
    ADD ./target/user-service-0.0.1-SNAPSHOT.jar /usr/src/user-service-0.0.1-SNAPSHOT.jar
    WORKDIR usr/src
    ENTRYPOINT ["java","-jar", "user-service-0.0.1-SNAPSHOT.jar"]

    The docker-compose File

    With Docker Compose, you use a YAML file, docker-compose.yml file to configure your application’s services.

    Create a docker-compose.yml to manage the MongoDB and user-service containers at the root of the project.


    version: '3'
        image: mongo:3.6.18-xenial
        container_name: mongo
        restart: always
          - 27017:27017
        network_mode: host
          - $HOME/mongo:/data/db
          test: "exit 0"
        build: user-service/
        image: user-service
          - mongo
        network_mode: "host"
        hostname: localhost
        restart: always
          - 8082:8082
          test: "exit 0"

    In the preceding docker-compose.yml file:

    • Line 01 sets the version of the docker-compose syntax. In this example, the version is 3.
    • In order to run Mongo in a Docker container, we need a Mongo image. mongo service is used to create Mongo image. Line 04 uses the mongo:3.6.18-xenial image for mongo.
    • In Line 05, container_name specifies a custom container name.
    • restart: always in Line 06 configures for how to restart containers when they exit.
    • Line – Line 08 maps the host port to the container’s port. Mongo listens to the port 27017 of the Docker network that is different from the network of your computer, also known as the host network. On the other hand, the mongo client will try to connect with the port 27017 of the host network. So to map both the networks, ports is used.
    • Line 09 specifies the host network mode. In this mode, the container shares the host’s networking namespace.
    • volumes in Line 16 – Line 17 maps a directory of our local filesystem to the directory in docker where mongo writes data. It means that our host/local folder $HOME/mongo will be mounted as /data/db inside our running container.
    • healthcheck in Line 12 – Line 13 determines whether or not the containers for this service are health”.
    • In Line 16 build instructs Docker COmpose to build the Spring Boot image from the Dockerfile present in the current folder.
    • Finally depends-on in Line 18 – Line 19 specifies that the user-service container depends on the mongo container. As a result, Docker Compose will start the user-service container after the mongo container starts.

    Passing Environment Variable

    At times, you will need to pass environment variables to containers.

    One use case for Mongo is to create additional users, initialize collections, and indexes at build time.

    The trick is to use the MONGO_INITDB_DATABASE variable that allows you to specify the name of a database to be used for creating scripts in /docker-entrypoint-initdb.d/*.js .

    What this means is, if you supply the image with init scripts, it will run them against the $MONGO_INITDB_DATABASE database.

    The docker-compose code is this.

    version: '3'
            image: mongo:latest
            container_name: mongodb
            restart: always
                MONGO_INITDB_ROOT_USERNAME: <admin-user>
                MONGO_INITDB_ROOT_PASSWORD: <admin-password>
                MONGO_INITDB_DATABASE: <database to create>
                - 27017:27017
                - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro

    The code of mongo-init.js is this

                user: "<user for database which shall be created>",
                pwd: "<password of user>",
                roles: [
                        role: "readWrite",
                        db: "<database to create>"

    Managing Containers

    Starting Containers

    We’ll start the services using the docker-compose up command.

    $ docker-compose up

    The figure shows the output.

    docker-compose upList the containers and check the status with this command:

    $ docker-compose ps

    This figure shows the currently running Docker containers.

    docker-compose ps

    Beside docker-compose up, there are two other commands that can confuse new developers: start and run.

    docker-compose up builds, (re)creates, starts, and attaches to containers for a service. Unless they are already running, this command also starts any linked services.

    On the other hand, docker-compose start starts the existing containers for a service. Therefore use start if you need to restart containers that were previously created, but were stopped. It never creates new containers.

    The docker-compose run command is for running “one-off” or “adhoc” tasks. You pass the service name you want to run and Docker Compose will only start the containers for services that the running service depends on.

  1. Typically, you will use the run command to run tests or perform an administrative task such as removing or adding data to a data volume container. Also, note that the run command acts like docker run -ti. It opens an interactive terminal to the container and returns an exit status matching the exit status of the process in the container.

    Viewing Logs

    To view the container logs, run this command.

    $ docker-compose logs

    The log output is this.

    docker-compose logs

    Now that the container is running, we’ll try accessing the RESTful service using Postman.
    We’ll access the endpoint on http://localhost:<port>/api/v1/user and insert a user.


    Testing the Application

    Next, I’ll verify if the user is saved in MongoDB. I’ll do this by entering the mongo container by attaching a bash to it.

  2. In my case the container name is mongo. So I’ll run this.
    $ docker exec -it mongo bash


    Mongo container bash

    By running the above command, as you can see the terminal changes to something like root@meghna-Lenovo-ideapad-Y700-15ISK:/# which means we’re inside the mongo docker container.

    After this, run the commands to enter the Mongo client prompt and query the inserted data.

    $ mongo$ 
    $ use user 
    $ db.User.find()

    The output of the query is this.
    Verify User data in mongo container

    Stopping Containers

    You can use the stop or down commands of Docker Compose to stop your containers. Although both the commands stop containers, they are different.

    The docker-compose stop command stops running containers, but it won’t remove them. Whereas, the docker-compose down command stops containers and also removes the stopped containers as well as any networks that were created.

    There is also a docker-compose kill command that you can use to force running containers to stop by sending a SIGKILL signal.


    Docker Compose is widely adopted by the industry to run automated test suite as part of Continuous Deployment or Continuous Integration process. Compose offers a convenient way to create and destroy isolated testing environments for test suites in just a few commands, by defining the full environment in a Compose file.

    You will hear lots of comments about Docker Compose not being fit for production. But, this is only partially true.

    If you have a distributed production environment across multiple machines. Also, if your containers should be distributed across those machines dynamically then Docker Compose is not the right tool for you.

    You would need to use some orchestration tools, such as Docker Swarm and Kubernetes to achieve this.

    But you can still have a perfectly fine production environment without dynamically distributing containers across multiple interconnected machines. This is where you should go for Docker Compose. It makes handling container configuration or multiple interconnected containers easier.

    Overall, it’s a cool tool to run multiple isolated services on a single host.

The source code for this post can be found here on GitHub.

About SFG Contributor

Staff writer account for Spring Framework Guru

    You May Also Like

    One comment

    1. November 6, 2020 at 1:14 pm

      Thanks for the post John!

      From what I understand from the docker docs and by simply running your example, I think the ports part in the docker compose file is not necessary when using network_mode: host. Seems it simply exposes the ports to the host network.


    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.