How to stop running node in docker

后端 未结 8 1987
广开言路
广开言路 2021-02-13 18:02

I have just installed dockers and installed node. I am able to run a basic express site. My issue now is I can\'t stop it. Control-C is not doing anything.

Temporarily w

相关标签:
8条回答
  • 2021-02-13 18:23

    As a part of solution, you can open your package.js and add 3 new commands/scripts :

    "scripts": {    
    
    "docker-build-and-run": "docker build -t image-dev-local . && docker run -p 3001:3001 --name container-dev-local image-dev-local",
    "docker-stop-and-clear": "(docker stop container-dev-local || true) && (docker rm container-dev-local || true)",
    "docker-run": "npm run docker-stop-and-clear && npm run docker-build-and-run"
    
    }
    

    and just simply run in the terminal :

    npm run docker-run
    

    to up your app on 3001 port in docker and have fun. Every next run will clear previous and build/run again.

    To stop and delete it, just run :

    npm run docker-stop-and-clear
    
    0 讨论(0)
  • 2021-02-13 18:29

    A docker run should have gave you back the prompt, avoiding the need for CTRL+C, or closing the docker terminal.

    Once you log back in that terminal, a docker ps -a + docker stop should be enough to make your container exit (you still need to remove it before trying to launch it again)

    0 讨论(0)
  • 2021-02-13 18:30

    As described here: github: docker-node best practice

    You can add the --init flag to your docker run command.

    docker run -it --init -p 3000:3000 --name nodetest mynodeimage
    
    0 讨论(0)
  • 2021-02-13 18:32

    I came across this same problem today, and struggled to find an explanation/solution. I discovered (through trial and error) that this only occurs when the CMD in the Dockerfile is set to:

    CMD [ "node", "server.js" ]
    

    However, Ctrl+C works fine when the CMD is changed to:

    CMD [ "npm", "start" ]
    

    The npm start script in my package.json file is set to node server.js, so I have no idea why this change works, but hopefully this helps.

    0 讨论(0)
  • 2021-02-13 18:33

    From what I can gather you need both -t and -i for Ctrl-C to work as expected. Command like this would be helpful i believe.

    Simple example which i can think of this below Case 1 to retain container:

    $ ID=$(sudo docker run -t -d ubuntu /usr/bin/top -b)
    $ sudo docker attach $ID
    Control-C
    $ sudo docker ps
    

    Case 2 to terminate the container:

    $ ID=$(sudo docker run -t -i -d ubuntu /usr/bin/top -b)
    $ sudo docker attach $ID
    Control-C
    $ sudo docker ps
    
    0 讨论(0)
  • 2021-02-13 18:44

    If you just want to stop node without stopping the container, you could go inside the container and run:

    $ ps aux | grep node #to obtain process ID (value in second column)
    $ kill <process ID>
    
    0 讨论(0)
提交回复
热议问题