Setup a Docker Server on a PC, in a cloud, or in a local Container, and login.
Now that you have your server set up lets start by updating the server
sudo apt update && sudo apt upgrade -y
Use cd command to move to the directory that you would like to place you "build" files in.
The first file that we are going to make as part of this process is the docker-compose.yml
sudo nano docker-compose.yml
Now copy and past in in info that docker will need to "Build" the MariaDB setup
version: '3.8' services: mariadb: image: mariadb:latest container_name: mariadb-container restart: always ports: - "3306:3306" environment: - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} - MYSQL_DATABASE=${MYSQL_DATABASE} - MYSQL_USER=${MYSQL_USER} - MYSQL_PASSWORD=${MYSQL_PASSWORD} volumes: - secure_data:/var/lib/mysql networks: - mariadb_network volumes: secure_data: driver: local driver_opts: type: none device: /var/lib/docker-volumes/mariadb_secure_data o: bind networks: mariadb_network:
version: '3.8'
services:
mariadb:
image: mariadb:latest
container_name: mariadb-container
restart: always
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
volumes:
- secure_data:/var/lib/mysql
networks:
- mariadb_network
volumes:
secure_data:
driver: local
driver_opts:
type: none
device: /var/lib/docker-volumes/mariadb_secure_data
o: bind
networks:
mariadb_network:
There are 3 parts of this file that we need to take a closer look at.
The first one going in order from top to bottom is
environment: - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD} - MYSQL_DATABASE=${MYSQL_DATABASE} - MYSQL_USER=${MYSQL_USER} - MYSQL_PASSWORD=${MYSQL_PASSWORD}
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_DATABASE=${MYSQL_DATABASE}
- MYSQL_USER=${MYSQL_USER}
- MYSQL_PASSWORD=${MYSQL_PASSWORD}
The environment part of the docker compose file is where we setup user names and passwords for the app that we are setting up
But here we are using variables. The variables here link back to a .env file that we have not setup yet
The second part of this fill is Volumes:
volumes: - secure_data:/var/lib/mysql
volumes:
- secure_data:/var/lib/mysql
The volumes key defines the storage mapping for the container. It ensures that certain data persists even if the container is stopped or recreated.
The syntax secure_data:/var/lib/mysql follows this pattern: <volume_name>:<container_path>
secure_data is the Volume Name and /var/lib/mysql is the Container Path
The last part of this file that I would like to look at is
volumes: secure_data: driver: local driver_opts: type: none device: /var/lib/docker-volumes/mariadb_secure_data o: bind
volumes:
secure_data:
driver: local
driver_opts:
type: none
device: /var/lib/docker-volumes/mariadb_secure_data
o: bind
volumes: Tells docker that we are setting up a volume next we have secure_data: this is the name of the volume that we are defining or setting up. Now we have driver: local witch Specifies the volume driver to be used local driver is the default driver provided by Docker. driver_opts: Provides additional options to configure the behavior of the volume driver. type: none Specifies the type of volume to be created. device: /var/lib/docker-volumes/mariadb_secure_data Specifies the host directory to use for the bind mount. And o: bind Specifies the mount option.
Now that we know what is going on in the docker-compose.yml file we can start setting up the .env file
sudo nano .env
And copy and past this info in editing as needed
MYSQL_ROOT_PASSWORD=your_root_password MYSQL_DATABASE=your_database MYSQL_USER=your_user MYSQL_PASSWORD=your_password
MYSQL_ROOT_PASSWORD=your_root_password
MYSQL_DATABASE=your_database
MYSQL_USER=your_user
MYSQL_PASSWORD=your_password
use the chmod command to restrict permissions for the file
sudo chmod 600 .env
Before we start up the project lets look at adding just a bit more security with two more commands
sudo mkdir -p /var/lib/docker-volumes/mariadb_secure_data
makes the directory before the build so that we car use chmod the restrict the directories permissions
sudo chmod 700 /var/lib/docker-volumes/mariadb_secure_data
chmod 700 command ensures that only the root user has access to this directory.
With all of this done it is time to start this project up
docker compose up -d
Run docker ps
to see if everything started up and is running
If you need to do more testing you can install mariadb-client-core
sudo apt install mariadb-client-core
and use the command
mysql -h 127.0.0.1 -P 3306 -u your_user -p
To login.