$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
Giải thích những command trong terminal
docker
: This is the command-line tool for interacting with Docker, which is a platform used to develop, ship, and run applications in containers.run
: This command tells Docker to create and start a new container.-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.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.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.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:
p host_port:container_port
: This syntax specifies which port on the host machine should be mapped to which port on the container.For example, if you use -p 3307:3306
, it means:
3307
is the port on your host machine (the one you will connect to).3306
is the port inside the container where the MySQL service is running.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.