2013-03-26 17:25:42 -04:00
|
|
|
:title: Hello world example
|
2013-03-25 22:52:52 -04:00
|
|
|
:description: A simple hello world example with Docker
|
|
|
|
:keywords: docker, example, hello world
|
|
|
|
|
2013-09-09 16:19:07 -04:00
|
|
|
.. _running_examples:
|
|
|
|
|
2014-01-21 02:26:45 -05:00
|
|
|
Check your Docker install
|
2014-01-20 21:58:04 -05:00
|
|
|
-------------------------
|
2013-09-09 16:19:07 -04:00
|
|
|
|
2014-01-20 21:58:04 -05:00
|
|
|
This guide assumes you have a working installation of Docker. To check
|
2014-01-21 02:26:45 -05:00
|
|
|
your Docker install, run the following command:
|
2013-09-09 16:19:07 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2014-01-20 21:58:04 -05:00
|
|
|
# Check that you have a working install
|
2014-02-14 06:27:57 -05:00
|
|
|
$ sudo docker info
|
2013-09-09 16:19:07 -04:00
|
|
|
|
2014-01-20 21:58:04 -05:00
|
|
|
If you get ``docker: command not found`` or something like
|
|
|
|
``/var/lib/docker/repositories: permission denied`` you may have an incomplete
|
2014-01-21 02:26:45 -05:00
|
|
|
Docker installation or insufficient privileges to access docker on your machine.
|
2013-09-09 16:19:07 -04:00
|
|
|
|
2014-01-20 21:58:04 -05:00
|
|
|
Please refer to :ref:`installation_list` for installation instructions.
|
2013-09-09 16:19:07 -04:00
|
|
|
|
|
|
|
|
2013-03-25 22:52:52 -04:00
|
|
|
.. _hello_world:
|
|
|
|
|
|
|
|
Hello World
|
2014-02-14 06:27:57 -05:00
|
|
|
-----------
|
2013-03-26 21:21:52 -04:00
|
|
|
|
2013-04-07 10:23:00 -04:00
|
|
|
.. include:: example_header.inc
|
|
|
|
|
2013-04-08 23:10:47 -04:00
|
|
|
This is the most basic example available for using Docker.
|
2013-03-26 21:21:52 -04:00
|
|
|
|
2014-02-14 06:27:57 -05:00
|
|
|
Download the small base image named ``busybox``:
|
2013-03-26 21:21:52 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2014-02-14 07:41:59 -05:00
|
|
|
# Download a busybox image
|
2014-02-14 06:27:57 -05:00
|
|
|
$ sudo docker pull busybox
|
2013-03-26 21:21:52 -04:00
|
|
|
|
2014-02-14 06:27:57 -05:00
|
|
|
The ``busybox`` image is a minimal Linux system. You can do the same
|
|
|
|
with any number of other images, such as ``debian``, ``ubuntu`` or ``centos``.
|
|
|
|
The images can be found and retrieved using the `Docker index`_.
|
2013-03-26 21:21:52 -04:00
|
|
|
|
2014-02-14 06:27:57 -05:00
|
|
|
.. _Docker index: http://index.docker.io
|
2013-03-25 22:52:52 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2014-02-14 06:27:57 -05:00
|
|
|
$ sudo docker run busybox /bin/echo hello world
|
2013-03-25 22:52:52 -04:00
|
|
|
|
2013-11-02 21:26:52 -04:00
|
|
|
This command will run a simple ``echo`` command, that will echo ``hello world`` back to the console over standard out.
|
|
|
|
|
2013-03-25 22:52:52 -04:00
|
|
|
**Explanation:**
|
|
|
|
|
2014-03-07 09:51:52 -05:00
|
|
|
- **"sudo"** execute the following commands as user *root*
|
|
|
|
- **"docker run"** run a command in a new container
|
2014-02-14 06:27:57 -05:00
|
|
|
- **"busybox"** is the image we are running the command in.
|
2013-03-25 22:52:52 -04:00
|
|
|
- **"/bin/echo"** is the command we want to run in the container
|
|
|
|
- **"hello world"** is the input for the echo command
|
|
|
|
|
2013-03-26 21:21:52 -04:00
|
|
|
|
|
|
|
|
2013-03-25 22:52:52 -04:00
|
|
|
**Video:**
|
|
|
|
|
|
|
|
See the example in action
|
|
|
|
|
|
|
|
.. raw:: html
|
|
|
|
|
2014-01-31 21:02:45 -05:00
|
|
|
<iframe width="560" height="400" frameborder="0"
|
2014-03-07 09:51:52 -05:00
|
|
|
sandbox="allow-same-origin allow-scripts"
|
|
|
|
srcdoc="<body><script type="text/javascript"
|
|
|
|
src="https://asciinema.org/a/7658.js"
|
2014-02-14 07:41:59 -05:00
|
|
|
id="asciicast-7658" async></script></body>">
|
2014-01-31 21:02:45 -05:00
|
|
|
</iframe>
|
2013-03-26 21:21:52 -04:00
|
|
|
|
2013-09-09 16:19:07 -04:00
|
|
|
----
|
|
|
|
|
|
|
|
.. _hello_world_daemon:
|
|
|
|
|
|
|
|
Hello World Daemon
|
2014-02-14 06:27:57 -05:00
|
|
|
------------------
|
2013-09-09 16:19:07 -04:00
|
|
|
|
|
|
|
.. include:: example_header.inc
|
|
|
|
|
|
|
|
And now for the most boring daemon ever written!
|
|
|
|
|
2014-01-20 21:58:04 -05:00
|
|
|
We will use the Ubuntu image to run a simple hello world daemon that will just print hello
|
2013-09-09 16:19:07 -04:00
|
|
|
world to standard out every second. It will continue to do this until
|
|
|
|
we stop it.
|
|
|
|
|
|
|
|
**Steps:**
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2014-03-07 09:51:52 -05:00
|
|
|
container_id=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done")
|
2013-09-09 16:19:07 -04:00
|
|
|
|
|
|
|
We are going to run a simple hello world daemon in a new container
|
2013-11-02 21:26:52 -04:00
|
|
|
made from the ``ubuntu`` image.
|
2013-09-09 16:19:07 -04:00
|
|
|
|
2013-11-02 21:26:52 -04:00
|
|
|
- **"sudo docker run -d "** run a command in a new container. We pass "-d"
|
2013-09-09 16:19:07 -04:00
|
|
|
so it runs as a daemon.
|
|
|
|
- **"ubuntu"** 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.
|
2014-03-07 09:51:52 -05:00
|
|
|
- **$container_id** the output of the run command will return a
|
2013-09-09 16:19:07 -04:00
|
|
|
container id, we can use in future commands to see what is going on
|
|
|
|
with this process.
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2014-03-07 09:51:52 -05:00
|
|
|
sudo docker logs $container_id
|
2013-09-09 16:19:07 -04:00
|
|
|
|
|
|
|
Check the logs make sure it is working correctly.
|
|
|
|
|
|
|
|
- **"docker logs**" This will return the logs for a container
|
2014-03-07 09:51:52 -05:00
|
|
|
- **$container_id** The Id of the container we want the logs for.
|
2013-09-09 16:19:07 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2014-03-13 13:46:02 -04:00
|
|
|
sudo docker attach --sig-proxy=false $container_id
|
2013-09-09 16:19:07 -04:00
|
|
|
|
2013-11-07 17:06:49 -05:00
|
|
|
Attach to the container to see the results in real-time.
|
2013-09-09 16:19:07 -04:00
|
|
|
|
|
|
|
- **"docker attach**" This will allow us to attach to a background
|
|
|
|
process to see what is going on.
|
2014-03-13 13:46:02 -04:00
|
|
|
- **"--sig-proxy=false"** Do not forward signals to the container; allows
|
2014-01-08 04:58:55 -05:00
|
|
|
us to exit the attachment using Control-C without stopping the container.
|
2014-03-07 09:51:52 -05:00
|
|
|
- **$container_id** The Id of the container we want to attach too.
|
2013-09-09 16:19:07 -04:00
|
|
|
|
|
|
|
Exit from the container attachment by pressing Control-C.
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
sudo docker ps
|
|
|
|
|
|
|
|
Check the process list to make sure it is running.
|
|
|
|
|
|
|
|
- **"docker ps"** this shows all running process managed by docker
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2014-03-07 09:51:52 -05:00
|
|
|
sudo docker stop $container_id
|
2013-09-09 16:19:07 -04:00
|
|
|
|
|
|
|
Stop the container, since we don't need it anymore.
|
|
|
|
|
|
|
|
- **"docker stop"** This stops a container
|
2014-03-07 09:51:52 -05:00
|
|
|
- **$container_id** The Id of the container we want to stop.
|
2013-09-09 16:19:07 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
sudo docker ps
|
|
|
|
|
|
|
|
Make sure it is really stopped.
|
|
|
|
|
|
|
|
|
|
|
|
**Video:**
|
|
|
|
|
|
|
|
See the example in action
|
|
|
|
|
|
|
|
.. raw:: html
|
|
|
|
|
2014-01-31 21:02:45 -05:00
|
|
|
<iframe width="560" height="400" frameborder="0"
|
2014-03-07 09:51:52 -05:00
|
|
|
sandbox="allow-same-origin allow-scripts"
|
|
|
|
srcdoc="<body><script type="text/javascript"
|
|
|
|
src="https://asciinema.org/a/2562.js"
|
2014-01-31 21:02:45 -05:00
|
|
|
id="asciicast-2562" async></script></body>">
|
|
|
|
</iframe>
|
2013-09-09 16:19:07 -04:00
|
|
|
|
2014-02-14 06:27:57 -05:00
|
|
|
The next example in the series is a :ref:`nodejs_web_app` example, or
|
2013-09-09 16:19:07 -04:00
|
|
|
you could skip to any of the other examples:
|
|
|
|
|
|
|
|
|
2013-09-24 22:47:25 -04:00
|
|
|
* :ref:`nodejs_web_app`
|
|
|
|
* :ref:`running_redis_service`
|
|
|
|
* :ref:`running_ssh_service`
|
|
|
|
* :ref:`running_couchdb_service`
|
|
|
|
* :ref:`postgresql_service`
|
2013-10-17 20:57:01 -04:00
|
|
|
* :ref:`mongodb_image`
|
2014-02-14 06:27:57 -05:00
|
|
|
* :ref:`python_web_app`
|