In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to make the container execute commands by docker". 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!
What if docker asks the container to execute the command? In Docker, we usually have two ways to execute commands, one is to go directly to the command line of the container, execute in the terminal and view the results, and the other is to execute in the background and do not check the results in the terminal.
1. Enter the container command line
Su rootdocker run-I-t ubuntu:16.04 / bin/bash# or execute the following sentence docker run-it ubuntu:16.04 / bin/bash
The purpose of-I and-t is to help you enter interactive mode, without which you will not be able to enter the container, let alone use the command line. / bin/bash means to enter commands using the command line of the container. The implementation results are as follows:
Root@7d150a0fb029:/#
Root is followed by container id, indicating that you have entered the container and are ready to enter commands. You can enter some common commands to try:
Root@7d150a0fb029:/# docker run-I-t ubuntu:16.04 / bin/bash
As a result, the kernel version information of the image is output:
Linux version 4.13.0-36-generic (buildd@lgw01-amd64-033) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)) # 40~16.04.1-Ubuntu SMP Fri Feb 16 23:25:58 UTC 2018
The container can be exited through exit.
2. Execute commands in the background
Enter the following code, and-d means running in the background, which can be viewed directly through docker run-help to see the function of the command. The following sentence means to add the code in quotes to the mirrored program and run it in the background.
Docker run-d ubuntu:16.04 / bin/sh-c "while true; do echo hello world; sleep 1 done"
Instead of showing the result of the run, the output shows the newly created container id. Here, each time you enter the docker run command, a new container will be created and a new id will be generated.
The output is as follows:
C7188be9bd7b5c0aa91c5eeb72013996c89bfdc2181d1a5ff7f880af6f8aff99
Take a look at the running container:
Root@cdl-XPS15R:/home/cdl# docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc7188be9bd7b ubuntu:16.04 "/ bin/sh-c'while t..." 13 seconds ago Up 11 seconds keen_vaughan1b6ba17f7df7 ubuntu:16.04 "/ bin/bash" 2 minutes ago Exited (0) 2 minutes ago cocky_noether7d150a0fb029 ubuntu:16.04 "/ bin/bash" 4 minutes ago Exited (0) 2 minutes ago hopeful_banach
Here, you will find that the id of the first container is exactly the same as the first few id of the container running in the background, but the length is much longer, in fact, it is the same, but only the first few bits are displayed for convenience when printing output through the ps command.
If you want to enter the command line of a container running in the background without creating a new container, simply enter the following command:
# exec is to enter an existing container, and run is to create a new container docker exec-it c7188be9bd7b / bin/bash
Entering that longer id has the same effect:
Docker exec-it c7188be9bd7b5c0aa91c5eeb72013996c89bfdc2181d1a5ff7f880af6f8aff99 / bin/bash
The output is as follows:
Root@c7188be9bd7b:/#
This is the end of the content of "how to make the container execute commands by docker". 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.