Reputation: 53993
I've built a Dockerfile
which ends with compiling my (Golang) code and then running the compiled binary:
# Compile my Go code
RUN go build -o bin *.go
# Run the binary
CMD /root/src/whisky/bin
After building the image (docker build -t whisky .
) I run the image (docker run --name whisky whisky_image
) and the program starts to give output in the terminal.
When I run the program from my laptop I can always stop it using CTRL+C
, but now it's running inside the container and hitting CTRL+C
doesn't do anything. The container simply keeps giving output and nothing stops.
I can still stop the container from another terminal docker stop whisky
, but I guess there should be a nicer way.
Does anybody know the recommended way to stop the command in the container or stop the whole container from the same terminal?
[EDIT]
From the answers given I learned I can run the container in detached mode using -d
and then tail the container logs using docker logs container_id --follow
. Although that works I prefer to run the container directly (in non-detached mode) because then I can't make the mistake of running it in the background and forgetting about it.
Does anybody know how I can run the container in a way that I can stop it from the same terminal by hitting CTRL+C
?
Upvotes: 5
Views: 3147
Reputation: 999
Stopping a docker container using command prompt
You can stop the container using the following command prompt
sudo docker stop <Container id>
After stopping the container you can check the information of all container
sudo docker ps -a
This will show list of the containers, find your container and see there will be written exit status which mean your container is stopped
Upvotes: 0
Reputation: 454
As mentionned ealier, there is 2 possibilities, the first, detaching the process (-d
option) or use a TTY with Interactive mode (-ti
option).
The detached version will allow you to work and manage the instance, but won't let you down it by pressing CTRL+C
The TTY version with an Interactive mode (-i
) will.
So, docker run -it
should do the trick
Docker Run reference and Docker Container process behaviors can also help
Upvotes: 1
Reputation: 13104
FROM golang
WORKDIR /go/src/app
ADD . .
# Compile my Go code
RUN go build -o bin *.go
# Run the binary
CMD /go/src/app/bin
package main
import (
"fmt"
"time"
)
func main() {
for {
fmt.Println("\nWHISKY TIME: ", time.Now().String())
time.Sleep(time.Second)
}
}
╭─exadra37@exadra37-Vostro-470 ~/Developer/DevNull/stackoverflow
╰─➤ sudo docker build -t whisky .
Sending build context to Docker daemon 3.072kB
Step 1/5 : FROM golang
---> c4d6fdf2260a
Step 2/5 : WORKDIR /go/src/app
---> Using cache
---> ab35c58a3608
Step 3/5 : ADD . .
---> 320a51d3e579
Step 4/5 : RUN go build -o bin *.go
---> Running in 5db9f4a9cbe1
Removing intermediate container 5db9f4a9cbe1
---> e16b272606da
Step 5/5 : CMD /go/src/app/bin
---> Running in f66039aeb53a
Removing intermediate container f66039aeb53a
---> d453f9e10c49
Successfully built d453f9e10c49
Successfully tagged whisky:latest
╭─exadra37@exadra37-Vostro-470 ~/Developer/DevNull/stackoverflow
╰─➤ sudo docker run -it --name whisky whisky 137 ↵
WHISKY TIME: 2019-11-25 16:19:26.397705029 +0000 UTC m=+0.000041700
WHISKY TIME: 2019-11-25 16:19:27.399189969 +0000 UTC m=+1.001526690
WHISKY TIME: 2019-11-25 16:19:28.399392173 +0000 UTC m=+2.001728894
WHISKY TIME: 2019-11-25 16:19:29.399566682 +0000 UTC m=+3.001903403
^C%
As you can see I was able o stop it with CTRL + C
, and I just needed to use the flags -it
.
The flags used:
╭─exadra37@exadra37-Vostro-470 ~/Developer/DevNull/stackoverflow
╰─➤ docker run --help | grep "\-t, \-\-tty" - 2 ↵
-t, --tty Allocate a pseudo-TTY
╭─exadra37@exadra37-Vostro-470 ~/Developer/DevNull/stackoverflow
╰─➤ docker run --help | grep "\-i, \-\-interactive" -
-i, --interactive Keep STDIN open even if not attached
╭─exadra37@exadra37-Vostro-470 ~/Developer/DevNull/stackoverflow
╰─➤ sudo docker start -i whisky
WHISKY TIME: 2019-11-25 16:21:03.159420042 +0000 UTC m=+0.000047654
WHISKY TIME: 2019-11-25 16:21:04.161264182 +0000 UTC m=+1.001891819
WHISKY TIME: 2019-11-25 16:21:05.161470301 +0000 UTC m=+2.002097939
^C%
When restarting the container we just need to use the flag -i
, and you are still able to stop it with CTRL + C
.
The flags used:
╭─exadra37@exadra37-Vostro-470 ~/Developer/DevNull/stackoverflow
╰─➤ docker start --help | grep "\-i, \-\-interactive" -
-i, --interactive Attach container's STDIN
Upvotes: 1
Reputation: 18608
you need to run your container using init
docker run --init --name whisky whisky_image
then kill it using Ctrl+C
the init
will start your program as PID 1
Upvotes: 1
Reputation: 429
Try this:
# Compile my Go code
RUN go build -o bin *.go
# Run the binary
CMD ["/root/src/whisky/bin"]
This is the exec form. in this form your executable is being called directly. What you used is the shell form which starts your process with /bin/sh -c. This form does not automatically pass signals to the child processes.
Upvotes: 0
Reputation: 28716
Allocate a pseudo-tty (-t
) and then you can control output by hitting CTRL+C
:
$ docker run --name whisky -t whisky_image
^C
But container will be still running, so add also stdin (-i
):
$ docker run --name whisky -ti whisky_image
^C
But container will be still there (see docker ps -a
- in exited status), so you may add cleanup flag --rm
:
$ docker run --name whisky --rm -ti whisky_image
^C
See Docker run doc for more details.
Upvotes: 2
Reputation: 947
To stop the whole container please use below commands.
$ docker stop <container>
The docker stop command stops running Docker containers. It sends the SIGTERM signal to the running process inside the docker container and requests it to terminate.
Upvotes: -1
Reputation: 71
you can use '-d' --> in detached mode
[EDIT] To add. After that you can tail to logs using
docker logs container_id --follow
Upvotes: 2