Docker run application in a volume - python

In my django docker app i would to use a volume for manage my application file here is my Dockerfile:
FROM python:3.6-alpine
EXPOSE 8000
RUN apk update
RUN apk add --no-cache make linux-headers libffi-dev jpeg-dev zlib-dev
RUN apk add postgresql-dev gcc python3-dev musl-dev
VOLUME /var/lib/cathstudio/data
WORKDIR /var/lib/cathstudio/data
COPY ./requirements.txt .
RUN pip install --upgrade pip
RUN pip install -t /var/lib/cathstudio/data -r requirements.txt
ENV PYTHONUNBUFFERED 1
ENV PYTHONPATH /var/lib/cathstudio/data
COPY . /var/lib/cathstudio/data
ENTRYPOINT python /var/lib/cathstudio/data/manage.py runserver 0.0.0.0:8000
but when i run my app:
docker run -d -it --rm --link postgres:postgres --name=cathstudio myrepo/app_studio:latest
i get
python: can't open file '/var/lib/cathstudio/data/manage.py': [Errno 2] No such file or directory
the same also if i in my Dockerfile write just ENTRYPOINT python manage.py runserver 0.0.0.0:8000
where is my file wrong? how can i run my app using a volume for storing app files?
So many thanks in advance

Can you try to use the -v flag rather than using VOLUME in dockerfile , im not sure why but VOLUME creates the exact empty volume rather than mounting along with all of the data :
remove VOLUME section from docker file and try the following
docker run -d -it --rm -v /var/lib/cathstudio/data/:/var/lib/cathstudio/data --link postgres:postgres --name=cathstudio myrepo/app_studio:latest

Volumes are not intended to hold code. Especially given your comment to #i4nk1t's answer ("I want to distribute my Docker image"), the code should be in the image itself, not in a volume.
You should just delete the VOLUME line.
Declaring VOLUME in a Dockerfile has some key side effects. One of them is that it prevents any RUN command on that directory from having any future effect, which is keeping your application from working.
I tend to recommend a workflow where you make sure your application works locally and then package it in Docker. If you can't use your host Python and really have to do live development in the container environment, you can use the docker run -v option (or equivalent) to mount host content on to any directory in any container, regardless of whether or not it was declared as a VOLUME.

Related

Unable to mount host directory to docker container

