Building a Multi-container App Using Docker Compose and Golang

Building a Multi-container App Using Docker Compose and Golang

Mastering Nginx: Essential Commands and Best Practices for Efficient Server Management πŸš€


3 min read

In today's rapidly evolving tech landscape, Docker and container orchestration have become indispensable tools for developers. In this blog post, we'll dive deep into creating a multi-container application using Docker Compose, with the backend powered by Golang's net/http package. Ready to dive in? 🌊

Explanation in a video πŸš€

1. Why Docker Compose? πŸ€”

Docker Compose allows developers to define and run multi-container Docker applications seamlessly. With a single command, you can get your app's multiple services running in defined containers, networked together. It’s a big leap in simplifying container orchestration.

2. Directory Layout πŸ—‚

Starting with a clear directory structure is crucial:

|-- frontend
|   `-- Dockerfile
|   `-- default.conf
|-- backend
|   |-- main.go
|   |-- go.mod (if using Go modules)
|   `-- Dockerfile
`-- docker-compose.yml

3. Golang Backend Using net/http 🐹

For our backend, we're using the lightweight net/http package from Golang. Here's a basic server setup:


package main

import (

func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello from Golang Backend!")

    http.ListenAndServe(":5000", nil)

When building with Docker, we need to be cautious about the files we're moving into our container. Our Dockerfile ensures our Go app and its dependencies are containerized:


FROM golang:latest


COPY ./main.go ./
COPY ./go.mod ./

RUN go build -o main

CMD [ "./main" ]

4. Nginx Frontend 🌍

Nginx acts as a straightforward proxy to our Golang backend.


FROM nginx:alpine

COPY ./default.conf /etc/nginx/conf.d/

This configuration tells Nginx to forward requests to our Golang backend.

5. Create the default.conf File

Ensure you have the default.conf file within the frontend directory. This file should contain the configuration to proxy requests to the backend.


server {
    listen 80;

    location / {
        proxy_pass http://backend:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;

6. Binding It All with Docker Compose πŸ”„

Our docker-compose.yml file sets the stage, defining how our services interlink:

version: '3'

      context: ./backend
      - "5000:5000"

      context: ./frontend
      - "80:80"
      - backend

With this setup, the front end knows to wait for the backend to start first, ensuring smooth communication.

7. The Magic Command ✨

Once everything's set up, first run a command to build and then make it up.

Build it

$ docker-compose build

Make it up

$ docker-compose up

Accessing http://localhost:5000 in your browser will display the "Hello from Golang Backend!" message.

8. Stop the Nginx Server

Using the nginx command:

If you started Nginx with the default configuration file, you can stop it using:

$ sudo nginx -s stop

This will stop Nginx immediately. Alternatively, you can use:

$ sudo nginx -s quit

This will do a graceful shutdown, allowing Nginx to finish processing current requests before shutting down.

Conclusion πŸŽ‰

Docker Compose is a powerful tool, especially when building complex apps with multiple services. Integrating Golang's net/http as the backend showcases how effortlessly different technologies can mesh together in a Dockerized world.

Remember, while this is a basic setup, real-world applications would require more configurations, error handling, and security settings. But with this foundation, the sky's the limit! 🌌

Happy coding! πŸš€πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»