Skip to content

Commit

Permalink
Merge pull request #24 from docksal/develop
Browse files Browse the repository at this point in the history
Release v1.2.0
  • Loading branch information
lmakarov authored Mar 15, 2018
2 parents cf12658 + 360c6b3 commit 2a65974
Show file tree
Hide file tree
Showing 16 changed files with 673 additions and 273 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
projects
42 changes: 35 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ sudo: required
language: generic

env:
OS: linux
IMAGE_NAME: vhost-proxy:test
REPO: docksal/vhost-proxy
IMAGE_VHOST_PROXY: ${REPO}:dev
DOCKSAL_VHOST_PROXY_ACCESS_LOG: 1
DOCKSAL_VHOST_PROXY_DEBUG_LOG: 1
DOCKSAL_VHOST_PROXY_STATS_LOG: 1
# Only use seconds here, so that these can be used with "sleep" as well
PROJECT_INACTIVITY_TIMEOUT: 30s
PROJECT_DANGLING_TIMEOUT: 60s
PROXY_DEBUG: 1
DOCKSAL_VERSION: master
PROJECTS_ROOT: ${TRAVIS_BUILD_DIR}/projects
DOCKSAL_VERSION: develop

services:
- docker
Expand All @@ -18,12 +21,37 @@ install:
- sudo sudo curl -L https://raw.githubusercontent.com/docksal/docksal/${DOCKSAL_VERSION}/bin/fin -o /usr/local/bin/fin && sudo chmod +x /usr/local/bin/fin
- fin version
- fin update
- fin docker build -t ${IMAGE_NAME} .
- PROJECTS_ROOT=$TRAVIS_BUILD_DIR IMAGE_VHOST_PROXY=$IMAGE_NAME fin reset proxy
- fin sysinfo

before_script:
# Pull curl image with http2 support (used in tests)
- docker pull badouralix/curl-http2
- .travis/before_script.sh
- ls -la "$PROJECTS_ROOT"
- fin pl -a

script:
- bats tests/smoke-test.bats
- make
- make start
- make exec -e CMD="env" # Views env variables picked up by proxy
- make exec -e CMD="ls -la /projects" # View the contents of the /projects folder
- make test

after_success: |
if [[ "${TRAVIS_PULL_REQUEST}" == "false" ]]; then
# develop => edge
[[ "${TRAVIS_BRANCH}" == "develop" ]] && TAG="edge"
# master => latest
[[ "${TRAVIS_BRANCH}" == "master" ]] && TAG="latest"
# tags/v1.2.0 => 1.2
[[ "${TRAVIS_TAG}" != "" ]] && TAG="${TRAVIS_TAG:1:3}"
if [[ "$TAG" != "" ]]; then
docker login -u "${DOCKER_USER}" -p "${DOCKER_PASS}"
docker tag ${IMAGE_VHOST_PROXY} ${REPO}:${TAG}
docker push ${REPO}:${TAG}
fi
fi
after_failure:
- make logs
56 changes: 45 additions & 11 deletions .travis/before_script.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
#!/usr/bin/env bash

# Store build directory
cwd=$(pwd)

rm -rf ${cwd}/projects
mkdir -p ${cwd}/projects/project1
mkdir -p ${cwd}/projects/project2
mkdir -p ${cwd}/projects/project3

# Regular project
git clone https://github.com/docksal/drupal7.git ../drupal7
cwd=$(pwd) && cd ../drupal7
fin start
cd $cwd

# Project with the "io.docksal.permanent" flag set to true
# This project will not be automatically cleaned even after DANGLING_TIMEOUT period.
git clone https://github.com/docksal/drupal8.git ../drupal8
cwd=$(pwd) && cd ../drupal8
cd ${cwd}/projects/project1
mkdir .docksal && mkdir docroot
echo "Hello world: Project 1" > docroot/index.html
fin reset -f

# Permanent project
# "io.docksal.permanent=true" (will not be automatically cleaned even after DANGLING_TIMEOUT period)
cd ${cwd}/projects/project2
mkdir .docksal && mkdir docroot
echo "Hello world: Project 2" > docroot/index.html
local_yml="
version: '2.1'
Expand All @@ -19,6 +28,31 @@ services:
- io.docksal.permanent=true
"
echo "$local_yml" > .docksal/docksal-local.yml
fin reset -f

# Nodejs app project
# "io.docksal.virtual-port=3000" (proxy forwards HTTP requests to a custom port 3000 instead of the default 80)
cd ${cwd}/projects/project3
git clone https://github.com/docksal/example-nodejs.git .
# Make this project permanent too, otherwise it will be killed before tests run
local_yml="
version: '2.1'
services:
cli:
labels:
- io.docksal.permanent=true
"
echo "$local_yml" > .docksal/docksal-local.yml
fin reset -f

