2015-06-07 23:07:20 -04:00
|
|
|
<!--[metadata]>
|
|
|
|
+++
|
2016-01-23 23:36:40 -05:00
|
|
|
aliases = ["/engine/userguide/dockerizing/"]
|
2015-09-30 16:11:36 -04:00
|
|
|
title = "Hello world in a container"
|
2015-06-07 23:07:20 -04:00
|
|
|
description = "A simple 'Hello world' exercise that introduced you to Docker."
|
2015-12-07 15:29:09 -05:00
|
|
|
keywords = ["docker guide, docker, docker platform, how to, dockerize, dockerizing apps, dockerizing applications, container, containers"]
|
2015-06-07 23:07:20 -04:00
|
|
|
[menu.main]
|
2016-01-23 23:36:40 -05:00
|
|
|
parent="engine_learn"
|
2015-09-30 16:11:36 -04:00
|
|
|
weight=-6
|
2015-06-07 23:07:20 -04:00
|
|
|
+++
|
|
|
|
<![end-metadata]-->
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2015-09-30 16:11:36 -04:00
|
|
|
# Hello world in a container
|
2014-05-21 17:05:19 -04:00
|
|
|
|
|
|
|
*So what's this Docker thing all about?*
|
|
|
|
|
2015-09-30 16:11:36 -04:00
|
|
|
Docker allows you to run applications, worlds you create, inside containers.
|
|
|
|
Running an application inside a container takes a single command: `docker run`.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2015-08-05 12:07:11 -04:00
|
|
|
>**Note**: Depending on your Docker system configuration, you may be required to
|
|
|
|
>preface each `docker` command on this page with `sudo`. To avoid this behavior,
|
|
|
|
>your system administrator can create a Unix group called `docker` and add users
|
2015-09-30 16:11:36 -04:00
|
|
|
>to it.
|
2014-12-29 03:19:42 -05:00
|
|
|
|
2015-09-30 16:11:36 -04:00
|
|
|
## Run a Hello world
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
Let's run a hello world container.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2015-12-16 13:13:01 -05:00
|
|
|
$ docker run ubuntu /bin/echo 'Hello world'
|
2014-07-01 18:07:05 -04:00
|
|
|
Hello world
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
You just launched your first container!
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
In this example:
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
* `docker run` runs a container.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
* `ubuntu` is the image you run, for example the Ubuntu operating system image.
|
|
|
|
When you specify an image, Docker looks first for the image on your
|
|
|
|
Docker host. If the image does not exist locally, then the image is pulled from the public
|
|
|
|
image registry [Docker Hub](https://hub.docker.com).
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
* `/bin/echo` is the command to run inside the new container.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
The container launches. Docker creates a new Ubuntu
|
|
|
|
environment and executes the `/bin/echo` command inside it and then prints out:
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2014-07-01 18:07:05 -04:00
|
|
|
Hello world
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
So what happened to the container after that? Well, Docker containers
|
|
|
|
only run as long as the command you specify is active. Therefore, in the above example,
|
|
|
|
the container stops once the command is executed.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
## Run an interactive container
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
Let's specify a new command to run in the container.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2015-12-16 13:13:01 -05:00
|
|
|
$ docker run -t -i ubuntu /bin/bash
|
2014-05-21 17:05:19 -04:00
|
|
|
root@af8bae53bdd3:/#
|
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
In this example:
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
* `docker run` runs a container.
|
|
|
|
* `ubuntu` is the image you would like to run.
|
|
|
|
* `-t` flag assigns a pseudo-tty or terminal inside the new container.
|
|
|
|
* `-i` flag allows you to make an interactive connection by
|
|
|
|
grabbing the standard in (`STDIN`) of the container.
|
|
|
|
* `/bin/bash` launches a Bash shell inside our container.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
The container launches. We can see there is a
|
2014-05-21 17:05:19 -04:00
|
|
|
command prompt inside it:
|
|
|
|
|
|
|
|
root@af8bae53bdd3:/#
|
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
Let's try running some commands inside the container:
|
2014-05-21 17:05:19 -04:00
|
|
|
|
|
|
|
root@af8bae53bdd3:/# pwd
|
|
|
|
/
|
|
|
|
root@af8bae53bdd3:/# ls
|
|
|
|
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
|
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
In this example:
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
* `pwd` displays the current directory, the `/` root directory.
|
|
|
|
* `ls` displays the directory listing of the root directory of a typical Linux file system.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
Now, you can play around inside this container. When completed, run the `exit` command or enter Ctrl-D
|
|
|
|
to exit the interactive shell.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
root@af8bae53bdd3:/# exit
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
>**Note:** As with our previous container, once the Bash shell process has
|
|
|
|
finished, the container stops.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
## Start a daemonized Hello world
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
Let's create a container that runs as a daemon.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2015-12-16 13:13:01 -05:00
|
|
|
$ docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
|
2014-05-21 17:05:19 -04:00
|
|
|
1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147
|
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
In this example:
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
* `docker run` runs the container.
|
|
|
|
* `-d` flag runs the container in the background (to daemonize it).
|
|
|
|
* `ubuntu` is the image you would like to run.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
Finally, we specify a command to run:
|
2014-05-21 17:05:19 -04:00
|
|
|
|
|
|
|
/bin/sh -c "while true; do echo hello world; sleep 1; done"
|
|
|
|
|
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
In the output, we do not see `hello world` but a long string:
|
2014-05-21 17:05:19 -04:00
|
|
|
|
|
|
|
1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147
|
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
This long string is called a *container ID*. It uniquely
|
2014-05-21 17:05:19 -04:00
|
|
|
identifies a container so we can work with it.
|
|
|
|
|
2015-09-30 16:11:36 -04:00
|
|
|
> **Note:**
|
2016-03-04 13:50:48 -05:00
|
|
|
> The container ID is a bit long and unwieldy. Later, we will cover the short
|
|
|
|
> ID and ways to name our containers to make
|
2014-05-21 17:05:19 -04:00
|
|
|
> working with them easier.
|
|
|
|
|
2014-11-02 09:55:36 -05:00
|
|
|
We can use this container ID to see what's happening with our `hello world` daemon.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
First, let's make sure our container is running. Run the `docker ps` command.
|
|
|
|
The `docker ps` command queries the Docker daemon for information about all the containers it knows
|
2014-05-21 17:05:19 -04:00
|
|
|
about.
|
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker ps
|
2014-05-21 17:05:19 -04:00
|
|
|
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
2015-12-16 13:13:01 -05:00
|
|
|
1e5535038e28 ubuntu /bin/sh -c 'while tr 2 minutes ago Up 1 minute insane_babbage
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
In this example, we can see our daemonized container. The `docker ps` returns some useful
|
|
|
|
information:
|
|
|
|
|
|
|
|
* `1e5535038e28` is the shorter variant of the container ID.
|
|
|
|
* `ubuntu` is the used image.
|
|
|
|
* the command, status, and assigned name `insane_babbage`.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
|
|
|
|
2015-09-30 16:11:36 -04:00
|
|
|
> **Note:**
|
2015-08-08 19:06:25 -04:00
|
|
|
> Docker automatically generates names for any containers started.
|
|
|
|
> We'll see how to specify your own names a bit later.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
Now, we know the container is running. But is it doing what we asked it to do? To
|
2015-09-30 16:11:36 -04:00
|
|
|
see this we're going to look inside the container using the `docker logs`
|
2016-03-04 13:50:48 -05:00
|
|
|
command.
|
|
|
|
|
|
|
|
Let's use the container name `insane_babbage`.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker logs insane_babbage
|
2014-05-21 17:05:19 -04:00
|
|
|
hello world
|
|
|
|
hello world
|
|
|
|
hello world
|
|
|
|
. . .
|
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
In this example:
|
|
|
|
|
|
|
|
* `docker logs` looks inside the container and returns `hello world`.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
Awesome! The daemon is working and you have just created your first
|
2014-05-21 17:05:19 -04:00
|
|
|
Dockerized application!
|
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
Next, run the `docker stop` command to stop our detached container.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker stop insane_babbage
|
2014-05-21 17:05:19 -04:00
|
|
|
insane_babbage
|
|
|
|
|
|
|
|
The `docker stop` command tells Docker to politely stop the running
|
2016-03-04 13:50:48 -05:00
|
|
|
container and returns the name of the container it stopped.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
|
|
|
Let's check it worked with the `docker ps` command.
|
|
|
|
|
2015-03-26 14:12:37 -04:00
|
|
|
$ docker ps
|
2014-05-21 17:05:19 -04:00
|
|
|
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
|
|
|
|
2016-03-04 13:50:48 -05:00
|
|
|
Excellent. Our container is stopped.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
|
|
|
# Next steps
|
|
|
|
|
2015-09-30 16:11:36 -04:00
|
|
|
So far, you launched your first containers using the `docker run` command. You
|
|
|
|
ran an *interactive container* that ran in the foreground. You also ran a
|
|
|
|
*detached container* that ran in the background. In the process you learned
|
|
|
|
about several Docker commands:
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2015-09-30 16:11:36 -04:00
|
|
|
* `docker ps` - Lists containers.
|
|
|
|
* `docker logs` - Shows us the standard output of a container.
|
|
|
|
* `docker stop` - Stops running containers.
|
2014-05-21 17:05:19 -04:00
|
|
|
|
2015-09-30 16:11:36 -04:00
|
|
|
Now, you have the basis learn more about Docker and how to do some more advanced
|
|
|
|
tasks. Go to ["*Run a simple application*"](usingdocker.md) to actually build a
|
|
|
|
web application with the Docker client.
|