Docker makes running programs as easy as popping cartridge in and press play!

tl;dr: Docker makes running databases and other utility really simple. You also save yourself from accidentally messing up your current environment!

There are many articles out there that talks about what Docker is on a system and abstract level, but not many elaborates the simpler point:

Docker, do I need it? Why would I use this?

If you have wondered about this before, and you are a developer, then this article is for you!

A little history: Developing SaaS products on a Mac in 2014

Prototyping and making web applications with a small team is an exciting thing. In 2014, one of the top contender was Ruby On Rails, which came with Sqlite3 as the build-in database.

If a new engineer joins the team, the on-boarding process would look something like this:

Install git via xcode-select

Install Rails (Mac came with Ruby)

Clone the project

Install dependencies

Run database migration and seeding script

And your environment is now ready (assuming you do not have crazy dependencies like ImageMagick).

This is all fine and good for local development, but as soon as we step into production deployment, we find that setup to leave more concerns to be desired.

This is because our local environment is very different from the staging or production environment.

It often happens that we would develop features and migrations that would work in the Mac Laptop, but not in the actual production environment. To solve this problem, we started to make developers connect to the shared databases that the production environment use. But, as you can imagine, we quickly run into issues where migrations are ran against each other and introduced more confusion on whether a feature is working correctly or not.

To work around that, we introduced running MySQL locally and creating better seeding scripts for local testing. Installing MySQL locally is easy enough, but the instruction can still vary between developer to developer, they also run into issues to keep MySQL running here and there.

To work around that, we started to play with running MySQL inside virtual machines (VirtualBox and Vagrant), so that each developer can have access to a MySQL database that’s as close to the production environment one as possible.

Then came Docker, and docker changed it all.

Step 0: Install Docker

Developer Setup: Version 2

docker run -p 3306:3306 --name local-mysql -e MYSQL_ROOT_PASSWORD=iamreallysecure -d mysql:8

That’s all.

If you type docker ps , you will see the following:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

a3138281dc6a mysql:8.0 "docker-entrypoint.s…" 3 weeks ago Up 2 weeks 0.0.0.0:3306->3306/tcp, 33060/tcp local-mysql

Now try to connect into it with mysql -h 127.0.0.1 -u root -p , type in the password:

mysql -h 127.0.0.1 -u root -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 1907

Server version: 8.0.18 MySQL Community Server - GPL Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

That’s all there is to it!

You can now connect to a local MySQL instance and do feature development and local testing. You can be sure that your code is coding against things that’s as close to the production environment as possible (at least from the perspective of database).

If your team uses MSSQL, that’s possible to run inside a Mac too:

docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=iamreallysecure' -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest

Previously, to get MSSQL running inside MacBookPros, you would have to either create one in Azure, have access to a windows machine, or spin up a Windows Virtual Machine in VirtualBox!

This is quite a time-saver.

Rebooted Your Laptop and can’t find the mysql process anymore?

Don’t worry, I got you covered.

Simply do docker ps -a , and you will see your old process in the list.

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

a3138281dc6a mysql:8.0 "docker-entrypoint.s…" 3 weeks ago Exited (0) 7 seconds ago local-mysql

Take the string a3138281dc6a and type docker restart a3138281dc6a

And the instance will spring back to life!

You can test that MySQL is now running with either:

use mysql command to log into the db. use telnet 127.0.0.1 3306 to verify that MySQL server is running and waiting for client connections.

In Conclusion

When starting a new SaaS or Web project, you can use docker and quickly get a local database instance running.

Google <database name|mysql|postgres|mssql> docker , and you can quickly find instruction on how to run it.

, and you can quickly find instruction on how to run it. If after rebooting your laptop/computer, and the last instance is not running anymore, simple do docker ps -a to find the container id, and then docker restart <container_id> to have it running again.

Want to do more?

We will cover in the future article on how to use docker-compose to spin up the database instead so you have a copy of database password and flags sticking around.

If you have any questions, feel free to reach out to us at info@teamzerolabs.com! We have been happily using docker for developing prototypes here.