R/docker_run_app.R

Defines functions docker_run_app

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

  if(is.null(container_name)){
    container_name <- gsub('dockerise/app/','',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 80:80 %s",
         container_name,
         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:80")
        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",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))
# }
#
# 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")
# }
#
# 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
# }
statisticiansix/dockerise documentation built on Nov. 5, 2019, 9:20 a.m.