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

Time:01-04

I am trying to run Docker-compose file that runs jupyter notebooks, and I want it to execute a command to export the current notebooks as html (for visual reference) every time I run it. But the container doesn't continue running. How do I fix that?

My docker-compose file:

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""

My Dockerfile:

FROM python:3.7-slim-stretch

# Setup and installations 

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

CodePudding user response:

You are overriding the command that would normally be executed in your container with the jupyter nbconvert command. Since this is a one-off command the behaviour you see is expected.

A simple solution would be to modify the CMD of your container to include the jupyter nbconvert, something like this:

FROM you_image

#
# YOUR DOCKERFILE LINES
#

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

CodePudding user response:

The nbconvert command is a one-off command, just set up the container for it's main purpose, to run jupyter, and use the nbconvert when you need it, as it's not needed for the container to run.

Maybe set up an alias or a Makefile to avoid typing the command, in other way you need to restart the container to re-export those html files, which is not worth it at all.

CodePudding user response:

Thank you all, your advice is appreciated.

The easiest ways I found is to run the command in a separate container and assign the same image name so it doesn't build again:

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"

Otherwise I could run this command on my notebooks as:

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

Which would allow me to keep working without stopping the container.

  • Related