This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Many back-end developers encounter problems like the following in their daily development:

  • The versions of MySQL used in the two projects at hand are different, but a test database needs to be built locally for the convenience of development.
  • Want to build a local MongoDB or Redis cluster Demo;
  • You need to test your software in different runtime environments;
  • Need to start/stop/install/uninstall various middleware frequently;
  • , etc.

This sums up the need to constantly modify the various environment configurations of the development computer. In fact, to solve this problem, using Docker locally to deploy various operating environments and middleware is a good solution. It is not as heavy as virtual machines and can easily run various environments independently.

This article uses MySQL as an example to introduce how to use Docker to deploy software and facilitate daily development. Based on this example, you can understand how to use Docker to deploy various basic components, middleware, runtime environment, etc., to solve the pain point of frequent environment configuration in daily development.

Find the official image of MySQL

The first step in running a MySQL database container is to find the corresponding image.

You can find the official mirror of MySQL MySQL /mysql-server by searching the Docker Hub. In addition to the official images of MySQL, Docker Hub can also find MySQL images maintained by Docker, as well as MySQL images maintained by Google, IBM and other companies. Here, we take MySQL /mysql-server images as an example. The differences between other images can be found in their respective documentation.

Once found, you can either pull it locally with the following command or have Docker pull it automatically when running the container.

docker pull mysql/mysql-server
Copy the code

Start a MySQL container

Next, run a MySQL container.

docker run --name=mysql_demo  --restart on-failure -d -p 3306:3306 mysql/mysql-server
Copy the code

In daily development, it is recommended to give all running containers a custom name, rather than having Docker automatically generated, so there is no confusion. Then, make the container restart in case of a problem.

Make sure it is running correctly with the docker ps command.

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 58b0e1ea4ad3 mysql/mysql-server "/entrypoint.sh mysq..." 4 seconds ago Up 3 seconds (health: starting) 0.0.0.0:3306->3306/ TCP, 33060-33061/ TCP mysql_demoCopy the code

Operate MySQL by entering containers from containers

When the MySQL database is installed for the first time, a temporary password is randomly generated and written in the log. If installed on the host, we can find the password in the log file and log in as root.

A log containing a temporary password would look something like this:

[Entrypoint] GENERATED ROOT PASSWORD: *****************
Copy the code

You can obtain the MySQL container by running the following command:

docker logs mysql_demo 2>&1 | grep GENERATED
Copy the code

Select * from the container log where the GENERATED row contains the temporary password GENERATED by MySQL.

Next, we need to go to the container’s command line to do something:

docker exec -it mysql_demo bash
Copy the code

In the above commands, Docker exec can execute a command in the container. It is short for -i -t, which allows Docker to assign a pseudo-terminal and bind it to the container’s standard input and keep it open. In short, we can execute commands directly in the container.

MySQL > connect to MySQL and change root password:

mysql -uroot -p
Copy the code

Then enter the temporary password just obtained, you can log in successfully.

Or you can do it all at once:

docker exec -it mysql1 mysql -uroot -p
Copy the code

Mysql > select * from ‘root’;

ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
Copy the code

You can also create a database, create a user, give the user authorization, and so on. After that, as long as the container is running, you can connect to it just as you would to a locally installed MySQL.