andy
andy

Reputation: 2161

How to see the docker state instead of interacting with the shell?

I use following command to build web server

docker run --name webapp -p 8080:4000 mypyweb

When it stopped and I want to restart, I always use:

sudo docker start webapp && sudo docker exec -it webapp bash 

But I can't see the server state as the first time:

Digest: sha256:e61b45be29f72fb119ec9f10ca660c3c54c6748cb0e02a412119fae3c8364ecd

Status: Downloaded newer image for ericgoebelbecker/stackify-tutorial:1.00

* Running on http://0.0.0.0:4000/ (Press CTRL+C to quit)

How can I see the state instead of interacting with the shell?

Upvotes: 0

Views: 172

Answers (3)

David Maze
David Maze

Reputation: 158714

If you think your container's misbehaving, it's often not wrong to just delete it and create a new one.

docker rm webapp
docker run --name webapp -p 8080:4000 mypyweb

Containers occasionally have more involved startup sequences and these can assume they're generally starting from a clean slate. It should also be extremely routine to delete and recreate a container; it's required for some basic tasks like upgrading the image underneath a container to a newer version or changing published ports or environment variables.

docker exec probably shouldn't be part of your core workflow, any more than you'd open a shell to interact with your Web browser. I generally don't tend to docker stop containers, except to immediately docker rm them.

Upvotes: 1

Z4-tier
Z4-tier

Reputation: 7978

Using docker exec is causing the shell to attach to the container. If you are comparing the behavior of docker run versus docker start, they behave differently, and it is confusing. Try this:

$ sudo docker start -a webapp

the -a flag tells docker to attach stdout/stderr and forward signals.

There are some other switches you can use with the start command (and a huge number for the run command). You can run docker [command] --help to get a summary of the options.

One other command that you might want to use is logs which will show the console output logs for a running container:

$ docker ps
[find the container ID]
$ docker logs [container ID]

Upvotes: 1

DazWilkin
DazWilkin

Reputation: 40071

When you use docker run, the default behavior is to run the container detached. This runs in the background and is detached from your shell's stdin/out.

To run the container in the foreground and connected to stdin/out:

docker run --interactive --tty --publish=8080:4000 mypyweb

To docker start a container, similarly:

docker start --interactive --attach [CONTAINER]

NB --attach rather than -tty

You may list (all add --all) running containers:

docker container ls

E.g. I ran Nginx:

CONTAINER ID  IMAGE  PORTS                 NAMES
7cc4b4e1cfd6  nginx  0.0.0.0:8888->80/tcp  nostalgic_thompson

NB You may use the NAME or any uniquely identifiable subset of the ID to reference the container Then:

docker stop nostalgic_thompson
docker start --interative --attach 7cc4

You may check the container's logs (when running detached or from another shell) by grabbing the container's ID or NAMES

docker logs nostalgic_thompson
docker logs 7cc4 

HTH!

Upvotes: 2

Related Questions