Docker container stops after a few seconds and shows 'in use (dangling)' - docker

I am using Docker Desktop in Windows 10. I dockerized a Machine Learning project using Django as a web framework. Whenever I am going to run the image, it just runs for a few seconds and stops. In Docker Desktop, it shows 'in use (dangling).'
I restarted the container and did other stuff but it did not work.
This is my Dockerfile:
FROM python
RUN mkdir /disease-prediction
WORKDIR /disease-prediction
ADD . /disease-prediction
RUN pip install -r requirements.txt
EXPOSE 8000
CMD python manage.py runserver 0:8000

Related

The Discord music bot does not play music in the Docker container

I'm new to Docker and I'm trying to deploy a Discord music bot on a remote Ubuntu Linux server in a Docker container.
The problem is that when the bot is launched in the container, the music does not play, while other parts of the bot that are not related to music playback work fine; at the same time, if the bot is launched outside the Docker container environment, the bot works fine and the music is played.
The bot's behavior when requesting music playback when it is operating in a container is as follows: the bot displays a message that it has found a song and that it starts playing it, and immediately thereafter displays a message that the song has finished playing.
I guess the problem lies in my Dockerfile, or in how I run the image.
My Dockerfile:
FROM node:lts-alpine
ENV WORK_FOLDER='/usr/MyBot'
WORKDIR ${WORK_FOLDER}
COPY ["package.json", "package-lock.json", "tsconfig.json", "./"]
COPY src ./src
RUN npm install
RUN npm install -g typescript
RUN npm run build
COPY . .
CMD ["node", "./dist/index" ]
I'm building an image with the command:
docker build -t mybot .
And I launch the container as follows:
docker run -d -p 80:80 --restart=always --name=mybot mybot
The bot is written in TypeScript using the library discord.js. To play music, the bot uses the library distube.js.
If your Docker host is a Linux machine, you should be able to start your container with docker run -d -p 80:80 --restart=always --name=mybot mybot --device /dev/snd in order to allow the Docker container to access the audio device of the host.
I have solved this problem. In my case, the problem was that my Docker was configured to create Alpine images. As far as I understand, the Alpine image is stripped down for better performance, and I assume that the Alpine image simply did not have the libraries necessary for the music part of the bot to work correctly.
My solution was to completely reinstall Docker according to the instructions for Ubuntu from the official Docker website. I also needed to change the version of Nodejs used in the image from node:lts-alpine to node:18.12, and I also changed the port used by my image from 8080 to 3000.
Ultimately, my Dockerfile looks like this:
FROM node:18.12
ENV WORK_FOLDER='/usr/MyBot'
WORKDIR ${WORK_FOLDER}
COPY ["package.json", "package-lock.json", "tsconfig.json", "./"]
COPY src ./src
RUN npm install npm -g
RUN npm install
RUN npm install -g typescript
RUN npm run build
COPY . .
EXPOSE 3000
CMD ["node", "./dist/index" ]
It took me a long time to find a solution to this problem, so I'm sharing with you one of the ways to check which image your Docker is configured for: just call the batch manager when building the image in your Dockerfile. RUN apk update will work for Alpine, and RUN apt-get update for Ubuntu.

Problem with my Docker image not running Flask

