简体   繁体   English

如何以与 docker-compose 相同的方式在远程服务器上运行 Docker 容器?

[英]How can I run a Docker container on a remote server in the same manner as docker-compose up?

TL;DR: Getting error /docker-entrypoint.sh: line 10: exec: nginx: cannot execute: Is a directory running docker run username/srdc_prod:2020.07.26 when docker-compose up works just fine. TL;DR:出现错误/docker-entrypoint.sh: line 10: exec: nginx: cannot execute: Is a directory运行docker run username/srdc_prod:2020.07.26docker-compose up工作正常时。

I have a docker container that I have pushed to DockerHub.我有一个已推送到 DockerHub 的 docker 容器。 I am able to run this locally with docker-compose up.我可以使用 docker-compose 在本地运行它。 I pulled this down on my remote server and now I am trying to use docker run username/srdc_prod:2020.07.26 .我在远程服务器上将其拉下来,现在我正在尝试使用docker run username/srdc_prod:2020.07.26

When I do this I get the error /docker-entrypoint.sh: line 10: exec: nginx: cannot execute: Is a directory .当我这样做时,我收到错误/docker-entrypoint.sh: line 10: exec: nginx: cannot execute: Is a directory Executing docker-compose up works just fine locally.执行docker-compose up在本地工作得很好。 I want to push this image to Docker Hub and pull this down and run it on my remote server.我想将此图像推送到 Docker 集线器并将其拉下并在我的远程服务器上运行。 However, I cannot use docker-compose up because the docker-compose.yaml file does not exist remotely.但是,我不能使用docker-compose up因为docker-compose.yaml文件不存在远程。 I realize I could try cloning my GitHub repo remotely and then run docker-compose up but this defeats the purpose of pushing the container to DockerHub and eventually getting CI/CD set up.我意识到我可以尝试远程克隆我的 GitHub 存储库,然后运行docker-compose up但这违背了将容器推送到 DockerHub 并最终设置 CI/CD 的目的。

Is there a way to run docker-compose up on a container?有没有办法在容器上运行 docker-compose ? Or is it possible to see all of the commands executed?或者是否可以看到所有执行的命令? I saw in this question that I can use docker ps --no-trunc to see all of the commands executed for running docker containers, but all of these rely on the docker-entrypoint.sh file with which I cannot seem to execute remotely.我在这个问题中看到我可以使用docker ps --no-trunc来查看为运行 docker 容器执行的所有命令,但所有这些都依赖于我似乎无法远程执行的docker-entrypoint.sh文件。 Am I doing this all wrong and I need to refactor my Dockerfile to run on the remote server?我做这一切都错了吗,我需要重构我的 Dockerfile 以在远程服务器上运行?

Image on Remote Server远程服务器上的图像

username@ubuntu-512mb-name:~$ docker image ls
WARNING: Error loading config file: /home/username/.docker/config.json: stat /home/username/.docker/config.json: permission denied
REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
username/srdc_prod        2020.07.26          af62927882ee        5 days ago          504MB

docker-entrypoint.sh码头入口点.sh

  1 #!/bin/bash
  2
  3 echo "Collect static files"
  4 python manage.py collectstatic --noinput
  5
  6 echo "Apply database migrations"
  7 python manage.py migrate --noinput
  8
  9 echo "Starting daphne server"
 10 exec "$@" # offending line

Dockerfile Dockerfile

FROM python:3-alpine

# set env vars
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN mkdir /app

RUN apk update && apk add --no-cache postgresql-dev gcc libffi-dev musl-dev build-base python3-dev bash

COPY requirements.txt /app/requirements.txt
RUN pip install --upgrade pip && pip install --no-cache-dir -r /app/requirements.txt

COPY test-requirements.txt /app/test-requirements.txt
RUN pip install --upgrade pip && pip install --no-cache-dir -r /app/test-requirements.txt

COPY . /app
# set working directory
WORKDIR /app

RUN mkdir -p /var/www/srdc/static/
RUN chmod 755 /var/www/srdc/static/

EXPOSE 8000

ENV DJANGO_SETTINGS_MODULE=srdc.settings

CMD ["nginx", "-g", "daemon off;"]

ADD docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod a+x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]

docker-compose.yml docker-compose.yml

version: '3'

