Docker-编写运行命令并保留容器运行端口

Docker-compose run command and keep the container running with ports

我正在尝试 运行 Docker 编写 运行s jupyter notebooks 的文件,我希望它执行命令将当前笔记本导出为 html(供视觉参考)每次我运行它。但是容器不会继续 运行ning。我该如何解决?

我的docker-撰写文件:

version: "3"
services:
  jupy:
    build: .
    volumes:
       - irrelevant:/app/
    ports:
     - "8888:8888"

    #This command executes and exists
    #I want it to run and then I continue working
    command: bash -c "jupyter nbconvert Untitled.ipynb --template toc2 --output "Untitled_toc2.html""

我的Docker文件:

FROM python:3.7-slim-stretch

# Setup and installations 

CMD ["jupyter", "notebook", "--port=8888", "--no-browser", "--ip=0.0.0.0", "--allow-root"]

您正在使用 jupyter nbconvert 命令覆盖通常在您的容器中执行的命令。由于这是一次性命令,因此您看到的行为是预期的。

一个简单的解决方案是修改容器的 CMD 以包含 jupyter nbconvert,如下所示:

FROM you_image

#
# YOUR DOCKERFILE LINES
#

CMD YOUR_CURRENT_CMD && jupyter nbconvert Untitled.ipynb --template toc2 --output Untitled_toc2.html

nbconvert 命令是一次性命令,只需为其主要目的设置容器,运行jupyter,并在需要时使用 nbconvert,因为容器不需要它运行.

也许可以设置别名或 Makefile 来避免键入命令,否则您需要重新启动容器才能重新导出那些 html 文件,这根本不值得。

谢谢大家,感谢您的建议。

我发现的最简单的方法是 运行 在单独的容器中执行命令并分配相同的图像名称,这样它就不会再次构建:

version: "3"
services:
  jupy_export:
    image: note-im
    build: .
    volumes:
       - irrelevant:/app/
    command: jupyter nbconvert Untitled.ipynb --template toc2 --output "Untitled.html"
    
  jupy:
    image: note-im
    build: .
    volumes:
       - irrelevant:/app/
    ports:
     - "8888:8888"

否则我可以 运行 我的笔记本上的这个命令为:

!jupyter nbconvert Untitled.ipynb --template toc2 --output "Untitled.html"

这将允许我在不停止容器的情况下继续工作。