I have a local docker image running with a django project that i am running. The docker image is up but when i go to the local host with port 8000 which is set, gives a message that the page is not working and that 127.0.0.1 didn’t send any data.
Here is the docker code:
omars-mbp:Split omarjandali$ docker build -t split .
Sending build context to Docker daemon 222.2kB
Step 1/7 : FROM python:3
---> 79e1dc9af1c1
Step 2/7 : WORKDIR user
---> 15e014da5b80
Removing intermediate container f4081817276f
Step 3/7 : COPY requirements.txt ./
---> 1f444390862b
Step 4/7 : EXPOSE 8000
---> Running in f75a6674ade2
---> 7641865ffc85
Removing intermediate container f75a6674ade2
Step 5/7 : RUN pip install -r requirements.txt
---> Running in 40738a20f481
Collecting Django==1.11.5 (from -r requirements.txt (line 1))
Downloading Django-1.11.5-py2.py3-none-any.whl (6.9MB)
Collecting gunicorn==19.6.0 (from -r requirements.txt (line 2))
Downloading gunicorn-19.6.0-py2.py3-none-any.whl (114kB)
Collecting pytz (from Django==1.11.5->-r requirements.txt (line 1))
Downloading pytz-2017.3-py2.py3-none-any.whl (511kB)
Installing collected packages: pytz, Django, gunicorn
Successfully installed Django-1.11.5 gunicorn-19.6.0 pytz-2017.3
---> 371a95617f78
Removing intermediate container 40738a20f481
Step 6/7 : COPY . .
---> fa31f9520063
Step 7/7 : CMD python manage.py runserver 0.0.0.0:8000
---> Running in 28b6a097dac4
---> c0d19bca8c2d
Removing intermediate container 28b6a097dac4
Successfully built c0d19bca8c2d
Successfully tagged split:latest
omars-mbp:Split omarjandali$ docker run -d -p 8000:8000 split
d70035b14ab0c2046f2bfc8418960a0b1e5dd8bb75e09a045b41e94bcb097aa4
omars-mbp:Split omarjandali$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d70035b14ab0 split "python manage.py ..." 4 seconds ago Up 2 seconds 0.0.0.0:8000->8000/tcp focused_shirley
docker file:
FROM python:3
WORKDIR user
COPY requirements.txt ./
EXPOSE 8000
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Related
I am trying to upload a Django app to Docker Hub. On the local machine (Ubuntu 18.04) everything works fine, but on Docker Hub there is an issue that the requirements.txt file cannot be found.
Local machine:
sudo docker-compose build --no-cache
Result (it's okay):
Step 5/7 : COPY . .
---> 5542d55caeae
Step 6/7 : RUN file="$(ls -1 )" && echo $file
---> Running in b85a55aa2640
Dockerfile db.sqlite3 hello_django manage.py requirements.txt venv
Removing intermediate container b85a55aa2640
---> 532e91546d41
Step 7/7 : RUN pip install -r requirements.txt
---> Running in e940ebf96023
Collecting Django==3.2.2....
But, Docker Hub:
Step 5/7 : COPY . .
---> 852fa937cb0a
Step 6/7 : RUN file="$(ls -1 )" && echo $file
---> Running in 281d9580d608
README.md app config docker-compose.yml
Removing intermediate container 281d9580d608
---> 99eaafb1a55d
Step 7/7 : RUN pip install -r requirements.txt
---> Running in d0e180d83772
[91mERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
Removing intermediate container d0e180d83772
The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1
app/Dockerfile
FROM python:3.8.3-alpine
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /code
COPY . .
RUN file="$(ls -1 )" && echo $file
RUN pip install -r requirements.txt
docker-composer.yml
version: '3'
services:
web:
build:
context: app
dockerfile: Dockerfile
volumes:
- ./app/:/code/
ports:
- "8000:8000"
env_file:
- ./config/.env.dev
command: python manage.py runserver 0.0.0.0:8000
Project Structure:
UPDATE:
Docker is building from Github.
File requirements.txt is in the GitHub repository (app folder), but for some reason during build Docker Hub copies files from the project root folder and not the contents of the app folder.
Github:
https://github.com/sigalglebru/django-on-docker
The problem is that you need to tell Docker Hub where to find your build context.
When you run docker-compose build locally, docker-compose reads your docker-compose.yml file and knows to build inside the app directory, because you've explicitly set the build context:
build:
context: app
dockerfile: Dockerfile
When you build on Docker Hub, by default it will assume the build
context is the top level of your repository. If you set the path to
your Dockerfile to, e.g., app/Dockerfile, this is equivalent to
running:
docker build -f app/Dockerfile .
If you try that, you'll see if fail the same way. Rather than setting
the path to the Dockerfile, you need to set the path to the build
context to the app directory. For example:
(Look at the "Build Context" column).
When configured correct, your repository builds on Docker Hub without errors.
Thank you, I found solution:
I just copied files from./app to the mounted volume, and little changed context, but still don't understand why it worked fine on the local machine
Dockerfile:
FROM python:3.8.3-alpine
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /code
COPY ./app .
RUN pip install -r requirements.txt
docker-compose.yml
version: "3.6"
services:
python:
restart: always
build:
context: .
dockerfile: docker/Dockerfile
expose:
- 8000
ports:
- 8000:8000
command: "python manage.py runserver 0.0.0.0:8000"
Quite simple: the docker-compose configuration below does not allow any files to persist after running. So when I do docker exec -i -t aas-job-hunter_web_1 ls /app -alt, I see nothing.
Here is the (non-)working minimal example: https://github.com/philastrophist/test-docker
I'm on Windows 10, I've allowed mounted drives and enabled the TLS connection. I'm not sure what else to do. The thing that most confuses me is that requirements.txt is clearly copied over (since it installs it all) but it isn't there when I have a look docker exec.
My directory structure is:
parent/
website/
manage.py
...
Dockerfile
docker-compose.yml
...
Dockerfile:
FROM python:3.6
#WORKDIR /app
# By copying over requirements first, we make sure that Docker will cache
# our installed requirements rather than reinstall them on every build
COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
# Now copy in our code, and run it
COPY . /app
EXPOSE 8000
CMD python website/manage.py runserver 0.0.0.0:8000
# CMD tail -f /dev/null # use when testing
docker-compose.yml:
version: '2'
services:
web:
build: .
ports:
- "8000:8000"
volumes:
- .:/app
links:
- db
db:
image: "postgres:9.6"
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: hunter2
Traceback:
> docker-compose -f docker-compose.yml up --build
Building web
Step 1/6 : FROM python:3.6
---> 0668df180a32
Step 2/6 : COPY requirements.txt /app/requirements.txt
---> Using cache
---> 3073d0bef876
Step 3/6 : RUN pip install -r /app/requirements.txt
---> Using cache
---> 8ad63bbb3de5
Step 4/6 : COPY . /app
---> 16390cdd6c2c
Step 5/6 : EXPOSE 8000
---> Running in f628000e8961
Removing intermediate container f628000e8961
---> 80e6994cfbd2
Step 6/6 : CMD python website/manage.py runserver 0.0.0.0:8000
---> Running in acb6b25eb558
Removing intermediate container acb6b25eb558
---> da8876d78103
Successfully built da8876d78103
Successfully tagged aas-job-hunter_web:latest
Starting aas-job-hunter_db_1 ... done
Recreating aas-job-hunter_web_1 ... done
Attaching to aas-job-hunter_db_1, aas-job-hunter_web_1
db_1 | LOG: database system was shut down at 2019-05-24 21:23:31 UTC
db_1 | LOG: MultiXact member wraparound protections are now enabled
db_1 | LOG: database system is ready to accept connections
web_1 | python: can't open file 'website/manage.py': [Errno 2] No such file or directory
aas-job-hunter_web_1 exited with code 2
Actually it copies files.
Solution 1
Change CMD to :
CMD python /app/website/manage.py runserver 0.0.0.0:8000
Solution 2
You call WORKDIR before the /app folder is created. So change your Dockerfile to :
FROM python:3.6.2
# By copying over requirements first, we make sure that Docker will cache
# our installed requirements rather than reinstall them on every build
COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
# Now copy in our code, and run it
COPY . /app
WORKDIR /app
#EXPOSE 8000
CMD python ./website/manage.py runserver 0.0.0.0:8000
# CMD tail -f /dev/null # use when testing
And call it after.
Moreover remember that in your current docker-compose file you are using bind mounts, not volumes, so the context - . will replace entirely the content of /app in your container.
Uncomment #WORKDIR /app.
I also cleaned the other parts a bit up to utilize the WORKDIR more.
FROM python:3.6
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD python website/manage.py runserver 0.0.0.0:8000
I think there is nothing wrong with COPY. But, you need to set the work directory to /app as your manage.py file is inside /app/website, not in /website inside Docker.
So, I think your Dockerfile should be like this:
FROM python:3.6
RUN mkdir /app
COPY requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
COPY . /app
WORKDIR /app
EXPOSE 8000
CMD python website/manage.py runserver 0.0.0.0:8000
I am trying to run a Django docker image. The image itself is running in the command line without any errors. However when I go to the URL my image is hosted at, the web page is not found.
Below is my docker file I used to build the image
FROM python:3.6
MAINTAINER c15523957
RUN apt-get -y update
RUN apt-get -y upgrade
RUN apt-get -y install libgdal-dev
RUN mkdir -p /usr/src/app
COPY requirements.txt /usr/src/app/
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Below is the command used to run the image
docker run -it -p8001:8000 c15523957/backendimage7
The console logs the message
System check identified no issues (0 silenced).
December 11, 2018 - 15:05:03
Django version 2.1.3, using settings 'backendproject.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
However when I go to the browser the web page is not found.
Note: When I run the Django application without the Docker image the webpage is seen. This is done with
python manage.py runserver
SOLUTION
I was using docker toolbox for windows. Instead of local dcker toolbox binds to the address 192.168.99.100 . So by going to port ....
192.168.99.100:8001
I was able to access my web page
My name is omar. I have a docker image and container for a project that I have just created and ran. So i am currently trying to test it on my local machine before pushing it to an online environment. I built it with no previous cache and it build the new docker which is running online. Why is it not working in my browser. Here is what I have.
(MySplit) omars-mbp:mysplit omarjandali$ docker build --no-cache -t validation_test_1 .
Sending build context to Docker daemon 56.07MB
Step 1/7 : FROM python:3
---> 79e1dc9af1c1
Step 2/7 : WORKDIR users/
Removing intermediate container 7030351beb91
---> 30ac3f4ccbae
Step 3/7 : COPY requirements.txt ./
---> 57cbbd7335ab
Step 4/7 : EXPOSE 80
---> Running in 592407a176ff
Removing intermediate container 592407a176ff
---> 523945ea529f
Step 5/7 : RUN pip install -r requirements.txt
---> Running in 48347f772fbe
Collecting Django==1.11.5 (from -r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/18/2d/b477232dd619d81766064cd07ba5b35e956ff8a8c5c5d41754e0392b96e3/Django-1.11.5-py2.py3-none-any.whl (6.9MB)
Collecting gunicorn==19.7 (from -r requirements.txt (line 2))
Downloading https://files.pythonhosted.org/packages/96/4b/bc4bc2dad60defaa3f7d8590dc51331a225a5399380c161047c1224cd86d/gunicorn-19.7.0-py2.py3-none-any.whl (112kB)
Collecting django-localflavor==1.5.3 (from -r requirements.txt (line 3))
Downloading https://files.pythonhosted.org/packages/da/47/69e53f69fb50a38766aa929c1f81fd2e6315edc7f3945174ead24ffcf6df/django-localflavor-1.5.3.tar.gz (4.7MB)
Collecting synapse_pay_rest_native==3.1.1 (from -r requirements.txt (line 4))
Downloading https://files.pythonhosted.org/packages/43/27/b73d83dd50d4dfec1680d22896b800ddbb6bf8fb3f3a1755a916a6e4f732/synapse_pay_rest_native-3.1.1.tar.gz
Collecting pytz (from Django==1.11.5->-r requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/dc/83/15f7833b70d3e067ca91467ca245bae0f6fe56ddc7451aa0dc5606b120f2/pytz-2018.4-py2.py3-none-any.whl (510kB)
Collecting requests (from synapse_pay_rest_native==3.1.1->-r requirements.txt (line 4))
Downloading https://files.pythonhosted.org/packages/49/df/50aa1999ab9bde74656c2919d9c0c085fd2b3775fd3eca826012bef76d8c/requests-2.18.4-py2.py3-none-any.whl (88kB)
Collecting chardet<3.1.0,>=3.0.2 (from requests->synapse_pay_rest_native==3.1.1->-r requirements.txt (line 4))
Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
Collecting idna<2.7,>=2.5 (from requests->synapse_pay_rest_native==3.1.1->-r requirements.txt (line 4))
Downloading https://files.pythonhosted.org/packages/27/cc/6dd9a3869f15c2edfab863b992838277279ce92663d334df9ecf5106f5c6/idna-2.6-py2.py3-none-any.whl (56kB)
Collecting urllib3<1.23,>=1.21.1 (from requests->synapse_pay_rest_native==3.1.1->-r requirements.txt (line 4))
Downloading https://files.pythonhosted.org/packages/63/cb/6965947c13a94236f6d4b8223e21beb4d576dc72e8130bd7880f600839b8/urllib3-1.22-py2.py3-none-any.whl (132kB)
Collecting certifi>=2017.4.17 (from requests->synapse_pay_rest_native==3.1.1->-r requirements.txt (line 4))
Downloading https://files.pythonhosted.org/packages/7c/e6/92ad559b7192d846975fc916b65f667c7b8c3a32bea7372340bfe9a15fa5/certifi-2018.4.16-py2.py3-none-any.whl (150kB)
Building wheels for collected packages: django-localflavor, synapse-pay-rest-native
Running setup.py bdist_wheel for django-localflavor: started
Running setup.py bdist_wheel for django-localflavor: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/36/41/a8/30c023fe29300f1a19d0520aaff1faf9e4c7ab176c53913ab5
Running setup.py bdist_wheel for synapse-pay-rest-native: started
Running setup.py bdist_wheel for synapse-pay-rest-native: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/33/93/7f/6bd8dd39a1c22e2ef85a259a45e603dff2b0ab4d6209a7a976
Successfully built django-localflavor synapse-pay-rest-native
Installing collected packages: pytz, Django, gunicorn, django-localflavor, chardet, idna, urllib3, certifi, requests, synapse-pay-rest-native
Successfully installed Django-1.11.5 certifi-2018.4.16 chardet-3.0.4 django-localflavor-1.5.3 gunicorn-19.7.0 idna-2.6 pytz-2018.4 requests-2.18.4 synapse-pay-rest-native-3.1.1 urllib3-1.22
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Removing intermediate container 48347f772fbe
---> 5749b63a2fef
Step 6/7 : COPY . .
---> 7754d9db1f38
Step 7/7 : CMD ["python", "manage.py", "runserver"]
---> Running in 362648ae0426
Removing intermediate container 362648ae0426
---> c26bf2de11b4
Successfully built c26bf2de11b4
Successfully tagged validation_test_1:latest
(MySplit) omars-mbp:mysplit omarjandali$ docker run -d -P validation_test_1:latest
b038c688ba4e681c03d8e093cf630a564d2f7cea27812b55dd0d1c33aa0f27b4
(MySplit) omars-mbp:mysplit omarjandali$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b038c688ba4e validation_test_1:latest "python manage.py ru…" 5 seconds ago Up 4 seconds 0.0.0.0:32768->80/tcp zealous_mcnulty
(MySplit) omars-mbp:mysplit omarjandali$
I have tried the following localhost:32768 and 127.0.0.1:32768 does anyone know why this is happening.
I have allowed all hosts in my django project.
dockerfile
FROM python:3
WORKDIR users/
COPY requirements.txt ./
EXPOSE 80
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "manage.py", "runserver"]
This will be because Django's runserver accepts connections from 127.0.0.1 only by default and when you hit localhost:32768 from your browser the connection isn't coming from 127.0.0.1 / localhost, but from your actual host IP.
Change your CMD to:
CMD ["python", "manage.py", "runserver", "0.0.0.0:80"]
I have a basic django project and I am trying to get it running locally through docker. I have the docker file. I build the docker image. I ran the docker image. It is running, but my webpage shows an error on the screen like it is not connecting to the docker server... Here is what I have:
docker file:
FROM python:3
WORKDIR general
COPY requirements.txt ./
EXPOSE 8000
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Here is how I am buiding and running this project:
omars-mbp:split omarjandali$ docker build -t splitbeta/testing2 .
Sending build context to Docker daemon 223.7kB
Step 1/7 : FROM python:3
---> 79e1dc9af1c1
Step 2/7 : WORKDIR general
---> 04a6f8a7f92a
Removing intermediate container b2ffb485e485
Step 3/7 : COPY requirements.txt ./
---> 649d77ec499e
Step 4/7 : EXPOSE 8000
---> Running in 7d8d6fe8de1d
---> c328d885a5f1
Removing intermediate container 7d8d6fe8de1d
Step 5/7 : RUN pip install -r requirements.txt
---> Running in 1c9aca43dc14
Collecting Django==1.11.5 (from -r requirements.txt (line 1))
Downloading Django-1.11.5-py2.py3-none-any.whl (6.9MB)
Collecting gunicorn==19.6.0 (from -r requirements.txt (line 2))
Downloading gunicorn-19.6.0-py2.py3-none-any.whl (114kB)
Collecting pytz (from Django==1.11.5->-r requirements.txt (line 1))
Downloading pytz-2017.3-py2.py3-none-any.whl (511kB)
Installing collected packages: pytz, Django, gunicorn
Successfully installed Django-1.11.5 gunicorn-19.6.0 pytz-2017.3
---> 602e88557c8b
Removing intermediate container 1c9aca43dc14
Step 6/7 : COPY . .
---> 55cff629cb51
Step 7/7 : CMD python manage.py runserver 0.0.0.0:8000
---> Running in efd75f8fb602
---> 2cef664a626d
Removing intermediate container efd75f8fb602
Successfully built 2cef664a626d
Successfully tagged splitbeta/testing2:latest
omars-mbp:split omarjandali$ docker run -d spltibeta/testing2
Here is the project running:
omars-mbp:split omarjandali$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fc14f03a18b0 splitbeta/testing2 "python manage.py ..." 3 seconds ago Up 3 seconds 8000/tcp loving_volhard
THe webpage is giving the following error when it is supposed to display a template page....
This site can’t be reached
127.0.0.1 refused to connect.
I got it running yesterday but it is not working any more... I dont know why. I didnt change anything
I am logged into my dockerhub account in my terminal
It seems your Docker run command doesn't publish port 8000. By default, docker won't publish any container ports on the host system if you don't tell it to explicitly. Try using the -p or --publish option of docker run:
docker run -d -p 8000:8000 spltibeta/testing2
Alternatively, you can use the -P or --publish-all option to publish all exposed ports of your container on your host system. This will assign a random port on the host.
docker run -d -P spltibeta/testing2