» Go: Building Full-Text Search API with ElasticSearch » 4. Deployment » 4.2 Docker Compose

Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to use a YAML file to configure your application's services, networks, and volumes, and then spin up all the containers required to run your application with a single command.

Note:
The easiest and recommended way to get Docker Compose is to install Docker Desktop. Docker Desktop includes Docker Compose along with Docker Engine and Docker CLI which are Compose prerequisites.

Install Compose if needed: https://docs.docker.com/compose/install/

Add compose/docker-compose.yml:

services:
  lr-fulltext-books:
    build:
      context: ../
      dockerfile: Dockerfile
    ports:
      - 8080:8080
    volumes:
      - ./config.yml:/home/.server/config.yml
    depends_on:
      es:
        condition: service_started
  es:
    image: bitnami/elasticsearch:latest
    ports:
      - 9200:9200
    volumes:
      - ~/lr-es-data:/bitnami/elasticsearch/data

Add compose/config.yml:

app:
  port: 8080
  page_size: 10
search:
  address: "http://es:9200"

You need a special version of the config file for Docker Compose because all the hosts, paths, and urls vary in different hosting environments.

Run it:

cd compose
docker compose up

You should see something like this:

[+] Running 2/2
 ✔ Container compose-es-1                 Recreated                                                                                                                                                                                   0.1s 
 ✔ Container compose-lr-fulltext-books-1  Created                                                                                                                                                                                     0.0s 
Attaching to es-1, lr-fulltext-books-1
es-1                 | elasticsearch 15:18:14.49 INFO  ==> Welcome to the Bitnami elasticsearch container
es-1                 | elasticsearch 15:18:14.51 INFO  ==> ** Starting Elasticsearch setup **
...

You don't need to manually install or setup elasticsearch anymore. They're all in good hands with docker compose.

Put in some test books if there isn‘t one.

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"title":"Harry Potter and the Philosopher\u0027s Stone","author":"J.K. Rowling","published_at":"1997-06-26","content":"A young boy discovers he is a wizard and begins his education at Hogwarts School of Witchcraft and Wizardry, where he uncovers the mystery of the Philosopher‘s Stone."}' \
  http://localhost:8080/books

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"title":"To Kill a Mockingbird","author":"Harper Lee","published_at":"1960-07-11","content":"Set in the American South during the Great Depression, the novel explores themes of racial injustice and moral growth through the eyes of young Scout Finch."}' \
  http://localhost:8080/books

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"title":"The Lord of the Rings","author":"J.R.R. Tolkien","published_at":"1954-07-29","content":"A hobbit named Frodo Baggins embarks on a perilous journey to destroy a powerful ring and save Middle-earth from the Dark Lord Sauron."}' \
  http://localhost:8080/books

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"title":"The Catcher in the Rye","author":"J.D. Salinger","published_at":"1951-07-16","content":"Holden Caulfield narrates his experiences in New York City after being expelled from prep school, grappling with themes of alienation, identity, and innocence."}' \
  http://localhost:8080/books

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"title":"The Alchemist","author":"Paulo Coelho","published_at":"1988-01-01","content":"Santiago, a shepherd boy, travels from Spain to Egypt in search of a treasure buried near the Pyramids. Along the way, he learns about the importance of following one‘s dreams."}' \
  http://localhost:8080/books

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"title":"The Hunger Games","author":"Suzanne Collins","published_at":"2008-09-14","content":"In a dystopian future, teenagers are forced to participate in a televised death match called the Hunger Games. Katniss Everdeen volunteers to take her sister‘s place and becomes a symbol of rebellion."}' \
  http://localhost:8080/books

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"title":"1984","author":"George Orwell","published_at":"1949-06-08","content":"Winston Smith lives in a totalitarian society ruled by the Party led by Big Brother. He rebels against the oppressive regime but ultimately succumbs to its control."}' \
  http://localhost:8080/books

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"title":"The Girl with the Dragon Tattoo","author":"Stieg Larsson","published_at":"2005-08-01","content":"Journalist Mikael Blomkvist and hacker Lisbeth Salander investigate the disappearance of a young woman from a wealthy family, uncovering dark secrets and corruption."}' \
  http://localhost:8080/books

curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"title":"Gone Girl","author":"Gillian Flynn","published_at":"2012-06-05","content":"On their fifth wedding anniversary, Nick Dunne‘s wife, Amy, disappears. As the media circus ensues and suspicions mount, Nick finds himself in a whirlwind of deception and betrayal."}' \
  http://localhost:8080/books

Now, if you visit your page at http://localhost:8080/books?q=new+york+girl, you should be able to see all the matched documents.

Search with other queries like http://localhost:8080/books?q=boy+mystery, then see what happens.

Your full-text search API works like a charm! 📢