'Docker-compose creates two containers but only runs one
I'm trying to deploy a Flask app in a container and a MongoDB instance in an other container , but I'm having a problem with my Dockerfile / docker-compose.yml file. When i run docker-compose up -d, both containers are created but only one of them ends up running after this command line.
(flaskenv) C:\Users\LMartin\Zoov_Flask_App>docker-compose up -d
[+] Running 2/2
- Container zoov_mongodb Started 1.1s
- Container zoov_flask_app-flaskapp1-1 Started 2.9s
(flaskenv) C:\Users\LMartin\Zoov_Flask_App>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e3012f3f3d6 testdocker2:latest "python3" 15 hours ago Exited (0) 5 seconds ago zoov_flask_app-flaskapp1-1
3dca04bda58c mongo:latest "docker-entrypoint.s…" 15 hours ago Up 6 seconds 27017/tcp zoov_mongodb
0717f357e2d6 alpine/git "git clone https://g…" 3 days ago Exited (0) 3 days ago repo
As you can see, my flask container is created but not ran when I use docker compose, whereas everything seems to be working fine for the MongoDB container.
I think there is a problem in my Dockerfile, especially in my CMD statement at the end of it, but I can't figure out what I'm doing wrong.
Dockerfile:
FROM python:3.8-slim-buster
WORKDIR /app1/app
COPY ../../requirements.txt ../../requirements.txt
RUN pip install -r ../requirements.txt
COPY .. .
CMD ["python", "app1/app/run.py"]
I've tried several commands for this last line, such as CMD ["python", "-m", "flask", "run", "--host=0.0.0.0", "--port=8081"], or to switch the CMD with a RUN statement but nothing seems to be working.
Here is the docker-compose.yml file
version: "3.8"
services:
mongodb:
image: mongo:latest
container_name: zoov_mongodb
restart: unless-stopped
command: mongod --auth
environment:
MONGO_INITDB_ROOT_USERNAME: zoovroot
MONGO_INITDB_ROOT_PASSWORD: zoovrootpwd
MONGODB_INITDB_DATABASE: zoovdb
MONGODB_DATA_DIR: mongodb/database
MONGODB_LOG_DIR: mongodb/logs
volumes:
- mongodbdata:/data/db
networks:
- backend
flaskapp1:
build:
context: app1
dockerfile: Dockerfile
image: testdocker2:latest
environment:
APP_DEBUG: True
APP_PORT: 8081
MONGODB_DATABASE: zoovdb
MONGODB_USERNAME: zoovuser
MONGODB_PASSWORD: lucas123
MONGODB_HOSTNAME: mongodb
volumes:
- appdata:/var/www
depends_on:
- mongodb
networks:
- frontend
- backend
volumes:
mongodbdata:
driver: local
appdata:
driver: local
networks:
frontend:
driver: bridge
backend:
driver: bridge
What's wrong with what I did ?
Here is how my project folder looks like
Edit: Python code
from flask import Flask, render_template, jsonify, request, url_for, redirect
from flask_pymongo import PyMongo
import json
import os
app = Flask(__name__, template_folder="../templates")
@app.route('/')
def home():
return render_template("home.html")
@app.route('/get', methods=['GET', 'POST'])
def selectbike():
if request.method == "POST":
b = request.form["nm"]
return redirect(url_for("get_bike", id=b))
else:
return render_template("get.html")
@app.route('/get/<id>')
def get_bike(id):
return id
@app.route('/all')
def get_all_bikes():
return "allbikes"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8081, debug=True)
Edit 2 :
(flaskenv) C:\Users\LMartin\Zoov_Flask_App>docker-compose up -d
[+] Running 2/2
- Container zoov_mongodb Started 1.4s
- Container zoov_flask_app-flaskapp1-1 Started 3.1s
(flaskenv) C:\Users\LMartin\Zoov_Flask_App>docker logs zoov_flask_app-flaskapp1-1
(flaskenv) C:\Users\LMartin\Zoov_Flask_App>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