services:
  django_web:
    build: .
    command: bash -c "daphne -b 0.0.0.0 -p 8000 srdc.asgi:application"
    expose:
       - 8000
    image: srdc:v0
    container_name: srdc_django_web
    volumes:
      - .:/app
      - static_volume:/var/www/srdc/static/
    depends_on:
      - db
  nginx:
    build: ./nginx
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - static_volume:/var/www/srdc/static/
      - ./data/certbot/conf:/etc/letsencrypt
      - ./data/certbot/www:/var/www/certbot
    depends_on:
      - django_web
  db:
    image: postgres
    container_name: "my_postgres"
    ports:
      - "54320:5432"
    volumes:                                                                                                                                                                                     - my_dbdata:/var/lib/postgresql/data
volumes:                                                                                                                                                                                       my_dbdata:
    static_volume:                                                                                                                                                                             media_volume:

The error you're seeing is accurate: nginx is a directory.您看到的错误是准确的: nginx是一个目录。 Based on your docker-compose.yml manifset, there is an nginx folder in your root directory which you use as a build context for your nginx service.根据您的docker-compose.yml清单,您的根目录中有一个nginx文件夹,您可以将其用作nginx服务的构建上下文。

  nginx:
    build: ./nginx # Here's the evidence for the nginx folder.
    ports:

When you build the django_web image, you copy over the entire context directory into /app , and that includes the nginx directory.构建django_web映像时,将整个上下文目录复制到/app中,其中包括nginx目录。

COPY . /app
# set working directory
WORKDIR /app

The CMD for your username/srdc_prod image is nginx -g daemon off;您的username/srdc_prod CMD的 CMD 是nginx -g daemon off; , which your docker-entrypoint.sh executes. ,你的docker-entrypoint.sh执行。 That fails because nginx is a directory.这失败了,因为nginx是一个目录。

Based on your docker-compose.yml manifest, it looks like the CMD you actually want is daphne -b 0.0.0.0 -p 8000 srdc.asgi:application or something like that, but not nginx which is not installed in that alpine-based image. Based on your docker-compose.yml manifest, it looks like the CMD you actually want is daphne -b 0.0.0.0 -p 8000 srdc.asgi:application or something like that, but not nginx which is not installed in that alpine-based image.

Some recommendations outside of the scope of the question.问题的 scope 之外的一些建议。

  1. If you're using docker-compose in dev, consider using that in your hosted environments, too, instead of running the raw docker run commands.如果您在开发中使用 docker-compose,请考虑在您的托管环境中也使用它,而不是运行原始docker run命令。
  2. Better yet, use Docker in swarm mode.更好的是,在集群模式下使用 Docker。 You can re-use your manifest file that way, albeit you would need to remove some of the deprecated stuff ( depends_on , for example) and expand on the service definitions a bit.您可以通过这种方式重新使用清单文件,尽管您需要删除一些已弃用的内容(例如, depends_on )并稍微扩展服务定义。 Using swarm mode--or some other orchestration tool--will make it easier to scale your service in the future, plus you get some other handy features like secrets, restart policies, network segregation, and so on.使用 swarm 模式——或其他一些编排工具——将使您在未来更容易扩展您的服务,此外您还可以获得一些其他方便的功能,如秘密、重启策略、网络隔离等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用docker-compose up与docker-compose run从容器内部执行容器中的命令 - executing commands in containers from within a container using docker-compose up vs docker-compose run 容器不停留在docker-compose up上 - Container not staying up on docker-compose up 为什么“ docker-compose run”可以在容器外部创建文件? - Why can “docker-compose run” create files outside the container? Docker 无法打开文件以在使用 django 的容器中运行 python 应用程序,使用 ZBAEDB53E845AE71F13945FCC00572 - Docker can't open file to run the python application in a container with django using docker-compose 运行 docker-compose 时如何将参数传递给 docker 容器 - How to pass argument to docker container when running docker-compose up 使用docker-compose run时,容器无法通过启动服务访问 - Container is unreachable from up services when using docker-compose run django runserver 在 docker-compose up 中挂起,但在 docker-compose run 中正确运行 - django runserver hangs in docker-compose up but runs correctly in docker-compose run 当用docker-compose run调用但gunicorn可以工作,但不能通过docker-compose up调用 - gunicorn works when called with docker-compose run but not docker-compose up docker-compose exec导致[Errno 2]没有此类文件或目录:docker容器中的'docker-compose':'docker-compose' - docker-compose exec causes [Errno 2] No such file or directory: 'docker-compose': 'docker-compose' in docker container 从 docker-compose 运行时容器以代码 0 退出 - Container exited with code 0 when run from docker-compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM