Problem
Suppose you are working on a project that relies on Mysql service but you do not want to install MySQL to your system. Using a Docker image can be a good solution. Remember that you can use many services through Docker containers.Steps
1- Install docker
Docker Community Edition can be freely downloaded from the official page. It requires registration.https://www.docker.com/community-edition
Install docker image. It may require a restart, if it does, do so.
In case any problems occur that prevent Docker for Windows from starting check trouble shooting page.
https://docs.docker.com/docker-for-windows/troubleshoot/
In Windows 10, hypervisor launch type needs to be "auto". In order to check current status bcdedit command can be used via PowerShell (Administrator Mode). In order to set to "auto" following command can be used.
bcdedit /set hypervisorlaunchtype auto
2- Find the Relevant Container
Go to Docker Store. Type Mysql. Select an appropriate container. In the container page, on the left, there is a command to pull the docker image to your system. Also, right below the command, there is a link to list available tags. Tags are the possible versions that can be installed. For the latest available image, "latest" tag can be used.3- Pull the Image
Open a command prompt and use suggested command to pull the image.docker pull mysql
4- Run the Container
Type run command to start the container.docker run -p 3306:3306 --name dmysql -e MYSQL_ROOT_PASSWORD=MY-PASSWORD -d mysql:tag
- "tag" is the version information. Check tags available in the container page.
- "MY-PASSWORD" is to be replaced by the desired root password.
- "-p" flag determines the port to expose to host environment.
docker ps
5- Configure the Application
Configure your application which will connect to the MySQL service. Username is root and password is the one given replaced MY-PASSWORD.For the JDBC url use the following:
jdbc:mysql://localhost:3306/testdb
testdb is the name of the db to connect to.
6- Clean the Container
After you are done with the container stop and remove it.docker stop dmysql docker rm dmysql
Conclusion
Docker containers are very useful for many use cases. Providing a local service is just an example.We used the service and removed it afterward without any modifications to the host system.
Comments
Post a Comment