为什么 docker-compose 可以工作而 docker run 不能

发布于 2025-01-16 19:54:26 字数 1084 浏览 4 评论 0原文

我有以下 local.yml 文件

version: '3'
services:
  celery-worker-default:
    image: weapp_app:latest
    container_name: weapp_celery-worker-default
    command: celery worker -A WeApp -Q default -n default --loglevel=INFO
    env_file:
      - .env
    volumes:
      - .:/opt/python/current/app/

当我运行时

docker-compose -f local.yml up celery-worker-default

一切都很好 也像

docker-compose -f local.yml run celery-worker-default celery worker -A WeApp -Q default -n default --loglevel=INFO

Everything run OK

一样运行,但是如果我尝试使用以下命令使用 docker 执行,

docker run -v "$(pwd):/opt/python/current/app/" -w /opt/python/current/app --env-file .env weapp_app:latest celery worker -A WeApp -Q default -n default --loglevel=INFO

我会收到此错误

Error: 
Unable to load celery application.
The module "WeApp was not found.

,我已经尝试更改 $(pwd) 为本地完整本地路径,但我收到相同的错误。

以其他方式,我尝试通过命令行使用选项 -e 传递所有环境值并得到相同的错误。 我尝试在 docker 选项中不使用工作目录。 我尝试在容器内打开外壳并执行命令,但出现同样的错误。

I have the following local.yml file

version: '3'
services:
  celery-worker-default:
    image: weapp_app:latest
    container_name: weapp_celery-worker-default
    command: celery worker -A WeApp -Q default -n default --loglevel=INFO
    env_file:
      - .env
    volumes:
      - .:/opt/python/current/app/

When I run

docker-compose -f local.yml up celery-worker-default

everything work great
also running like

docker-compose -f local.yml run celery-worker-default celery worker -A WeApp -Q default -n default --loglevel=INFO

Everything run OK

But If I try to execute with docker with the following command

docker run -v "$(pwd):/opt/python/current/app/" -w /opt/python/current/app --env-file .env weapp_app:latest celery worker -A WeApp -Q default -n default --loglevel=INFO

I get this error

Error: 
Unable to load celery application.
The module "WeApp was not found.

I already try change $(pwd) for the local complete local path but I get the same error.

In other way I try to pass all the environment value by command line with option -e and get the same error.
I try without working directory in the docker options.
I try opening a shell inside the container and execute the command, but the same error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉 2025-01-23 19:54:26

首先,感谢所有评论并帮助提出想法的人

在遵循塔伦的评论后,我可以看到问题,但我认为每个人都知道它是件好事。

docker-compose 与 docker 读取环境文件(在我的例子中为 .env)的方式不同。

然后,当您使用 docker-compose 时,您可以声明类似的值

VAR="VALUE"

,但对于 docker,您必须声明类似

VAR=VALUE 

而不带引号。
这使我的情况变得不同。

再次感谢所有花时间阅读我的帖子的人。

First, Thanks to everybody who commented and help with theirs ideas

After follow Tarun's comment, I could see the problem, but I think is good to everyone know about it.

The environment file (in my case .env) is read in a different way by docker-compose than docker.

Then when you use docker-compose you can declare the values like

VAR="VALUE"

But for docker you must have to declare like

VAR=VALUE 

without quotation marks.
This made all the difference in my case.

Thanks again to everyone who took a time to read my post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文