Skip to content

Commit 963272a

Browse files
Misty Stanley-Jonesjohndmulhausen
Misty Stanley-Jones
authored andcommitted
Updates to multi-process container topic (#3026)
1 parent 481c09b commit 963272a

File tree

4 files changed

+104
-157
lines changed

4 files changed

+104
-157
lines changed

_data/toc.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ guides:
265265
title: Using Chef
266266
- path: /engine/admin/puppet/
267267
title: Using Puppet
268-
- path: /engine/admin/using_supervisord/
269-
title: Using Supervisor with Docker
268+
- path: /engine/admin/multi-service_containers/
269+
title: Run multiple services in a container
270270
- path: /engine/admin/runmetrics/
271271
title: Runtime metrics
272272
- path: /engine/admin/ambassador_pattern_linking/
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
description: How to run more than one process in a container
3+
keywords: docker, supervisor, process management
4+
redirect_from:
5+
- /engine/articles/using_supervisord/
6+
- /engine/admin/using_supervisord/
7+
title: Run multiple services in a container
8+
---
9+
10+
A container's main running process is the `ENTRYPOINT` and/or `CMD` at the
11+
end of the `Dockerfile`. It is generally recommended that you separate areas of
12+
concern by using one service per container. That service may fork into multiple
13+
processes (for example, Apache web server starts multiple worker processes).
14+
It's ok to have multiple processes, but to get the most benefit out of Docker,
15+
avoid one container being responsible for multiple aspects of your overall
16+
application. You can connect multiple containers using user-defined networks and
17+
shared volumes.
18+
19+
The container's main process is responsible for managing all processes that it
20+
starts. In some cases, the main process isn't well-designed, and doesn't handle
21+
"reaping" (stopping) child processes gracefully when the container exists. If
22+
your process falls into this category, you can use the `--init` option when you
23+
run the container. The `--init` flag inserts a tiny init-process into the
24+
container as the main process, and handles reaping of all processes when the
25+
container exits. Handling such processes this way is superior to using a
26+
full-fledged init process such as `sysvinit`, `upstart`, or `systemd` to handle
27+
process lifecycle within your container.
28+
29+
If you need to run more than one service within a container, you can accomplish
30+
this in a few different ways.
31+
32+
- Put all of your commands in a wrapper script, complete with testing and
33+
debugging information. Run the wrapper script as your `CMD`. This is a very
34+
naive example. First, the wrapper script:
35+
36+
```bash
37+
#!/bin/bash
38+
39+
# Start the first process
40+
./my_first_process -D
41+
status=$?
42+
if [ $status -ne 0 ]; then
43+
echo "Failed to start my_first_process: $status"
44+
exit $status
45+
fi
46+
47+
# Start the second process
48+
./my_second_process -D
49+
status=$?
50+
if [ $status -ne 0 ]; then
51+
echo "Failed to start my_second_process: $status"
52+
exit $status
53+
fi
54+
55+
# Naive check runs checks once a minute to see if either of the processes exited.
56+
# This illustrates part of the heavy lifting you need to do if you want to run
57+
# more than one service in a container. The container will exit with an error
58+
# if it detects that either of the processes has exited.
59+
while /bin/true; do
60+
PROCESS_1_STATUS=$(ps aux |grep -q my_first_process)
61+
PROCESS_2_STATUS=$(ps aux |grep -q my_second_process)
62+
if [ $PROCESS_!_STATUS || $PROCESS_2_STATUS ]; then
63+
echo "One of the processes has already exited."
64+
exit -1
65+
fi
66+
sleep 60
67+
done
68+
```
69+
70+
Next, the Dockerfile:
71+
72+
```conf
73+
FROM ubuntu:latest
74+
COPY my_first_process my_first_process
75+
COPY my_second_process my_second_process
76+
COPY my_wrapper_script.sh my_wrapper_script.sh
77+
CMD ./my_wrapper_script.sh
78+
```
79+
80+
- Use a process manager like `supervisord`. This is a moderately heavy-weight
81+
approach that requires you to package `supervisord` and its configuration in
82+
your image (or base your image on one that includes `supervisord`), along with
83+
the different applications it will manage. Then you start `supervisord`, which
84+
manages your processes for you. Here is an example Dockerfile using this
85+
approach, that assumes the pre-written `supervisord.conf`, `my_first_process`,
86+
and `my_second_process` files all exist in the same directory as your
87+
Dockerfile.
88+
89+
```conf
90+
FROM ubuntu:latest
91+
RUN apt-get update && apt-get install -y supervisor
92+
RUN mkdir -p /var/log/supervisor
93+
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
94+
COPY my_first_process my_first_process
95+
COPY my_second_process my_second_process
96+
CMD ["/usr/bin/supervisord"]
97+
```

engine/admin/using_supervisord.md

-148
This file was deleted.

engine/faq.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Server 2016, or Windows 10.
4141

4242
### How do containers compare to virtual machines?
4343

44-
Containers and virtual machines (VMs) are complementary. VMs excel at providing extreme isolation (for example with hostile tenant applications where you need the ultimate break out prevention). Containers operate at the process level, which makes them very lightweight and perfect as a unit of software delivery. While VMs take minutes to boot, containers can often be started in less than a second.
44+
Containers and virtual machines (VMs) are complementary. VMs excel at providing extreme isolation (for example with hostile tenant applications where you need the ultimate break out prevention). Containers operate at the process level, which makes them very lightweight and perfect as a unit of software delivery. While VMs take minutes to boot, containers can often be started in less than a second.
4545

4646
### What does Docker technology add to just plain LXC?
4747

@@ -139,12 +139,10 @@ pattern](admin/ambassador_pattern_linking.md).
139139

140140
### How do I run more than one process in a Docker container?
141141

142-
Any capable process supervisor such as [http://supervisord.org/](
143-
http://supervisord.org/), runit, s6, or daemontools can do the trick. Docker
144-
will start up the process management daemon which will then fork to run
145-
additional processes. As long as the processor manager daemon continues to run,
146-
the container will continue to as well. You can see a more substantial example
147-
[that uses supervisord here](admin/using_supervisord.md).
142+
This approach is discouraged for most use cases. For maximum efficiency and
143+
isolation, each container should address one specific area of concern. However,
144+
if you need to run multiple services within a single container, see
145+
[Run multiple services in a container](admin/multi-service_container.md).
148146

149147
### What platforms does Docker run on?
150148

0 commit comments

Comments
 (0)