杀死一个Docker容器

时间:2018-06-25 02:29:07

标签: shell docker pm2

我已经为我的节点应用程序设置了一个Docker容器并使用

运行它
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, 
NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
import random
import sys
import time
import tkinter as tk
from tkinter import ttk
import matplotlib.pyplot as plt #import matplotlib library
from drawnow import *
import urllib
import json
import serial # import Serial Library
import numpy  # Import numpy
import pandas as pd
import numpy as np


LARGE_FONT= ("Verdana", 12)
style.use("ggplot") #ggplot...dark_background

do = []
tempF= []

f = Figure(figsize=(10,6), dpi=100)
a = f.add_subplot(111)

arduinoData = serial.Serial('com3', 115200) #Creating our serial object 



def animate(i):

  if(arduinoData.inWaiting()>0):

#read serial data
arduinoString = arduinoData.readline()
xList = []
yList = []



#Parse serial data
arduinoString.split()
['', '', '', '', '', '', '', '']
words = arduinoString.split()

reading = words[3]

if words[1] == (b'TEMP') :
    print (words[0])
    print (words[1])
    print (words[3])
    tempF.append(reading)                  #Build our tempF array by appending temp readings

a.clear()
a.plot(*yList, *yList)   


title = "    D.O : "+str(do) + "\n Temp : " + str(tempF)
a.set_title(title)




arduinoData.flushInput()
arduinoData.flushOutput()




class Application(tk.Tk):

def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)
    tk.Tk.wm_title(self, "Stylibleue Dashboard")


    # the container is where we'll stack a bunch of frames on top each other
    container = tk.Frame(self)
    container.pack(side="top", fill="both", expand = True)
    container.grid_rowconfigure(0, weight=1)
    container.grid_columnconfigure(0, weight=1)

    #Switch through pages
    self.frames = {}

    for F in (StartPage, Page1,):

        frame = F(container, self)

        self.frames[F] = frame

        frame.grid(row=0, column=0, sticky="nsew")

    self.show_frame(StartPage)

def show_frame(self, cont):

    frame = self.frames[cont]
    frame.tkraise()



class StartPage(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)

    #Page Labels
    label = tk.Label(self, text=("""     D.O : 
    """), font=LARGE_FONT)
    label.grid(row=100, column=20, sticky="nsew")

    label = tk.Label(self, text=("""<Sensor reading here>
    """), font=LARGE_FONT)
    label.grid(row=100, column=30, sticky="nsew")

    label = tk.Label(self, text=(""" TEMP : 
    """), font=LARGE_FONT)
    label.grid(row=100, column=40, sticky="nsew")

    label = tk.Label(self, text=("""<Sensor reading here>        
    """), font=LARGE_FONT)
    label.grid(row=100, column=50, sticky="nsew") 


    #Go to Page1 button
    button1 = ttk.Button(self, text="Page1",
                        command=lambda: controller.show_frame(Page1))
    button1.grid(row=100, column=60, sticky="nsew")



class Page1(tk.Frame):

def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    label = tk.Label(self, text="Bassin 2!!!", font=LARGE_FONT)
    label.pack(pady=10,padx=10)

    #Return home button
    button1 = ttk.Button(self, text="Back to Home",
                        command=lambda: controller.show_frame(StartPage))
    button1.pack()


    #This is the embedded matplotlib graph
    canvas = FigureCanvasTkAgg(f, self)
    canvas.draw()
    canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

    toolbar = NavigationToolbar2Tk(canvas, self)
    toolbar.update()
    canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)




app = Application()
ani = animation.FuncAnimation(f, animate, interval=1000)
app.mainloop()

它以无守护进程模式启动了pm2。 CTRL + C并退出不起作用。我试过docker在另一个终端窗口中停止my_node_app,但无济于事。 感谢任何帮助。

4 个答案:

答案 0 :(得分:3)

您将可以使用以下命令查看当前正在运行的Docker容器。

docker ps

然后复制正在运行的容器的容器ID,并执行以下命令

docker stop <container_id>

请替换为真实值。

答案 1 :(得分:1)

如果尚不知道,请执行docker container ls查找容器名称,然后docker kill container_name

来源:Docker documentation

答案 2 :(得分:0)

您可以尝试这个非常简单的脚本

docker container kill $(docker ps | grep "image_name" | awk '{print $1}')

解释

以表格结构列出容器

docker ps

结果

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
763e1a00808e        7bb2586065cd        "docker-entrypoint.s…"   2 months ago        Up 2 weeks          3306/tcp, 33060/tcp   mysql_1
763e1a00999b        7bb2586065cd        "docker-entrypoint.s…"   1 months ago        Up 2 weeks          3307/tcp, 33061/tcp   mysql_2

隔离容器

grep "mysql_1"

结果

STATUS              PORTS                 NAMES
763e1a00808e        7bb2586065cd        "docker-entrypoint.s…"   2 months ago        Up 2 weeks          3306/tcp, 33060/tcp   mysql_1

隔离第一个制表符值,即容器ID     awk'{print $ 1}'

结果

763e1a00808e

因此,总而言之,这将隔离并发送kill或stop命令与图像名称匹配的容器ID

答案 3 :(得分:0)

  1. “Docker 容器 ls” 查看现有容器
  2. “Docker container kill container_id”强制终止或“docker container stop container_id”正常终止