docker-在 Django 的欢迎页面上编写错误

docker-compose error on the Django's welcome page

访问http://192.168.99.100:8000/地址时,在浏览器中看不到django的欢迎页面

gmondesi@MRT-3-011 MINGW64 ~/Documents/GitHub/docker-django-tutorial (master)
$ docker-machine ls
NAME      ACTIVE   DRIVER       STATE     URL                         SWARM   DO
CKER        ERRORS
default   *        virtualbox   Running   tcp://192.168.99.100:2376           v1
7.04.0-ce

gmondesi@MRT-3-011 MINGW64 ~/Documents/GitHub/docker-django-tutorial (master)
$ docker-machine ip
192.168.99.100

gmondesi@MRT-3-011 MINGW64 ~/Documents/GitHub/docker-django-tutorial (master)
$ docker-compose run web
time="2017-04-09T18:03:29-04:00" level=info msg="Unable to use system certificat
e pool: crypto/x509: system root pool is not available on Windows"
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you
 apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

April 09, 2017 - 22:03:31
Django version 1.11, using settings 'composeexample.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

是否有其他方法(或本地主机 link)可以访问我的 django 的欢迎页面?

Docker 文件

 FROM python:2.7
 ENV PYTHONUNBUFFERED 1
 RUN mkdir /code
 WORKDIR /code
 ADD requirements.txt /code/
 RUN pip install -r requirements.txt
 ADD . /code/

docker-compose.yml

 version: '2'
 services:
   db:
     image: postgres
   web:
     build: .
     command: python manage.py runserver 0.0.0.0:8000
     volumes:
       - .:/code
     ports:
       - "8000:8000"
     depends_on:
       - db

docker 撰写

gmondesi@MRT-3-011 MINGW64 ~/Documents/GitHub/docker-django-tutorial (master)
$ docker-compose up
dockerdjangotutorial_db_1 is up-to-date
Creating dockerdjangotutorial_web_1
Attaching to dockerdjangotutorial_db_1, dockerdjangotutorial_web_1
db_1   | The files belonging to this database system will be owned by user "post
gres".
db_1   | This user must also own the server process.
db_1   |
db_1   | The database cluster will be initialized with locale "en_US.utf8".
db_1   | The default database encoding has accordingly been set to "UTF8".
db_1   | The default text search configuration will be set to "english".
db_1   |
db_1   | Data page checksums are disabled.
db_1   |
db_1   | fixing permissions on existing directory /var/lib/postgresql/data ... o
k
db_1   | creating subdirectories ... ok
db_1   | selecting default max_connections ... 100
db_1   | selecting default shared_buffers ... 128MB
db_1   | selecting dynamic shared memory implementation ... posix
db_1   | creating configuration files ... ok
db_1   | running bootstrap script ... ok
db_1   | performing post-bootstrap initialization ... ok
db_1   |
db_1   | WARNING: enabling "trust" authentication for local connections
db_1   | You can change this by editing pg_hba.conf or using the option -A, or
db_1   | --auth-local and --auth-host, the next time you run initdb.
db_1   | syncing data to disk ... ok
db_1   |
db_1   | Success. You can now start the database server using:
db_1   |
db_1   |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1   |
db_1   | ****************************************************
db_1   | WARNING: No password has been set for the database.
db_1   |          This will allow anyone with access to the
db_1   |          Postgres port to access your database. In
db_1   |          Docker's default configuration, this is
db_1   |          effectively any other container on the same
db_1   |          system.
db_1   |
db_1   |          Use "-e POSTGRES_PASSWORD=password" to set
db_1   |          it in "docker run".
db_1   | ****************************************************
db_1   | waiting for server to start....LOG:  could not bind IPv6 socket: Cannot
 assign requested address
db_1   | HINT:  Is another postmaster already running on port 5432? If not, wait
 a few seconds and retry.
