GithubHelp home page GithubHelp logo

isabella232 / go-dockerpty Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gocardless/go-dockerpty

0.0 0.0 0.0 97 KB

Pseudo-tty handler for docker Go client https://github.com/fsouza/go-dockerclient

License: MIT License

Go 100.00%

go-dockerpty's Introduction

go-dockerpty

Provides the functionality needed to operate the pseudo-tty (PTY) allocated to a docker container, using the Go client.

Inspired by https://github.com/d11wtq/dockerpty

Usage

This package provides two functions: dockerpty.Start and dockerpty.StartExec.

The following example will run Busybox in a docker container and place the user at the shell prompt via Go. It is the same as running docker run -ti --rm busybox /bin/sh.

This obviously only works when run in a terminal.

package main

import (
	"fmt"
	"github.com/fgrehm/go-dockerpty"
	"github.com/fsouza/go-dockerclient"
	"os"
)

func main() {
	endpoint := "unix:///var/run/docker.sock"
	client, _ := docker.NewClient(endpoint)

	// Create container
	container, err := client.CreateContainer(docker.CreateContainerOptions{
		Config: &docker.Config{
			Image:        "busybox",
			Cmd:          []string{"/bin/sh"},
			OpenStdin:    true,
			StdinOnce:    true,
			AttachStdin:  true,
			AttachStdout: true,
			AttachStderr: true,
			Tty:          true,
		},
	})

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Cleanup when done
	defer func() {
		client.RemoveContainer(docker.RemoveContainerOptions{
			ID: container.ID,
			Force: true,
		})
	}()

	// Fire up the console
	if err = dockerpty.Start(client, container, &docker.HostConfig{}); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

The following example will run a "date service" on a Busybox docker container and place the user at the shell prompt via Go. It is the same as running docker exec -ti date-service /bin/sh.

This obviously only works when run in a terminal.

package main

import (
	"fmt"
	"github.com/fgrehm/go-dockerpty"
	"github.com/fsouza/go-dockerclient"
	"os"
)

func main() {
	endpoint := "unix:///var/run/docker.sock"
	client, _ := docker.NewClient(endpoint)

	// Create container
	container, err := client.CreateContainer(docker.CreateContainerOptions{
		Name: "date-service",
		Config: &docker.Config{
			Image: "busybox",
			Cmd:   []string{"/bin/sh", "-c", "while true; do date >> /tmp/date.log; sleep 1; done"},
		},
	})

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Cleanup when done
	defer func() {
		client.RemoveContainer(docker.RemoveContainerOptions{
			ID: container.ID,
			Force: true,
		})
	}()

	err = client.StartContainer(container.ID, &docker.HostConfig{})

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	exec, err := client.CreateExec(docker.CreateExecOptions{
		Container:    container.ID,
		AttachStdin:  true,
		AttachStdout: true,
		AttachStderr: true,
		Tty:          true,
		Cmd:          []string{"/bin/sh"},
	})

	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Fire up the console
	if err = dockerpty.StartExec(client, exec); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

When dockerpty.Start or dockerpty.StartExec gets called, control is yielded to the container's PTY until the container exits, or the container's PTY is closed.

This is a safe operation and all resources should be restored back to their original states.

go-dockerpty's People

Contributors

fgrehm avatar hmarr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.