[英]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.26
当docker-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 以在远程服务器上运行?
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
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
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"]
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 之外的一些建议。
docker run
commands.如果您在开发中使用 docker-compose,请考虑在您的托管环境中也使用它,而不是运行原始docker run
命令。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.