Ever had issues deploying multiple databases on your development machine? This post is a tutorial on how Docker can be used to launch database containers in a few commands. Not sure what Docker is? Check out their website at www.docker.com.
First things first, if you haven’t already installed Docker, follow their installation tutorial for your system here. You’ll also have to make an account on Docker Hub which is a public repository that has thousands of container images ready to be deployed.
For this tutorial, we’ll be using MariaDB & their official Docker Hub Repository.
Step 1) Login to Docker
With your Docker Hub username & password handy, log into Docker.
> docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username (proulxsean): proulxsean Password:
Step 2) Pull the latest MariaDB image. We’ll use the latest version for this tutorial.
> docker pull mariadb:latest Pulling from library/mariadb Digest: sha256:9d443337dfbb2a34583ed7c968cde6115ce1b10630530ff1f0f5c7f1e6f0a76b Status: Downloaded newer image for mariadb:latest
Step 3) Run the container.
> docker run -p 3306:3306 -d --name test_db -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=db -e MYSQL_USER=super -e MYSQL_PASSWORD=testing123 mariadb:latest --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci 1ad38cccab91216555e2885a0f6f0d03c00360fc07a25ee3f9bbafce3f76ee7c
Let’s break down what is happening here.
“-p 3306:3306”: Assigns a port to the port in the Docker container, in this case, we just map 3306 to 3306, however, this can be any available port on your system.
“-d”: Demonizes the container so it runs in the background. This allows the container to stay open & not close immediately.
“-e”: Allows Docker to set the environment variables in the container. From the MariaDB repository, we see that it allows us to set a default root password using the MYSQL_ROOT_PASSWORD, create a default database with MYSQL_DATABASE, set a default user & password with the variables MYSQL_USER & MYSQL_PASSWORD.
“mariadb:latest”: The latest MariaDB version we pulled earlier. Note that step 2 isn’t entirely necessary, if you don’t have the image locally, Docker will conveniently download it for you.
“–character-set-server=utf8mb4 –collation-server=utf8mb4_unicode_ci”: Docker containers run a single command at the end of the deployment process, in MariaDb’s image, we see that it runs the “mysqld“ command which accepts parameters. These ones create a UTF-8 MariaDB server.
Step 4) Testing 1…2
With the local mysql client, connect to our created database server.
> mysql -usuper -ptesting123 -h 127.0.0.1 -P 3306 db mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 14 Server version: 5.5.5-10.3.10-MariaDB-1:10.3.10+maria~bionic mariadb.org binary distribution Copyright (c) 2000, 2018, 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>
“-u”: Default username used with the MYSQL_USER environment variable.
“-p”: Default password created with the MYSQL_PASSWORD variable. Note: There shouldn’t be any spaces between the param & password.
“-h”: Docker host – usually 127.0.0.1.
“-P”: Port mapped earlier with the docker run “-p” param.
“db”: Default database we created with the MYSQL_DATABASE variable.
Step 5) Rejoice! You have successfully created a MariaDB database container.
*Bonus* Using Docker Compose
Docker Compose abstracts out a lot of Docker commands into an easy to use YAML configuration file. We will go into more detail on this in a later post.
Step 1) Create a YAML configuration file for Docker Compose.
> touch .docker-compose.yml
Step 2) Let’s configure! With your favorite text editor, copy in the following.
# Composer version (https://docs.docker.com/compose/compose-file) version: '3.4' # List of containers services: # Name of the container db: # Docker image image: mariadb:latest # Container environment variables environment: - MYSQL_ROOT_PASSWORD=root - MYSQL_DATABASE=db - MYSQL_USER=super - MYSQL_PASSWORD=testing123 # Mapped ports ports: - '3308:3306' # Add params to default command (or you can override it completely) command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
Step 3) Testing 1…2
> docker-compose up
Have any questions, concerns, found a bug or mistake? Leave us a comment below.
Need a Web Application or Website developed? Get in touch with us!