R/docker_run.R

Defines functions docker_run get_containers docker_prune stop_container stop_shiny_container restart_container restart_shiny_container remove_container get_images save_image load_image

docker_run <- function(repository,user_name='rstudio',password='password',container_name=NULL,wait_time=1,loops=5){

  if(is.null(container_name)){
    container_name <- gsub('dockerise/project/','',repository)
  }

  if(container_name%in%get_containers()$NAMES){
    stop('Container with this name already exists and is active.')
    # stop_container(container_name)
    # remove_container(container_name)
  }

  system(
    sprintf("docker run --name %s -p 8787:8787 -e USER=%s -e PASSWORD=%s %s",
         container_name,
         user_name,
         password,
         repository),
    wait=FALSE
  )

  # Checking to make sure that the container is active. With wait set to false we don't know if the container wasn't created
  i=0

  repeat({
    print(get_containers())
    if(!is.null(get_containers())){
      if(container_name%in%get_containers()$NAMES){
        message('Container successfully created')
        browseURL("http://localhost:3838")
        break()
      }
    }

    if(i<loops){
      i=i+1
    }else{
      if(!is.null(get_containers())){
        if(!container_name%in%get_containers()$NAMES){
          stop('Container not created.')
        }
      }else{
        stop('Container not created, most likely because an inactive container with that name already exists. Please either use a different name or remove the other container')
      }
    }
    Sys.sleep(wait_time)
  })

}

get_containers <- function(){
  raw <- system("docker container ls -a",intern=TRUE)
  names0 <- gsub('^ ','',strsplit(raw[1],'  ')[[1]])
  names <- names0[names0!='']
  all <- raw[-1]
  out <- do.call(
    rbind,lapply(all,function(x){
    splitOutRaw <- gsub('^ ','',strsplit(x,'  ')[[1]])
    splitOut <- splitOutRaw[splitOutRaw!='']
    data.frame(t(splitOut))
  })
  )
  if(!is.null(out)){
  colnames(out) <- names
  return(out)
  }else{
    return(out)
  }
}

docker_prune <- function(){
  system("docker system prune -f")
}

stop_container <- function(container_name){
  containers <- get_containers()
  containerID <- containers[containers$NAMES==container_name,1]
  system(sprintf("docker exec -t %s sudo rstudio-server suspend-all",containerID))
  system(sprintf("docker stop %s",container_name),wait=FALSE)
}

stop_shiny_container <- function(container_name){
  containers <- get_containers()
  containerID <- containers[containers$NAMES==container_name,1]
  system(sprintf("docker stop %s",container_name),wait=FALSE)
}

restart_container <- function(container_name){
  system(sprintf("docker start %s",container_name))
  containers <- get_containers()
  containerID <- containers[containers$NAMES==container_name,1]
  system(sprintf("docker attach %s",container_name),wait = FALSE)
  browseURL("http://localhost:8787")
}

restart_shiny_container <- function(container_name){
  system(sprintf("docker start %s",container_name))
  containers <- get_containers()
  containerID <- containers[containers$NAMES==container_name,1]
  system(sprintf("docker attach %s",container_name),wait = FALSE)
  browseURL("http://localhost:80")
}

remove_container <- function(container_name){
  containers <- get_containers()
  system(sprintf("docker container rm %s",container_name))
}

get_images <- function(){
  raw <- system("docker image ls",intern=TRUE)
  names0 <- gsub('^ ','',strsplit(raw[1],'  ')[[1]])
  names <- names0[names0!='']
  all <- raw[-1]
  out <- do.call(
    rbind,lapply(all,function(x){
      splitOutRaw <- gsub('^ ','',strsplit(x,'  ')[[1]])
      splitOut <- splitOutRaw[splitOutRaw!='']
      data.frame(t(splitOut))
    })
  )
  colnames(out) <- names
  out
}

save_image <- function(repository,save_location=getwd()){
  image_name <- gsub('/','_',gsub('dockerise/app/|dockerise/project','',repository))
  save_command <- sprintf("docker image save -o \"%s/%s.tar\" %s",save_location,image_name,repository)
  print(save_command)
  system(save_command)
}

load_image <- function(file_location){
  system(sprintf("docker load -i \"%s\"",file_location))
}

save_image('dockerise/app/docker_test')
statisticiansix/dockerise documentation built on Nov. 5, 2019, 9:20 a.m.