Skip to content

Instantly share code, notes, and snippets.

@ashton314
Created August 22, 2019 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashton314/430c76a2cccfd0f518490f46bc5d068a to your computer and use it in GitHub Desktop.
Save ashton314/430c76a2cccfd0f518490f46bc5d068a to your computer and use it in GitHub Desktop.
Custom mix task for development environments to start/stop a Postgres database in a Docker container
defmodule Mix.Tasks.Docker.Db do
use Mix.Task
@project_name "Program Builder"
@project_sname "program_builder"
@shortdoc "Start/stop the docker Postgres container for development"
@moduledoc """
Fire up a postgres database for #{@project_name}, named #{@project_sname} in Docker.
## Usage:
$ mix docker.db start
$ mix docker.db stop
"""
@impl Mix.Task
def run(["start"]) do
Mix.shell().info("Starting container named #{@project_sname}...")
volume = "#{@project_sname}_volume"
System.cmd("docker", [
"run",
"--rm",
"-d",
"-e",
"POSTGRES_PASSWORD=postgres",
"-p",
"5432:5432",
"--mount",
"type=volume,src=#{volume},dst=/var/lib/postgresql",
"--name",
@project_sname,
"postgres"
])
Mix.shell().info("done.")
end
@impl Mix.Task
def run(["stop"]) do
System.cmd("docker", ["stop", @project_sname])
Mix.shell().info("Container #{@project_sname} stopped")
end
end
@ashton314
Copy link
Author

Stuff this in a file at lib/mix/tasks/docker.db.exs, recompile with mix compile and you should be good to go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment