大网数据广告活动
服务器运维问答

The right way to keep docker container started when it used for periodic tasks

· 52CE 我爱测

I have docker container with installed and configured software. There is no any programm supposed to be started/runned all the time. What I want - its ability to start some command depending on external events. like: docker exec …

问题原文

原帖链接 · Korjavin Ivan · CC BY-SA 3.0

I have docker container with installed and configured software.

There is no any programm supposed to be started/runned all the time.

What I want - its ability to start some command depending on external events. like:

docker exec mysupercont /path/to/mycommand -bla -for

and

docker exec mysupercont /path/to/myothercommand 

But "exec" impossible when container is stopped, and also this container have some "working" data inside, which used for that commands, so I can't use

docker run ...

each time, because it recreate container from image and destroy my data.

What is the "right" and the "best" way to keep such container runned? Which command I can start inside?

已采纳解答

原帖链接 · MSemochkin · CC BY-SA 3.0

You do not need to perform each time docker run.

docker run is actually a sequence of two commands: "create" and "start".

When you run the container, you must specify the "-it":

-i, --interactive=false Keep STDIN open even if not attached
-t, --tty=false Allocate a pseudo-TTY

Example:

docker run -it debian:stable bash

After the work was completed command specified at startup (in my example bash). For example, you perform the "exit". Container stops:

CONTAINER ID        IMAGE                      COMMAND                CREATED             STATUS                     PORTS               NAMES
1329c99a831b        debian:stable              "bash"                 51 seconds ago      Exited (0) 1 seconds ago                       goofy_bardeen

Now you can start it again

docker start 1329c99a831b

The container is started and again executes the command "bash".
Connect to this session "bash" with the command

docker attach 1329c99a831b

To sum up: you have to understand the difference between the run and start container.
Besides, look at the documentation for the role of parameters "-i t" and "-d" for the "Run"

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-01-23)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。