Brian
Brian

Reputation: 13593

Must I provide a command when running a docker container?

I'd like to install mysql server on a centos:6.6 container.

However, when I run docker run --name myDB -e MYSQL_ROOT_PASSWORD=my-secret-pw -d centos:6.6, I got docker: Error response from daemon: No command specified. error.

Checking the document from docker run --help, I found that the COMMAND seems to be an optional argument when executing docker run. This is because [COMMAND] is placed inside a pair of square brackets.

$ docker run --help

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Run a command in a new container

I also find out that the official repository of mysql doesn't specify a command when starting a MySQL container:

Starting a MySQL instance is simple:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

Why should I provide a command when running a centos:6.6 container, but not so when running a mysql container?

I'm guessing that maybe centos:6.6 is specially-configured so that the user must provide a command when running it.

Upvotes: 2

Views: 1630

Answers (1)

Mike Zhao
Mike Zhao

Reputation: 490

if you use centos:6.6, you do need to provide a command when you issue "docker run" command.

The reason the officical repository of mysql does not specify a command is because it has CMD command in it's docker file: CMD ["mysqld"]. Check it's docker file here.

The CMD in docker file is the default command when run the container without a command.

You can read here to better understand what you can use in a docker file.

In your case, you can

  1. Start your centos 6.6 container
  2. Take official mysql docker file as reference, issue similar command (change apt-get to yum ( or sudo yum if you don't use the default root user)
  3. Once you can successfully start mysql, you can put all your command in your docker file, just to make sure the first line is "From centos:6.6"
  4. Build your image
  5. Run a container with your image, then you don't need to provide a command in docker run
  6. You can share your docker file in docker hub, so that other people can user yours.

good luck.

Upvotes: 3

Related Questions