DEV Community

Cover image for Dockerizing a Golang API with MySQL and adding Docker Compose Support

Dockerizing a Golang API with MySQL and adding Docker Compose Support

Pradumna Saraf on September 04, 2024

Developing and testing APIs locally connected to a database is no joke. The database often becomes a pain point. However, using Docker, the process...
Collapse
 
francescoxx profile image
Francesco Ciulla

I love these kind of posts!

Collapse
 
pradumnasaraf profile image
Pradumna Saraf

Thank you so much, Francesco!

Collapse
 
francescoxx profile image
Francesco Ciulla

you know I love this sort of stuff for real ahah. I am planning another one but it's still in the process

Thread Thread
 
pradumnasaraf profile image
Pradumna Saraf

I KNOW! I can't wait to hear more about it!

Collapse
 
shricodev profile image
Shrijal Acharya

Good one, @pradumnasaraf brother. I am also starting with Golang these days. Really helpful. 🤍

Collapse
 
pradumnasaraf profile image
Pradumna Saraf

Thnak you for reading, Shrijal

Collapse
 
mohamed_karim_2dddebb42bd profile image
mohamed karim

Thank for sharing

Collapse
 
pradumnasaraf profile image
Pradumna Saraf

Thank you for reading, Mohamed

Collapse
 
luvgupta014 profile image
Luv Gupta

As I'm learning Docker this helped me alot💖

Collapse
 
pradumnasaraf profile image
Pradumna Saraf

Thnak you for reading, Luv

Collapse
 
ffsales profile image
Felipe Ferreira de Sales

Good job!!!

Collapse
 
pradumnasaraf profile image
Pradumna Saraf

Thnak you for reading, Felipe

Collapse
 
goodevilgenius profile image
Dan Jones

For go, since everything is compiled into the binary, you should consider using distroless images as your base.

They're smaller than Alpine, but should have all the necessary files (like timezone files, and ssl certs) that you should need.

Just change:

FROM alpine:3.20
Enter fullscreen mode Exit fullscreen mode

To:

FROM gcr.io/distroless/static-debian12
Enter fullscreen mode Exit fullscreen mode

That should save you a bit of space on your final image without sacrificing functionality.

You could also, if you really want to strip it down, do FROM scratch, and copy over SSL certs and timezone files from alpine. That should get you what you need for full functionality, and an absolute minimum image size. But, there might be some other OS dependencies that aren't obvious, which is why I usually use distroless, just in case.

Here's another really good approach that also uses FROM scratch.

Collapse
 
frankzonarix profile image
Frank Müller

Thank you for the health-check on the docker-compose. Really helpful!