Skip to content

Commit

Permalink
♻️ 🐳 improve init execution on entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
marcellodesales committed Mar 8, 2023
1 parent 7ae6f3c commit 549c259
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
12 changes: 8 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ COPY *.sql /viasat/minitwit
####
FROM builder AS tester

# Only copy the development tools from the requirements for tests
COPY requirements-dev.txt .
RUN pip install -r requirements-dev.txt

# Note that we are executing the tests here because we don't want to build
# A runtime image before the tests are executed successfully!
RUN pytest test_minitwit.py
# All the requirements are installed and so the test cases can be executed
CMD ["pytest", "test_minitwit.py"]

###
### runtime environment containing only the runtime dependencies.
### Runtime environment containing only the runtime dependencies.
### Note that this image does NOT have anything for testing!
###
FROM builder AS runtime

# All the other resources are there, so let's add the run-app as it's part of the runtime
# Note that the builder runtime defines the WORKDIR already so we can COPY to .
COPY run-app .
COPY init-db .

RUN rm -rf requirements* test_minitwit.py

ENTRYPOINT ["bash", "/viasat/minitwit/run-app"]
17 changes: 2 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,10 @@ minitwit-minitwit-runtime-1 | Press CTRL+C to quit

You betcha. Run the `test_minitwit.py` file to see the tests pass.

1. Initialize the database for testing
2. Install the dev dependencies
3. Execute the test cases
1. Install the dev dependencies

> **NOTE**: the runtime dependencies must have been installed as well.
1. Initialize the dabase for testing
* Just initialize the database with `init-db`

```console
$ ./init-db
[2023-03-08 00:25:47,358] INFO in minitwit: Using local db sqlite:////var/minitwit/minitwit.db
Initialized the database.
```

2. Install the dev dependencies

```
$ pip install -r requirements-dev.txt
Collecting pytest==5.3.2
Expand Down Expand Up @@ -148,7 +135,7 @@ WARNING: You are using pip version 22.0.4; however, version 23.0.1 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
```

3. Execute the test cases
2. Execute the test cases

```console
pytest test_minitwit.py
Expand Down
6 changes: 4 additions & 2 deletions init-db
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ export FLASK_APP=minitwit.py
export LC_ALL=en_US.utf-8
export LANG=en_US.utf-8

DB_DIR=${DB_DIR:-/var/minitwit}

# Initialize the path where the db is
if [ ! -d /var/minitwit ]; then
mkdir -p /var/minitwit/
if [ ! -d ${DB_DIR} ]; then
mkdir -p ${DB_DIR}
fi

flask initdb
6 changes: 4 additions & 2 deletions run-app
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ export FLASK_APP=minitwit.py
export LC_ALL=en_US.utf-8
export LANG=en_US.utf-8

if [ ! -f /var/minitwit/minitwit.db ] || [ -n "${INIT_DB}" ]; then
echo "Initializing database at /var/minitwit/minitwit.db"
export DB_DIR=${DB_DIR:-/var/minitwit}

if [ ! -f ${DB_DIR}/minitwit.db ] || [ -n "${INIT_DB}" ]; then
echo "Initializing database at ${DB_DIR}/minitwit.db"
./init-db
fi

Expand Down

0 comments on commit 549c259

Please sign in to comment.