![](/img/trans.png)
[英]executing commands in containers from within a container using docker-compose up vs docker-compose run
[英]How can I run a Docker container on a remote server in the same manner as docker-compose up?
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
工作正常時。
我有一個已推送到 DockerHub 的 docker 容器。 我可以使用 docker-compose 在本地運行它。 我在遠程服務器上將其拉下來,現在我正在嘗試使用docker run username/srdc_prod:2020.07.26
。
當我這樣做時,我收到錯誤/docker-entrypoint.sh: line 10: exec: nginx: cannot execute: Is a directory
。 執行docker-compose up
在本地工作得很好。 我想將此圖像推送到 Docker 集線器並將其拉下並在我的遠程服務器上運行。 但是,我不能使用docker-compose up
因為docker-compose.yaml
文件不存在遠程。 我意識到我可以嘗試遠程克隆我的 GitHub 存儲庫,然后運行docker-compose up
但這違背了將容器推送到 DockerHub 並最終設置 CI/CD 的目的。
有沒有辦法在容器上運行 docker-compose ? 或者是否可以看到所有執行的命令? 我在這個問題中看到我可以使用docker ps --no-trunc
來查看為運行 docker 容器執行的所有命令,但所有這些都依賴於我似乎無法遠程執行的docker-entrypoint.sh
文件。 我做這一切都錯了嗎,我需要重構我的 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:
您看到的錯誤是准確的: nginx
是一個目錄。 根據您的docker-compose.yml
清單,您的根目錄中有一個nginx
文件夾,您可以將其用作nginx
服務的構建上下文。
nginx:
build: ./nginx # Here's the evidence for the nginx folder.
ports:
構建django_web
映像時,將整個上下文目錄復制到/app
中,其中包括nginx
目錄。
COPY . /app
# set working directory
WORKDIR /app
您的username/srdc_prod
CMD
的 CMD 是nginx -g daemon off;
,你的docker-entrypoint.sh
執行。 這失敗了,因為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.
問題的 scope 之外的一些建議。
docker run
命令。depends_on
)並稍微擴展服務定義。 使用 swarm 模式——或其他一些編排工具——將使您在未來更容易擴展您的服務,此外您還可以獲得一些其他方便的功能,如秘密、重啟策略、網絡隔離等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.