I have a python app that runs every day to download images and saves them into specified folders with each day folder creation like this /home/ubuntu/images/yyyymmdd.
I have built a docker container of my python app on ubuntu 20. When I try to run the app by mounting the host directory then log message prints folder created /home/ubuntu/images/20220123 but I can not see any folder.
I have checked the docker folder /var/lib/docker and found that random hash is created inside folder containers and overlay2. So I have tried to mount with both directories as below but no luck.
sudo docker run -t -i -v /home/ubuntu/images:/var/lib/docker/containers --network=host testapp/img-downloader:0.0.1
sudo docker run -t -i -v /home/ubuntu/images:/var/lib/docker/overlay2 --network=host testapp/img-downloader:0.0.1
I can see the data folder created inside the images folder and image files got saved like this
/var/lib/docker/overlay2/d52bcf61cae2e563c3c8561bab53b4bb2dd2ea2d633a14d40c96d7992fffae28/diff/home/ubuntu/images/20220123
What I am missing here so that it's not saving images to host directory like /home/ubuntu/images/20220123 instead of the inside docker container.
My Dockerfile is as below -
FROM alpine:3.14
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3-dev mariadb-dev gcc musl-dev g++ && ln -sf python3 /usr/bin/python
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools
COPY ./requirements.txt /requirements.txt
WORKDIR /
RUN pip3 install -r requirements.txt
COPY ./ /
ENTRYPOINT [ "python3" ]
CMD [ "./main.py" ]
Please help here. thanks
...What i am missing here so that its not saving images to host directory like /home/ubuntu/images/20220123 instead of inside docker container.
Presumed you meant you want to save images to a directory on the host and not inside the container. There's no need to mount /var/lib/docker/.... You need to ensure your program saved files to a path that is bind mounted to the host. Examples:
mkdir images # Create a directory on the host to hold the images
docker run -it --rm -v ~/images:/images alpine ash -c "mkdir /images/yesterday; mkdir /images/today; echo 'hello' > /images/today/msg.txt; echo 'done.'"
After the container exited, issue ls -ld images/* on the host will show you the 2 directories created; cat images/today/msg.txt will print you the content you saved via the container (simulate if you download images via the container).

Django Docker app container don't open on browser after run [duplicate]

This question already has answers here:
Docker Toolbox - Localhost not working
(7 answers)
Closed 3 years ago.
I dockerize my django app, this is my Dockerfile:
FROM python:3.6-alpine
EXPOSE 8000
RUN apk add --no-cache make linux-headers libffi-dev jpeg-dev zlib-dev
RUN apk update && apk add postgresql-dev gcc python3-dev musl-dev
RUN mkdir /Code
WORKDIR /Code
COPY ./requirements.txt .
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ENV PYTHONUNBUFFERED 1
COPY . /Code/
ENTRYPOINT python /Code/core/manage.py runserver 0.0.0.0:8000
well, i build my image and I run it:
docker run -p 8000:8000 --link postgres:postgres cath11/test_app
all seems to be done:
but when I open my browser on http://0.0.0.0:8000/ or http://127.0.0.1:8000/ my app does not open
Why my django app seems running on my container, I expose port but I cannot see it in my hosted browser?
So many thanks in advance
Use docker-machine ip default to check for the IP, and use it instead of localhost.
Technically, the django app is served inside the container, not your local host.
So in your case, you should try http://<ip from the command>:8000/

how to run exsisting django project in docker?

I'm trying to containerize my existing django project, which works just fine in my local machine.
dockerfile is as follows:
FROM django
ADD . /
WORKDIR /site
RUN pip install django-elasticsearch-dsl==0.5.1
RUN pip install tika==1.19
CMD python manage.py runserver 0.0.0.0:8000
i was able to create an image using :
docker build -t test1 .
and was able to create a container using the image by command :
docker run -d --name test1 -p 8000:8000 test1
as a result, i can see that the container test1 is up and running
Now, my understanding is if i do localhost:8000 in my browser, i should be able to see the view the required pages.
However, I don't see it.
I've have tried similar solutions available in stackoverflow, yet no success.
This image is officially deprecated in favor of the standard python
image, and will receive no further updates after 2016-12-31 (Dec 31,
2016). Please adjust your usage accordingly.
For most usages of this image, it was already not bringing in django
from this image, but actually from your project's requirements.txt, so
the only "value" being added here was the pre-installing of
mysql-client, postgresql-client, and sqlite3 for various uses of the
django framework.
For example, a Dockerfile similar to the following would be a good starting point for a Django project using PostgreSQL:
FROM python:3.4
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
postgresql-client \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
source
PS: your Dockerfile is complaining about manage.py is not found

socket.error: [Errno 99] Address not available..Get this error while trying to link a container with another container of CouchDB in Docker

I am trying to make a Docker Image of a Web Application which is based on python. As I am trying to link the official CouchDB container with the Docker Container that I have created I am getting the "Address not available error". docker ps -a shows me that the couchdb is working in a perfect way.
The command that I have used in the terminal is:
docker run -p 5984:5984 --name my-couchdb -d couchdb
docker run --name my-couchdb-app --link my-couchdb:couchdb webpage:test
My Docker File for webpage:test is given below:
FROM python:2.7.15-alpine3.6
RUN apk update && \^M
apk upgrade && \^M
apk add bash vim sudo
RUN mkdir /home/WebDocker/
ADD ./Webpage1 /home/WebDocker/Webpage1
ADD ./requirements.txt /home/WebDocker/requirements.txt
WORKDIR /home/WebDocker
RUN pip install -r /home/WebDocker/requirements.txt
RUN chmod +x /home/WebDocker/Webpage1/main.py
EXPOSE 8080
ENTRYPOINT ["python","/home/WebDocker/Webpage1/main.py"]

Docker. No such file or directory

I have some files which I want to move them to a docker container.
But at the end docker can't find a file..
The folder with the files on local machine are at /home/katalonne/flask4
File Structure if it matters:
The Dockerfile:
#
# First Flask App Dockerfile
#
#
# Pull base image.
FROM centos:7.0.1406
# Build commands
RUN yum install -y python-setuptools mysql-connector mysql-devel gcc python-devel
RUN easy_install pip
RUN mkdir /opt/flask4
WORKDIR /opt/flask4
ADD requirements.txt /opt/flask4
RUN pip install -r requirements.txt
ADD . /opt/flask4
# Define deafult command.
CMD ["python","hello.py"]
# Expose ports.
EXPOSE 5000
So I built the image with this command :
docker build -t flask4 .
I ran the container with volume by :
docker run -d -p 5000:5000 -v /home/Katalonne/flask4:/opt/flask4 --name web flask4
And when I want to run the file on the container :
docker logs -f web
I get this error that it can not find my hello.py file :
python: can't open file 'hello.py': [Errno 2] No such file or directory
What is my fault?
P.S. : I'm a Docker and Linux partially-noob.
The files and directories that are located in the same location as your Dockerfile are indeed available (temporarily) to your docker build. But, after the docker build, unless you have used ADD or COPY to move those files permanently to the docker container, they will not be available to your docker container after the build is done. This file context is for the build, but you want to move them to the container.
You can add the following command:
...
ADD . /opt/flask4
ADD . .
# Define deafult command.
CMD ["python","hello.py"]
The line ADD . . should copy over all the things in your temporary build context to the container. The location that these files will go to is where your WORKDIR is pointing to (/opt/flask4).
If you only wanted to add hello.py to your container, then use
ADD hello.py hello.py
So, when you run CMD ["python","hello.py"], the pwd that you will be in is /opt/flask4, and hello.py should be in there, and running the command python hello.py in that directory should work.
HTH.

Categories

Resources