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-24 22:47:25 -04:00
|
|
|
.. _examples:
|
|
|
|
|
|
|
|
Hello World
|
|
|
|
-----------
|
|
|
|
|
2013-09-09 16:19:07 -04:00
|
|
|
.. _running_examples:
|
|
|
|
|
|
|
|
Running the Examples
|
|
|
|
====================
|
|
|
|
|
2013-11-02 21:26:52 -04:00
|
|
|
All the examples assume your machine is running the ``docker`` daemon. To
|
|
|
|
run the ``docker`` daemon in the background, simply type:
|
2013-09-09 16:19:07 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
sudo docker -d &
|
|
|
|
|
2013-11-02 21:26:52 -04:00
|
|
|
Now you can run Docker in client mode: by default all commands will be
|
2013-09-09 16:19:07 -04:00
|
|
|
forwarded to the ``docker`` daemon via a protected Unix socket, so you
|
2013-11-02 21:26:52 -04:00
|
|
|
must run as the ``root`` or via the ``sudo`` command.
|
2013-09-09 16:19:07 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
sudo docker help
|
|
|
|
|
|
|
|
----
|
|
|
|
|
2013-03-25 22:52:52 -04:00
|
|
|
.. _hello_world:
|
|
|
|
|
|
|
|
Hello World
|
|
|
|
===========
|
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
|
|
|
|
2013-11-02 21:26:52 -04:00
|
|
|
Download the base image which is named ``ubuntu``:
|
2013-03-26 21:21:52 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-08-04 18:16:00 -04:00
|
|
|
# Download an ubuntu image
|
2013-08-13 21:05:35 -04:00
|
|
|
sudo docker pull ubuntu
|
2013-03-26 21:21:52 -04:00
|
|
|
|
2013-11-02 21:26:52 -04:00
|
|
|
Alternatively to the ``ubuntu`` image, you can select ``busybox``, a bare
|
2013-08-13 21:05:35 -04:00
|
|
|
minimal Linux system. The images are retrieved from the Docker
|
|
|
|
repository.
|
2013-03-26 21:21:52 -04:00
|
|
|
|
2013-03-25 22:52:52 -04:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2013-08-13 21:05:35 -04:00
|
|
|
sudo docker run ubuntu /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:**
|
|
|
|
|
2013-08-13 21:05:35 -04:00
|
|
|
- **"sudo"** execute the following commands as user *root*
|
2013-03-25 22:52:52 -04:00
|
|
|
- **"docker run"** run a command in a new container
|
2013-08-13 21:05:35 -04:00
|
|
|
- **"ubuntu"** is the image we want to run the command inside of.
|
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
|
|
|
|
|
|
|
|
<div style="margin-top:10px;">
|
2013-03-26 21:21:52 -04:00
|
|
|
<iframe width="560" height="350" src="http://ascii.io/a/2603/raw" frameborder="0"></iframe>
|
2013-03-25 22:52:52 -04:00
|
|
|
</div>
|
|
|
|
|
2013-03-26 21:21:52 -04:00
|
|
|
|
2013-09-09 16:19:07 -04:00
|
|
|
----
|
|
|
|
|
|
|
|
.. _hello_world_daemon:
|
|
|
|
|
|
|
|
Hello World Daemon
|
|
|
|
==================
|
|
|
|
|
|
|
|
.. include:: example_header.inc
|
|
|
|
|
|
|
|
And now for the most boring daemon ever written!
|
|
|
|
|
|
|
|
This example assumes you have Docker installed and the Ubuntu
|
|
|
|
image already imported with ``docker pull ubuntu``. We will use the Ubuntu
|
|
|
|
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:**
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
CONTAINER_ID=$(sudo docker run -d ubuntu /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
|
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.
|
|
|
|
- **$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
|
|
|
|
|
|
|
|
sudo docker logs $CONTAINER_ID
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
sudo docker attach $CONTAINER_ID
|
|
|
|
|
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.
|
2013-11-15 14:38:03 -05:00
|
|
|
- **"-sig-proxy=true"** Proxify all received signal to the process
|
|
|
|
(even in non-tty mode)
|
2013-09-09 16:19:07 -04:00
|
|
|
- **$CONTAINER_ID** The Id of the container we want to attach too.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
sudo docker stop $CONTAINER_ID
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
sudo docker ps
|
|
|
|
|
|
|
|
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>
|
|
|
|
|
|
|
|
The next example in the series is a :ref:`python_web_app` example, or
|
|
|
|
you could skip to any of the other examples:
|
|
|
|
|
|
|
|
|
2013-09-24 22:47:25 -04:00
|
|
|
* :ref:`python_web_app`
|
|
|
|
* :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`
|