Creating MySQL container

We have been using a MySQL server installed on the host machine so far in this book. We will now see how to create a Docker container with MySQL. If you are running an instance of MySQL on your host OS (the OS in which Docker is running), then stop the instance or configure MySQL to run on a different port than 3306 in the Docker container (we will see how to do this shortly).

We will use the official MySQL Docker image; see https://hub.docker.com/_/mysql/. Run the following command:

docker run --name course-management-mysql -e MYSQL_ROOT_PASSWORD=your_password –p 3306:3306 --network=coursemanagement -d mysql

Replace your_password with the root password you want to set. This command will install the latest version of MySQL. The -d option runs the container in detached/background mode. Also note that the container is created in the coursemanagement network that we created in the previous section. If you want to use a specific version of MySQL, then tag that version; for example, to install MySQL Version 5.5.58, use the following command:

docker run --name course-management-mysql -e MYSQL_ROOT_PASSWORD=your_password -d –p 3306:3306 --network=coursemanagement mysql:5.5.58

MySQL will run on port 3306 in the container, and the container exposes the service at the same port on the host machine. To expose this service at a different port on the host machine, say port 3305, use the –p or --publish option:

docker run --name course-management-mysql -e MYSQL_ROOT_PASSWORD=your_password –p 3305:3306 --network=coursemanagement –d mysql

The -p option in this command maps port 3306 in the Docker container to port 3305 on the host machine.

Once the command is executed successfully, you can verify that the container is running by executing the docker ps command. The container will also be visible in Docker Explorer in Eclipse. Switch to the Docker Tooling perspective in Eclipse and expand the Containers group under the Local connection:

Figure 12.11: Docker Explorer listing containers and images

Right-click on the container name to show menu options for different actions on the container, such as Start, Stop, and Restart.

The Execute Shell option is very useful for opening a shell in the container and executing commands. For example, to execute MySQL commands from within the container, select the Execute Shell option and execute the mysql -u root –p command:

Figure 12.12: The Execute Shell in a Docker container

Assuming you have mapped port 3306 from the container to the same port on the host machine, you can connect to the instance of MySQL in the container from the host machine as follows:

mysql -h 127.0.0.1 –u root -p

Make sure you specify the -h or --host option, or it will try to connect using the local .sock file and that will fail. You can also connect to this MySQL instance from MySQL Workbench.

Next, create the course_management schema in the database. See the Setting up Database section of this chapter for details.

If you do not want to type long Docker commands and remember options, you can use Docker Explorer’s user interface to create containers. We used the run command of Docker to run a MySQL container using the mysql image. The command first checks whether the required image is already downloaded on the local machine, and if not, it downloads it. Docker images can also be downloaded explicitly using the docker pull command. For example, we could have first downloaded the mysql image by executing the following command:

docker pull mysql

Once the image is downloaded, it will be displayed in Docker Explorer. Right-click the image and select Run:

Figure 12.13: Create Docker container from image in Docker Explorer

Follow the wizard to create a container. You can use this option to create multiple instances from the same image, for example, to run multiple MySQL containers.


The last page in this wizard lets you specify a network for the container.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.220.16