I am trying to build a Flask docker image. I get the error:
zsh: command not found: flask
I followed this old tutorial to get things working. https://medium.com/#rokinmaharjan/running-a-flask-application-in-docker-80191791e143
In order to just learn how to start flask website with Docker I have made everything simple. My Docker image should just open a Hello world front page.
My example.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World'
if __name__ == '__main__':
app.run()
My Dockerfile:
FROM ubuntu:16.04
RUN apt-get update -y
RUN apt-get install python -y
RUN apt-get install python-pip -y
RUN pip install flask
COPY example.py /home/example.py
ENTRYPOINT FLASK_APP=/home/example.py flask run --host=0.0.0.0
I run
sudo docker build . -t flask-app
to build the image.
When I run
docker run -p 8080:5000 flask-app
I get the error:
zsh: command not found: flask
What am I missing here?
Well, indeed you're following a really old tutorial.
I'm not going to enter into detail whether using Flask directly without a WSGI server is something you should do, so I'm just going to focus on your question.
Concise answer: you don't have the installed modules by pip in your PATH, so of course you cannot invoke them. Flask is one of this modules.
Extended answer: keep reading.
First of all, using that base image you're downloading an old version of both Python and pip, secondary: you don't need a fully fledged operative system in order to run a Flask application. There are already base images with Python like python:3.9.10-slim-buster with way less dependencies and possible vulnerabilities than an old image from Ubuntu 16.
FROM python:3.9.10-slim-buster
Second, you shouldn't rely on what do you have on the base image and you should use an environment (venv) for your application, where you can install Flask and any other dependency of the application which should be listed on the requirements.txt. Also you should choose in which working directory you would like to place your code (/usr/src/app is a common place normally).
Indicating which port are you exposing by default is also a good thing to do (even though everyone knows that Flask exposes port 5000).
FROM python:3.9.10-slim-buster
WORKDIR /usr/src/app
ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
RUN python3 -m pip install flask
COPY example.py .
ENTRYPOINT FLASK_APP=example flask run --host=0.0.0.0
EXPOSE 5000
and as a result:
❯ docker run -p 8080:5000 flask-app
* Serving Flask app 'example' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://172.17.0.2:5000/ (Press CTRL+C to quit)

Unable to create folders from django app with docker container

I am unable to create folder through the below command when my django app is running through the container is running.
os.makedirs(path)
The same command is able create when i run my django project from command prompt locally without docker.
my Dockerfile
FROM python:3.6.9-stretch
ENV PYTHONUNBUFFERED 1
ENVC_FORCE_ROOT true
RUN mkdir /src
RUN mkdir /static
WORKDIR /src
ADD ./src /src
RUN pip install -r requirements.pip
CMD python manage.py collectstatic --no-input;python manage.py migrate; gunicorn specextractor.wsgi -b 0.0.0.0:8000
when in my views.py i used to create the folders with the username whoever logged into the application. The functionality is working when running without docker , but it fails when running through docker container.
Any suggestion would be really helpful.

How to run both neo4j and flask webapplication from the same docker container

I want to run a whole application from a single docker container, the application has three components.
neo4j database that, must be accessible via a localhost port say bolt port 7687
a flask application that must access the database and the results or output of the same available across a localhost port say 5000
a web application page index.html that acts as the front end of the flask application. this will access the flask application via 5000 port.
i need the first two coponents to run from the same container.
i got the flask application containorised but could not get both running.
i use a neo4j-community version and #not a neo4j docker image. so inorder to run the same we must execute neo4j start from neo4j-community/bin file
the docker file is stated below
FROM python:3.7
VOLUME ./:app/
RUN apt-get update -y
RUN apt-get install -y python-pip python-dev build-essential
COPY . /app/
WORKDIR /app
RUN cd neo4j-community-3.5.3/bin/
CMD ["neo4j start"]
RUN cd ../../
RUN cd flask_jan_24/
RUN pip install -r requirements.txt
CMD ["flask_jan_24/app_flask.py"]
EXPOSE 5000
Issue is that you have actually started Neo4j in the RUN statement (which is part of the build process).
Actually you should have a shell script which must launch all the required services (like neo4j or anything else) in background and then at the end you should launch the actual flask application.

Asp.net 5 - Docker

I'm about to lose my head ;-)
I've tried all tutorials I can find on running ASP.NET 5 using Docker in Linux.
It won't work....
I'm using Ubuntu 14.04 no matter what I do I always get :
mountpoint for devices not ready.
For some Dockerfile setup I'll get this message in build for other configuration I'll get it on run.
There is no point to shave my Dockerfile because I had tens of them.
I'm using Windows 10 Hyper-V for the virualization
Dockerfile
FROM microsoft/aspnet:1.0.0-rc1-final
COPY . /app
WORKDIR /app
RUN ["dnu", "restore"]
EXPOSE 5000
ENTRYPOINT ["dnx","-p", "project.json", "kestrel"]
Build - docker build -t dev3 .
Run - docker run -t -d -p 87:5000 dev3
Docker Install - apt-get install docker.io
Im running ASP.NET 5 in docker on ubuntu 15.04, I noticed in my Dockerfile im using this image:
FROM microsoft/aspnet:1.0.0-rc1-final-coreclr
You could try using that image, but it sounds like its your Docker install.

Resources