为什么我不能使用Dockerfile访问容器,但是可以使用docker-compose访问容器?

时间:2018-10-12 21:07:02

标签: docker docker-compose dockerfile

我正在尝试学习Docker。我有一个Hello World Django服务器应用程序。当我尝试使用import requests data = {"j_username": "admin","j_password":"password"} r = requests.post('http://(serverip):8080/pentaho/j_spring_security_check', data = data) 运行服务器时,服务器无法访问。但是当我使用Dockerfile时,我可以访问它。

我的问题是为什么,特别是当它们非常相似时。

我的Dockerfile:

docker-compose

使用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! docker build -t swyne-latest

结果:无法访问docker run swyne-latest上的服务器

我的127.0.0.1:8000

docker-compose.yml

使用docker-compose运行服务器时使用的命令: 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"

结果:能够通过docker-compose up访问我的服务器

谢谢

编辑:Dockerfile构建的输出:

127.0.0.1:8000

$ 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

1 个答案:

答案 0 :(得分:0)

我认为与docker-compose up不同的是,docker run swyne-latest不允许您访问127.0.0.1:8000上的Web应用程序。

因为docker-compose.yml文件(由docker-compose本身而非docker读取)指定了许多参数,尤其是端口映射,否则应将其作为CLI参数传递。 docker run

您是否可以尝试运行docker run -p 8000:8000

此外,我猜想command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000"行应该使用DockerfileCMD伪指令放置在ENTRYPOINT本身内, docker-compose.yml文件。

实际上,我只是看了您的docker build命令的输出,并且存在一个正交的问题:

命令

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

应替换为

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

(有关此问题的更多反馈,请参见此SO answer,尽管使用的是另一种语言,Java而不是Python。)

顺便说一句,编译Dockerfile的完整命令不是docker build -t swyne-latest而是docker build -t swyne-latest .(最后一个点对应于Docker构建上下文的文件夹)。