For developers working with databases, the combination of MySQL, a robust open-source relational database, and phpMyAdmin, a user-friendly web-based database administration tool, is a staple. This powerful duo becomes even more potent when containerized with Docker.
This comprehensive guide will walk you through the process of setting up and running both MySQL and phpMyAdmin within separate yet linked Docker containers. We'll explore the benefits of this approach, provide step-by-step instructions, and equip you with the knowledge to streamline your database management workflow.
By encapsulating applications and their dependencies into portable containers, Docker ensures consistency across different environments.
The following are the two references for this.
- MySQL Docker Container - https://hub.docker.com/_/mysql
- phpmyadmin Docker Container - https://hub.docker.com/_/phpmyadmin
Why containerize though??
Why Containerize MySQL and phpMyAdmin with Docker?
Before diving into the how-to, let's understand the compelling reasons for containerizing these essential tools:
- Isolation and Consistency: Docker containers provide isolated environments for MySQL and phpMyAdmin, preventing conflicts with other software or dependencies on your host machine. This ensures consistent behavior across development, staging, and production environments.
- Simplified Setup: Setting up MySQL and phpMyAdmin traditionally can involve installing various packages and configuring them correctly. Docker simplifies this process significantly. With a few commands, you can have fully functional instances up and running.
- Portability: Docker containers are lightweight and portable. You can easily move your MySQL and phpMyAdmin setup between different machines or servers without worrying about compatibility issues.
- Reproducibility: Docker images ensure that everyone on your team is working with the exact same versions and configurations of MySQL and phpMyAdmin, minimizing "it works on my machine" problems.
- Easy Management: Docker provides a consistent interface for managing containers – starting, stopping, restarting, and removing them becomes straightforward.
- Resource Efficiency: Docker containers share the host OS kernel, making them more lightweight and resource-efficient compared to traditional virtual
machines. - Clean Removal: When you're done with your project, you can easily remove the Docker containers without leaving behind residual files or configurations on your host system.
Streamlining with Docker Compose (Recommended for Production and Complex Setups)
docker run
commands works, for more complex setups and easier management, Docker Compose is highly recommended. Docker Compose allows you to define and manage multi-container Docker applications using a YAML file.1. Creating a docker-compose.yml
File:
Create a new file named docker-compose.yml
in your project directory.
2. Defining Services:
Add the following content to your docker-compose.yml
file:
services:
db:
image: mysql:latest
container_name: mysql-container
environment:
MYSQL_ROOT_PASSWORD: your_root_password
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql
restart: always
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin-container
links:
- db:db
ports:
- "8080:80"
environment:
PMA_HOST: db
PMA_PORT: 3306
restart: always
volumes:
mysql_data:
docker-compose.yml
file:version: '3.8'
: Specifies the Docker Compose file format version.services
: Defines the different containers (services) that will run.db
: Defines the MySQL service:image: mysql:latest
: Uses the official MySQL image.container_name: mysql-container
: Sets the container name.environment
: Sets environment variables, including the MySQL root password.ports
: Maps host port 3306 to container port 3306.volumes
: Mounts themysql_data
named volume to persist data.restart: always
: Ensures the container restarts automatically if it crashes.
phpmyadmin
: Defines the phpMyAdmin service:image: phpmyadmin/phpmyadmin
: Uses the official phpMyAdmin image.container_name: phpmyadmin-container
: Sets the container name.links
: Links to thedb
service (aliased asdb
). Note: Whilelinks
is still supported, using Docker networks (the default in Compose) is generally preferred for inter-container communication in newer Docker versions. Theenvironment
variables below achieve the same result.ports
: Maps host port 8080 to container port 80.environment
:PMA_HOST: db
: Tells phpMyAdmin to connect to the service nameddb
(our MySQL container).PMA_PORT: 3306
: Specifies the MySQL port.
restart: always
: Ensures the container restarts automatically.
volumes
: Defines the named volumemysql_data
.
3. Starting the Services with Docker Compose:
Navigate to the directory where you saved the docker-compose.yml
file in your terminal and run:
docker-compose up -d
This command will create and start both the mysql-container
and the phpmyadmin-container
in detached mode.
4. Accessing phpMyAdmin (Same as before):
Open your web browser and navigate to http://localhost:8080
. Use the root
username and the password you defined in the docker-compose.yml
file to log in.
5. Stopping and Removing Services:
To stop the containers:
docker-compose down
To stop and remove the containers and the network they created:
docker-compose down --rmi all
To remove the named volume as well:
docker-compose down --rmi all -v
No comments:
Post a Comment