Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Hello World with Docker

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "how to use Docker to achieve Hello World", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Docker to achieve Hello World.

Docker Hello World

Docker allows you to run an application inside a container and use the docker run command to run an application inside the container.

Output Hello world

[root@huixuan ~] # docker run ubuntu:15.10 / bin/echo "Hello World"

Unable to find image 'ubuntu:15.10' locally

Trying to pull repository docker.io/library/ubuntu...

15.10: Pulling from docker.io/library/ubuntu

7dcf5a444392: Pull complete

759aa75f3cee: Pull complete

3fa871dc8a2b: Pull complete

224c42ae46e7: Pull complete

Digest: sha256:02521a2d079595241c6793b2044f02eecf294034f31d6e235ac4b2b54ffc41f3

Status: Downloaded newer image for docker.io/ubuntu:15.10

Hello World

[root@huixuan ~] #

Each parameter is resolved:

Docker: the binary execution file for Docker.

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

Ubuntu:15.10 specifies the image to run. Docker first looks up whether the image exists on the local host. If it does not exist, Docker downloads the public image from the image repository Docker Hub.

/ bin/echo "Hello world": commands executed in the startup container

The complete meaning of the above command can be interpreted as: Docker creates a new container in the ubuntu15.10 image, then executes bin/echo "Hello world" in the container, and then outputs the result.

Run an interactive container

We use the two parameters of docker-I-t to enable the container running by docker to achieve the ability of "dialogue".

[root@huixuan] # docker run-I-t ubuntu:15.10 / bin/bash

Root@9eb6867b6b7a:/#

Each parameter is resolved:

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

-I: allows you to interact with standard input (STDIN) in the container.

At this point we have entered a container of ubuntu15.10 system

We try to run the commands cat / proc/version and ls in the container to view the version information of the current system and the list of files in the current directory, respectively.

Root@9eb6867b6b7a:/# cat / proc/version

Linux version 3.10.0-514.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)) # 1 SMP Tue Nov 22 16:42:41 UTC 2016

Root@9eb6867b6b7a:/# ls

Bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

Root@9eb6867b6b7a:/#

We can exit the container by running the exit command or by using CTRL+D.

Start the container (background mode)

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

[root@huixuan] # docker run-d ubuntu:15.10 / bin/sh-c "while true; do echo hello world; sleep 1; done"

Bf9a047d6e66312523cf21d36e737d706076d9e7549c9677010006f882045741

[root@huixuan ~] #

In the output, we do not see the expected "hello world", but a string of long characters

Bf9a047d6e66312523cf21d36e737d706076d9e7549c9677010006f882045741

This long string is called container ID and is unique to each container. We can use the container ID to see what happens to the corresponding container.

First, we need to make sure that the container is running, which can be checked through docker ps.

[root@huixuan ~] # docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

Bf9a047d6e66 ubuntu:15.10 "/ bin/sh-c 'while..." 48 seconds ago Up 47 seconds elastic_einstein

[root@huixuan ~] #

CONTAINER ID: container ID

NAMES: automatically assigned container name

Use the docker logs command within the container to view the standard output within the container

[root@huixuan ~] # docker logs bf9a047d6e66

Hello world

Hello world

Hello world

Hello world

Hello world

Hello world

[root@huixuan ~] # docker logs elastic_einstein

Hello world

Hello world

Hello world

Hello world

Hello world

Hello world

Hello world

Hello world

Hello world

Stop the container

We use the docker stop command to stop the container:

[root@huixuan ~] # docker stop bf9a047d6e66

Bf9a047d6e66

[root@huixuan ~] #

Through the docker ps view, the container has stopped working:

[root@huixuan ~] # docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

[root@huixuan ~] #

You can also stop it with the following command:

[root@huixuan ~] # docker stop elastic_einstein

At this point, I believe you have a deeper understanding of "how to use Docker to achieve Hello World". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report