Skip to content

Commit

Permalink
feat: Script for web container init
Browse files Browse the repository at this point in the history
A script has been included to check that the mysql container is active before starting the web container.
  • Loading branch information
drorganvidez committed Feb 19, 2024
1 parent ceb18bc commit d094652
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 41 deletions.
23 changes: 14 additions & 9 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
# Use an official Python runtime as a parent image
FROM python:3.11-alpine

# Set the working directory in the container to /app
# Instala el cliente de MySQL para poder usarlo en el script de espera
RUN apk add --no-cache mysql-client

# Establece el directorio de trabajo en el contenedor en /app
WORKDIR /app

# Copy the contents of the local app/ directory to the /app directory in the container
# Copia el contenido del directorio local app/ al directorio /app en el contenedor
COPY app/ ./app

# Copy requirements.txt at the /app working directory
# Copia requirements.txt en el directorio de trabajo /app
COPY requirements.txt .

# Install any needed packages specified in requirements.txt
# Copia el script wait-for-db.sh y establece los permisos de ejecución
COPY --chmod=+x scripts/wait-for-db.sh ./scripts/

# Instala los paquetes necesarios especificados en requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Update pip
# Actualiza pip
RUN pip install --no-cache-dir --upgrade pip

# Expose port 5000
# Expone el puerto 5000
EXPOSE 5000

# Run the application
CMD flask db upgrade && flask run --host=0.0.0.0 --port=5000 --reload --debug

# Ajusta el comando CMD para ejecutar correctamente el script wait-for-db.sh
CMD sh ./scripts/wait-for-db.sh && flask db upgrade && flask run --host=0.0.0.0 --port=5000 --reload --debug
32 changes: 12 additions & 20 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
version: '3.8'

services:

web:
build:
context: .
Expand All @@ -11,29 +12,11 @@ services:
- "5000"
environment:
FLASK_ENV: development
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
depends_on:
- db

flamapyapi:
image: flamapy/flamapy-fm-dist:v1.5.3
deploy:
replicas: 3
restart_policy:
condition: on-failure
expose:
- 8000

nginx:
image: nginx:latest
volumes:
- ./nginx/nginx.dev.conf:/etc/nginx/nginx.conf
ports:
- "80:80"
- "8000:8000"
depends_on:
- web
- flamapyapi

db:
image: mysql:latest
command: --default-authentication-plugin=mysql_native_password
Expand All @@ -48,5 +31,14 @@ services:
volumes:
- db_data:/var/lib/mysql

nginx:
image: nginx:latest
volumes:
- ./nginx/nginx.dev.conf:/etc/nginx/nginx.conf
ports:
- "80:80"
depends_on:
- web

volumes:
db_data:
12 changes: 0 additions & 12 deletions nginx/nginx.dev.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,10 @@ events {}

http {

upstream flamapyapi {
server flamapyapi:8000;
}

upstream web {
server web:5000;
}

server {
listen 8000;

location / {
proxy_pass http://flamapyapi;
}
}

server {
listen 80;

Expand Down
10 changes: 10 additions & 0 deletions scripts/wait-for-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

until mysql -h db -P 3306 -u"$MYSQL_USER" -p"$MYSQL_PASSWORD" -e 'SELECT 1' &> /dev/null
do
echo "MySQL is unavailable - sleeping"
sleep 1
done

echo "MySQL is up - executing command"
exec "$@"

0 comments on commit d094652

Please sign in to comment.