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

Giải thích những command trong terminal

  1. docker: This is the command-line tool for interacting with Docker, which is a platform used to develop, ship, and run applications in containers.
  2. run: This command tells Docker to create and start a new container.
  3. -name some-mysql: This option gives a name to the container. In this case, the container will be called "some-mysql." Naming containers makes it easier to manage them later.
  4. e MYSQL_ROOT_PASSWORD=my-secret-pw: This option sets an environment variable inside the container. Here, MYSQL_ROOT_PASSWORD is the name of the variable, and my-secret-pw is its value. This specific variable is used to set the password for the MySQL root user.
  5. d: This flag stands for "detached mode." It means that the container will run in the background, allowing you to continue using the command line without being tied to the container's output.
  6. mysql:tag: This specifies the image to use for the container. mysql is the name of the image (which contains the MySQL database software), and tag is a placeholder for a specific version of the MySQL image you want to use (like latest, 5.7, etc.).

Putting it all together, this command creates and starts a new MySQL container named "some-mysql" with a root password of "my-secret-pw," running in the background using a specified version of the MySQL image.


The -p option in a Docker command is used to publish a container's port to the host. This allows you to access the services running inside the container from outside the container, such as from your local machine or other devices on the network.

Here's how it works:

For example, if you use -p 3307:3306, it means:

So, if you include -p 3307:3306 in your command, it would look like this:

With this command, you can connect to the MySQL database running in the container by accessing localhost:3307 on your host machine.