English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Docker Hello World

Docker allows you to run applications inside containers, using docker run The command to run an application inside the container.

Output Hello world

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

Parameter parsing:

  • docker: Docker's binary executable file.

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

  • ubuntu:15.10 Specify the image to be run, Docker first checks if the image exists on the local host, if it does not exist, Docker will download the public image from the image repository Docker Hub.

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

The complete meaning of the above command can be explained as: Docker runs ubuntu15.10 Create a new container with the image, and then execute bin in the container/echo "Hello world", and then output the result.

to run an interactive container

We use two parameters of docker -i -t, to enable the docker container to run"Dialogue"abilities:

w3codebox@w3codebox:~$ docker run -i -t ubuntu:15.10 /bin/bash
root@0123ce188bd8:/#

Parameter parsing:

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

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

Note the second line root@0123ce188bd8:/#, at this point we have entered an ubuntu15.10 the container of the system

We try to run commands in the container cat /proc/versionandlsTo view the current system version information and the file list in the current directory

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 To exit the container.

root@0123ce188bd8:/#  exit
exit
root@w3codebox:~#

Note the third line root@w3codebox:~# Indicates that we have exited the current container and returned to the current host.

Start the container (in background mode)

Use the following command to create a container that runs as a process

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

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

2b1b7a428627c51ab8810d541d759f072b4fc75487eed05812646b8534a2fe63

This long string is called the container ID, which is unique for each container, and we can use the container ID to view what happened to the corresponding container.

Firstly, we need to confirm that the container is running, which can be done through docker ps To view:

w3codebox@w3codebox:~$ docker ps
CONTAINER ID        IMAGE                  COMMAND              ...  
5917eac21c36        ubuntu:15.10           "/bin/sh -c 'while t…"    ...

Output details introduction:

CONTAINER ID: Container ID.

IMAGE: The image used.

COMMAND: Command executed when starting a container.

CREATED: Container creation time.

STATUS: Container status.

The status has7Types:

  • created (created)

  • restarting (restarting)

  • running or Up (running)

  • removing (moving)

  • paused (paused)

  • exited (stopped)

  • dead (dead)

PORTS: Container port information and the type of connection used (tcp\udp).

NAMES: Automatically assigned container name.

Use the docker logs command on the host machine to view the standard output inside the container:

w3codebox@w3codebox:~$ docker logs 2b1b7a428627

w3codebox@w3codebox:~$ docker logs amazing_cori

to stop the container

We use docker stop Command to stop the container:

Use the docker stop command to stop the container: 2b1b7a428627

By checking with docker ps, you can see that the container has stopped working:

w3codebox@w3codebox:~$ docker ps

You can see that the container is no longer there.

You can also stop using the following command:

w3codebox@w3codebox:~$ docker stop amazing_cori