در حال بارگذاری ...

Using Docker for Databases0:00

A good local development environment is important for the efficiency of a web developer. There are many tools available to make this easy, like MAMP, Laravel Homestead, and Laravel Valet. My personal preference is to use Laravel Valet, however it does not provide any tooling for database management. This usually leads to developers installing local versions of a database. But maintaining a local database management system can be a pain, especially if you need different versions for different projects. I found that I can utilize Docker to make managing my database systems much easier. I can have multiple versions running at the same time, I can start and stop their services.

Running MySQL Container2:28

and then the --tag to set the tag. In this case, MySQL, and then whatever tag you want to give it. In our case, it's going to be 5.7. So let's go ahead and copy this over and see if we can boot up a container. So we'll leave everything else set except for the tag, 5.7. And let's run this. So now let's check to see if we have a running container. And we do. Now that we have a running MySQL container, we need to be able to access it from our machine. I want to connect to the server by using my local port 3306.

Now that we have a running MySQL container, we need to be able to access it from our machine. I want to connect to the server by using my local port 3306. All we have to do is add a few flags to the docker run command, and we can configure the server to meet our needs. But first, let's go ahead and delete the container that we have running. By running docker stop and the name of the container, we're able to bring the container down and we can remove the container using docker rm. If I check my running containers now, I should see that there are no running containers. I want to run the same docker run command as before, except this time I want to add a port flag.

Connecting via Port Mapping3:30

I want to run the same docker run command as before, except this time I want to add a port flag. To do that, we're going to add a -p flag and then set the local port and tell it which port we want to connect to in the container. Now if I check the running containers, I'll see that there's a local port 3306 that's connected to port 3306 in the container itself. So let's try to connect to that. Let's open up our MySQL client and create a new connection. We're going to use our local IP address, port 3306, username root, and the password that we set, mysecretpw.

Start, Stop, Delete Containers4:05

We're going to use our local IP address, port 3306, username root, and the password that we set, mysecretpw. Looks good. We can connect to it. I'd like to show you how we can start, stop, and even delete a MySQL container. But first, let's create some sample data and see how docker persists the file system for each command. Let's first create a new database and let's use the database and let's go ahead and create a table in the database. I'm going to call it people and it's just going to have a name of varchar(255).

a table in the database. I'm going to call it people and it's just going to have a name of varchar(255). Now if we connect to the database itself, we will see the people table. Let's go ahead and add a couple people, myself, Jeffrey, and save it. So now those two entries are in the table. If we go back to our terminal, we can see that our MySQL container is still running. We can actually start and stop it by using the name and the docker start and docker stop command. So if we do docker stop some-mysql, we can see that the container is no longer running. Going back to our client, if we try to reconnect, it's not available. So let's go ahead and bring the server back up.

Going back to our client, if we try to reconnect, it's not available. So let's go ahead and bring the server back up. docker start and when we connect to that database, the data is still available. If we delete the container, however, the data itself will be gone. Let me show you what that looks like. A container can be deleted with the docker rm command. And we can see the container is no longer available. To recreate the container, we need to rerun the docker run command. And we can see that the MySQL container is running again. So let's try to connect to it.

Persisting Data with Volumes6:19

And we can see that the MySQL container is running again. So let's try to connect to it. We can connect to it, however, the database is no longer available. This is because the database is being stored on the file system that we do not have direct access to. Once I delete a container, it will also delete the data that we have stored in the database. We can fix this by using a docker volume. You can think of a docker volume as a virtual hard drive that you attach to a container. Since the volume needs to be attached when the container is created, we'll go ahead and destroy the container that we've created already and bring up a new one with a docker

Since the volume needs to be attached when the container is created, we'll go ahead and destroy the container that we've created already and bring up a new one with a docker volume. We should have no containers running now. Great. The docker CLI comes with a docker volume command. If you ever need more information on a specific command, you can always add the --help flag to get more information. In this case, we're going to create a volume. So we're going to run docker volume create and give it a name.

In this case, we're going to create a volume. So we're going to run docker volume create and give it a name. I'm going to name this mysql_data. We can list the volumes by running docker volume ls. And as you can see, we have the mysql_data volume created. What I'd like to do now is create a new docker container with this volume attached, but I'm not really sure which path in the container I need to attach the volume to. So let's check out the documentation. In the docker run command, we can use the -v flag for volume. Give it the volume name and tell the volume where to attach in the container itself.

In the docker run command, we can use the -v flag for volume. Give it the volume name and tell the volume where to attach in the container itself. Now we know that we need to attach our volume to /var/lib/mysql. Let's go ahead and use that in our docker command. So now we're going to add the -v flag for volume. Give it our volume name and then tell it that we want to attach it to /var/lib/mysql. Let's run this command. Let's check to make sure the container is running and let's add some data to it so we can test out the volume. I'll connect to our new MySQL container and add the same test data that we added before.

can test out the volume. I'll connect to our new MySQL container and add the same test data that we added before. So when I open up that database, we should see the same two entries in the people table. We can test out volume persistence by deleting the current container and bringing it back up, connecting with the same volume. Let's try that out. We've lost the connection because the container is destroyed. Now let's bring it back up using the same docker run command we used with the -v flag. And when we connect to the container, the database is still available and the tables are still intact.

Running Multiple MySQL Versions9:13

And when we connect to the container, the database is still available and the tables are still intact. At this point, we have a MySQL server connected with our local port 3306 and using a file system that we have access to. For a normal local setup, this is more than enough. However, the real power behind using docker is that everything is containerized. This gives us the ability to run a MySQL server version 5.7 next to a MySQL server version 8. If you give me just a few more minutes of your time, I can show you how we can accomplish this. The first thing we need to do is pull down the docker image for MySQL version 8. Now let's create a new docker volume that we'll use for MySQL version 8.

The first thing we need to do is pull down the docker image for MySQL version 8. Now let's create a new docker volume that we'll use for MySQL version 8. Let's utilize the same docker run command that we used before with just a few minor tweaks. First, we're going to change the name to some mysql8, same root password, the new volume that we just created, and instead of attaching to our local port 3306, we're going to attach it to local port 3307 and keep it 3306 in the container itself. Finally, let's change the tag to version 8 and run it. Now let's see our running docker containers. As you can see, we have two running containers. One is running MySQL version 5.7, the other is running MySQL version 8.

Looks great. Let's connect. And you can see that we are now connected to a server running MySQL version 8. You can start and stop each container separately by using the docker start and docker stop commands and the name of the container. The services running these containers are completely containerized so we know they won't cause each other to break. And if one of your containers starts acting up, the solution is as simple as destroying it and rerunning the docker run command. This is how I run and manage all kinds of servers that I need for different applications.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