Building a Multi-container App Using Docker Compose and Golang
Mastering Nginx: Essential Commands and Best Practices for Efficient Server Management π
Table of contents
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:
/myapp
|-- 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:
backend/main.go:
package main
import (
"fmt"
"net/http"
)
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:
backend/Dockerfile:
FROM golang:latest
WORKDIR /app
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.
frontend/Dockerfile:
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.
frontend/default.conf:
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'
services:
backend:
build:
context: ./backend
ports:
- "5000:5000"
frontend:
build:
context: ./frontend
ports:
- "80:80"
depends_on:
- 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! ππ©βπ»π¨βπ»