1

I am trying to learn Docker. I have a Hello World Django server application. When I try to run my server using a Dockerfile, my server is unreachable. But when I use docker-compose, I am able to access it.

My question is why, especially when they are quite similar.

My Dockerfile:

FROM python:3

# Set the working directory to /app
WORKDIR /bryne

# Copy the current directory contents into the container at /app
ADD . /bryne

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# EXPOSE port 8000 to allow communication to/from server
EXPOSE 8000

# CMD specifcies the command to execute to start the server running.
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
# done!

Commands used when running server using Dockerfile:

docker build -t swyne-latest docker run swyne-latest

Result: Cannot access server at 127.0.0.1:8000

My docker-compose.yml:

version: '3'

services:
  web:
    build: .
    command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
    container_name: swyne
    volumes:
      - .:/bryne
    ports:
      - "8000:8000"

Commands used when running server using docker-compose: docker-compose up

Result: Able to access my server at 127.0.0.1:8000

Thanks

Edit: Output from Dockerfile build:

$ docker build -t swyne-latest .
Sending build context to Docker daemon  60.15MB
Step 1/6 : FROM python:3
3: Pulling from library/python
05d1a5232b46: Already exists 
5cee356eda6b: Already exists 
89d3385f0fd3: Already exists 
80ae6b477848: Already exists 
28bdf9e584cc: Already exists 
523b203f62bd: Pull complete 
e423ae9d5ac7: Pull complete 
adc78e8180f7: Pull complete 
60c9f1f1e6c6: Pull complete 
Digest: sha256:5caeb1a2119661f053e9d9931c1e745d9b738e2f585ba16d88bc3ffcf4ad727b
Status: Downloaded newer image for python:3
 ---> 7a35f2e8feff
Step 2/6 : WORKDIR /bryne
 ---> Running in 9ee8283c6cc6
Removing intermediate container 9ee8283c6cc6
 ---> 5bbd14170c84
Step 3/6 : ADD . /bryne
 ---> 0128101457f5
Step 4/6 : RUN pip install --trusted-host pypi.python.org -r requirements.txt
 ---> Running in 55ab661b1b55
Collecting Django>=2.1 (from -r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/32/ab/22530cc1b2114e6067eece94a333d6c749fa1c56a009f0721e51c181ea53/Django-2.1.2-py3-none-any.whl (7.3MB)
Collecting pytz (from Django>=2.1->-r requirements.txt (line 1))
  Downloading https://files.pythonhosted.org/packages/30/4e/27c34b62430286c6d59177a0842ed90dc789ce5d1ed740887653b898779a/pytz-2018.5-py2.py3-none-any.whl (510kB)
Installing collected packages: pytz, Django
Successfully installed Django-2.1.2 pytz-2018.5
Removing intermediate container 55ab661b1b55
 ---> dce5400552b2
Step 5/6 : EXPOSE 8000
 ---> Running in c74603a76b54
Removing intermediate container c74603a76b54
 ---> ee5ef2bf2999
Step 6/6 : CMD ["python", "manage.py", "runserver", "127.0.0.1:8000"]
 ---> Running in 4f5ea428f801
Removing intermediate container 4f5ea428f801
 ---> 368f73366b69
Successfully built 368f73366b69
Successfully tagged swyne-latest:latest

$ docker run swyne-latest (no output)

ritratt
  • 1,703
  • 4
  • 25
  • 45
  • Is there any output from running just the Dockerfile? I'm not super familiar with Django, maybe the webserver fails because you're not running migrations in the Dockerfile, but you are in the docker-compose definition? – Dylan Pierce Oct 12 '18 at 21:10
  • output added... – ritratt Oct 12 '18 at 21:29
  • 1
    EXPOSE in Dockerfile does not really map ports between host and container. It’s just a notice for people who run this image. You must use -p to achieve the port mapping. – Light.G Oct 13 '18 at 09:05

1 Answers1

0

I guess it's normal that unlike docker-compose up, docker run swyne-latest does not allow you to access the web application at 127.0.0.1:8000.

Because the docker-compose.yml file (which is read by docker-compose but not by docker itself) specifies many parameters, in particular the port mapping, which should otherwise be passed as CLI parameters of docker run.

Could you try running docker run -p 8000:8000 instead?

Also, I guess that the line command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000" should probably put inside the Dockerfile itself with a CMD or ENTRYPOINT directive, not in the docker-compose.yml file.

Actually, I've just taken a look at the output of your docker build command and there is an orthogonal issue:

the command

CMD ["python", "manage.py", "runserver", "127.0.0.1:8000"]

should be replaced with

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

(See this SO answer for more feedback on this issue, albeit in another language, Java instead of Python.)

As an aside, the complete command to compile the Dockerfile is not docker build -t swyne-latest but docker build -t swyne-latest . (with the final dot corresponding to the folder of the Docker build context).

ErikMD
  • 13,377
  • 3
  • 35
  • 71