Use docker commands easily with Makefile!

Fukuryu Ramen Wadachi Main Branch (Honmachi, Osaka City)

Hello!
I'm Hide, the Ramen King from Beyond Inc.'s Osaka office.
This is my first post, so I'm super nervous (lol).

The impetus for writing this article came from my job search.
During my job search, I prepared a development environment using Vagrant to create a portfolio, but
my PC specs were poor...it was extremely slow...it was the worst environment.
I learned about the popular Docker on the internet and social media, and tried setting it up...but
what struck me at the time was..."Docker commands are so long and tedious..." (TДT) It's
a shame, as it runs so smoothly...

If you add various options, the command will be over 20 characters long, and it's very tedious to keep typing it every time.
And when the command is this long, you make typos, so it's really frustrating.
Everyone reading this has probably had a similar experience ( ・`ω・´)キリッ

So let's take a look at how you can run docker commands with just a few characters!

 

What is docker?

 

First of all, do you know what Docker is?

 

"It's the container technology one!"
"The whale one!" "
It's very lightweight, isn't it?"

All of the above is correct

 

Docker is a platform that uses container-based virtualization technology
to run programs very quickly and lightweightly.
This alone might not be enough to understand, so let's take a closer look.

 

 

The difference between hypervisor-based virtualization and container-based virtualization

 

 

There are two virtualization technologies that are often compared when explaining Docker:
hypervisor-based virtualization and container-based virtualization, and Docker uses the latter.

 

First, to explain the difference between the two, we need to understand hypervisors and the Docker Engine

 

 

hypervisor

 

 


emulates and controls the hardware used by the VM (virtual machine), such as the CPU and memory, between the host OS and the VM

This allows you to create multiple guest OSes of your choice on the host OS, and
the host OS and guest OSes are isolated from each other.

 

However, using multiple operating systems means using multiple kernels, which
creates overhead and slows down processing, making it longer to start and create VMs.

 

 

Docker Engine

 

Docker Engine controls the creation, startup, shutdown, and deletion of Docker containers.
You can use it by installing a resident Docker client such as Docker Desktop.

 

Unlike a hypervisor, the Docker Engine does not create a guest OS and
shares the kernel with the host OS, which reduces overhead and makes it very fast and lightweight.

 

Although Docker seems to have only advantages, one of its disadvantages is that the commands are very long.
Specifically, you may have to type the following long commands...

docker-compose exec db bash -c 'mysql -u root -psecret'

 

 

What is the Make tool?

 


This Make tool frees us from having to type out long docker commands.

 

How easy is it?...

docker-compose exec db bash -c 'mysql -u root -psecret'

This command

 

 

make mysql

You can run it with just this command ヽ(´∀`*)ノ

 

The make tool was originally
designed to easily build programs written in compiled programming languages ​​such as C and C++.
However, it can also be used to abbreviate commands as shown above!

 

Now, let's get started with the installation!

 

 

 

Install the Make tool

⚠If you are using a Mac, it is installed in the terminal by default, so please see if you are using Windows

 

 

 

 

 

Install from the official installer

 

 

 

 

 

Click Setup under [Complete package, except sources] to install the installer

 

 

 

 

 

 

 

Click [Next] and follow the instructions in the installer

 

Configure your Windows environment so you can use Make anywhere

 

 

 

 

Go to Control Panel > System and Security > System and click Advanced System Settings

 

 

 

 

 

 

 

 

 

 

Click [Environment Variables (N)..]

It is up to you to decide whether to set it as a user environment variable or a system environment variable.
If "Path" is already set in the variable list, see ①. If it is not set, see ②.

 

① Add to “Path” variable

 

 

 

 

 

 

 

 

Select “Path” from the variables list and click Edit

 

 

 

 

 

 

 

 

 

Double-click the blank field, paste the absolute path below, and press the [OK] button to complete the setup!

C:\Program Files (x86)\GnuWin32\bin

 

②Add a new “Path” variable

 

 

 

Click [New], enter the following variable name and variable value as shown in the image, and
press the [OK] button to complete the setup!

 

 

 

After entering "make" in Powershell, if you see the following, the setup is complete!

PS C:\Users\beyond_tokuhara>make make: *** No targets specified and no makefile found. Aborting.

 

 

 

What is a Makefile?

Now Make is installed yay (≧▽≦★)
But just installing it doesn't mean you can omit the docker command...
You need to create a Makefile to omit it.


a file that describes rules
for performing compilation, dependency management, etc. executes the process based on those rules.
Let's take a look at how to write one!

 

The writing method is very simple.
It can be written in the following four steps!!

1. Enter the arguments to be set for the make command.
2. Press Enter.
3. Windows users should press the tab key, and Mac users should press the →| key.
4. Enter the docker command.

 

mysql: docker-compose exec db bash -c 'mysql -u root -psecret'

 

 

⚠If you run UTF-8 with a BOM and leave a space, the following error will occur.

please create a space by pressing the Tab key instead of the Space key

 

make mysql makefile:5: *** Missing separator. Abort.

 

 

Enter the Make command

Now that everything is set up, let's start typing commands!

If it looks like the one below, you've succeeded ヽ(*·ᗜ·)ノヽ(·ᗜ·* )ノ High touch!

PS C:\Users\beyond_tokuhara>make mysql docker-compose exec db bash -c 'mysql -u root -psecret' Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 14 Server version: 8.0.21 MySQL Community Server - GPL Copyright (c) 2000, 2020, 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>

 

⚠Notes
・Docker command cannot be omitted if there is no Makefile in the current directory.

・You can use the specified Makefile anywhere by specifying it with make -f, but please note that if
in a directory higher than the directory containing docker-compose.yml and docker-compose.yaml,
the following error will occur.

PS C:\Users>make -f \Users\beyond_tokuhara\Makefile sql docker-compose exec db bash -c 'mysql -u root -psecret' ERROR: Can't find a suitable configuration file in this directory or any parent. Are you in the right directory? Supported filenames: docker-compose.yml, docker-compose.yaml make: *** [sql] Error 1

 

summary

What do you think? It was
very easy!
And I think you were able to execute the docker command with just a few characters (-`ω-)Tsk!
Also, you can also tweak the artisan command by doing the following!

seed: docker-compose exec app php artisan db:seed

Compared to aliases, Makefiles are less environment-dependent and
are very convenient because they can be used immediately by simply sharing the file.
I hope you all enjoy a comfortable Docker life with Makefiles.
Thank you for reading this article!

If you found this article useful, please click [Like]!
2
Loading...
2 votes, average: 1.00 / 12
18,756
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Hide@Infrastructure Engineer

It all started with a really interesting interview.
I joined the System Solutions Department in Osaka mid-career and
am currently involved in the construction and operation of servers and clouds!
I hold LPIC1, AWS SAA, and OCI Architect Associate certifications.

In fact, I love ramen and
have investigated over 100 ramen shops in Osaka (。-∀-) I am striving to become the ramen king of Nibi Beyond

I'm also on Twitter, so please follow me (´∇`)
Click on the Twitter mark in the right corner!!