Docker Hello World


Docker allows you to run applications within a container. Use the docker run command to run an application within a container.

Output Hello world

php@php:~$ docker run ubuntu:15.10 /bin/echo "Hello world"
Hello world

docker19.png

Each parameter analysis:

  • docker: Docker binary executable file.

  • run:Combined with the previous docker to run a container.

  • ubuntu:15.10Specify the image to be run. Docker first checks whether the image exists on the local host. If it does not exist, Docker will retrieve it from the image warehouse Docker Hub Download public images.

  • /bin/echo "Hello world": Commands executed in the started container

The above commands are complete The meaning can be interpreted as: Docker creates a new container with the ubuntu15.10 image, then executes bin/echo "Hello world" in the container, and then outputs the result.


Run interactive containers

We use the two parameters of docker -i -t to allow the container run by docker to realize the "dialogue" ability

php@php:~$ docker run -i -t ubuntu:15.10 /bin/bash
root@dc0050c79503:/#

Analysis of each parameter:

  • -t:Specify a pseudo terminal or terminal in the new container.

  • -i:Allows you to interact with the standard input (STDIN) within the container.

At this point we have entered a container of ubuntu15.10 system

We try to run the command in the container cat /proc/versionandlsView the version information of the current system and the file list in the current directory respectively

docker20.png

We can exit the container by running the exit command or using CTRL+D.


Start the container (background mode)

Use the following command to create a container running as a process

php@php:~$ docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63

docker21.png

In the output , we did not see the expected "hello world", but a long string of characters

2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63

This long string is called the container ID, for each container are all unique, we can use the container ID to see what happened to the corresponding container.

First of all, we need to confirm that the container is running. You can check it through docker ps

php@php:~$ docker ps

docker22.png

CONTAINER ID: Container ID

NAMES:Automatically assigned container name

Use the docker logs command in the container to view the standard output in the container

php@php:~$ docker logs 2b1b7a428627

docker23.png

php@php:~$ docker logs amazing_cori

docker24.png


Stop the container

We use the docker stop command to stop the container:

docker25.png

Check through docker ps and the container has stopped working:

php@php:~$ docker ps

docker26.png

You can also use the following command to stop:

php@php:~$ docker stop amazing_cori