33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/docker_task/task.rb', line 33
def define!
namespace ns do
desc 'Perform whole cycle of destroy (if any), build, and run'
task :reset do
invoke_task('destroy')
invoke_task('build')
invoke_task('run')
end
desc 'Rebuild the docker image'
task :rebuild do
invoke_task('destroy')
invoke_task('build')
end
desc 'Build a new docker image based on the Dockerfile'
task :build do
docker_do 'build -t %s .' % image_name
end
desc 'Show help, and how to use this docker tool'
task :help do
puts help_text
end
desc 'Run the latest docker image'
task :run do
run_opts = [ ]
if ENV['INTERACTIVE'] == '1'
run_opts << '--rm -t -i'
else
run_opts = @options[:run].call(self, run_opts)
run_opts << '-d'
run_opts << '--name=%s' % container_name
end
run_opts << '%s:%s' % [ @options[:image_name], @options.fetch(:image_tag, 'latest') ]
if ENV['INTERACTIVE'] == '1'
docker_do 'run %s %s' % [ run_opts.join(' '), '/bin/bash -l' ], :ignore_fail => true
else
docker_do 'run %s' % run_opts.join(' ')
end
end
desc 'Run docker in interactive mode (with bash shell)'
task :runi do
ENV['INTERACTIVE'] = '1'
invoke_task('run')
end
task :bash do
if @options.include?(:bash)
exec_cmd = @options[:bash]
else
exec_cmd = 'bash -l'
end
docker_do('exec -it %s %s' % [ container_name, exec_cmd ], :ignore_fail => true)
end
desc 'Run docker container'
task :start do
docker_do 'start %s' % container_name
end
desc 'Stop docker container'
task :stop do
docker_do 'stop %s; true' % container_name
end
desc 'Restart docker container'
task :restart do
invoke_task('stop')
invoke_task('start')
end
desc 'Delete container, and create a new one'
task :reload do
docker_do 'kill %s; true' % container_name
docker_do 'rm %s; true' % container_name
invoke_task('run')
end
desc 'Push latest built image to repo'
task :push do
if @options[:push_repo]
should_create_tag = false
if !ENV['PUSH_MIRROR'].nil? && !ENV['PUSH_MIRROR'].empty?
mk = ENV['PUSH_MIRROR']
pm = @options[:push_mirrors]
if !pm.nil? && !pm.empty?
push_repo = pm[mk.to_sym]
else
push_repo = nil
end
if push_repo.nil? || push_repo.empty?
fail "Mirror %s not found" % mk
end
else
push_repo = '%s/%s' % [ @options[:registry], @options[:push_repo] ]
end
docker_do 'tag %s %s' % [ @options[:image_name], push_repo ]
docker_do 'push %s' % push_repo
else
puts 'Please specify a push_repo for this docker context'
end
end
desc 'Pull from registry based on push_repo options'
task :pull do
if @options[:push_repo]
pull_repo = '%s/%s' % [ @options[:registry], @options[:push_repo] ]
docker_do 'pull %s' % pull_repo
docker_do 'tag %s %s' % [ pull_repo, @options[:image_name] ]
else
puts 'Please specify a push_repo for this docker context'
end
end
desc 'Re-tag a local copy from latest remote (will not pull)'
task :retag do
if @options[:push_repo]
pull_repo = '%s/%s' % [ @options[:registry], @options[:push_repo] ]
docker_do 'tag %s %s' % [ pull_repo, @options[:image_name] ]
else
puts 'Please specify a push_repo for this docker context'
end
end
desc 'Destroy container and delete image'
task :destroy do
invoke_task('remove')
docker_do 'rmi %s' % image_name, :ignore_fail => true
end
desc 'Stop and remove container'
task 'remove' do
docker_do 'kill %s' % container_name, :ignore_fail => true
docker_do 'rm %s' % container_name, :ignore_fail => true
end
end
end
|