Docker Hello World
Docker allows you to run applications inside containers. Use the **docker run** command to run an application inside a container.
Output Hello world
@:~$ docker run ubuntu:15.10 /bin/echo "Hello world"Hello world
!(#)
Parameter analysis:
* **docker:** The Docker binary executable.
* **run:** Combined with the preceding `docker` to run a container.
* **ubuntu:15.10** Specifies the image to run. Docker first checks if the image exists on the local host. If not, Docker downloads the public image from the image repository Docker Hub.
* **/bin/echo "Hello world":** The command to execute inside the started container.
The complete meaning of the above command can be interpreted as: Docker creates a new container using the ubuntu15.10 image, then executes `bin/echo "Hello world"` inside the container, and outputs the result.
* * *
## Running an Interactive Container
We use two Docker parameters, -i and -t, to enable the Docker container to have **"conversation"** capabilities:
@:~$ docker run -i -t ubuntu:15.10 /bin/bash root@0123ce188bd8:/#
Parameter analysis:
* **-t:** Specifies a pseudo-terminal or terminal inside the new container.
* **-i:** Allows you to interact with the standard input (STDIN) inside the container.
Note the second line `root@0123ce188bd8:/#`, at this point we have entered a container with the ubuntu15.10 system.
We try to run the commands **cat /proc/version** and **ls** inside the container to view the current system version information and the file list in the current directory, respectively.
root@0123ce188bd8:/# cat /proc/version Linux version 4.4.0-151-generic (buildd@lgw01-amd64-043) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.10) ) #178-Ubuntu SMP Tue Jun 11 08:30:22 UTC 2019 root@0123ce188bd8:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@0123ce188bd8:/#
We can exit the container by running the `exit` command or using CTRL+D.
root@0123ce188bd8:/# exitexit root@:~#
Note that the third line `root@:~#` indicates we have exited the current container and returned to the current host.
* * *
## Starting a Container (Background Mode)
Use the following command to create a container that runs as a process
@:~$ docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63
In the output, we do not see the expected "hello world", but a long string
**2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63**
This long string is called the container ID, which is unique for each container. We can use the container ID to see what happened to the corresponding container.
First, we need to confirm that the container is running. We can check with **docker ps**:
@:~$ docker ps CONTAINER ID IMAGE COMMAND ... 5917eac21c36 ubuntu:15.10 "/bin/sh -c 'while tβ¦" ...
Output details:
**CONTAINER ID:** Container ID.
**IMAGE:** The image used.
**COMMAND:** The command run when starting the container.
**CREATED:** The time the container was created.
**STATUS:** Container status.
There are 7 status types:
* created (Created)
* restarting (Restarting)
* running or Up (Running)
* removing (Removing)
* paused (Paused)
* exited (Stopped)
* dead (Dead)
**PORTS:** Container port information and connection type used (tcpudp).
**NAMES:** Automatically assigned container name.
Use the `docker logs` command on the host to view the standard output inside the container:
@:~$ docker logs 2b1b7a428627
!(#)
@:~$ docker logs amazing_cori
!(#)
* * *
## Stopping a Container
We use the **docker stop** command to stop the container:
!(#)
Check with `docker ps`, the container has stopped working:
@:~$ docker ps
You can see the container is no longer there.
You can also use the following command to stop:
@:~$ docker stop amazing_cori
YouTip