Issue
Docker compose failure on azure
I am failing to startup a multi container on azure for a simple test app. In essence I want an app service and a db service that persists so I can access the SQLITE db files and backup/download them, probably using built-in docker tools. I would really appreciate knowing where I am going wrong. I did consult this site first and saw the article on getting the ports right etc, but still no luck.
Here is the relevant info. TIA.
(1) Trace output from the container log files:
2023-12-03T11:59:23.064Z INFO - Pulling image: ankwilitas/ddm:003
2023-12-03T11:59:24.218Z INFO - 003 Pulling from ankwilitas/ddm
2023-12-03T11:59:24.229Z INFO - Digest: sha256:8e3e612a9d3d51fed11bcd44dedf6c28b0c52137d44e0bc2180451c958c50f9b 2023-12-03T11:59:24.229Z INFO - Status: Image is up to date for ankwilitas/ddm:003
2023-12-03T11:59:24.240Z INFO - Pull Image successful, Time taken: 1 Seconds
2023-12-03T11:59:24.247Z INFO - Starting container for site
2023-12-03T11:59:24.247Z INFO - docker run -d -p 9469:80 --name ddmtestapp_web_0_20931625 -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=false -e WEBSITE_SITE_NAME=DDMtestApp -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=ddmtestapp.azurewebsites.net -e WEBSITE_INSTANCE_ID=6dc581a871e89ead50d499457423099a1bf94aee4ac35dda377966ff7460f7c1 -e HTTP_LOGGING_ENABLED=1 ankwilitas/ddm:003
2023-12-03T11:59:24.264Z INFO - Pulling image: docker.io/library/sqlite:latest
2023-12-03T11:59:25.319Z ERROR - DockerApiException: Docker API responded with status code=NotFound, response={"message":"pull access denied for sqlite, repository does not exist or may require 'docker login': denied: requested access to the resource is denied"}
2023-12-03T11:59:25.319Z ERROR - Pulling docker image docker.io/library/sqlite:latest failed:
2023-12-03T11:59:26.333Z ERROR - DockerApiException: Docker API responded with status code=NotFound, response={"message":"pull access denied for sqlite, repository does not exist or may require 'docker login': denied: requested access to the resource is denied"}
2023-12-03T11:59:26.333Z WARN - Image pull failed. Defaulting to local copy if present.
2023-12-03T11:59:26.334Z ERROR - Image pull failed: Verify docker image configuration and credentials (if using private repository)
2023-12-03T11:59:26.334Z ERROR - multi-container unit was not started successfully
2023-12-03T11:59:26.335Z INFO - Container logs from ddmtestapp_web_0_20931625 =
2023-12-03T11:59:26.345Z INFO - Container logs from ddmtestapp_db_0_20931625 =
2023-12-03T11:59:26.437Z INFO - Stopping site ddmtestapp because it failed during startup.
<<< so i have defined a private repo here and provided my login creds for dockerhub... so i don't know why this still fails with sqlite:latest...
(2) Deployment Center Settings: [![deployment center settings][1]][1]
(3) Docker compose:
services:
web:
image: ankwilitas/ddm:003
ports:
- "5000:5000"
depends_on:
- db
db:
image: "sqlite:latest"
volumes:
- db_data:/app/db # Adjust the volume path based on your app's structure
volumes:
db_data:```
(4) Dockerfile:
```FROM python:3.7
USER root
#dir for docker:
WORKDIR /ddm
#copy requirements to workdir:
COPY requirements.txt .
#pip install
RUN pip install --upgrade pip
RUN pip install -v --no-cache-dir -r requirements.txt
#copy ALL files to workdir
COPY . .```
(5) App.py:
```from flask import Flask, render_template, request, redirect, url_for
import sqlite3
app = Flask(__name__)
app.secret_key = "tbase11"
@app.route('/')
def index():
db = sqlite3.connect("db/core/core.db")
cursor = db.cursor()
cursor.execute("SELECT welcome_message FROM service_messages")
welcome_message = cursor.fetchone()[0]
db.close()
return render_template('index.html', welcome_message=welcome_message)```
[1]: https://i.stack.imgur.com/BT1Rd.png
Solution
While trying your Docker configuration, I encountered the same error and made changes in the Docker file.
- The simple Flask code below uses SQLite to store and display user names.
- The route (
/
) handles both GET and POST requests. - If it's a POST request, the user name is retrieved from the form and inserted into the database.
- All users are then retrieved from the database, and the
index.html
template is rendered with the user names.Folder structure. - Code reference from this DOC.
from flask import Flask, render_template, request
import sqlite3
app = Flask(__name__)
def create_table():
conn = sqlite3.connect('users.db')
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL)')
conn.commit()
conn.close()
def insert_user(name):
conn = sqlite3.connect('users.db')
c = conn.cursor()
c.execute('INSERT INTO users (name) VALUES (?)', (name,))
conn.commit()
conn.close()
def get_users():
conn = sqlite3.connect('users.db')
c = conn.cursor()
c.execute('SELECT name FROM users')
users = [row[0] for row in c.fetchall()]
conn.close()
return users
@app.route('/', methods=['GET', 'POST'])
def home():
create_table()
if request.method == 'POST':
user_name = request.form['name']
if user_name:
insert_user(user_name)
users = get_users()
return render_template('index.html', users=users)
if __name__ == '__main__':
app.run(debug=True)
<body>
<h1>Welcome to the Flask App</h1>
<p>This app uses Python, Flask, and SQLite. Gunicorn is used as the HTTP server to run the Flask application.</p>
<form method="POST" action="/">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<input type="submit" value="Submit">
</form>
<h2>Users:</h2>
<ul>
{% for user in users %}
<li>{{ user }}</li>
{% endfor %}
</ul>
</body>
</html>
Dockerfile:
FROM python:3.8-slim
WORKDIR /app
COPY . /app
RUN pip install --trusted-host pypi.python.org -r requirements.txt
RUN pip install pipenv
EXPOSE 5000
CMD ["pipenv", "run", "gunicorn", "--bind=0.0.0.0:5000", "--reload", "app:app"]
docker-compose.yml:
version: "3.7"
services:
app:
image: python:3.9
container_name: python-flask-sqlite
environment:
- FLASK_ENV=development
- FLASK_APP=/src/app.py
- DATABASE=/src/database.db
working_dir: /src
volumes:
- ./src:/src
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 30s
timeout: 10s
retries: 3
ports:
- 5000:5000
entrypoint: bash -c "apt-get update && apt-get install -y sqlite3 && pip install --upgrade pip==23.1.2 && pip install -r /src/requirements.txt && gunicorn --bind 0.0.0.0:5000 --workers 4 app:app"
requirements.txt
Flask==2.3.2
Gunicorn==20.1.0
Local:
- Command to build a Docker image:
docker build --tag python-docker1 .
- Command to run a Docker image:
docker run -p 5000:5000 python-docker1
- Docker Output:
- Create a Container registry in azure and login to Container registry using this command:
az acr login --name "ContainerRegistryName" --password "Password" --username "ContainerRegistryName"
- The
docker images
command is used to list all the Docker images
- The
docker tag
command is used to create a tag for a Docker image.
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
- Push a Docker image to a specified container registry.
docker push IMAGE_NAME
Azure:
Answered By - Sampath
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.