Docker Hello World
Docker allows you to run applications inside a container; use the docker run command to run an application inside a container.
Output Hello world
runoob@runoob:~$ docker run ubuntu:15.10 /bin/echo "Hello world"
Hello world

Explanation of each parameter:
-
docker: Docker's binary executable file.
-
run: Combined with the preceding docker to run a container.
-
ubuntu:15.10 Specify the image to run. Docker first checks whether 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 executed in the started container
The full meaning of the above command can be explained as: Docker creates a new container from the ubuntu15.10 image, then executes bin/echo "Hello world" inside the container, and outputs the result.
Run an interactive container
We use docker's two parameters, -i and -t, to enable the container run by docker to have the ability to **"converse"**:
runoob@runoob:~$ docker run -i -t ubuntu:15.10 /bin/bash
root@0123ce188bd8:/#
Explanation of each parameter:
-
-t: Allocate a pseudo-TTY or terminal in the new container.
-
-i: Allow you to interact with the container's standard input (STDIN).
Note the second line root@0123ce188bd8:/#; at this point we have entered a container of the ubuntu15.10 system
We try to run the command ** cat /proc/version in the containerandls** to view the current system's 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 using CTRL+D.
root@0123ce188bd8:/# exit
exit
root@runoob:~#
Note that root@runoob:~# in the third line indicates we have exited the current container and returned to the current host.
Start a container (detached mode)
Use the following command to create a container that runs as a process
runoob@runoob:~$ 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 of characters
2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63
This long string is called the container ID, which is unique to each container; we can use the container ID to see what happened to the corresponding container.
First, we need to confirm the container is running, which we can do through docker ps to check:
runoob@runoob:~$ 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 the container was started.
CREATED: The container's creation time.
STATUS: Container status.
There are 7 statuses:
- created (created)
- restarting (restarting)
- running or Up (running)
- removing (removing)
- paused (paused)
- exited (stopped)
- dead (dead)
PORTS: The container's port information and the connection type used (tcp/udp)
NAMES: The automatically assigned container name.
Use the docker logs command on the host to view the container's standard output:
runoob@runoob:~$ docker logs 2b1b7a428627

runoob@runoob:~$ docker logs amazing_cori

Stop a container
We use docker stop command to stop the container:

Check with docker ps; the container has stopped working:
runoob@runoob:~$ docker ps
You can see the container is no longer there.
You can also stop it with the following command:
runoob@runoob:~$ docker stop amazing_cori