Merge pull request #887 from brownleej/packaged-dockerfile

Add an official dockerfile
This commit is contained in:
John Brownlee 2018-12-03 13:47:51 -08:00 committed by GitHub
commit 33d2ad5753
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 488 additions and 0 deletions

View File

@ -0,0 +1,71 @@
# Dockerfile
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
FROM ubuntu:18.04
# Install dependencies
RUN apt-get update && \
apt-get install -y wget=1.19.4-1ubuntu2 \
dnsutils=1:9.11.3+dfsg-1ubuntu1.3 && \
rm -r /var/lib/apt/lists/*
# Install FoundationDB Binaries
ARG FDB_VERSION
ARG FDB_WEBSITE=https://www.foundationdb.org
WORKDIR /var/fdb/tmp
RUN wget $FDB_WEBSITE/downloads/$FDB_VERSION/linux/fdb_$FDB_VERSION.tar.gz && \
tar -xzf fdb_$FDB_VERSION.tar.gz --strip-components=1 && \
rm fdb_$FDB_VERSION.tar.gz && \
chmod u+x fdb* && \
mv fdb* /usr/bin && \
rm -r /var/fdb/tmp
WORKDIR /var/fdb
# Install FoundationDB Client Libraries
ARG FDB_ADDITIONAL_VERSIONS="5.1.7"
COPY download_multiversion_libraries.bash scripts/
RUN wget $FDB_WEBSITE/downloads/$FDB_VERSION/linux/libfdb_c_$FDB_VERSION.so -O /usr/lib/libfdb_c.so && \
bash scripts/download_multiversion_libraries.bash $FDB_WEBSITE $FDB_ADDITIONAL_VERSIONS
# Set Up Runtime Scripts and Directoris
COPY fdb.bash scripts/
COPY create_server_environment.bash scripts/
COPY create_cluster_file.bash scripts/
RUN chmod u+x scripts/*.bash && \
mkdir -p logs
VOLUME /var/fdb/data
CMD /var/fdb/scripts/fdb.bash
# Runtime Configuration Options
ENV FDB_PORT 4500
ENV FDB_CLUSTER_FILE /var/fdb/fdb.cluster
ENV FDB_NETWORKING_MODE container
ENV FDB_COORDINATOR ""
ENV FDB_CLUSTER_FILE_CONTENTS ""
ENV FDB_PROCESS_CLASS unset

View File

@ -0,0 +1,71 @@
# Overview
This directory provides a Docker image for running FoundationDB.
The image in this directory is based on Ubuntu 18.04, but the commands and
scripts used to build it should be suitable for most other distros with small
tweaks to the installation of dependencies.
The image relies on the following dependencies:
* bash
* wget
* dig
* glibc
# Build Configuration
This image supports several build arguments for build-time configuration.
### FDB_VERSION
The version of FoundationDB to install in the container. This is required.
### FDB_WEBSITE
The base URL for the FoundationDB website. The default is
`https://www.foundationdb.org`.
### FDB_ADDITIONAL_VERSIONS
A list of additional client library versions to include in this image. These
libraries will be in a special multiversion library folder.
# Runtime Configuration
This image supports several environment variables for run-time configuration.
### FDB_PORT
The port that FoundationDB should bind to. The default is 4500.
### FDB_NETWORKING_MODE
A networking mode that controls what address FoundationDB listens on. If this
is `container` (the default), then the server will listen on its public IP
within the docker network, and will only be accessible from other containers.
If this is `host`, then the server will listen on `127.0.0.1`, and will not be
accessible from other containers. You should use `host` networking mode if you
want to access your container from your host machine, and you should also
map the port to the same port on your host machine when you run the container.
### FDB_COORDINATOR
A name of another FDB instance to use as a coordinator process. This can be
helpful when setting up a larger cluster inside a docker network, for instance
when using Docker Compose. The name you provide must be resolvable through the
DNS on the container you are running.
# Copying Into Other Images
You can also use this image to provide files for images that are clients of a
FoundationDB cluster, by using the `from` argument of the `COPY` command. Some
files you may want to copy are:
* `/usr/lib/libfdb_c.so`: The primary FoundationDB client library
* `/usr/lib/fdb/multiversion/libfdb_*.so`: Additional versions of the client
library, which you can use if you are setting up a multiversion client.
* `/var/fdb/scripts/create_cluster_file.bash`: A script for setting up the
cluster file based on an `FDB_COORDINATOR` environment variable.
* `/usr/bin/fdbcli`: The FoundationDB CLI.

View File

@ -0,0 +1,51 @@
#! /bin/bash
#
# create_cluster_file.bash
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This script creates a cluster file for a server or client.
# This takes the cluster file path from the FDB_CLUSTER_FILE
# environment variable, with a default of /etc/foundationdb/fdb.cluster
#
# The name of the coordinator must be defined in the FDB_COORDINATOR environment
# variable, and it must be a name that can be resolved through DNS.
function create_cluster_file() {
FDB_CLUSTER_FILE=${FDB_CLUSTER_FILE:-/etc/foundationdb/fdb.cluster}
mkdir -p $(dirname $FDB_CLUSTER_FILE)
if [[ -n "$FDB_CLUSTER_FILE_CONTENTS" ]]; then
echo "$FDB_CLUSTER_FILE_CONTENTS" > $FDB_CLUSTER_FILE
elif [[ -n $FDB_COORDINATOR ]]; then
coordinator_ip=$(dig +short $FDB_COORDINATOR)
if [[ -z "$coordinator_ip" ]]; then
echo "Failed to look up coordinator address for $FDB_COORDINATOR" 1>&2
exit 1
fi
echo "docker:docker@$coordinator_ip:4500" > $FDB_CLUSTER_FILE
else
echo "FDB_COORDINATOR environment variable not defined" 1>&2
exit 1
fi
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
create_cluster_file "$@"
fi

View File

@ -0,0 +1,46 @@
#! /bin/bash
#
# create_server_environment.bash
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
source /var/fdb/scripts/create_cluster_file.bash
function create_server_environment() {
fdb_dir=/var/fdb
env_file=$fdb_dir/.fdbenv
: > $env_file
if [[ "$FDB_NETWORKING_MODE" == "host" ]]; then
public_ip=127.0.0.1
elif [[ "$FDB_NETWORKING_MODE" == "container" ]]; then
public_ip=$(grep `hostname` /etc/hosts | sed -e "s/\s *`hostname`.*//")
else
echo "Unknown FDB Networking mode \"$FDB_NETWORKING_MODE\"" 1>&2
exit 1
fi
echo "export PUBLIC_IP=$public_ip" >> $env_file
if [[ -z $FDB_COORDINATOR ]]; then
FDB_CLUSTER_FILE_CONTENTS="docker:docker@$public_ip:$FDB_PORT"
fi
create_cluster_file
}

