This post shows how to create and use a docker-compose YAML file to generate a MariaDB Docker container with initial user accounts. First, we need to install Docker for Windows. Then, use its docker-compose to process our YAML file. We tested everything using MariaDB 10 and Windows 10.
Create A docker-compose YAML File
Next, we create a docker-compose YAML file as shown below. Use any text editor and, for simplicity, save it as docker-compose.yml. Notice we set some environment variables to create an initial user in MariaDB – MYSQL_USER and MYSQL_PASSWORD. Moreover, we used environment variables to specify the root account’s password and our database. Also, we specified the version of MariaDB, which is version 10 for this post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | version: "2.3" services: demo-container-db: image: mariadb:10 #restart: always environment: MYSQL_USER: turreta MYSQL_PASSWORD: turreta+pwd! MYSQL_ROOT_PASSWORD: a1128f69-e6f7-4e93-a2df-3d4db6030abc MYSQL_DATABASE: turretadb ports: - "3306:3306" networks: - turreta_network networks: turreta_network: driver: bridge |
We could use another label or name for our networks. In this post, we are using turreta_network. In case we want a different label, replace all instances of turreta_network with something else.
We use the same port MariaDB uses. We could change these ports, though.
Create Docker Container Using Docker-Compose
Then, we create a MariaDB Docker container using the docker-compose YAML and the docker-compose application. To achieve that, we first open a Windows Command Line prompt and cd to the directory where the docker-compose.yml file is. Then, run the following commands.
First, pull the image from Docker Hub to the local machine.
1 | docker-compose pull |
Then, create and run a MariaDB container using the following command.
1 | docker-compose up demo-container-db |
With Docker-compose YAML, we can easily recreate the same MariaDB Docker container whenever we need a fresh copy.
This post is part of the Docker For Developers tutorial.