Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nvanheuverzwijn committed Mar 6, 2017
1 parent ab1d730 commit 7638426
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Docker Init Entrypoint

The script `entrypoint.sh` is a basic service init framework for containers. It will search scripts in `/k/start.d` and `/k/stop.d` to start and stop services respectively. The `CMD` directive of the dockerfile is also set in background and is the process the entrypoint will wait on before shutting down.

## Install

Simply use the `ADD` and `ENTRYPOINT` directive i.e.:

```
FROM debian:latest
ADD https://github.com/kronostechnologies/docker-init-entrypoint/releases/download/1.0.0/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
```

## Bootstraping Folders

The bootstraping folders are `/k/start.d/` and `/k/stop.d` which contains scripts to start and stop services respectively.

Here are some general advice about those scripts :

- Scripts must not be blocking (hanging).
- Scripts' name should start with three (3) digits `100_apache2.sh`.

The `CMD` directive of the dockerfile is also set in background and is the process the entrypoint will wait on before shutting down. Generally, `CMD` would be a command that would agregate logs to stdout such as `tail`. You can also use a custom script.

> Note: you cannot set `CMD` to an interactive command such as `/bin/bash`.
## Examples

Examples can be found in the `examples` directory of this repository.
37 changes: 37 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

execute_script() {
local script=$1

chmod +x "${script}"
echo "> ${script}... processing"
$script
}

process_scripts() {
local path=$1

for script in $(ls $path | sort -n); do
execute_script "${path}/${script}"
done
}

finish() {
echo '> Stopping all services..'
process_scripts /k/stop.d
echo '> Shutting down now.'
}

trap finish SIGTERM SIGQUIT SIGINT

echo '> Starting all services..'
process_scripts /k/start.d
echo '> Fully Booted.'

if [[ -n "${@}" ]]; then
echo "> Executing \`${@}\`"
$@ &
wait $!
else
sleep infinity
fi
3 changes: 3 additions & 0 deletions examples/apache2/100-start-apache2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

service apache2 start
3 changes: 3 additions & 0 deletions examples/apache2/100-stop-apache2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

service apache2 stop
14 changes: 14 additions & 0 deletions examples/apache2/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM debian:latest

RUN apt-get update && apt-get install -y -q apache2 && apt-get clean

ADD https://github.com/kronostechnologies/docker-init-entrypoint/releases/download/1.0.0/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

COPY ./100-start-apache2.sh /k/start.d/100-apache2.sh
COPY ./100-stop-apache2.sh /k/stop.d/100-apache2.sh

EXPOSE 80

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["tail", "-F", "/var/log/apache2/access.log", "/var/log/apache2/error.log"]
76 changes: 76 additions & 0 deletions examples/apache2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Examples

Below a simple docker file project for apache2.

## File tree
```
14:31:33 nvanheuverzwijn ~/Projects/test tree
.
├── 100-start-apache2.sh (0755)
├── 100-stop-apache2.sh (0755)
└── dockerfile (0644)
```

> Notice the +x mod on shell script
### 100-start-apache2.sh
```
#!/bin/bash
service apache2 start
```

### 100-stop-apache2.sh
```
#!/bin/bash
service apache2 stop
```

### entrypoint.sh
See the file `entrypoint.sh` in this repository

#### dockerfile
```
FROM debian:latest
RUN apt-get update && apt-get install -y -q apache2 && apt-get clean
ADD https://raw.githubusercontent.com/kronostechnologies/docker-init-entrypoint/master/entrypoint.sh /usr/local/bin/entrypoint.sh
COPY ./100-start-apache2.sh /k/start.d/100-apache2.sh
COPY ./100-stop-apache2.sh /k/stop.d/100-apache2.sh
EXPOSE 80
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["tail", "-F", "/var/log/apache2/access.log", "/var/log/apache2/error.log"]
```

## Starting this docker

```
$ docker run --rm --name test apache2
> Starting all services..
> /k/start.d/100-apache2.sh... processing
Starting web server: apache2AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message
.
> Fully Booted.
> Executing `tail -F /var/log/apache2/access.log /var/log/apache2/error.log`
==> /var/log/apache2/access.log <==
==> /var/log/apache2/error.log <==
[Mon Mar 06 19:35:44.938598 2017] [mpm_event:notice] [pid 35:tid 140286129534848] AH00489: Apache/2.4.10 (Debian) configured -- resuming normal operations
[Mon Mar 06 19:35:44.938686 2017] [core:notice] [pid 35:tid 140286129534848] AH00094: Command line: '/usr/sbin/apache2'
```
> The docker is now waiting on the `tail` command.
### Stoping this docker

```
> Stopping all services..
> /k/stop.d/100-apache2.sh... processing
Stopping web server: apache2[Mon Mar 06 19:36:16.956781 2017] [mpm_event:notice] [pid 35:tid 140286129534848] AH00491: caught SIGTERM, shutting down
.
> Shutting down now.
```

0 comments on commit 7638426

Please sign in to comment.