mirror of
https://github.com/moby/moby.git
synced 2025-12-15 11:29:09 +03:00
2.5 KiB
2.5 KiB
- title
- Hello world daemon example
- description
- A simple hello world daemon example with Docker
- keywords
- docker, example, hello world, daemon
Hello World Daemon
The most boring daemon ever written.
This example assumes you have Docker installed and with the base
image already imported docker pull base. We will use the
base image to run a simple hello world daemon that will just print hello
world to standard out every second. It will continue to do this until we
stop it.
Steps:
CONTAINER_ID=$(docker run -d base /bin/sh -c "while true; do echo hello world; sleep 1; done")We are going to run a simple hello world daemon in a new container made from the base image.
- "docker run -d " run a command in a new container. We pass "-d" so it runs as a daemon.
- "base" is the image we want to run the command inside of.
- "/bin/sh -c" is the command we want to run in the container
- "while true; do echo hello world; sleep 1; done" is the mini script we want to run, that will just print hello world once a second until we stop it.
- $CONTAINER_ID the output of the run command will return a container id, we can use in future commands to see what is going on with this process.
docker logs $CONTAINER_IDCheck the logs make sure it is working correctly.
- "docker logs" This will return the logs for a container
- $CONTAINER_ID The Id of the container we want the logs for.
docker attach $CONTAINER_IDAttach to the container to see the results in realtime.
- "docker attach" This will allow us to attach to a background process to see what is going on.
- $CONTAINER_ID The Id of the container we want to attach too.
docker psCheck the process list to make sure it is running.
- "docker ps" this shows all running process managed by docker
docker stop $CONTAINER_IDStop the container, since we don't need it anymore.
- "docker stop" This stops a container
- $CONTAINER_ID The Id of the container we want to stop.
docker psMake sure it is really stopped.
Video:
See the example in action
Continue to the python_web_app example.