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 run the hello-world image of docker

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the knowledge of "how the hello-world image of docker works". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Introduction

Download the hello-world image.

Use the docker run command.

For example:

$docker run hello-world

Mainly output some text messages, and then quit.

Use docker ps to view the progress of the container.

But it is empty, because as soon as it is run, it will exit, just output the information, and the output will be over.

You can use docker ps-a to view exited processes.

For example:

$docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb25a96c17bf4 hello-world "/ hello" About a minute ago Exited (0) About a minute ago mystifying_saha2. Run the container

The above is just a demonstration of hello world, we run some useful containers.

For example:

$docker run-it ubuntu bash

The ubuntu image is running. If you don't have this image on your system, it will be downloaded first.

Bash is the shell,-t option that indicates that the container entering that ubuntu's image asks Docker to assign a pseudo terminal (pseudo-tty) and bind it to the container's standard input, while-I keeps the container's standard input open.

This is equivalent to entering another ubuntu.

And this ubuntu is isolated from your original system.

This doesn't make much sense. We often use docker to run some services, such as web services.

Try running a nginx container.

$docker run-d-p 80:80-name webserver nginx

The name of the mirror is nginx,--name, which means to name the container webserver.

After running, you use docker ps to check the container.

$docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa1d4b615709b nginx "nginx-g'daemon..." 5 seconds ago Up 4 seconds 0.0.0.0 seconds 80-> 80/tcp, 443/tcp webserver

You can then use curl http://127.0.0.1 to see if the nginx service is running.

Use the docker stop command to stop the container from running.

For example:

$docker stop webserver

After stopping, typing docker ps is found to be empty. At this time, type docker ps-a to view the exit container.

You can use the following command to clear all exited containers.

$docker rm-f $(docker ps-a | grep Exit | awk'{print $1}')

When you ran the nginx container, you used the parameter-p 80:80.

This represents the port mapping.

Before we explain, let's make some changes.

$docker run-d-p 8080 80-- name webserver nginx

Check the running status:

$docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES45e47ed889f8 nginx "nginx-g'daemon..." 4 seconds ago Up 3 seconds 443/tcp, 0.0.0.0 daemon 8080-> 80/tcp webserver

Now you need to use curl http://127.0.0.1:8080 to access it.

That is, in the-p parameter, the first port is the exposed port, the external accessible port.

As for-d, it runs in a guarded state.

Some applications do not need to expose the interface at all, such as the following:

$docker psmposenginx_gitlab_17e4493b736d2 sameersbn/postgresql:9.5-4 "/ sbin/entrypoint.sh" 2 days ago Up 2 days 5432/tcp gitlabcomposenginx_postgresql_18cd6c90f9a52 sameersbn/redis:latest "/ sbin/entrypoint.sh" 2 days ago Up 2 days 6379/tcp

These two ports are inaccessible externally and are not bound to the 0.0.0.0 ip. What is the use of this?

In fact, there is a parameter-- link, which provides a network channel between containers of a stand-alone machine, so that it does not have to be accessed through ip, but is an alias.

For example, run two containers first:

$docker run-- name gitlab-postgresql-d\-- env 'DB_NAME=gitlabhq_production'\-- env' DB_USER=gitlab'-- env 'DB_PASS=password'\-- env' DB_EXTENSION=pg_trgm'\-- volume / srv/docker/gitlab/postgresql:/var/lib/postgresql\ sameersbn/postgresql:9.6-2$ docker run-- name gitlab-redis-d\-- volume / srv/docker/gitlab/redis:/var/lib/redis\ sameersbn/redis:latest

These two containers have two names, gitlab-postgresql and gitlab-redis.

Then start a container that exposes the interface to connect the two containers.

$docker run-- name gitlab- d\-- link gitlab-postgresql:postgresql-- link gitlab-redis:redisio\-- publish 10022 link gitlab-redis:redisio 22-- publish 10080 link gitlab-redis:redisio 80\-- env 'GITLAB_PORT=10080'-- env' GITLAB_SSH_PORT=10022'\-- env 'GITLAB_SECRETS_DB_KEY_BASE=long-and-random-alpha-numeric-string'\-- env' GITLAB_SECRETS_SECRET_KEY_BASE=long-and-random-alpha-numeric-string '\-env' GITLAB_SECRETS_OTP_KEY_BASE=long-and-random-alpha-numeric-string'\-volume / srv/docker/gitlab/gitlab:/home/git/data\ sameersbn/gitlab:8.16.6

Where postgresql and redisio are host aliases for the container.

The container can be restarted using the docker restart command.

Another parameter that is more commonly used is-rm, which means that the container will be deleted after it exits. By default, exited containers are not deleted immediately in order to troubleshoot requirements, except for manual docker rm. We are just executing a random command here to see the results, and there is no need to troubleshoot and retain the results, so using-rm can avoid wasting space.

That's all for "how the hello-world Image of docker works". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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