mirror of
https://github.com/containers/podman.git
synced 2025-08-06 03:19:52 +08:00
kpod attach
Attach to a running container Signed-off-by: baude <bbaude@redhat.com> Closes: #95 Approved by: rhatdan
This commit is contained in:
@ -34,7 +34,7 @@ libpod is currently in active development.
|
||||
| Command | Description | Demo|
|
||||
| ---------------------------------------------------- | --------------------------------------------------------------------------|-----|
|
||||
| [kpod(1)](/docs/kpod.1.md) | Simple management tool for pods and images ||
|
||||
| [kpod-attach(1)](/docs/kpod-attach.1.md) | Instead of providing a `kpod attach` command, the man page `kpod-attach` describes how to use the `kpod logs` and `kpod exec` commands to achieve the same goals as `kpod attach`.||
|
||||
| [kpod-attach(1)](/docs/kpod-attach.1.md) | Attach to a running container.
|
||||
| [kpod-cp(1)](/docs/kpod-cp.1.md) | Instead of providing a `kpod cp` command, the man page `kpod-cp` describes how to use the `kpod mount` command to have even more flexibility and functionality.||
|
||||
| [kpod-diff(1)](/docs/kpod-diff.1.md) | Inspect changes on a container or image's filesystem |[](https://asciinema.org/a/FXfWB9CKYFwYM4EfqW3NSZy1G)|
|
||||
| [kpod-export(1)](/docs/kpod-export.1.md) | Export container's filesystem contents as a tar archive |[](https://asciinema.org/a/913lBIRAg5hK8asyIhhkQVLtV)|
|
||||
|
86
cmd/kpod/attach.go
Normal file
86
cmd/kpod/attach.go
Normal file
@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/projectatomic/libpod/libpod"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var (
|
||||
attachFlags = []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "detach-keys",
|
||||
Usage: "Override the key sequence for detaching a container. Format is a single character [a-Z] or ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _.",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "no-stdin",
|
||||
Usage: "Do not attach STDIN. The default is false.",
|
||||
},
|
||||
}
|
||||
attachDescription = "The kpod attach command allows you to attach to a running container using the container's ID or name, either to view its ongoing output or to control it interactively."
|
||||
attachCommand = cli.Command{
|
||||
Name: "attach",
|
||||
Usage: "Attach to a running container",
|
||||
Description: attachDescription,
|
||||
Flags: attachFlags,
|
||||
Action: attachCmd,
|
||||
ArgsUsage: "",
|
||||
}
|
||||
)
|
||||
|
||||
func attachCmd(c *cli.Context) error {
|
||||
args := c.Args()
|
||||
if err := validateFlags(c, attachFlags); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(c.Args()) < 1 || len(c.Args()) > 1 {
|
||||
return errors.Errorf("attach requires the name or id of one running container")
|
||||
}
|
||||
|
||||
runtime, err := getRuntime(c)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "error creating libpod runtime")
|
||||
}
|
||||
defer runtime.Shutdown(false)
|
||||
|
||||
ctr, err := runtime.LookupContainer(args[0])
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to exec into %s", args[0])
|
||||
}
|
||||
|
||||
conState, err := ctr.State()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to determine state of %s", args[0])
|
||||
}
|
||||
if conState != libpod.ContainerStateRunning {
|
||||
return errors.Errorf("you can only attach to running containers")
|
||||
}
|
||||
// Create a bool channel to track that the console socket attach
|
||||
// is successful.
|
||||
attached := make(chan bool)
|
||||
// Create a waitgroup so we can sync and wait for all goroutines
|
||||
// to finish before exiting main
|
||||
var wg sync.WaitGroup
|
||||
|
||||
// We increment the wg counter because we need to do the attach
|
||||
wg.Add(1)
|
||||
// Attach to the running container
|
||||
go func() {
|
||||
logrus.Debug("trying to attach to the container %s", ctr.ID())
|
||||
defer wg.Done()
|
||||
if err := ctr.Attach(c.Bool("no-stdin"), c.String("detach-keys"), attached); err != nil {
|
||||
logrus.Errorf("unable to attach to container %s: %q", ctr.ID(), err)
|
||||
}
|
||||
}()
|
||||
if !<-attached {
|
||||
return errors.Errorf("unable to attach to container %s", ctr.ID())
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
return nil
|
||||
}
|
@ -34,6 +34,7 @@ func main() {
|
||||
app.Version = v
|
||||
|
||||
app.Commands = []cli.Command{
|
||||
attachCommand,
|
||||
createCommand,
|
||||
diffCommand,
|
||||
exportCommand,
|
||||
|
@ -653,6 +653,17 @@ __kpod_images() {
|
||||
__kpod_q images $images_args | awk "$awk_script" | grep -v '<none>$'
|
||||
}
|
||||
|
||||
_kpod_attach() {
|
||||
local options_with_args="
|
||||
--detach-keys
|
||||
"
|
||||
local boolean_options="
|
||||
--help
|
||||
-h
|
||||
--no-stdin"
|
||||
_complete_ "$options_with_args" "$boolean_options"
|
||||
}
|
||||
|
||||
_kpod_diff() {
|
||||
local options_with_args="
|
||||
--format
|
||||
@ -1443,6 +1454,7 @@ _kpod_kpod() {
|
||||
--version -v
|
||||
"
|
||||
commands="
|
||||
attach
|
||||
create
|
||||
diff
|
||||
export
|
||||
|
@ -1,31 +1,40 @@
|
||||
% kpod(1) kpod-attach - See the output of pid 1 of a container or enter the container
|
||||
% Dan Walsh
|
||||
# kpod-attach "1" "September 2017" "kpod"
|
||||
# kpod-attach "1" "December 2017" "kpod"
|
||||
|
||||
## NAME
|
||||
kpod-attach - Attach to a running container
|
||||
|
||||
## Description
|
||||
## SYNOPSIS
|
||||
**kpod attach [OPTIONS] CONTAINER**
|
||||
|
||||
We chose not to implement the `attach` feature in `kpod` even though the upstream Docker
|
||||
project has it. The upstream project has had lots of issues with attaching to running
|
||||
processes that we did not want to replicate. The `kpod exec` and `kpod log` commands
|
||||
offer you the same functionality far more dependably.
|
||||
## DESCRIPTION
|
||||
The attach command allows you to attach to a running container using the container's ID
|
||||
or name, either to view its ongoing output or to control it interactively.
|
||||
|
||||
**Reasons to attach to the primary PID of a container:**
|
||||
You can detach from the container (and leave it running) using a configurable key sequence. The default
|
||||
sequence is CTRL-p CTRL-q. You configure the key sequence using the --detach-keys option
|
||||
|
||||
## OPTIONS
|
||||
**--detach-keys**
|
||||
Override the key sequence for detaching a container. Format is a single character [a-Z] or
|
||||
ctrl-<value> where <value> is one of: a-z, @, ^, [, , or _.
|
||||
|
||||
1) Executing commands inside of the container
|
||||
**--no-stdin**
|
||||
Do not attach STDIN. The default is false.
|
||||
|
||||
We recommend that you use `kpod exec` to execute a command within a container
|
||||
|
||||
`kpod exec CONTAINERID /bin/sh`
|
||||
|
||||
2) Viewing the output stream of the primary process in the container
|
||||
|
||||
We recommend that you use `kpod logs` to see the output from the container
|
||||
|
||||
`kpod logs CONTAINERID`
|
||||
## EXAMPLES ##
|
||||
|
||||
```
|
||||
kpod attach foobar
|
||||
[root@localhost /]#
|
||||
```
|
||||
```
|
||||
kpod attach 1234
|
||||
[root@localhost /]#
|
||||
```
|
||||
```
|
||||
kpod attach --no-stdin foobar
|
||||
```
|
||||
## SEE ALSO
|
||||
kpod(1), kpod-exec(1), kpod-logs(1)
|
||||
kpod(1), kpod-exec(1), kpod-run(1)
|
||||
|
@ -52,6 +52,9 @@ has the capability to debug pods/images created by crio.
|
||||
|
||||
## COMMANDS
|
||||
|
||||
### attach
|
||||
Attach to a running container
|
||||
|
||||
### create
|
||||
create a new container
|
||||
|
||||
|
32
test/kpod_attach.bats
Normal file
32
test/kpod_attach.bats
Normal file
@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load helpers
|
||||
|
||||
function teardown() {
|
||||
cleanup_test
|
||||
}
|
||||
|
||||
function setup() {
|
||||
copy_images
|
||||
}
|
||||
|
||||
@test "attach to a bogus container" {
|
||||
run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} attach foobar"
|
||||
echo "$output"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "attach to non-running container" {
|
||||
${KPOD_BINARY} ${KPOD_OPTIONS} create --name foobar -d -i ${ALPINE} ls
|
||||
run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} attach foobar"
|
||||
echo "$output"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "attach to multiple containers" {
|
||||
${KPOD_BINARY} ${KPOD_OPTIONS} run --name foobar1 -d -i ${ALPINE} /bin/sh
|
||||
${KPOD_BINARY} ${KPOD_OPTIONS} run --name foobar2 -d -i ${ALPINE} /bin/sh
|
||||
run bash -c "${KPOD_BINARY} ${KPOD_OPTIONS} attach foobar1 foobar2"
|
||||
echo "$output"
|
||||
[ "$status" -eq 1 ]
|
||||
}
|
Reference in New Issue
Block a user