Module: Dockerun::CommandHelper::DockerContainerHelper

Includes:
BundlerHelper, DockerCommandFactoryHelper
Included in:
Dockerun::Command::Run, Dockerun::Command::RunNewContainer, Dockerun::Command::RunNewImage
Defined in:
lib/dockerun/docker_container_helper.rb

Defined Under Namespace

Classes: DockerContainerBuildFailed, DockerContainerStartFailed

Instance Method Summary collapse

Methods included from BundlerHelper

#find_local_dev_gems

Methods included from DockerCommandFactoryHelper

#dcFact

Instance Method Details

#run_docker_container(image_name, container_name, mount_points = [], &block) ⇒ Object



14
15
16
17
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
# File 'lib/dockerun/docker_container_helper.rb', line 14

def run_docker_container(image_name, container_name, mount_points = [], &block)
     
  raise DockerContainerBuildFailed, "block is required" if not block
  raise DockerContainerBuildFailed, "Image name is required" if is_empty?(image_name)

  reuse = nil

  if is_empty?(container_name)
    container_name = block.call(:new_container_name)
    loop do
      st, _ = is_container_exist?(container_name)
      if st
        reuse = block.call(:container_name_exist, container_name)
        break if reuse
        container_name = block.call(:new_container_name)
      else
        break
      end
    end

  else
    st, _ = is_container_exist?(container_name)
    if st
      reuse = true
    else
      # if not found shall drop into the next block's else clause 
    end
  end

  if reuse == true
  
    # 
    # container already exist so no configuration is required
    #
    res = dcFact.find_running_container(container_name).run
    if not res.failed? and res.is_out_stream_empty?
      # not running
      ress = dcFact.start_container(container_name).run
      raise DockerContainerStartFailed, "Failed to start container '#{container_name}'. Error was : #{ress.err_stream}" if ress.failed?
    end

    ucmd = cli.ask("Command to be run inside the container. Empty to attach to existing session : ", value: "/bin/bash")
    if is_empty?(ucmd)
      dcFact.attach_container(container_name).run
    else
      dcFact.run_command_in_running_container(container_name, ucmd, tty: true, interactive: true).run
    end

  else

    #@workspace_root = "/opt"
    #@shared_dirs = {}

    #mount = []
    #sharedInsideDocker = []
    #res = find_local_dev_gems
    #puts "Found #{res.length} local gems #{res}"
    #if not res.empty?
    #  
    #  transferMapping = block.call(:transfer_dev_gem_mapping?)
    #  if transferMapping
    #    res.each do |name, path|
    #      tsrc = block.call(:workspace_root_inside_docker, @workspace_root, name, path)
    #      inPath = File.join(tsrc, name)
    #      mount << { path => inPath }
    #      @shared_dirs[name] = inPath 
    #    end
    #  end

    #end

    #mapProjectDir = block.call(:map_project_dir, @workspace_root)
    #if not_empty?(mapProjectDir)
    #  mount << { Dir.getwd => mapProjectDir }
    #end

    #reqVolMap = block.call(:volume_mapping_required?)
    #if reqVolMap

    #  loop do
    #    
    #    block.call(:already_mapped, mount)

    #    src = block.call(:source_prompt)
    #    dest = block.call(:destination_prompt, src)
    #    mount << { src => dest }
    #    
    #    add_to_bundle = block.call(:add_to_bundle?, dest)
    #    if add_to_bundle
    #      @shared_dirs[File.basename(dest)] = dest
    #    end

    #    block.call(:add_mount_to_container, container_name, mount.last)
    #    repeat = block.call(:add_more_volume_mapping?)
    #    break if not repeat

    #  end

    #end


    #insideDockerConfig = File.join(File.dirname(__FILE__),"..","..","template","setup_ruby_devenv.rb.erb")
    #if File.exist?(insideDockerConfig)
    #  
    #  @docker_init_file_path = File.join(Dir.getwd,"on_docker_config")

    #  cont = File.read(insideDockerConfig)
    #  
    #  b = binding

    #  res = ERB.new(cont)
    #  out = res.result(b)

    #  # fixed this name to be used inside Dockerfile 
    #  File.open(@docker_init_file_path, "w") do |f|
    #    f.write out
    #  end

    #  block.call(:on_docker_init_file_path,@docker_init_file_path) 

    #end



    dcFact.create_container_from_image(image_name, interactive: true, tty: true, container_name: container_name, mount: mount_points).run

  end

  container_name

end