Docker for local Dev

Andrew Allison
3 min readDec 20, 2023

--

I’m dumping this here more for reference than anything as I’ve been asked several times for info on how I run local envs. I tend to run as much as possible in docker containers. I don’t profess to be a docker expert and I’m open to suggestions on how I can improve this workflow but for me, it’s worked fine for a good few years now. Having come from an era where getting a local dev env up and running either meant bare metal installs or some dodgy Xamp stack, docker hit home as being an ideal solution for me and my local dev workflow.

In this article, I will demonstrate a couple of different examples of using docker to allow you some local development options for databases and other services.

Prerequisites

You are going to need a local instance of docker up and running. Currently, I’m using Docker Desktop on both my Windows box and my Mac but I understand there are probably alternative solutions for this and reading up on https://blog.logrocket.com/docker-desktop-alternatives/ is on my todo list. For now, I’m just going to assume that you managed to win whatever install/permissions/licencing-based battle you had and have a working docker

Docker Compose

Below are a few docker-compose files showing different databases and configs. Some of the things to note are the images and versions. This may seem obvious but there may often be situations where you need to use different versions of MySQL or Postgress, for example.

The env_file file is something I tend to rely on heavily. This way I don’t have to put my passwords and user names int the compose file. This allows the compose file to be committed. I then tend to place an example .env.<platform> file in the folder so the user can easily create their own.

“Volumes” is also a significant property/option as this allows for the container to keep data even after it’s been killed or restarted. The volume alias also means it can be shared if needed.

Examples

An example of a simple MySQL docker-compose file

version: '3.7'
services:
mysql-db:
image: mysql:8.0.33
restart: always
env_file:
- .env.mysql
ports:
- '3308:3306'
volumes:
- mysql-db-data:/var/lib/mysql
networks:
- mysql-server

Postgress example

version: '3.7'
services:
postgres:
image: postgres:latest
container_name: my-postgres-container
env_file:
- .env.postgres
ports:
- "5432:5432"
volumes:
- postgress-db-data:/var/lib/mysql
networks:
- postgress-server

A full-on example which uses a dockerfile for building and running a NestJS server with redis, mysql and a seq logging server.

version: '3.7'
services:
basic-server-docker:
env_file:
- .env
build:
context: .
dockerfile: Dockerfile
volumes:
- ./:/usr/app
container_name: basic-server
expose:
- "6008"
ports:
- "6008:6006"
mysql-db:
image: mysql:8.0.33
restart: always
env_file:
- .env.mysql
ports:
- '3307:3306'
expose:
- '3307'
volumes:
- basic-server-db-data:/var/lib/mysql
networks:
- basic-server
seq:
environment:
- ACCEPT_EULA=Y
volumes:
- seq:/data
ports:
- '8080:80' # exposes ui ports
- '5341:5341' # handles ingestion http end-point
image: 'datalust/seq:latest'
cache:
image: redis:7.2.2-alpine
restart: always
ports:
- '6379:6379'
command: redis-server --save 20 1 --loglevel warning --requirepass eYVX7EwVmmxKPCDmwMtyKVge8oLd2t81
volumes:
- cache:/data
volumes:
basic-server-data:
basic-server-db-data:
seq:
cache:
driver: local
networks:
basic-server:
cache:
driver: local

Example .env.mysql

MYSQL_DATABASE=""
MYSQL_USER="sqluser123"
MYSQL_PASSWORD="^Y"
MYSQL_ROOT_PASSWORD="^"

Round up

I did this document more as a reminder and as something I could point people at when they ask about my local dev environments. It's things that have helped devs get up and running when they just want a quick local env setting up. If you have thoughts or experiences with how to do similar drop them in the comments.

--

--

Andrew Allison

15 plus years of professionally putting characters in places that produces great software systems. A life time of hacking on computers.