Method: Cnvrg::Images#create_container

Defined in:
lib/cnvrg/Images.rb

#create_container(port = 7654, is_remote = false) ⇒ Object



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/cnvrg/Images.rb', line 270

def create_container(port=7654, is_remote=false)
  begin
    image_settings = {
        'Image' => "#{@image_name}:latest",
        'User' => 'ds',
        'Cmd' => '/usr/local/cnvrg/run_ipython.sh',
        'WorkingDir' => '/home/ds/notebooks',
        'ExposedPorts' => {
            '8888/tcp' => {},
        },
        'HostConfig' => {
            'Binds' => ["#{@working_dir}:/home/ds/notebooks"],
            'PortBindings' => {
                '8888/tcp' => [
                    {'HostPort' => "#{port}", 'HostIp' => 'localhost'}
                ],
            },
        },
    }
    container = Docker::Container.create(image_settings)
    container.start()
    netrc = File.open(File.expand_path('~')+"/.netrc", "rb")
    netrc_content = netrc.read
    container.store_file("/home/ds/.netrc", netrc_content)
    command = ["/bin/bash", "-lc", "sudo chmod 600 /home/ds/.netrc"]
    p = container.exec(command, tty: true)
    command = ["/bin/bash", "-lc", "sudo chown -R ds /home/ds/.netrc"]
    p = container.exec(command, tty: true)
    config = File.open(File.expand_path('~')+"/.cnvrg/config.yml", "rb")
    config_content = config.read
    container.store_file("/home/ds/.cnvrg/config.yml", config_content)
    command = ["/bin/bash", "-lc", "sudo chown -R ds /home/ds/.cnvrg"]
    container.exec(command, tty: true)
    # Libraries instlled
    save_installed_libraries(container)
    config = {project_name: @project_name,
              project_slug: @project_slug,
              owner: @owner,
              docker: true, image_base: @image_name, image_tag: @image_tag, container: container.id, port: port, image_slug: @image_slug}

    File.open(@working_dir+"/.cnvrg/config.yml", "w+") { |f| f.write config.to_yaml }


    return container


  rescue => e
    if e.message.include? "is not running"
      return create_container(port-1)
    end
    return false
  rescue SignalException

    say "\nAborting", Thor::Shell::Color::RED
    exit(1)
  end


end