For the past few months, I've been guilty of abusing and exploiting docker containers. I search for the image registry for every new technology or tool that I come across and use it through a container if possible.
Believe me when I say this, using containerized versions of new technologies and tools for learning is the best. It's not just handy but efficient at the same point. Most of the time the image and the containers themselves are more lightweight than the actual software itself. Not only does this make the usage and application of the software more concise but way easier to install and manage. I don't have to care about loads of dependencies and path variables. I pull, run, execute and exit. Straight and simple.
Hitting the nail now, let's talk about how we can implement this kind of methodology in a mainstream and widely used technology: MySql.
Getting Started
Today's blog will be a small walkthrough of using MySql in a container and some practices I do to keep things organised and easy to pick up from where I last left. (the steps are heavily abstracted from the official documentation)
0. Prerequisites
Make sure you have installed docker, using a Linux-based os, preferably Ubuntu and an active internet connection.
1. Downloading the docker image
This is fairly simple. We'd be using the Oracle container registry to pull our image:
docker pull container-registry.oracle.com/mysql/community-server
A download of several layers should start in your terminal. As soon as the download is complete a simple docker images
command would show you the downloaded stuff.
Expect something like this:
$> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
container-registry.oracle.com/mysql/community-server latest 1d9c2219ff69 2 months ago 496MB
2. Starting a MySQL Server Instance
To start a new Docker container for a MySQL Server, use the following command: docker run --name=container_name --restart on-failure -d image_name:tag
This is pretty self-explanatory, but for a beginner:
image_name
is the name of the image to be used to start the container;The
--name
option, for supplying a custom name for your server container, is optional; if no container name is supplied, a random one is generated.The
--restart
option is for configuring the restart policy for your container; it should be set to the valueon-failure
The
-d
means dry mode, that is, the container would run in the background
Run the following command for quick implementation:
docker run --name=mysql1 --restart on-failure -d container-registry.oracle.com/mysql/community-server:latest
a simple docker ps
command would show something like:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fec700271662 container-registry.oracle.com/mysql/community-server:latest "/entrypoint.sh mysqโฆ" 2 hours ago Up 5 seconds 3306/tcp, 33060-33061/tcp mysql1
Since we are running the container in Dry mode (-d) we'd need to activate the container logs so we can see the temporary password generated by the initialization process.
A simple docker logs mysql1
command would let you see the password somewhere within the generated logs. Expect something along the lines of:
GENERATED ROOT PASSWORD: Axegh3kAJyDLaRuBemecis&EShOs
You might struggle here for a while but don't lose patience yet!
3. Connecting to MySQL Server from within the Container
Once the server is ready, you can run the mysql client within the MySQL Server container you just started, and connect it to the MySQL Server. Use the docker exec -it
command to start a mysql client inside the Docker container you have started, like the following:
docker exec -it mysql1 mysql -uroot -p
Put in the password generated in the logs when asked.
nilay@nilay-HP-Pavilion-Gaming-Laptop-15-ec1xxx:~$ docker exec -it mysql1 mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.1.0 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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>
If you've made it this far, congrats you have now discovered a new superpower.
Anyways, the generated password is a bit too crampy so we will have to generate our original password.
Use the following command and replace 'password'
with you Password:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
That's it! You've now got your MySql server running inside a container ready to serve you any day. Try out some basic commands and make yourself at home :)
Pro Tips:
Now as you've seen, this process is too lengthy and a person shouldn't have to go through this every time they want to use MySql. Here are some steps to practice when dealing with such scenarios:
Don't delete but only stop the container when not using. (
docker stop mysql1
)Once you've stopped the container, the container won't be directly visible using the
docker ps
command. Usedocker container list -a
to view all the stopped containers. You might see too many stopped containers that you have used previously and don't require anymore and might want to delete/remove them permanently. You might even have some containers that you don't want to delete amongst them, for example, themysql1
container we just created. I'd suggest that you start those containers that you don't want to remove usingdocker start mysql1
for example. Now prune the ones that are stopped usingdocker container prune
command. This would leave only the running containers. Stop or restart those containers as per your use now.
Now to reuse your container just type docker start mysql1
, might ask you for a password that you declared. Stop the container using docker stop mysql1
.
Voila!
Don't complain if you get too comfortable using containers for every possible tech out there just like me. I mean who doesn't like easy setup and management, especially a lazy developer like me? See you next time :3