I'm trying to make a simple example of Django app running in docker container.
using this image https://hub.docker.com/_/django/
just for simplicity. Don't tell me please that i shouldn't use it in production :) app is very simple and i'm ok with using very basic Django server.
So, the problem is i'm always getting this error when trying to run the container image
C:\Users\slipo\PycharmProjects\simple_blog>docker run -p 8000:8000 my-blog
python: can't open file './manage.py runserver 0.0.0.0:8000 --settings=mysite.settings.prod': [Errno 2] No such file or directory
however, ./manage.py and mysite.settings.prod both definitely existing in container.
container creation log showing the file exists:
Step 7 : RUN ls -a
---> Running in 932ed2ad3e4c
.
..
.idea
Dockerfile
blog
manage.py
mysite
requirements.txt
templates
---> e7f938c1cbf2
Removing intermediate container 932ed2ad3e4c
Step 8 : CMD python ./manage.py runserver 0.0.0.0:8000 --settings=mysite.settings.prod
---> Running in f99bcafbc269
---> aca534e9ccb6
Removing intermediate container f99bcafbc269
Successfully built aca534e9ccb6
Dockerfile:
FROM django
EXPOSE 8000
ADD . /simple_blog
WORKDIR /simple_blog
RUN pip install -r requirements.txt
RUN pip install django-tinymce
RUN ls -a
CMD [ "python", "./manage.py runserver 0.0.0.0:8000 --settings=mysite.settings.prod" ]
Thank you.
can't open file './manage.py runserver 0.0.0.0:8000 --settings=mysite.settings.prod'
This is telling you that it is treating that entire string as a single filename.
I assume something like this works:
CMD [ "python", "./manage.py", "runserver", "0.0.0.0:8000", "--settings=mysite.settings.prod" ]
Try to execute this code.
CMD [ "python", "../manage.py runserver 0.0.0.0:8080 --settings=my_site.settings.prd"
Related
I defined a command in compose.yml like command: "python manage.py runserver 0.0.0.0:8000" and a port mapping when I try docker-compose up it works fine.
But when I built the image and try to run docker run image it exit immediately.
Only things like docker run -d -p 8000:8000 twitch_test_app python manage.py runserver 0.0.0.0:8000 works even though I already defined the command and the port mapping in docker-compose.yml.
Is there any way to just run ```docker run image without repeating the command that is defined in compose.yml file?
Thanks alot.
I think you are confused between docker-compose and docker.
Think docker-compose as a helper utility that help you easily setup docker environment. When you do docker-compose up, it will create a new image and setup docker container for you with all the configuration that you have specified. Your application will run inside the docker container. Docker-compose just makes life little easier.
However, if you want to just use the docker command you need to specify the same port-mapping and other configurations in the dockerfile itself.
When you do docker run image it will create container for you. But you need to provide entrypoint or command to start your application otherwise it will just exit as docker does not know what to do next. In your case I suppose is a Django project and you need to specify "python manage.py runserver" to start your django server. In dockerfile you can specify it using CMD attribute:
CMD [ "python", "manage.py", "runserver" ]
Django server is running well in localhost. however, When I try to run server on the docker container, it doesn't find the manage.py file when using docker-compose file and even I run the container manually and run the server, it doesn't appear in browser. how can I solve this problem?
So I wrote all the code testing on my local server and using the dockerfile, I built the image of my project.
and I tried to run server on the docker container, suddenly this doesn't run.
what's worse, if I use docker-compose to run the server, it doesn't find the manage.py file though I already checked with 'docker run -it $image_name sh'
here is the code of my project
I am new to docker and new to programming.
hope you can give me a help. thanks!
file structure
current directory
└─example
└─db.sqlite3
└─docker-compose.yml
└─Dockerfile
└─manage.py
└─Pipfile
└─Pipfile.lock
Docker file
# Base image - Python version
FROM python:3.6-alpine
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /code
# Copy Pipfile
COPY Pipfile /code
COPY Pipfile.lock /code
# Install dependencies
RUN pip install pipenv
RUN pipenv install --system
# Copy files
COPY . /code/
docker-compose.yml
# docker-compose.yml
version: '3.3'
services:
web:
build: .
command: python /code/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- 8000:8000
expected result : running server in web browser like in chrome
actual result :
when using docker-compose :
ERROR like this in the prompt : web_1 | python: can't open file '/code/manage.py': [Errno 2] No such file or directory
when running the container manually with 'docker run -it $image_name sh' and 'python manage.py runserver' on the shell :
server is running but, doesn't connect to web browser. (doesn't show up in browser like chrome'
Yo have done same thing in many ways. You have copy source files using a COPY command and then you have mounted a host volume in your docker-compose.yml file. In first place you don't need a volume because volume mounts are to persisting data generated by and used by Docker containers.
Following simplified Dockerfile and docker-compose file would fix the problem.
# Base image - Python version
FROM python:3.6-alpine
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Copy files
COPY . /code/
# Set work directory
WORKDIR /code
# Install dependencies
RUN pip install pipenv
RUN pipenv install --system
docker-compose.yml -:
# docker-compose.yml
version: '3.3'
services:
web:
build: .
command: python ./manage.py runserver 0.0.0.0:8000
ports:
- 8000:8000
I am trying to get a django project that I have built to run on docker and create an image and container for my project so that I can push it to my dockerhub profile.
Now I have everything set up and I've created the initial image of my project. However, when I run it I am not getting any port number attached to the container. I need this to test and see if the container is actually working.
Here is what I have:
Successfully built a047506ef54b
Successfully tagged test_1:latest
(MySplit) omars-mbp:mysplit omarjandali$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test_1 latest a047506ef54b 14 seconds ago 810MB
(MySplit) omars-mbp:mysplit omarjandali$ docker run --name testing_first -d -p 8000:80 test_1
01cc8173abfae1b11fc165be3d900ee0efd380dadd686c6b1cf4ea5363d269fb
(MySplit) omars-mbp:mysplit omarjandali$ docker container ls -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
01cc8173abfa test_1 "python manage.py ru…" 13 seconds ago Exited (1) 11 seconds ago testing_first
(MySplit) omars-mbp:mysplit omarjandali$ Successfully built a047506ef54b
You can see there is no port number so I don't know how to access the container through my local machine on my web browser.
dockerfile:
FROM python:3
WORKDIR tab/
COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "manage.py", "runserver", "0.0.0.0"]
This line from the question helps reveal the problem;
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
01cc8173abfa test_1 "python manage.py ru…" 13 seconds ago Exited (1) 11 seconds ago testing_first
Exited (1) (from the STATUS column) means that the main process has already exited with a status code of 1 - usually meaning an error. This would have freed up the ports, as the docker container stops running when the main process finishes for any reason.
You need to view the logs in order to diagnose why.
docker logs 01cc will show the logs of the docker container that has the ID starting with 01cc. You should find that reading these will help you on your way. Knowing this command will help you immensely in debugging weirdness in docker, whether the container is running or stopped.
An alternative 'quick' way is to drop the -d in your run command. This will make your container run inline rather than as a daemon.
Created Dockerise django seed project
django-admin.py startproject djangoapp
Need a requirements.txt file outlining the Python dependencies
cd djangoapp/
RUN follwoing command to create the files required for dockerization
cat <<EOF > requirements.txt
Django
psycopg2
EOF
Dockerfile
cat <<EOF > Dockerfile
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app
ADD requirements.txt /app/
RUN pip install -r requirements.txt
ADD . /app/
EXPOSE 8000
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
EOF
docker-compose.yml
cat <<EOF > docker-compose.yml
version: "3.2"
services:
web:
image: djangoapp
command: python manage.py runserver 0.0.0.0:8000
ports:
- "8000:8000"
EOF
Run the application with
docker-compose up -d
When you created the container you published the ports. Your container would be accessible via port 8000 if it successfully built. However, as Shadow pointed out, your container exited with an error. That is why you must add the -a flag to your docker container ls command. docker container ls only shows running containers without the -a flag.
I recommend forgoing the detached flag -d to see what is causing the error. Then creating a new container after you have successfully launched the one you are working on. Or simply run the following commands once you fix the issue. docker stop testing_first then docker container rm testing_first finally run the same command you ran before. docker run --name testing_first -d -p 8000:80 test_1
I ran into similar problems with the first docker instances I attempted to run as well.
I am using:
Ubuntu 16.04.
Docker version 1.12.6.
I want to containerize my existing Django app., knowing that everything goes well in this app. => no bugs, no errors...
My Dockerfile:
FROM django
ADD . /BackendServer
WORKDIR /BackendServer
RUN pip install -r requirements.txt
CMD [ "python", "BackendServer/manage.py runserver 0.0.0.0:8000" ]
requirements.txt
djangorestframework
gunicorn
Now everything goes well, except the last line when executing the manage.py python, it says: "python: can't open file 'BackendServer/manage.py runserver 0.0.0.0:8000': [Errno 2] No such file or directory".
So, I execute the following command: "sudo docker run backendserver ./BackendServer/manage.py runserver 0.0.0.0:8000"
I got no errors and still the server is not running!!
What shall I do so that I can access the django server !? Please help!!
Additional note:
thanks in advance!
You have already changed the directory into /BackendServer.
Use this instead:
CMD [ "python", "./manage.py runserver 0.0.0.0:8000" ]
Also note that docker run executes without a tty by default, which will suppress the output. Run with -it to use interactive terminal.
I have looked through the questions on this site, but I have not been able to fix this problem.
I created and ran an image of my django app, but when I try to view the app from the browser, the page does not load (can't establish a connection to the server)
I am using docker toolbox, I am using OS X El Capitan and the Macbook is from 2009.
The container IP is: 192.168.99.100
The django project root is called "Web app" and is the directory containing manage.py. My Dockerfile and my requirements.txt files are in this directory.
My dockerfile is:
FROM python:3.5
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"]
My requirements.txt has django and mysqlclient
My django app uses Mysql, and I tried to view the dockerized django app in the browser with and without linking it to the standard mysql image. In both cases, I only see the following error:
problem loading page couldn't establish connection to server
When I did try linking the django container to the mysql container I used:
docker run --link mysqlapp:mysql -d app
Where mysqlapp is my mysql image and 'app' is my django image.
In my django settings.py, the allowed hosts are:
ALLOWED_HOSTS: ['localhost', '127.0.0.1', '0.0.0.0', '192.168.99.100']
Again, the image is successfully created when I used docker build, and it is successfully run as a container. Why is the page not loading in the browser?
I suggest to use yml file and docker compose. Below is a template to get you started:
[Dockerfile]
FROM python:2.7
RUN pip install Django
RUN mkdir /code
WORKDIR /code
COPY code/ /code/
where your files are located in code directory.
[docker-compose.yml]
version: '2'
services:
db:
image: mysql
web0:
build: .
command: python manage.py runserver 0.0.0.0:8000
ports:
- "8000:8000"
depends_on:
- db
There might be a problem with your working directory path defined in Dockerfile. Hope above helps.
Solution provided by salehinejad seems to be good enough ,although i have not tested it personally but if you do not want to use yml file and want to go your way then you should expose the port by adding
-p 0:8000
in your run command
So your should look like this :
docker run -p 0:8000 --link mysqlapp:mysql -d app
I suspect you have not told Docker to talk to your VM, and that your containers are running on your host machine (if you can access at localhost, this is the issue).
Please see this post for resolution:
Connect to docker container using IP