View File

@ -0,0 +1,31 @@
#! /bin/bash
#
# download_multiversion_libraries.bash
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
mkdir -p /usr/lib/fdb/multiversion
website=$1
shift
for version in $*; do
origin=$website/downloads/$version/linux/libfdb_c_$version.so
destination=/usr/lib/fdb/multiversion/libfdb_c_$version.so
echo "Downloading $origin to $destination"
wget $origin -o $destination
done

29
packaging/docker/fdb.bash Normal file
View File

@ -0,0 +1,29 @@
#! /bin/bash
#
# fdb.bash
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
source /var/fdb/scripts/create_server_environment.bash
create_server_environment
source /var/fdb/.fdbenv
echo "Starting FDB server on $PUBLIC_IP:4500"
fdbserver --listen_address 0.0.0.0:$FDB_PORT --public_address $PUBLIC_IP:4500 \
--datadir /var/fdb/data --logdir /var/fdb/logs \
--locality_zoneid=`hostname` --locality_machineid=`hostname` --class $FDB_PROCESS_CLASS

View File

@ -0,0 +1,20 @@
# .env
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
COMPOSE_PROJECT_NAME=fdbpythonsample

View File

@ -0,0 +1,41 @@
# Dockerfile
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
FROM python:3.6
RUN apt-get update; apt-get install -y dnsutils
RUN mkdir -p /app
WORKDIR /app
COPY --from=foundationdb:5.2.5 /usr/lib/libfdb_c.so /usr/lib
COPY --from=foundationdb:5.2.5 /usr/bin/fdbcli /usr/bin/
COPY --from=foundationdb:5.2.5 /var/fdb/scripts/create_cluster_file.bash /app
COPY requirements.txt /app
RUN pip install -r requirements.txt
COPY start.bash /app
COPY server.py /app
RUN chmod u+x /app/start.bash
CMD /app/start.bash
ENV FLASK_APP=server.py
ENV FLASK_ENV=development

View File

@ -0,0 +1,21 @@
# requirements.txt
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
Flask==1.0.2
foundationdb==5.1.5

View File

@ -0,0 +1,48 @@
# server.py
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from flask import Flask
import fdb
app = Flask(__name__)
fdb.api_version(510)
db=fdb.open()
COUNTER_KEY=fdb.tuple.pack(('counter',))
def _increment_counter(tr):
counter_value = tr[COUNTER_KEY]
if counter_value == None:
counter = 1
else:
counter = fdb.tuple.unpack(counter_value)[0] + 1
tr[COUNTER_KEY] = fdb.tuple.pack((counter,))
return counter
@app.route("/counter", methods=['GET'])
def get_counter():
counter_value = db[COUNTER_KEY]
if counter_value == None:
return '0'
else:
return str(fdb.tuple.unpack(counter_value)[0])
@app.route("/counter/increment", methods=['POST'])
def increment_counter():
return str(_increment_counter(db))

View File

@ -0,0 +1,23 @@
#! /bin/bash
# start.bash
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
/app/create_cluster_file.bash
FLASK_APP=server.py flask run --host=0.0.0.0

View File

@ -0,0 +1,36 @@
# docker-compose.yaml
#
# This source file is part of the FoundationDB open source project
#
# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
version: '3'
services:
fdb:
image: foundationdb:5.2.5
environment:
FDB_COORDINATOR: fdb-coordinator
fdb-coordinator:
image: foundationdb:5.2.5
environment:
FDB_COORDINATOR: fdb-coordinator
app:
build:
context: app
ports:
- 5000:5000
environment:
FDB_COORDINATOR: fdb-coordinator