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 enter instructions in a docker container

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

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to enter instructions in a docker container". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to enter instructions in a docker container.

Docker is an open source project that provides an open platform for lightweight containers that package, distribute, and run arbitrary programs. It has no language support, framework, or packaging system restrictions, and can be run anywhere, at any time, from small home computers to high-end servers.

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 means to add the code in quotation marks 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, indicating that 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, except that when printing the output through the ps command, only the first few bits are displayed for convenience.

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:/#

At this point, I believe you have a deeper understanding of "how to enter instructions in the docker container". 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