2013-03-26 17:25:42 -04:00
:title: Hello world daemon example
2013-03-25 22:52:52 -04:00
:description: A simple hello world daemon example with Docker
:keywords: docker, example, hello world, daemon
.. _hello_world_daemon:
Hello World Daemon
==================
2013-04-07 10:23:00 -04:00
.. include :: example_header.inc
2013-03-25 22:52:52 -04:00
The most boring daemon ever written.
2013-03-26 21:21:52 -04:00
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.
2013-03-25 22:52:52 -04:00
**Steps:**
.. code-block :: bash
2013-03-28 14:50:00 -04:00
CONTAINER_ID=$(docker run -d base /bin/sh -c "while true; do echo hello world; sleep 1; done")
2013-03-25 22:52:52 -04:00
2013-04-07 10:23:00 -04:00
We are going to run a simple hello world daemon in a new container made from the base image.
2013-03-25 22:52:52 -04:00
- **"docker run -d "** run a command in a new container. We pass "-d" so it runs as a daemon.
2013-03-26 21:21:52 -04:00
- **"base"** is the image we want to run the command inside of.
2013-03-25 22:52:52 -04:00
- **"/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.
.. code-block :: bash
2013-03-28 14:50:00 -04:00
docker logs $CONTAINER_ID
2013-03-25 22:52:52 -04:00
Check 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.
.. code-block :: bash
2013-03-26 21:21:52 -04:00
docker attach $CONTAINER_ID
2013-03-25 22:52:52 -04:00
Attach 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.
.. code-block :: bash
2013-03-26 21:21:52 -04:00
docker ps
2013-03-25 22:52:52 -04:00
Check the process list to make sure it is running.
- **"docker ps"** this shows all running process managed by docker
.. code-block :: bash
2013-03-28 14:50:00 -04:00
docker stop $CONTAINER_ID
2013-03-25 22:52:52 -04:00
Stop 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.
.. code-block :: bash
2013-03-26 21:21:52 -04:00
docker ps
2013-03-25 22:52:52 -04:00
Make sure it is really stopped.
**Video:**
See the example in action
.. raw :: html
<div style="margin-top:10px;">
<iframe width="560" height="350" src="http://ascii.io/a/2562/raw" frameborder="0"></iframe>
</div>
2013-03-26 21:21:52 -04:00
Continue to the :ref: `python_web_app` example.