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
tutorialpro@tutorialpro:~$ docker run ubuntu:15.10 /bin/echo "Hello world"
Hello world
Parameter breakdown:
- docker: The Docker binary executable.
- run: Used with docker to run a container.
- ubuntu:15.10: Specifies the image to run. Docker first checks if the image exists locally, and if not, it downloads the public image from Docker Hub.
- /bin/echo "Hello world": The command to execute inside the started container.
The complete command means: Docker creates a new container from the ubuntu15.10 image, then executes bin/echo "Hello world" inside the container, and outputs the result.
Running Interactive Containers
By using the -i and -t parameters with docker, we enable the container to have an interactive "conversation" capability:
tutorialpro@tutorialpro:~$ docker run -i -t ubuntu:15.10 /bin/bash
root@0123ce188bd8:/#
Parameter breakdown:
- -t: Assigns a pseudo-terminal or terminal inside the new container.
- -i: Allows you to interact with the container's standard input (STDIN).
Note the second line root@0123ce188bd8:/#
, indicating we have entered a container running the ubuntu15.10 system.
We can try running commands like cat /proc/version and ls 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@tutorialpro:~#
Note the third line root@tutorialpro:~#
, indicating we have exited the current container and returned to the host system.
Starting Containers (Detached Mode)
Use the following command to create a container that runs as a process:
tutorialpro@tutorialpro:~$ 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 instead a long string of characters:
2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63
This long string is called the container ID, which is unique for each container. We can use the container ID to see what the container is doing.
First, we need to confirm the container is running, which can be done with docker ps:
tutorialpro@tutorialpro:~$ docker ps
CONTAINER ID IMAGE COMMAND ...
5917eac21c36 ubuntu:15.10 "/bin/sh -c 'while t…" ...
Output details:
- CONTAINER ID: The container ID.
- IMAGE: The image used.
- COMMAND: The command run when starting the container.
- CREATED: The creation time of the container.
- STATUS: The container status.
Status can be one of seven types:
- created
- restarting
- running or Up
- removing
- paused
- exited
dead
PORTS: The container's port information and the type of connection used (tcp\udp).
- NAMES: The automatically assigned container name.
To view the standard output inside the container from the host, use the docker logs
command:
tutorialpro@tutorialpro:~$ docker logs 2b1b7a428627
tutorialpro@tutorialpro:~$ docker logs amazing_cori
Stopping Containers
We use the docker stop command to stop a container:
By checking with docker ps
, we can see the container has stopped:
tutorialpro@tutorialpro:~$ docker ps
The container is no longer present.
Alternatively, you can stop it with:
tutorialpro@tutorialpro:~$ docker stop amazing_cori