Skip to content

Commit d8269ea

Browse files
committed
Ability to execute any command passed to container
1 parent ec02f07 commit d8269ea

File tree

3 files changed

+57
-24
lines changed

3 files changed

+57
-24
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ RUN \
1313
# Add supervisord conf, bootstrap.sh files
1414
ADD container-files /
1515

16-
CMD ["/config/bootstrap.sh"]
16+
ENTRYPOINT ["/config/bootstrap.sh"]

README.md

+28-11
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
1-
# CentOS-7 with supervisord launcher
1+
# CentOS-7 with supervisord launcher | Docker
22

33
This is a CentOS-7 Docker [million12/centos-supervisor](https://registry.hub.docker.com/u/million12/centos-supervisor/) image, perfect in case when you need to launch more then one process inside a container. This image is based on official [centos:centos7](https://registry.hub.docker.com/_/centos/) and it adds only ca. 20MB on top of it.
44

5-
Things included:
5+
## What's included
66

7-
##### - init scripts
7+
##### - bootstrap.sh script
8+
9+
The container has an **ENTRYPOINT** set to `/config/bootstrap.sh`. It iterates through all `/config/init/*.sh` scripts and runs them, then launches supervisord. See [bootstrap.sh](container-files/config/bootstrap.sh) for details.
810

9-
Add your .sh scripts to `/config/init` to have them executed when container starts. The bootstrap script is configured to run them just before supervisord starts. See [million12/nginx](https://github.com/million12/docker-nginx) for examples.
11+
By default, the **CMD** option in Dockerfile is empty, but the bootstrap.sh script is configured to run everything which is passed into it. Therefore you can launch it in several ways:
12+
* detached mode, no argument(s) passed: supervisord starts in foreground mode and stays until container is stopped.
13+
* detached mode, some argument(s) passed: arguments are executed; supervisord starts in foreground mode and stays until container is stopped.
14+
* interactive mode with TTY (-it), no argument(s) passed: supervisord starts in background mode; interactive bash waits for user input. Exiting from bash (CMD+D) exists the container.
15+
* interactive mode with TTY (-it), some argument(s) passed: supervisord starts in background mode, passed command is executed; container exits.
1016

1117
##### - supervisord
1218

13-
Just add you supervisord config(s) to `/etc/supervisor.d/` directory to launch your service. For example in your `Dockerfile` you could put:
19+
Supervisord is installed and loads services to run from `/etc/supervisor.d/` directory. Add your own files there to launch your services. For example in your `Dockerfile` you could put:
1420
```ADD my-supervisord-service.conf /etc/supervisord.d/my-supervisord-service.conf```
1521

1622
Learn more about about [supervisord inside containers on official Docker documentation](https://docs.docker.com/articles/using_supervisord/).
1723

24+
##### - init scripts
25+
26+
You can add your .sh scripts to `/config/init` directory to have them executed when container starts. The bootstrap script is configured to run them just before supervisord starts. See [million12/nginx](https://github.com/million12/docker-nginx) for example usage.
27+
1828
##### - error logging
1929

2030
Logfile for supervisord is switched off to avoid logging inside container. Instead, all logs are easily available via `docker logs [container name]`.
2131

22-
This is probably the best approach if you'd like to source your logs from outside the container via `docker logs` (also via CoreOS `journald') and you don't want to worry about logging and log management inside your container and/or data volume.
32+
This is probably the best approach if you would like to source your logs from outside the container via `docker logs` (also via CoreOS `journald') and you do not want to worry about logging and log management inside your container and/or data volume.
2333

2434
##### - /data volume
2535

@@ -36,20 +46,27 @@ Recommended structure:
3646

3747
## Usage
3848

39-
This container is configured to run your service(s) both in interactive and non-interactive mode (see [bootstrap.sh](config/init/bootstrap.sh) script).
49+
As explained above, this container is configured to run your service(s) both in interactive and non-interactive modes.
4050

41-
`docker run -it million12/centos-supervisor` (interactive)
42-
`docker run -d million12/centos-supervisor` (detached, non-interactive)
51+
`docker run -it million12/centos-supervisor`: runs supervisord, then interactive bash shell and waits for user's input. Exiting from the shell kills the container.
52+
53+
`docker run -it million12/centos-supervisor ps aux`: runs supervisord, then `ps aux` command inside container and exists.
54+
55+
`docker run -it million12/centos-supervisor top`: runs supervisord, then `top` tool. Exiting from top exits the container.
56+
57+
`docker run -d million12/centos-supervisor`: detached, runs supervisord in foreground mode and its configured services
58+
59+
`docker run -d million12/centos-supervisor touch 'test-file'`: detached, runs `touch 'test-file'` command, then supervisord in foreground mode and its configured services
4360

4461

4562
## Build
4663

47-
`docker build --tag=million12/centos-supervisor`
64+
`docker build --tag=million12/centos-supervisor .`
4865

4966

5067
## Author
5168

5269
Author: Marcin Ryzycki (<marcin@m12.io>)
5370
Author: Przemyslaw Ozgo (<linux@ozgo.info>)
5471

55-
This work is also inspired by [maxexcloo](https://github.com/maxexcloo)'s work on his [docker images](https://github.com/maxexcloo/Docker). Thanks!
72+
This work is also inspired by [maxexcloo](https://github.com/maxexcloo)'s work on his [docker images](https://github.com/maxexcloo/Docker). Many thanks!

container-files/config/bootstrap.sh

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#!/bin/bash
22

33
set -e
4+
set -u
5+
6+
# Supervisord default params
7+
SUPERVISOR_PARAMS='-c /etc/supervisord.conf'
8+
49

510
# Create directories for supervisor's UNIX socket and logs (which might be missing
611
# as container might start with /data mounted from another data-container).
@@ -13,19 +18,30 @@ if [ "$(ls /config/init/)" ]; then
1318
fi
1419

1520

16-
SUPERVISOR_PARAMS='-c /etc/supervisord.conf'
17-
18-
# Do we have TTY?
21+
# We have TTY, so probably an interactive container...
1922
if test -t 0; then
23+
# Run supervisord detached...
2024
supervisord $SUPERVISOR_PARAMS
21-
while true; do
22-
# echo "Exit supervisorctl with Ctrl-D. Detach with Ctrl-P + Ctrl-Q."
23-
# supervisorctl $SUPERVISOR_PARAMS
24-
echo "Exit shell with Ctrl-D. Detach with Ctrl-P + Ctrl-Q."
25+
26+
# Some command(s) has been passed to container? Execute them and exit.
27+
# No commands provided? Run bash.
28+
if [[ $@ ]]; then
29+
eval $@
30+
else
2531
export PS1='[\u@\h : \w]\$ '
26-
bash
27-
done
28-
fi
32+
/bin/bash
33+
fi
2934

30-
# Run supervisord in foreground when no TTY
31-
supervisord -n $SUPERVISOR_PARAMS
35+
# Detached mode? Run supervisord in foreground, which will stay until container is stopped.
36+
else
37+
# If some extra params were passed, execute them before.
38+
# @TODO It is a bit confusing that the passed command runs *before* supervisord,
39+
# while in interactive mode they run *after* supervisor.
40+
# Not sure about that, but maybe when any command is passed to container,
41+
# it should be executed *always* after supervisord? And when the command ends,
42+
# container exits as well.
43+
if [[ $@ ]]; then
44+
eval $@
45+
fi
46+
supervisord -n $SUPERVISOR_PARAMS
47+
fi

0 commit comments

Comments
 (0)