Skip to content

Commit

Permalink
Migrate to docker compose V2
Browse files Browse the repository at this point in the history
See #98
  • Loading branch information
sepulzera committed Apr 22, 2023
1 parent 5a7709a commit a8fea56
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 42 deletions.
2 changes: 1 addition & 1 deletion docs/Creating_a_proxy_server_for_docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ services:
- Restart your docker containers
- `sudo docker-compose -f docker-prod.yml -f docker-prod.override.yml -f docker-prod.version.yml restart`
- `sudo docker compose -f docker-prod.yml -f docker-prod.override.yml -f docker-prod.version.yml restart`
- OR
- Just run the `quick-start` script again: `./quick-start`
- Load localhost:7000 to confirm it worked.
Expand Down
4 changes: 2 additions & 2 deletions docs/Install_Prerequisites.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ You can [install docker here](https://www.docker.com/community-edition#/download

### Required docker-compose version

For this project it is recommended and tested to use [docker-compose v1.29.0](https://github.com/docker/compose/releases/tag/1.29.0) and later. There is also a [v2 of docker compose](https://docs.docker.com/compose/compose-v2/). That latest version should also work. but is umtested.
For this project it is recommended and tested to use [docker-compose v1.29.0](https://github.com/docker/compose/releases/tag/1.29.0) and later.

You can inspect the installed version by running ```docker-compose version```. If you want to install docker-compose via the OS software repository (e. g. via apt), then you can check the provided version by running ```apt-cache policy docker-compose```. If you want to stick with the docker-compose provided by your OS repository, then you can get OwnRecipes running with [docker-compose 1.25.0](https://github.com/docker/compose/releases/tag/1.25.0) and later, though you will need to make few adjustments as written below.
You can inspect the installed version by running ```docker compose version``` if you are using docker-compose V2, or ```docker-compose version``` if you are using docker-compose V1. If you want to install docker-compose via the OS software repository (e. g. via apt), then you can check the provided version by running ```apt-cache policy docker-compose```. If you want to stick with the docker-compose provided by your OS repository, then you can get OwnRecipes running with [docker-compose 1.25.0](https://github.com/docker/compose/releases/tag/1.25.0) and later, though you will need to make few adjustments as written below.

<details>
<summary>Run OwnRecipes with docker-compose 1.25.0 and higher</summary>
Expand Down
42 changes: 24 additions & 18 deletions docs/Running_the_App.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,6 @@ E.g. Given that the IP address for the OwnRecipes server is `192.168.0.12` and p

``REACT_APP_API_URL=http://192.168.0.1:1234``

### Connecting to a remote DB
If you are connecting the API to a remote DB (any non-dockerized DB) you need to setup the following configs to your env file.

- [MYSQL_DATABASE](Setting_up_env_file.md#MYSQL_DATABASE)
- [MYSQL_USER](Setting_up_env_file.md#MYSQL_USER)
- [MYSQL_PASSWORD](Setting_up_env_file.md#MYSQL_PASSWORD)
- [MYSQL_HOST](Setting_up_env_file.md#MYSQL_HOST)
- [MYSQL_PORT](Setting_up_env_file.md#MYSQL_PORT)

You will also need to edit your `docker-prod.yml` file to remove the database from the setup process. See [this docker yml](samples/sample_docker_prod_remote_db.yml) for an example.

## Start docker containers

Once the files have been created run the command below and replace the version with version of OwnRecipes you want to run. You can also leave this blank (this will pull the latest code)
Expand All @@ -103,33 +92,39 @@ OR
sudo ./quick-start.py --help
```

The quick start script will do a few things.
1. Creates a `docker-prod.version.yml` file with the required image tags.
2. Downloads the required images.
3. Takes a backup of the database and your images.
4. Restarts the OwnRecipes containers.
<details>
<summary>What exactly does the quick-start script do?</summary>
<ol>
<li>Creates a `docker-prod.version.yml` file with the required image tags.</li>
<li>Downloads the required images.</li>
<li>Takes a backup of the database and your images.</li>
<li>Restarts the OwnRecipes containers.</li>
</ol>
</details>

After completing the startup, you should see the following line in your terminal:

```
App started. Please wait ~30 seconds for the containers to come online.
```

:warning: THE SETUP IS NOT COMPLETE, YET! Please follow the further instructions! :warning:

If you encounter any issue, please read the [Troubleshooting guide](Troubleshooting.md).

## First Time Setup

To create a super user:
``` bash
sudo docker-compose -f docker-prod.yml run --rm --entrypoint 'python manage.py createsuperuser' api
sudo docker compose -f docker-prod.yml run --rm --entrypoint 'python manage.py createsuperuser' api
```
Follow the prompts given to create your user. You can do this as many times as you like.

_[Click here if docker-compose throws an error](Troubleshooting.md#docker-compose-throws-an-error)._

If you want to add some test data you can load a few recipes and some news data. This data isn't really needed unless you just wanna see how the app looks and if its working.
```bash
sudo docker-compose -f docker-prod.yml run --rm --entrypoint 'sh' api
sudo docker compose -f docker-prod.yml run --rm --entrypoint 'sh' api
./manage.py loaddata course_data.json
./manage.py loaddata cuisine_data.json
./manage.py loaddata news_data.json
Expand All @@ -156,3 +151,14 @@ It is highly recommended that you serve your content over https. See [Setting up
### Custom Proxy Server

See [Creating a proxy server for docker](Creating_a_proxy_server_for_docker.md) for more information on how to configure a custom nginx server to serve OwnRecipes.

### Connecting to a remote DB
If you are connecting the API to a remote DB (any non-dockerized DB) you need to setup the following configs to your env file.

- [MYSQL_DATABASE](Setting_up_env_file.md#MYSQL_DATABASE)
- [MYSQL_USER](Setting_up_env_file.md#MYSQL_USER)
- [MYSQL_PASSWORD](Setting_up_env_file.md#MYSQL_PASSWORD)
- [MYSQL_HOST](Setting_up_env_file.md#MYSQL_HOST)
- [MYSQL_PORT](Setting_up_env_file.md#MYSQL_PORT)

You will also need to edit your `docker-prod.yml` file to remove the database from the setup process. See [this docker yml](samples/sample_docker_prod_remote_db.yml) for an example.
7 changes: 3 additions & 4 deletions docs/Running_the_App_Tricks.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ To only set up the api + MariaDB via docker, follow the guide [Running the App i

You can only run the api + MariaDB like this:

_Note: This will require docker-compose version 1.28+, as it makes use of the
new profiles-feature._
_Note: This will require docker-compose version 1.28+, as it makes use of the profiles-feature._

```bash
cd OwnRecipes
sudo docker-compose build
sudo docker-compose up
sudo docker compose build
sudo docker compose up
```

Then, [set up the web without docker for development](Running_the_App_Without_Docker_in_dev.md/#ownrecipes-web). If you didn't change the environment-files for ownrecipes-api, then you probably don't need to for ownrecipes-web, too! \o/
Expand Down
18 changes: 9 additions & 9 deletions docs/Running_the_App_in_dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ git clone https://github.com/ownrecipes/ownrecipes-web.git

Then run it:
```bash
sudo docker-compose --profile all build
sudo docker-compose --profile all up
sudo docker compose --profile all build
sudo docker compose --profile all up
```

_[Click here if docker-compose throws an error](Troubleshooting.md#docker-compose-throws-an-error)._
_[Click here if docker compose throws an error](Troubleshooting.md#docker-compose-throws-an-error)._

All container, db, api and web, should start successfully. Check the terminal output for any error. If you encounter any issue, please read the [Troubleshooting guide](Troubleshooting.md).

Expand All @@ -44,15 +44,15 @@ Seed the database.

To create a super user:
``` bash
sudo docker-compose run --rm --entrypoint 'python manage.py makemigrations' api
sudo docker-compose run --rm --entrypoint 'python manage.py migrate' api
sudo docker-compose run --rm --entrypoint 'python manage.py createsuperuser' api
sudo docker compose run --rm --entrypoint 'python manage.py makemigrations' api
sudo docker compose run --rm --entrypoint 'python manage.py migrate' api
sudo docker compose run --rm --entrypoint 'python manage.py createsuperuser' api
```
Follow the prompts given to create your user. You can do this as many times as you like.

If you want to add some test data you can load a few recipes and some news data. This data isn't really needed unless you just wanna see how the app looks and if its working.
```bash
sudo docker-compose run --rm --entrypoint 'sh' api
sudo docker compose run --rm --entrypoint 'sh' api
./manage.py loaddata course_data.json
./manage.py loaddata cuisine_data.json
./manage.py loaddata news_data.json
Expand All @@ -71,7 +71,7 @@ Or you can straight away log in to the OwnRecipes web app. By default, the url w
OwnRecipes will shut down with your system. You can simply launch OwnRecipes by running:
```bash
cd OwnRecipes
sudo docker-compose --profile all up
sudo docker compose --profile all up
```

If you encounter any issue, please read the [Troubleshooting guide](Troubleshooting.md).
Expand All @@ -89,7 +89,7 @@ have to clean up the related local docker stuff.
First, clean up everything:
```bash
cd OwnRecipes
sudo docker-compose --profile all down
sudo docker compose --profile all down
```

Then, update your local repositories:
Expand Down
2 changes: 1 addition & 1 deletion docs/Setting_up_https.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ HTTP_X_FORWARDED_PROTO=true
Restart your docker containers.
- `sudo docker-compose -f docker-prod.yml -f docker-prod.override.yml -f docker-prod.version.yml restart`
- `sudo docker compose -f docker-prod.yml -f docker-prod.override.yml -f docker-prod.version.yml restart`
- OR
- Just run the `quick-start` script again: `./quick-start`
Expand Down
10 changes: 9 additions & 1 deletion docs/Troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@

## docker-compose throws an error

If running a `docker compose`-command throws an error like:
```
docker: command not found
```

Then your setup is missing docker, or you are using the older docker-compose V1.
In the latter case, use the older command `docker-compose`.

If running a `docker-compose`-command throws an error like:
```
ERROR: The Compose file './docker-prod.yml' is invalid because:
Expand All @@ -32,7 +40,7 @@ You have started the db and api, but can not [create the superuser](Running_the_
Then probably your api can not connect to the db. Please double check your env-file (`.env.docker.production.api` when running via docker in production).
The vars `MYSQL_DATABASE` and `MYSQL_ROOT_PASSWORD` are used to connect to the db, make sure they are correct.

You may also want to check the terminal output for any errors during start. If you run the `quick-start.py`-script, then some errors may be hidden. Run the following command instead to get a more comprehensive output: `docker-compose -f docker-prod.yml -f docker-prod.override.yml -f docker-prod.version.yml restart`
You may also want to check the terminal output for any errors during start. If you run the `quick-start.py`-script, then some errors may be hidden. Run the following command instead to get a more comprehensive output: `docker compose -f docker-prod.yml -f docker-prod.override.yml -f docker-prod.version.yml restart`

<p align="right">(<a href="#readme-top">back to top</a>)</p>

Expand Down
26 changes: 20 additions & 6 deletions quick-start.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from time import sleep
from os import getcwd, name as os_name
from subprocess import call, Popen, PIPE
from subprocess import call, Popen, DEVNULL, PIPE, STDOUT


def update_image_tags(version=None):
Expand Down Expand Up @@ -40,6 +40,15 @@ def download_images(version=None):
call(['docker', 'pull', 'ownrecipes/ownrecipes-nginx:' + version])


def getDockerCompose():
""" Check if docker-compose V1 or V2 is available """
try:
Popen(['docker-compose', '--version'], stdin=DEVNULL, stdout=DEVNULL, stderr=STDOUT)
return 'docker-compose'
except OSError:
return 'docker compose'


def start_containers():
"""
Takes a back up of the Recipe images and DB.
Expand All @@ -49,6 +58,11 @@ def start_containers():
print("Starting OwnRecipes")
print("==================")

dockerCompose = getDockerCompose()
if dockerCompose is None:
raise RuntimeError("docker-compose not found. Please install the requirements and try again.")
dockerComposeArr = dockerCompose.split(' ')

# Check if the DB is up and running locally.
# If it is then take a backup.
# If the user is using a remote DB, do nothing.
Expand Down Expand Up @@ -81,7 +95,7 @@ def start_containers():
print("Using remote DB...")
else:
print("Creating the DB. This may take a minute...")
call(['docker-compose', '-f', 'docker-prod.yml', 'up', '-d', 'db'])
call([*dockerComposeArr, '-f', 'docker-prod.yml', 'up', '-d', 'db'])
sleep(45)

# Check if the API is up.
Expand All @@ -104,21 +118,21 @@ def start_containers():
# Stop each container that needs to be updated.
# Don't stop the DB! There is no reason to.
call([
'docker-compose',
*dockerComposeArr,
'-f', 'docker-prod.yml',
'-f', 'docker-prod.version.yml',
'-f', 'docker-prod.override.yml',
'stop', 'nginx'
])
call([
'docker-compose',
*dockerComposeArr,
'-f', 'docker-prod.yml',
'-f', 'docker-prod.version.yml',
'-f', 'docker-prod.override.yml',
'stop', 'api'
])
call([
'docker-compose',
*dockerComposeArr,
'-f', 'docker-prod.yml',
'-f', 'docker-prod.version.yml',
'-f', 'docker-prod.override.yml',
Expand All @@ -127,7 +141,7 @@ def start_containers():

# Start all the containers
call([
'docker-compose',
*dockerComposeArr,
'-f', 'docker-prod.yml',
'-f', 'docker-prod.version.yml',
'-f', 'docker-prod.override.yml',
Expand Down

0 comments on commit a8fea56

Please sign in to comment.