db_1   | LOG:  database system was shut down at 2017-04-09 21:55:03 UTC
db_1   | LOG:  MultiXact member wraparound protections are now enabled
db_1   | LOG:  database system is ready to accept connections
db_1   | LOG:  autovacuum launcher started
db_1   |  done
db_1   | server started
db_1   | ALTER ROLE
db_1   |
db_1   |
db_1   | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb
.d/*
db_1   |
db_1   | LOG:  received fast shutdown request
db_1   | LOG:  aborting any active transactions
db_1   | LOG:  autovacuum launcher shutting down
db_1   | LOG:  shutting down
db_1   | waiting for server to shut down....LOG:  database system is shut down
db_1   |  done
db_1   | server stopped
db_1   |
db_1   | PostgreSQL init process complete; ready for start up.
db_1   |
db_1   | LOG:  database system was shut down at 2017-04-09 21:55:04 UTC
db_1   | LOG:  MultiXact member wraparound protections are now enabled
db_1   | LOG:  database system is ready to accept connections
db_1   | LOG:  autovacuum launcher started
web_1  | Performing system checks...
web_1  |
web_1  | System check identified no issues (0 silenced).

我刚打开Kitematic软件

gmondesi@MRT-3-011 MINGW64 ~/Documents/GitHub/docker-django-tutorial (master)
$ docker --version
time="2017-04-09T20:00:54-04:00" level=info msg="Unable to use system certificat
e pool: crypto/x509: system root pool is not available on Windows"
Docker version 17.03.1-ce, build c6d412e

gmondesi@MRT-3-011 MINGW64 ~/Documents/GitHub/docker-django-tutorial (master)
$ docker-compose --version
docker-compose version 1.11.1, build 7afaa436

gmondesi@MRT-3-011 MINGW64 ~/Documents/GitHub/docker-django-tutorial (master)
$ docker-machine --version
docker-machine.exe version 0.10.0, build 76ed2a6
docker-

运行-重新组合

gmondesi@MRT-3-011 MINGW64 ~/Documents/GitHub/docker-django-tutorial (master)
$ docker-compose up
Starting dockerdjangotutorial_db_1
Starting dockerdjangotutorial_web_1
Attaching to dockerdjangotutorial_db_1, dockerdjangotutorial_web_1
db_1   | LOG:  database system was interrupted; last known up at 2017-04-09 22:0
0:10 UTC
db_1   | LOG:  database system was not properly shut down; automatic recovery in
 progress
db_1   | LOG:  invalid record length at 0/150F2C0: wanted 24, got 0
db_1   | LOG:  redo is not required
db_1   | LOG:  MultiXact member wraparound protections are now enabled
db_1   | LOG:  database system is ready to accept connections
web_1  | Performing system checks...
web_1  |
web_1  | System check identified no issues (0 silenced).
web_1  |
web_1  | You have 13 unapplied migration(s). Your project may not work properly
until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
web_1  | Run 'python manage.py migrate' to apply them.
web_1  | April 09, 2017 - 23:58:45
web_1  | Django version 1.11, using settings 'composeexample.settings'
web_1  | Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.
web_1  | Invalid HTTP_HOST header: '192.168.99.100:8000'. You may need to add u'
192.168.99.100' to ALLOWED_HOSTS.
web_1  | [10/Apr/2017 00:00:03] "GET / HTTP/1.1" 400 57374
web_1  | Invalid HTTP_HOST header: '192.168.99.100:8000'. You may need to add u'
192.168.99.100' to ALLOWED_HOSTS.
web_1  | [10/Apr/2017 00:00:24] "GET / HTTP/1.1" 400 57486
web_1  | Invalid HTTP_HOST header: '192.168.99.100:8000'. You may need to add u'
192.168.99.100' to ALLOWED_HOSTS.
web_1  | [10/Apr/2017 00:00:25] "GET /favicon.ico HTTP/1.1" 400 57409
web_1  | Invalid HTTP_HOST header: '192.168.99.100:8000'. You may need to add u'
192.168.99.100' to ALLOWED_HOSTS.
web_1  | [10/Apr/2017 00:01:51] "GET / HTTP/1.1" 400 57486
web_1  | Invalid HTTP_HOST header: '192.168.99.100:8000'. You may need to add u'
192.168.99.100' to ALLOWED_HOSTS.
web_1  | [10/Apr/2017 00:01:51] "GET /favicon.ico HTTP/1.1" 400 57670
web_1  | Invalid HTTP_HOST header: '192.168.99.100:8000'. You may need to add u'
192.168.99.100' to ALLOWED_HOSTS.
web_1  | [10/Apr/2017 00:02:29] "GET / HTTP/1.1" 400 57159
web_1  | Invalid HTTP_HOST header: '192.168.99.100:8000'. You may need to add u'
192.168.99.100' to ALLOWED_HOSTS.
web_1  | [10/Apr/2017 00:02:30] "GET /favicon.ico HTTP/1.1" 400 57280
web_1  | Invalid HTTP_HOST header: '192.168.99.100:8000'. You may need to add u'
192.168.99.100' to ALLOWED_HOSTS.
web_1  | [10/Apr/2017 00:02:30] "GET /favicon.ico HTTP/1.1" 400 57280
web_1  | Invalid HTTP_HOST header: '192.168.99.100:8000'. You may need to add u'
192.168.99.100' to ALLOWED_HOSTS.
web_1  | [10/Apr/2017 00:02:37] "GET / HTTP/1.1" 400 57374
web_1  | Performing system checks...
web_1  |
web_1  | System check identified no issues (0 silenced).
web_1  |
web_1  | You have 13 unapplied migration(s). Your project may not work properly
until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
web_1  | Run 'python manage.py migrate' to apply them.
web_1  | April 10, 2017 - 00:03:08
web_1  | Django version 1.11, using settings 'composeexample.settings'
web_1  | Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.
web_1  | Invalid HTTP_HOST header: '192.168.99.100:8000'. You may need to add u'
192.168.99.100' to ALLOWED_HOSTS.
web_1  | [10/Apr/2017 00:03:10] "GET / HTTP/1.1" 400 57292
web_1  | Performing system checks...
web_1  |
web_1  | System check identified no issues (0 silenced).
web_1  |
web_1  | You have 13 unapplied migration(s). Your project may not work properly
until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
web_1  | Run 'python manage.py migrate' to apply them.
web_1  | April 10, 2017 - 00:03:17
web_1  | Django version 1.11, using settings 'composeexample.settings'
web_1  | Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.
Gracefully stopping... (press Ctrl+C again to force)
Stopping dockerdjangotutorial_web_1 ... done
Stopping dockerdjangotutorial_db_1 ... done

Dockerfile 写这个尝试:

FROM python:2.7
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
RUN python manage.py migrate
RUN python manage.py runserver

del command: python manage.py runserver 0.0.0.0:8000

尝试更改端口。

这是我的看法,希望对你有所帮助

不清楚具体是什么问题,但要访问 Docker 中的已发布端口,您需要做一些事情:

  1. 端口本身需要推送到您的 docker 主机上。要将主机上的端口 8888 映射到容器内的端口 8000,通常使用 docker run -p 8888:8000 ... 或在服务下的 docker-compose.yml 部分使用:

ports: - 8888:8000

  1. 容器应用程序本身需要监听容器端口(本例中为 8000)上的所有接口(0.0.0.0)。

  2. 您需要在发布的端口上连接到 docker 主机。使用 docker-machine 时,IP 将是 VM 的 IP。您可以使用 docker-machine ip 或在任何环境中查找 docker 主机的 IP,echo $DOCKER_HOST 会告诉您 docker 命令发送到哪里。

您已经完成了第 1 部分和第 3 部分。容器本身正在监听所有接口,但是从粘贴的各种命令来看,我怀疑服务本身并没有完全启动(可能是在寻找数据库,因为您在某一点做了 docker-compose run web,而不是 docker-compose up),因为有一个输出部分没有服务 运行 行。也许您在检查网页之前使用 cont+C 关闭了服务。