在ENTRYPOINT之后退出docker交互模式

时间:2016-10-05 01:28:04

标签: docker

给出以下Dockerfile:

From python:2.7-wheezy
RUN apt-get update && apt-get upgrade --assume-yes
RUN apt-get install mysql-server --assume-yes
ENTRYPOINT service mysql start

当我运行docker时,它会在启动mysql服务器后立即退出:

Jamess-iMac:docker-python-test jameslin$ docker run -i -t 9618f71f65e4 /bin/bash
[ ok ] Starting MySQL database server: mysqld ..
[info] Checking for tables which need an upgrade, are corrupt or were 
not closed cleanly..
Jamess-iMac:docker-python-test jameslin$ 

如何让它自动启动mysql但保持交互模式?

2 个答案:

答案 0 :(得分:2)

ubuntu 12.04 into docker “service mysql start”

如果你阅读上面的帖子,你可以看到Docker没有任何运行级别,所以mysql不知道它什么时候开始。

您可以运行两个容器并在它们之间创建一个nework。一个用于 mysql ,另一个用于 pythonapp 。以下是如何在两个容器之间创建网络

docker network create <network_name>

使用--net=<network_name>

启动将容器附加到新网络的容器
docker run -d --net=anetwork --name=mysql -e MYSQL_USER=ROOT -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql

docker run --net=anetwork --name=pythonapp -it python:2.7-wheezy bash

enter image description here

我认为您在ENTRYPOINTCMD之间感到困惑。要理解的关键点是,即使向 docker run 调用提供了命令,在映像启动时也始终会运行ENTRYPOINT。如果您尝试提供命令,它会将其作为参数添加到ENTRYPOINT,替换CMD指令中定义的默认值。如果您明确将ENTRYPOINT标记传递给 docker run 命令,则只能覆盖--entrypoint

这意味着使用/bin/bash命令运行图像不会给你一个shell;相反,它会提供/bin/bash作为 service mysql start 的参数。

Network between container ubuntu 12.04 into docker “service mysql start”

Difference Between CMD and ENTRYPOINT in Dockerfile

答案 1 :(得分:1)

您的 { { connect.Open(); OleDbCommand command = new OleDbCommand(); command.Connection = connect; command.CommandText = "select * from Table1 where Username ='" + tbName.Text + "'"; OleDbDataReader reader = command.ExecuteReader(); int count = 0; while (reader.Read()) { count = count + 1; //count++; } if (count == 0) { if (tbName.Text != "Name" && tbPass.Text != "Password") { if (tbEmail.Text != "Email" && tbMobile.Text != "Number") { if (tbFirstName.Text != "" && tbLastName.Text != "") { const int MIN_LENGTH = 8; string password = tbPass.Text; if (password.Length >= MIN_LENGTH && upperCase(password) >= 1) { r2.Text = ""; r2.ForeColor = Color.Red; } else { r2.Text = "*Password Is Bad*"; r2.ForeColor = Color.Red; } if (RegularExpression.checkForEmail(tbEmail.Text.ToString())) { r3.Text = ""; } else { r3.Text = "Invalid email ! Email Contains a @ , .Com "; r3.ForeColor = Color.Red; } if (r2.Text == "" && r3.Text == "") { goto na; } else { goto ne; } } else { goto ne; } } else { goto ne; } } else { goto ne; ; } ne: if (tbName.Text == "Username") { r1.Text = "*USERNAME REQUIRED*"; r1.ForeColor = Color.Red; } if (tbPass.Text == "Password") { r2.Text = "*PASSWORD REQUIRED*"; r2.ForeColor = Color.Red; } if (tbEmail.Text == "Email") { r3.Text = "*EMAIL REQUIRED*"; r3.ForeColor = Color.Red; } if (tbMobile.Text == "Number") { r4.Text = "*MOBILE NUMBER REQUIRED*"; r4.ForeColor = Color.Red; } if (tbFirstName.Text == "") { label3.Text = "*FIRST NAME REQUIRED*"; r4.ForeColor = Color.Red; } else { label3.Text = ""; } if (tbLastName.Text == "") { label4.Text = "*LAST NAME REQUIRED*"; r4.ForeColor = Color.Red; } else { label4.Text = ""; } MessageBox.Show("Please fill up all the required information correctly before proceeding"); return; na: try { connect.Open(); OleDbCommand command1 = new OleDbCommand(); command1.Connection = connect; command1.CommandText = "insert into Table1([Username], [Password], [Email], [Number], [FirstName], [LastName]) values('" + tbName.Text + "','" + tbPass.Text + "','" + tbEmail.Text + "','" + tbMobile.Text + "','" + tbFirstName.Text + "','" + tbLastName.Text + "')"; command1.ExecuteNonQuery(); MessageBox.Show("Data Saved"); MessageBox.Show("Successfully registered, Please log in"); regPage log = new regPage(); this.Hide(); log.ShowDialog(); this.Close(); connect.Close(); } catch (Exception ex) { // MessageBox.Show("Error " + ex); connect.Close(); } } // if (count > 1) // { // MessageBox.Show("Duplicate username and password"); // } else { // MessageBox.Show("Username and password is incorrect"); MessageBox.Show("Duplicate Name , Please Use Other Username"); regPage log = new regPage(); this.Hide(); log.ShowDialog(); this.Close(); connect.Close(); } connect.Close(); } } / ENTRYPOINT指令需要是一个长时间运行的命令。 CMD不是连续命令,因为它在Ubuntu中运行,服务本身然后命令退出。

为了简单起见 - 如果您只是尝试运行mysql容器,则可以运行service mysql start。如果你绝对需要运行一个一次性的mysql容器,你可以捎带默认的mysql容器从这里开始的方式:MySql Dockerfile - docker run mysql - 这应该与你看到的相似CMD ["mysqld"]

中的实际启动命令