Created
August 22, 2019 02:51
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stuff this in a file at
lib/mix/tasks/docker.db.exs
, recompile withmix compile
and you should be good to go!