Class: Docker::Cli::Operations::Run

Inherits:
Object
  • Object
show all
Defined in:
lib/docker/cli/operations/run.rb

Overview

Normal run by finding Dockerfile By default is interactive

Instance Method Summary collapse

Constructor Details

#initializeRun

Returns a new instance of Run.



14
15
16
# File 'lib/docker/cli/operations/run.rb', line 14

def initialize
  @pmt = TTY::Prompt.new
end

Instance Method Details

#build_image_from_dockerfile(df, dfName) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/docker/cli/operations/run.rb', line 155

def build_image_from_dockerfile(df, dfName)

  iName = File.basename(Dir.getwd)
  name = @pmt.ask(" Please provide an image name for Dockerfile '#{dfName}' : ", required: true, default: "#{iName}_image") 
  ctx = @pmt.ask(" Context to run to the dockerfile? ", required: true, default: ".")

  DockerImage.build(name, dfName ,ctx)
  DockerRunLog.instance.log_dockerfile_image(df, name)

  name

end

#runObject

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/docker/cli/operations/run.rb', line 18

def run
 
  # find dockerfile to build an image
  df = Dockerfile.find_available
  raise NoDockerfileFound, "No Dockerfile found. Please create one and re-run this operation" if df.length == 0

  if df.length > 1
    @selDf = @pmt.select(" Please select one of the Dockerfile to execute : ") do |m|
      df.each do |f|
        m.choice f, f
      end
    end
  else
    @selDf = df.first
  end

  @mount_root = @pmt.ask(" Please provide a root for folder mount into Docker env : ", default: "/opt", required: true)

  df = Dockerfile.new(@selDf)
  # parameter pass in is to build the template
  # e.g. docker_root is to build the dup_gem_bundler_env directive
  ndf = df.render_dockerfile(docker_root: @mount_root)

  @dev_gems = {}
  devGems = Cli.find_dev_gems
  if devGems.length > 0
    devGems.each do |k,v|
      @dev_gems[v] = File.join(@mount_root, File.basename(v))
    end
  end

  dfName = @pmt.ask(" Please provide the new name for the generated Dockerfile : ", required: true, default: "Dockerfile.docli")
  dfPath = File.join(Dir.getwd, dfName)
  File.open(dfPath,"w") do |f|
    f.write ndf
  end

  if Dockerfile.run_before?(dfPath)
    proceed = @pmt.yes?(" Given dockerfile '#{@selDf}' seems already run before. Do you want to use existing image instead? ")
    if proceed
      img = Dockerfile.images(dfPath)
      if img.length > 1
        @selImg = @pmt.select(" There are multiple images being run from the same Dockerfile. Please select one of them : ") do |m|
          img.each do |i|
            m.choice i, i
          end
        end
      elsif img.length > 0
        @selImg = img.first
      end

    else
      # not using existing image.. built one
      #@df, @selImg = build_image_from_dockerfile_bin(ndf)
      @selImg = build_image_from_dockerfile(dfPath, dfName)
    end

  else
    #logger.debug "Dockerfile not being run before"
    #@df, @selImg = build_image_from_dockerfile_bin(ndf)  # => 
    @selImg = build_image_from_dockerfile(dfPath, dfName)
  end

  logger.debug "selected image : #{@selImg}"

  di = DockerImage.new(@selImg)
  if di.has_containers?
    if di.containers.length > 1
      @selCont = @pmt.select(" Please select one of the container to run : ") do |m|

        di.containers.each do |c|
          m.choice c.name_for_display, c
        end

        m.choice "New container", :new

      end

    else
      @selCont = di.containers.first
    end

  end # has_containers?


  case @selCont
  when :new, nil
    @selCont = @pmt.ask(" Please provide a new container name : ", required: true, default: "#{File.basename(Dir.getwd)}_#{SecureRandom.hex(4)}")

    cp = ContainerProfile.new 
    if devGems.length > 0
      devGems.each do |name, path|
        cp.add_mount_point(path, File.join(@mount_root, name))
      end
    end

    # add current dir as mount point
    cp.add_mount_point(Dir.getwd, File.join(@mount_root, File.basename(Dir.getwd)))

    while true
      STDOUT.puts "\n\n Mount points : \n"
      ctn = 1
      cp.mount_points.each do |local, docker|
        STDOUT.puts " #{ctn}. #{local} ==> #{docker}"
      end
      apath = @pmt.ask("\n Please provide full path to mount in the docker (just enter if done) : ")
      break if is_empty?(apath)

      if File.exist?(apath)
        mpath = @pmt.ask(" Please provide mount point inside the docker : ", default: File.dirname(apath))
        cp.add_mount_point(mpath, File.join(@mount_root, File.basename(mpath)))
      else
        STDERR.puts "Given path '#{apath}' doesn't exist. Please try again."
      end
    end

    cmd = @pmt.ask(" What command should be inside the docker? : ", required: true, default: "/bin/bash" )
    cp.run_command = cmd
    cp.image_name = @selImg

    dc = DockerContainer.new(@selCont)
    dc.create(cp)
    DockerRunLog.instance.log(@selImg, @selCont)

  else

    #cmd = @pmt.ask(" What command should be inside the docker? : ", required: true, default: "/bin/bash" )
    dc = DockerContainer.new(@selCont.name)
    dc.start
    dc.attach
    #dc.run(cmd)

  end


end