2014-04-16 10:53:12 +10:00
|
|
|
|
page_title: Hello world example
|
|
|
|
|
page_description: A simple hello world example with Docker
|
|
|
|
|
page_keywords: docker, example, hello world
|
|
|
|
|
|
|
|
|
|
# Check your Docker installation
|
|
|
|
|
|
|
|
|
|
This guide assumes you have a working installation of Docker. To check
|
|
|
|
|
your Docker install, run the following command:
|
|
|
|
|
|
|
|
|
|
# Check that you have a working install
|
|
|
|
|
$ sudo docker info
|
|
|
|
|
|
|
|
|
|
If you get `docker: command not found` or something
|
|
|
|
|
like `/var/lib/docker/repositories: permission denied`
|
|
|
|
|
you may have an incomplete Docker installation or insufficient
|
|
|
|
|
privileges to access docker on your machine.
|
|
|
|
|
|
2014-04-24 22:12:21 +10:00
|
|
|
|
Please refer to [*Installation*](/installation/)
|
2014-04-16 10:53:12 +10:00
|
|
|
|
for installation instructions.
|
|
|
|
|
|
|
|
|
|
## Hello World
|
|
|
|
|
|
2014-04-18 23:21:55 +03:00
|
|
|
|
> **Note**:
|
|
|
|
|
>
|
|
|
|
|
> - This example assumes you have Docker running in daemon mode. For
|
|
|
|
|
> more information please see [*Check your Docker
|
|
|
|
|
> install*](#check-your-docker-installation).
|
2014-04-23 23:48:28 +03:00
|
|
|
|
> - **If you don't like sudo** then see [*Giving non-root
|
2014-04-24 22:12:21 +10:00
|
|
|
|
> access*](/installation/binaries/#dockergroup)
|
2014-04-16 10:53:12 +10:00
|
|
|
|
|
|
|
|
|
This is the most basic example available for using Docker.
|
|
|
|
|
|
|
|
|
|
Download the small base image named `busybox`:
|
|
|
|
|
|
|
|
|
|
# Download a busybox image
|
|
|
|
|
$ sudo docker pull busybox
|
|
|
|
|
|
2014-04-26 08:00:01 -04: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.io](http://index.docker.io) registry.
|
2014-04-16 10:53:12 +10:00
|
|
|
|
|
|
|
|
|
$ sudo docker run busybox /bin/echo hello world
|
|
|
|
|
|
|
|
|
|
This command will run a simple `echo` command, that
|
|
|
|
|
will echo `hello world` back to the console over
|
|
|
|
|
standard out.
|
|
|
|
|
|
|
|
|
|
**Explanation:**
|
|
|
|
|
|
|
|
|
|
- **"sudo"** execute the following commands as user *root*
|
|
|
|
|
- **"docker run"** run a command in a new container
|
|
|
|
|
- **"busybox"** is the image we are running the command in.
|
|
|
|
|
- **"/bin/echo"** is the command we want to run in the container
|
|
|
|
|
- **"hello world"** is the input for the echo command
|
|
|
|
|
|
|
|
|
|
**Video:**
|
|
|
|
|
|
|
|
|
|
See the example in action
|
|
|
|
|
|
|
|
|
|
<iframe width="640" height="480" frameborder="0" sandbox="allow-same-origin allow-scripts" srcdoc="<body><script type="text/javascript"src="https://asciinema.org/a/7658.js"id="asciicast-7658" async></script></body>"></iframe>
|
|
|
|
|
|
|
|
|
|
<iframe width="640" height="480" frameborder="0" sandbox="allow-same-origin allow-scripts" srcdoc="<body><script type="text/javascript"src="https://asciinema.org/a/7658.js"id="asciicast-7658" async></script></body>"></iframe>
|
|
|
|
|
|
|
|
|
|
## Hello World Daemon
|
|
|
|
|
|
2014-04-18 23:21:55 +03:00
|
|
|
|
> **Note**:
|
|
|
|
|
>
|
|
|
|
|
> - This example assumes you have Docker running in daemon mode. For
|
|
|
|
|
> more information please see [*Check your Docker
|
|
|
|
|
> install*](#check-your-docker-installation).
|
2014-04-23 23:48:28 +03:00
|
|
|
|
> - **If you don't like sudo** then see [*Giving non-root
|
2014-04-24 22:12:21 +10:00
|
|
|
|
> access*](/installation/binaries/#dockergroup)
|
2014-04-16 10:53:12 +10:00
|
|
|
|
|
|
|
|
|
And now for the most boring daemon ever written!
|
|
|
|
|
|
|
|
|
|
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:**
|
|
|
|
|
|
2014-05-01 17:13:34 +03:00
|
|
|
|
$ CONTAINER_ID=$(sudo docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done")
|
2014-04-16 10:53:12 +10:00
|
|
|
|
|
|
|
|
|
We are going to run a simple hello world daemon in a new container made
|
|
|
|
|
from the `ubuntu` image.
|
|
|
|
|
|
2014-04-23 23:48:28 +03:00
|
|
|
|
- **"sudo docker run -d "** run a command in a new container. We pass
|
|
|
|
|
"-d" 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.
|
2014-04-16 10:53:12 +10:00
|
|
|
|
|
|
|
|
|
<!-- -->
|
|
|
|
|
|
2014-05-01 17:13:34 +03:00
|
|
|
|
$ sudo docker logs $container_id
|
2014-04-16 10:53:12 +10:00
|
|
|
|
|
|
|
|
|
Check the logs make sure it is working correctly.
|
|
|
|
|
|
2014-04-23 23:48:28 +03:00
|
|
|
|
- **"docker logs**" This will return the logs for a container
|
|
|
|
|
- **$container_id** The Id of the container we want the logs for.
|
2014-04-16 10:53:12 +10:00
|
|
|
|
|
|
|
|
|
<!-- -->
|
|
|
|
|
|
2014-05-01 17:13:34 +03:00
|
|
|
|
$ sudo docker attach --sig-proxy=false $container_id
|
2014-04-16 10:53:12 +10:00
|
|
|
|
|
|
|
|
|
Attach to the container to see the results in real-time.
|
|
|
|
|
|
2014-04-23 23:48:28 +03:00
|
|
|
|
- **"docker attach**" This will allow us to attach to a background
|
|
|
|
|
process to see what is going on.
|
|
|
|
|
- **"–sig-proxy=false"** Do not forward signals to the container;
|
|
|
|
|
allows us to exit the attachment using Control-C without stopping
|
|
|
|
|
the container.
|
|
|
|
|
- **$container_id** The Id of the container we want to attach to.
|
2014-04-16 10:53:12 +10:00
|
|
|
|
|
|
|
|
|
Exit from the container attachment by pressing Control-C.
|
|
|
|
|
|
2014-05-01 17:13:34 +03:00
|
|
|
|
$ sudo docker ps
|
2014-04-16 10:53:12 +10:00
|
|
|
|
|
|
|
|
|
Check the process list to make sure it is running.
|
|
|
|
|
|
2014-04-23 23:48:28 +03:00
|
|
|
|
- **"docker ps"** this shows all running process managed by docker
|
2014-04-16 10:53:12 +10:00
|
|
|
|
|
|
|
|
|
<!-- -->
|
|
|
|
|
|
2014-05-01 17:13:34 +03:00
|
|
|
|
$ sudo docker stop $container_id
|
2014-04-16 10:53:12 +10:00
|
|
|
|
|
2014-04-23 23:48:28 +03:00
|
|
|
|
Stop the container, since we don't need it anymore.
|
2014-04-16 10:53:12 +10:00
|
|
|
|
|
2014-04-23 23:48:28 +03:00
|
|
|
|
- **"docker stop"** This stops a container
|
|
|
|
|
- **$container_id** The Id of the container we want to stop.
|
2014-04-16 10:53:12 +10:00
|
|
|
|
|
|
|
|
|
<!-- -->
|
|
|
|
|
|
2014-05-01 17:13:34 +03:00
|
|
|
|
$ sudo docker ps
|
2014-04-16 10:53:12 +10:00
|
|
|
|
|
|
|
|
|
Make sure it is really stopped.
|
|
|
|
|
|
|
|
|
|
**Video:**
|
|
|
|
|
|
|
|
|
|
See the example in action
|
|
|
|
|
|
|
|
|
|
<iframe width="640" height="480" frameborder="0" sandbox="allow-same-origin allow-scripts" srcdoc="<body><script type="text/javascript"src="https://asciinema.org/a/2562.js"id="asciicast-2562" async></script></body>"></iframe>
|
|
|
|
|
|
|
|
|
|
<iframe width="640" height="480" frameborder="0" sandbox="allow-same-origin allow-scripts" srcdoc="<body><script type="text/javascript"src="https://asciinema.org/a/2562.js"id="asciicast-2562" async></script></body>"></iframe>
|
|
|
|
|
|
2014-04-23 23:48:28 +03:00
|
|
|
|
The next example in the series is a [*Node.js Web App*](
|
|
|
|
|
../nodejs_web_app/#nodejs-web-app) example, or you could skip to any of the
|
|
|
|
|
other examples:
|
|
|
|
|
|
|
|
|
|
- [*Node.js Web App*](../nodejs_web_app/#nodejs-web-app)
|
|
|
|
|
- [*Redis Service*](../running_redis_service/#running-redis-service)
|
|
|
|
|
- [*SSH Daemon Service*](../running_ssh_service/#running-ssh-service)
|
|
|
|
|
- [*CouchDB Service*](../couchdb_data_volumes/#running-couchdb-service)
|
|
|
|
|
- [*PostgreSQL Service*](../postgresql_service/#postgresql-service)
|
|
|
|
|
- [*Building an Image with MongoDB*](../mongodb/#mongodb-image)
|
|
|
|
|
- [*Python Web App*](../python_web_app/#python-web-app)
|