Skip to content

Commit

Permalink
Add instructions for Docker deployment
Browse files Browse the repository at this point in the history
Changes to be committed:
	modified:   Dockerfile
	modified:   docker-compose.yml
	modified:   docs/deployment/dockerDeployment.md
	new file:   entrypoint.sh
  • Loading branch information
HadleyKing committed Aug 7, 2024
1 parent f54925b commit efebfbe
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 25 deletions.
21 changes: 7 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,22 @@ LABEL org.opencontainers.image.source=https://github.com/octocat/my-repo
LABEL org.opencontainers.image.description="My container image"
LABEL org.opencontainers.image.licenses=MIT

# Note that this is just for debug / test purposes; should not be set via the setup for production
# ENV DJANGO_SUPERUSER_PASSWORD="BioCompute123"
# ENV DJANGO_SUPERUSER_USERNAME="BioComputeSuperUser"
# ENV DJANGO_SUPERUSER_EMAIL="[email protected]"

RUN apt-get -qq update && apt-get install -y python3.9 python3-dev python3-pip

RUN python3 -m pip install --upgrade pip

WORKDIR /biocompute_api

COPY requirements.txt .

COPY requirements.txt /biocompute_api/
RUN python3 -m pip install -r requirements.txt

COPY . ./

WORKDIR /biocompute_api/
COPY . /biocompute_api/

# RUN python3 manage.py migrate
# RUN python3 manage.py createsuperuser --no-input
WORKDIR /biocompute_api

EXPOSE 8000
#CMD ["bash"]
ENTRYPOINT ["python3", "manage.py", "runserver"]
CMD ["0.0.0.0:8000"]

ENTRYPOINT ["./entrypoint.sh"]

CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
16 changes: 5 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
#cat docker-compose.yml

version: '3.4'

services:
bco-api:
platform: linux/amd64
build: .
image: ghcr.io/biocompute-objects/bco_api:23.07
container_name: bco-api
volumes:
- ./data/db.sqlite3:/biocompute_api/db.sqlite3
- ./data/server.conf:/biocompute_api/server.conf
# volumes:
# - ./db.sqlite3:/biocompute_api/db.sqlite3
# - ./.secrets:/biocompute_api/.secrets
environment:
TZ: 'America/New_York'
ENV DJANGO_SUPERUSER_PASSWORD: "BioCompute123"
ENV DJANGO_SUPERUSER_USERNAME: "BioComputeSuperUser"
ENV DJANGO_SUPERUSER_EMAIL: "[email protected]"

ports:
- "8000:8000"
read_only: true

tmpfs:
- /tmp
restart: unless-stopped
Expand Down
88 changes: 88 additions & 0 deletions docs/deployment/dockerDeployment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# BCODB Docker Deployment


### Requirements
- Python 3: [3.10.6 reccomended](https://www.python.org/downloads/release/python-3106/)
- [PyEnv](https://github.com/pyenv/pyenv) (optional but recommended for Mac/Linux)
- Docker:
- [Docker Desktop for Linux](https://docs.docker.com/desktop/install/linux-install/)
- [Docker Desktop for Mac (macOS)](https://docs.docker.com/desktop/install/mac-install/)
- [Docker Desktop for Windows](https://docs.docker.com/desktop/install/windows-install/)


## Clone the repository
```
git clone https://github.com/biocompute-objects/bco_api
```

**Make sure you are on the desired branch (Check for latest branch):**

```
git switch [DESIRED BRANCH TAG]
```

## Enter the repository
```
cd bco_api
```
## Configure the DB settings using the `.secrets` file:

### OPTION 1: Generate the secrets file

In the project root copy the `.secrets.example` to `.secrets`

```
cp .secrets.example .secrets
```
#### Generate the DJANGO_KEY
Generate a 32-bytes long PSK key using the `openssl` command or `PowerShell` command.

##### Mac/Linux:
```
openssl rand -base64 32
```
##### Windows:
```
[Convert]::ToBase64String((1..32 | ForEach-Object { Get-Random -Minimum 0 -Maximum 256 }) -as [byte[]])
```

Use a text editor to open the `.secrets` file and update the rest of the variables. For details on each of the variables see the [configuration](/docs/config.md) documentation.

### OPTION 2: Use the `local_deployment.secrets` file
Fromt the project root:
```
cp admin_only/local_deployment.secrets .secrets
```

### Building the BCO API via Docker

A docker file is provided to allow easy building of the BCO API. This can be done from the root directory (the directory with Dockerfile in it) by running:

`docker build -t bco_api:latest .`

This will build a container named `bco_api` with the tag `latest`.

The build process (via the `entrypoint.sh` script) will check for an existing database in the repository and run migrations. If no database is present one will be created and the test data will be loaded (taken from `config/fixtures/local_data.json`).

### Running the container via Docker

The BCO Api container can be run via docker on the command line in Linux/Windows by running:

`docker run --rm --network host -it bco_api:latest`

The BCO Api container can be run via docker on the command line in MacOS by running:

`docker run --rm -p 8000:8000 -it bco_api:latest`

This will expose the server at `http://127.0.0.1:8000`, whitch is where all of the default settings will expect to find the BCODB.

#### Overriding the port

It is possible to override the port 8000 to whatever port is desired. This is done by running the container with 8080 representing the desired port.

`docker run --rm --network host -it bco_api:latest 0.0.0.0:8080`


NOTE: The ip address of 0.0.0.0 is to allow the web serer to properly associate with 127.0.0.1 - if given 127.0.0.1 it will not allow communications outside of the container!

You can also give it a specific network created with `docker network create` if you wanted to give assigned IP addresses.
15 changes: 15 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
# entrypoint.sh

# Check if the SQLite database file exists and run migrations if it doesn't
if [ ! -f "db.sqlite3" ]; then
echo "Database not found. Running migrations..."
python3 manage.py migrate
python3 manage.py loaddata config/fixtures/local_data.json
else
echo "Database exists. Running migrations."
python3 manage.py migrate
fi

# Start the Django server
exec "$@"

0 comments on commit efebfbe

Please sign in to comment.