官术网_书友最值得收藏!

Keeping a process running in a background container

The last type of container is the one that you'll use most in production - the background container, which keeps a long-running process running in the background. It's a container that behaves like a Windows Service. In Docker terminology, it's called a detached container, and it's the Docker service that keeps it running in the background. Inside the container, the process runs in the foreground. The process might be a web server or a console application polling a message queue for work, but as long as the process keeps running, Docker will keep the container alive.

I can run a background container from the same image again, specifying the detach option and a command that runs for some minutes:

> docker container run --detach dockeronwindows/ch02-powershell-env ` 
powershell Test-Connection 'localhost' -Count 100

ce7b2604f681871a8dcd2ffd8898257fad26b24edec7135e76aedd47cdcdc427

In this case, when the container has launched control returns to the terminal; the long random string is the ID of the new container. You can run docker container ls and see the container running, and the docker container logs command shows you the console output from the container. For commands operating on specific containers, you can reference them by the container name or by part of the container ID:

> docker container logs ce7

Source Destination IPV4Address IPV6Address
------ ----------- ----------- -----------
CE7B2604F681 localhost
CE7B2604F681 localhost

The --detach flag detaches the container so it moves into the background, and the command in this case just pings localhost repeatedly one hundred times. After a few minutes, the PowerShell command completes so there's no process running and the container exits. That's a key thing to remember - if you want to keep a container running in the background, the process that Docker starts when it runs the container has to keep running.

Now you've seen that a container is created from an image, but it can run in different ways - so you can use the image exactly as it was prepared, or treat the image as a template, with a default startup mode built in. Next, I'll show you how to build that image.

主站蜘蛛池模板: 象州县| 乐山市| 息烽县| 莎车县| 和田县| 禄劝| 保定市| 丹凤县| 稻城县| 绥宁县| 宁津县| 榆社县| 府谷县| 镇康县| 莱芜市| 南开区| 濮阳市| 西宁市| 襄城县| 潞城市| 大冶市| 五寨县| 乐至县| 大庆市| 岳阳县| 武汉市| 喜德县| 安塞县| 湖州市| 罗山县| 徐闻县| 南召县| 东明县| 邛崃市| 大埔区| 会泽县| 大英县| 铜鼓县| 宿松县| 阿瓦提县| 芜湖市|