2. Docker Hello World¶
In this webpage, we will create and run our first docker example.
2.1. v1.0 Hello World¶
docker run ubuntu:15.10 /bin/echo "Hello world"
Most docker commands starts with docker
. run
means running an container if exists. If the container doesn’t exist, we will create one. ubuntu:15.10
is a docker image’s name.
/bin/echo
is the bash command of the ubuntu container.
2.1.1. What is Expected¶
In the docker application, you will see an image ubuntu
with tag 15.10
.
Note that, this is an image rather than the running container. You can also check that by running
docker images
and you should see
To view the created container, we can click the “container” tab on the left hand side of the docker application or execute
docker container ls -a
and you should see
In the command, we add a -a
flag to list existing but inactive containers. Since the container we created will only run one command, it will destroy itself after presenting you the result. This is one difference between a virtual machine and a docker container such that docker is a task based technology that can but is not supposed to handle multiple tasks.
2.2. v1.1 Interactable Container¶
We would like to interact with the docker container rather than let the container destroy itself after running. We can achieve so by adding two tags
-i
: Allow using standard input (the terminal) to interact with the container.-t
Specify a terminal in the container. In this example, the terminal is/bin/bash
.flags can be merged as
-it
.-i
and-t
cannot be placed afterubuntu:15.10
.
The command to execute is
docker run -i -t ubuntu:15.10 /bin/bash
and you should see a bash terminal that you can play with
If we list the containers, we can see that a new container has been instantialized with container id 6f0cf4ee88c7
, which can also be seen in the bash terminal.
To exit the environment, execute exit
or press ctrl-D.
2.3. v1.2 Run A Container In A Process¶
In v1.0, we showed that the container will destroy itself once the task is done. For services such as webserver, the program naturally has a while-loop that will keep running until you turn it down. In this example, we will create a while-loop and check its running status.
The while-loop is a simple dead loop with one single line of print out.
while true;
do echo hello world;
sleep 1;
done
So the command to be ran is
docker run -d ubuntu:15.10 /bin/bash -c "while true;do echo hello world;sleep 1; done"
-d
means Detach, which means the output of the running log will not show in the terminal.
We will see a long container id corresponding to the running docker, but no echo messages.
We can access the log file to see the output message by
# get the information of running container
docker ps
# using container id 4250a7496c2b
docker logs 4250a7496c2b
# using container names quirky_heisenberg
docker logs quirky_heisenberg
To stop the container, we can use the stop
command by executing
docker stop quirky_heisenberg
# or
docker stop 4250a7496c2b