Skip to content

Commit

Permalink
initial commit of script plus documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiah committed Feb 16, 2019
1 parent 21a84fb commit a5b0a88
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
67 changes: 66 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,67 @@
# dnuke
Ruby script for conveniently nuking Docker containers and volumes by project name
Ruby script for conveniently nuking Docker containers and volumes by name

### Usage
Pass the name(s) of your containers / volumes to dnuke:

`dnuke project1`

Your output should look something like this:

```bash
$ dnuke project1

I, the mighty dnuke, was passed: project1

removing containers that match: project1
container a0123456789b was removed
container b9876543210a was removed

removing volumes that match: project1
volume project1_postgres was removed

--------------------------------------------------------------------
```

You can pass multiple container / volume names as arguments:

```bash
$ dnuke project1 project2

I, the mighty dnuke, was passed: project1

removing containers that match: project1
container a0123456789b was removed
container b9876543210a was removed

removing volumes that match: project1
volume project1_postgres was removed

--------------------------------------------------------------------

I, the mighty dnuke, was passed: project2

removing containers that match: project2
container b9876543210a was removed
container a0123456789b was removed

removing volumes that match: project2
volume project2_postgres was removed

--------------------------------------------------------------------
```

dnuke uses fuzzy matching when searching your local Docker containers and volumes:

```bash
$ dnuke cat

I, the mighty dnuke, was passed: cat

...

removing volumes that match: cat
volume caterpillar_postgres was removed

--------------------------------------------------------------------
```
55 changes: 55 additions & 0 deletions dnuke
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env ruby

# TODO: make sure no matching containers are
# running before attempting removal
def remove_containers(args)
puts "\n"
containers = `docker ps -aqf name=#{args}` # String
containers = containers.split(/\n/) # Array
unless containers.empty?
puts "removing containers that match: #{args}"
# TODO: remove false message of "container x was removed"
# when container x wasn't actually removed
# e.g. if the container is still running
containers.map do |c|
`docker container rm #{c}`
puts "container #{c} was removed"
end
else
puts "no containers removed: no containers match #{args}"
end
end

def remove_volumes(args)
puts "\n"
volumes = `docker volume ls` # String
volumes = volumes.squeeze(' ').chop
volumes = volumes.split(/\nlocal /) # Array
volumes = volumes.map do |v|
if v.match(args)
v
end
end
volumes = volumes.compact # remove any lingering nil values
unless volumes.empty?
puts "removing volumes that match: #{args}"
volumes.map do |v|
`docker volume rm #{v}`
puts "volume #{v} was removed"
end
else
puts "no volumes removed: no volumes match #{args}"
end
end

unless ARGV.length == 0
ARGV.each do |value|
puts "\n"
puts "I, the mighty " + File.basename(__FILE__) + ", was passed: " + value
remove_containers(value)
remove_volumes(value)
puts "\n--------------------------------------------------------------------"
end
else
puts "Please pass an argument"
end

0 comments on commit a5b0a88

Please sign in to comment.