Docker HelloWorld
Docker allows you to run an application inside a container. Use the docker run command to run an application inside a container.
Output Hello world
apidemos@apidemos:~$ docker run ubuntu:15.10 /bin/echo "Hello world"
Hello world
Explanation of each parameter.
- docker: The binary executable of Docker.
- run: Combine with the previous docker to run a container.
- ubuntu:15.10 Specifies the image to run. Docker first looks for the image to exist on the local host, and if it doesn’t, Docker downloads the public image from the image repository Docker Hub.
- /bin/echo "Hello world": Command to be executed in the started container
The full meaning of the above command can be explained as follows: Docker creates a new container with ubuntu15.10 image, then executes bin/echo "Hello world" in the container, and outputs the result.
Running an interactive container
We give docker the ability to run containers that "talk " with the two docker arguments -i -t.
apidemos@apidemos:~$ docker run -i -t ubuntu:15.10 /bin/bash
root@0123ce188bd8:/#
Explanation of each parameter.
- -t: Specifies a pseudo-terminal or terminal within the new container.
- -i: Allows you to interact with the standard input (STDIN) inside the container.
Note the second line root@0123ce188bd8:/#, at which point we are in a container on an ubuntu 15.10 system
Let’s try running the commands cat /proc/version and ls in the container to see the current system version information and the list of files 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 by using CTRL+D.
root@0123ce188bd8:/# exit
exit
root@apidemos:~#
Note in the third line root@apidemos:~# indicates that we have exited the current container and returned to the current host.
Start the container (background mode)
Create a container running in process mode with the following command
apidemos@apidemos:~$ docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63
In the output, instead of the expected "hello world", we see a long string of characters
2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63
This long string is called the container ID, which is unique for each container, and we can use the container ID to see what is happening with the corresponding container.
First, we need to make sure that the container is running, which can be checked with docker ps at
apidemos@apidemos:~$ docker ps
CONTAINER ID IMAGE COMMAND ...
5917eac21c36 ubuntu:15.10 "/bin/sh -c 'while t…" ...
Output Details Description.
CONTAINER ID: The container ID.
IMAGE: The image used.
COMMAND: The command that was run when the container was started.
CREATED: The creation time of the container.
STATUS: The status of the container.
There are 7 types of status.
- created (created)
- restarting (restarting)
- running or Up
- removing
- paused
- exited
- dead
PORTS: Port information of the container and the type of connection used (tcp\udp).
NAMES: The name of the automatically assigned container.
Use the docker logs command within the host host to view the standard output within the container.
apidemos@apidemos:~$ docker logs 2b1b7a428627
apidemos@apidemos:~$ docker logs amazing_cori
Stopping the container
We use the docker stop command to stop the container:
The container has stopped working as seen by docker ps:
apidemos@apidemos:~$ docker ps
You can see that the container is no longer there.
You can also stop it with the following command:
apidemos@apidemos:~$ docker stop amazing_cori