fin start
cd $cwd
# Nodejs app (standalone container)
# Node need to make this one permanent ("io.docksal.permanent=true") since it is a standalone container and not a project
fin docker rm -vf nodejs || true
fin docker run -d --name=nodejs \
-v $(pwd):/app \
--label=io.docksal.virtual-host=nodejs.docksal \
--label=io.docksal.virtual-port=3000 \
--expose 3000 \
node:alpine \
node /app/index.js
44 changes: 28 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ RUN apk add --no-cache \
nginx-lua \
&& rm -rf /var/cache/apk/*

ENV DOCKER_VERSION 17.06.0-ce
ENV DOCKER_GEN_VERSION 0.7.3
ARG DOCKER_VERSION=17.09.1-ce
ARG DOCKER_GEN_VERSION=0.7.4
ARG GOTPL_VERSION=0.1.5

# Install docker client binary from Github (if not mounting binary from host)
RUN curl -sSL -O "https://download.docker.com/linux/static/stable/x86_64/docker-$DOCKER_VERSION.tgz" \
# Install docker client binary (if not mounting binary from host)
RUN curl -sSL -O "https://download.docker.com/linux/static/stable/x86_64/docker-${DOCKER_VERSION}.tgz" \
&& tar zxf docker-$DOCKER_VERSION.tgz && mv docker/docker /usr/local/bin && rm -rf docker-$DOCKER_VERSION*

# Install docker-gen
ENV DOCKER_GEN_TARFILE docker-gen-alpine-linux-amd64-$DOCKER_GEN_VERSION.tar.gz
RUN curl -sSL https://github.com/jwilder/docker-gen/releases/download/$DOCKER_GEN_VERSION/$DOCKER_GEN_TARFILE -O \
ARG DOCKER_GEN_TARFILE=docker-gen-alpine-linux-amd64-$DOCKER_GEN_VERSION.tar.gz
RUN curl -sSL -O "https://github.com/jwilder/docker-gen/releases/download/${DOCKER_GEN_VERSION}/${DOCKER_GEN_TARFILE}" \
&& tar -C /usr/local/bin -xvzf $DOCKER_GEN_TARFILE && rm $DOCKER_GEN_TARFILE

# Install gotpl
ARG GOTPL_TARFILE=gotpl-alpine-linux-amd64-${GOTPL_VERSION}.tar.gz
RUN curl -sSL -O "https://github.com/wodby/gotpl/releases/download/${GOTPL_VERSION}/${GOTPL_TARFILE}" \
&& tar -C /usr/local/bin -xvzf $GOTPL_TARFILE && rm $GOTPL_TARFILE

RUN chown -R nginx:nginx /var/lib/nginx

# Generate a self-signed cert
Expand All @@ -29,24 +35,30 @@ RUN apk add --no-cache openssl \
-keyout /etc/nginx/server.key -out /etc/nginx/server.crt \
&& apk del openssl

COPY conf/nginx/nginx.conf /etc/nginx/nginx.conf
COPY conf/nginx/default.conf.tmpl /etc/nginx/default.conf.tmpl
COPY conf/nginx/default_locations.conf /etc/nginx/default_locations.conf
COPY conf/nginx /opt/conf/nginx
COPY conf/sudoers /etc/sudoers
COPY conf/supervisord.conf /etc/supervisor.d/supervisord.ini
# Override the main supervisord config file, since some parameters are not overridable via an include
# See https://github.com/Supervisor/supervisor/issues/962
COPY conf/supervisord.conf /etc/supervisord.conf
COPY conf/crontab /var/spool/cron/crontabs/root
COPY bin /usr/local/bin
COPY www /var/www/proxy

# Fix permissions
RUN chmod 0440 /etc/sudoers

# Disable INACTIVITY_TIMEOUT by default
ENV PROJECT_INACTIVITY_TIMEOUT 0
# Disable DANGLING_TIMEOUT by default
ENV PROJECT_DANGLING_TIMEOUT 0
# Disable debug output by default
ENV PROXY_DEBUG 0

ENV \
# Disable INACTIVITY_TIMEOUT by default
PROJECT_INACTIVITY_TIMEOUT=0 \
# Disable DANGLING_TIMEOUT by default
PROJECT_DANGLING_TIMEOUT=0 \
# Disable access log by default
ACCESS_LOG=0 \
# Disable debug output by default
DEBUG_LOG=0 \
# Disable stats log by default
STATS_LOG=0

ENTRYPOINT ["docker-entrypoint.sh"]

Expand Down
50 changes: 50 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
-include env_make

VERSION ?= dev

REPO = docksal/vhost-proxy
NAME = docksal-vhost-proxy

.PHONY: build test push shell run start stop logs debug clean release

build:
fin docker build -t $(REPO):$(VERSION) .

test:
IMAGE=$(REPO):$(VERSION) bats tests/smoke-test.bats

push:
fin docker push $(REPO):$(VERSION)

exec:
fin docker exec -it $(NAME) $(CMD)

shell:
make exec -e CMD=bash

start:
fin system reset vhost-proxy

stop:
fin docker stop $(NAME)

logs:
fin docker logs $(NAME)

clean:
fin docker rm -f $(NAME)
rm -rf projects
fin cleanup

debug: build start
fin docker logs -f $(NAME)

release: build
make push -e VERSION=$(VERSION)

# Curl command with http2 support via a docker container
# Usage: make curl -e ARGS='-kI https://docksal.io'
curl:
docker run -t --rm badouralix/curl-http2 $(ARGS)

default: build
63 changes: 53 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,50 @@ This image(s) is part of the [Docksal](http://docksal.io) image library.

Start the proxy container:

```
```bash
docker run -d --name docksal-vhost-proxy --label "io.docksal.group=system" --restart=always --privileged --userns=host \
-p "${DOCKSAL_VHOST_PROXY_PORT_HTTP:-80}":80 -p "${DOCKSAL_VHOST_PROXY_PORT_HTTPS:-443}":443 \
-e PROJECT_INACTIVITY_TIMEOUT="${PROJECT_INACTIVITY_TIMEOUT:-0}" \
-v /var/run/docker.sock:/var/run/docker.sock \
docksal/vhost-proxy
```

## Configuration
## Container configuration

Proxy reads routing settings from container labels. The following labels are supported:

`io.docksal.virtual-host`

Virtual host mapping. Supports any domain (but does not handle DNS), multiple values separated by commas, wildcard
sub-domains.

Example: `io.docksal.virtual-host=example1.com,*.example2.com`


`io.docksal.virtual-port`

Virtual port mapping. Useful when a container exposes an non-default HTTP port (other than port `80`).
Only supports HTTP, single value.

Example: `io.docksal.virtual-port=3000`

### Example

Launching a nodejs app container using port `3000` and host `myapp.example.com`

```bash
docker run -d --name=nodejs \
-v $(pwd):/app \
--label=io.docksal.virtual-host=myapp.example.com \
--label=io.docksal.virtual-port=3000 \
--expose 3000 \
node:alpine \
node /app/index.js
```

## Advanced proxy configuration

These advanced settings can be used in CI sandbox environments and help keep the resource usage down by stopping
Docksal project containers after a period of inactivity. Projects are automatically restarting upon a new HTTP request.

`PROJECT_INACTIVITY_TIMEOUT`

Expand All @@ -32,34 +67,42 @@ This option is inactive by default (set to `0`).

`PROJECT_DANGLING_TIMEOUT`

!!!warning "WARNING"
This is a destructive option. Use at your own risk!
**WARNING: This is a destructive option. Use at your own risk!**

Defines the timeout (e.g. 168h) of inactivity after which the project stack and code base will be entirely wiped out from the host.
This option is inactive by default (set to `0`).

For the cleanup job to work, proxy needs access to the directory, where project code bases are located on the host.
For the cleanup job to work, proxy needs access to the projects directory on the host.
Create a Docker bind volume pointing to the directory where projects are stored:

```
docker volume create --name docksal_projects --opt type=none --opt device=$PROJECTS_ROOT --opt o=bind
```

Start the proxy container with two additional options (in the middle):
then pass it using `-v docksal_projects:/projects` in `docker run` command.

```
Example (extra configuration in the middle):

```bash
docker run -d --name docksal-vhost-proxy --label "io.docksal.group=system" --restart=always --privileged --userns=host \
-p "${DOCKSAL_VHOST_PROXY_PORT_HTTP:-80}":80 -p "${DOCKSAL_VHOST_PROXY_PORT_HTTPS:-443}":443 \
-e PROJECT_INACTIVITY_TIMEOUT="${PROJECT_INACTIVITY_TIMEOUT:-0}" \

-e PROJECT_INACTIVITY_TIMEOUT="${PROJECT_INACTIVITY_TIMEOUT:-0}" \
-e PROJECT_DANGLING_TIMEOUT="${PROJECT_DANGLING_TIMEOUT:-0}" \
-v docksal_projects:/projects \

-v /var/run/docker.sock:/var/run/docker.sock \
docksal/vhost-proxy
```

`PROXY_DEBUG`
## Logging and debugging

The following container environment variables can be used to enabled various logging options (disabled by default).

`ACCESS_LOG` - Set to `1` to enable access logging.
`DEBUG_LOG` - Set to `1` to enable debug logging.
`STATS_LOG` - Set to `1` to enable project stats logging.

Set to `1` to enable debug logging. Check logs with `docker logs docksal-vhost-proxy`.
Check logs with `docker logs docksal-vhost-proxy`.
4 changes: 4 additions & 0 deletions bin/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

# Service mode (run as root)
if [[ "$1" == "supervisord" ]]; then
# Generate config files from templates
gotpl /opt/conf/nginx/nginx.conf.tmpl > /etc/nginx/nginx.conf
gotpl /opt/conf/nginx/proxyctl.conf.tmpl > /etc/nginx/proxyctl.conf

exec supervisord -c /etc/supervisord.conf
# Command mode (run as docker user)
else
Expand Down
Loading

0 comments on commit 2a65974

Please sign in to comment.