Setting up databases can potentially mess up your environment especially if you work with multiple different databases. I used to install them directly on my machine. I am here today to teach you how to do so but in a totally segregated environment and it will never mess your local machine. We are going to use Docker.
I am not going to cover docker in depth. My goal here is to help you set up any database on the fly and get going with your project in maybe 5 min. So if you haven't docker installed yet please download it from here and set up an account.
Docker works with system images that once instantiated are called containers (which are essentially a linux machine running in an isolated environment on your machine with a bunch pre-configured stuff)
MySQL
If you want to download a MySQL image run the following command:
//download the latest mysql version
docker image pull mysql
or maybe you want a specific version of mysql:
docker image pull mysql:5.7
Alright. You already have the system image for mysql server on your machine. But perhaps you need some other database like Postgres for instance...
Postgress
If you want to download a Postgress image run the following command:
//download the latest postgres version
docker image pull postgres
or maybe you want a specific version of postgres:
docker image pull postgres:9.5
Done. You have another system image but now for a postgres server.
Time to get our hands dirty
Up until now we have downloaded system images but they are no good if we don't instantiate them.
Let's start with out mysql server. Please run this:
docker run -e MYSQL_ALLOW_EMPTY_PASSWORD=yes --name mysql-5.7 -p 3306:3306 -d mysql:5.7
Let me explain what that does. The -e
is an option to set environment variable inside the container in our case I just told the container that we can access the database without a password. The --name
is self explanatory, I just gave a name to the container. The -p
option means I am publishing the container at a specific port. In this case I set my machine to redirect any connection on port 3306 to the container's 3306 port. The -d
option means it should run in background so I can still use my terminal window. Finally the mysql:5.7.32
is the name of the image I've previously downloaded
I am going to run the postgres server now:
docker run --name postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres
See, it is just as simple as that. And you can run how many database servers you want without messing your local environment.
Now, I challenge you. Go ahead and install a mongodb server using docker. ๐
Listing containers
If you want to check which containers are running type this on your terminal:
docker ps
But if you want to see a list of all containers even the stopped ones, run this:
docker ps -a
Where do I search for system images?
There is a community called dockerhub and from there you can query all sorts of images.
I hope this tutorial suits you well and please don't forget to support it by slapping the like button now. ๐
See you on the next post.