Docker Tutorial
Docker Installation
How to use Docker
Docker Instance
Docker Container Lifecycle Command
Docker Container Operation Command
Docker Container rootfs Command
Docker Mirror Repository Command
Docker Local Image Management Command
Docker info|version Command
In this tutorial, you'll learn how to create a Docker container running MySQL, a popular relational database management system. We'll use the official MySQL image provided by the Docker community.
Prerequisites:
Pull the official MySQL image:
To pull the official MySQL image, run the following command:
docker pull mysql
This will download the latest version of the MySQL image to your system.
Create a new container with MySQL:
To create a new Docker container running MySQL, use the docker run
command. We'll map the container's port 3306 (the default MySQL port) to the host's port 3306, so you can access the MySQL server from your host machine. Additionally, you'll need to set the MYSQL_ROOT_PASSWORD
environment variable for the MySQL root user:
docker run --name mysql-container -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql
Replace my-secret-pw
with a secure password for the MySQL root user.
This command creates a new container named mysql-container
, maps the container's port 3306 to the host's port 3306, sets the root password, and runs the container in detached mode (-d
flag). The container will run the MySQL server using the default configuration.
Verify the MySQL container is running:
To check if the MySQL container is running, use the docker ps
command:
docker ps
You should see your mysql-container
listed as running.
Connect to the MySQL server:
You can connect to the MySQL server running inside the container using a MySQL client or driver. If you have the MySQL command line client (mysql
) installed on your host machine, you can connect to the server using:
mysql -h localhost -P 3306 -u root -p
Enter the root password you set earlier when prompted.
This command connects to the MySQL server running at localhost:3306
, which is the mapped port of the mysql-container
.
Customize the MySQL configuration (Optional):
If you want to customize the MySQL configuration, you can create a local configuration file (e.g., my.cnf
) and mount it as a volume inside the container.
First, create a new directory for your configuration files:
mkdir mysql-config
Next, create a new my.cnf
file in the mysql-config
directory and customize it as needed. You can use the default my.cnf
file from the MySQL image as a starting point.
After customizing the configuration file, stop and remove the existing mysql-container
:
docker stop mysql-container docker rm mysql-container
Finally, create a new container with the customized configuration, mounting the local mysql-config
directory as a volume inside the container:
docker run --name custom-mysql-container -p 3306:3306 -v $(pwd)/mysql-config:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql
Replace my-secret-pw
with the password you set for the MySQL root user.
This command creates a new container named custom-mysql-container
, maps the container's port 3306 to the host's port 3306, mounts the local mysql-config
directory as a volume at /etc/mysql/conf.d
inside the container, sets the root password, and runs the container in detached mode.
Running MySQL in Docker Container:
docker run -d -p 3306:3306 --name my-mysql-container -e MYSQL_ROOT_PASSWORD=my-secret-pw mysql:latest
How to Create a Docker Image with MySQL:
FROM mysql:latest # Add custom configurations or scripts
Docker-Compose MySQL Installation:
version: '3' services: mysql: image: mysql:latest ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: my-secret-pw
MySQL Dockerfile Example:
FROM mysql:latest COPY my.cnf /etc/mysql/my.cnf
Setting Up MySQL in a Docker Environment:
version: '3' services: mysql: image: mysql:latest ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: my-secret-pw MYSQL_DATABASE: mydb MYSQL_USER: myuser MYSQL_PASSWORD: myuserpassword
Configuring MySQL in Docker-Compose:
version: '3' services: mysql: image: mysql:latest ports: - "3306:3306" volumes: - ./my.cnf:/etc/mysql/my.cnf
Docker MySQL Image Customization:
FROM mysql:latest COPY my-scripts /scripts RUN chmod +x /scripts/setup.sh
Docker MySQL Volume Mounts:
version: '3' services: mysql: image: mysql:latest volumes: - ./mysql-data:/var/lib/mysql
Custom MySQL Image Creation in Docker:
FROM mysql:latest COPY my-scripts /scripts RUN chmod +x /scripts/setup.sh
Docker Swarm and MySQL Service:
version: '3' services: mysql: image: mysql:latest deploy: replicas: 3 # Other Swarm deployment configurations
Troubleshooting MySQL in Docker:
docker logs <mysql_container_id_or_name>