Article directory
- Preface
- mysql
-
- Pull image
- Run container
- redis
-
- Pull image
- Run container
- mongodb
-
- Pull image
- Run container
- neo4j
-
- Pull image
- Run container
- docker-compose
-
- General configuration
- Use .env files for environment variable configuration
-
- .env file case
- docker-compose.yml file case
- Bug description
- Summarize
Foreword
In the world of software development, the database is an indispensable part, and how to deploy and manage the database efficiently and quickly is a problem that every developer and operation and maintenance personnel must face. Docker, as a lightweight containerization solution, provides a simple yet powerful answer to this problem. This article will show you how to use Docker to easily deploy and manage popular databases such as MySQL, Redis, MongoDB, and Neo4J through practical commands and code examples.
There is no need for cumbersome configuration and installation. With just a few simple commands, you can have a database environment that is stable and easy to maintain. Whether you’re new to Docker or an experienced user, this article will provide you with practical, actionable steps right away.
mysql
Pull the image
docker pull mysql:latest
Run container
docker run --name mysql-container \ -e MYSQL_ROOT_PASSWORD=my-secret-pw \ -v $HOME/mysql-data:/var/lib/mysql \ -p 3306:3306 \ -d \ mysql
In this command:
- –name mysql-container: Name the container mysql-container.
- -v /my/own/datadir:/var/lib/mysql: Map /my/own/datadir on the host to /var/lib/mysql in the container.
- -e MYSQL_ROOT_PASSWORD=my-secret-pw: Set the MySQL root password to my-secret-pw.
- -p 3306:3306: Maps port 3306 in the container to port 3306 on the host.
- -d mysql:latest: Specifies that the container should use the latest MySQL image.
redis
Pull the image
docker pull redis:latest
Run container
docker run --name redis-container \ -p 6379:6379 \ -v $HOME/redis-data:/data \ -d \ redis
In this command:
- –name my-redis-container: Name the container my-redis-container.
- -p 6379:6379: Map port 6379 in the container to the host.
- -v $HOME/redis-data:/data: Mount the $HOME/redis-data directory on the host to the /data directory in the container.
- redis redis-server –appendonly yes: Run the Redis server in append mode for persistence.
mongodb
Pull the image
docker pull mongo:latest
Run container
docker run \ --name mongo-container \ -v $HOME/mongo-data:/data/db \ -p 27017:27017 \ -d \ mongo
In this command:
- –name mongo-container: Name the container mongo-container.
- -v /my/own/mongodir:/data/db: Map /my/own/mongodir on the host to /data/db in the container.
- -p 27017:27017: Maps port 27017 in the container to port 27017 on the host.
- -d mongo:latest: Specifies that the container should use the latest MongoDB image.
neo4j
Pull the image
docker pull neo4j:latest
Run container
docker run \ --name my-neo4j-container \ -p7474:7474 -p7687:7687 \ -v $HOME/neo4j/data:/data \ -v $HOME/neo4j/logs:/logs \ -d \ -e NEO4J_AUTH=neo4j/password \ neo4j
In this command:
- –name my-neo4j-container: Name the container my-neo4j-container.
- -p 7474:7474 -p 7687:7687: Map port 7474 (HTTP) and port 7687 (Bolt) in the container to the host.
- -v $HOME/neo4j/data:/data: Mount the $HOME/neo4j/data directory in the host to the /data directory in the container.
- -v $HOME/neo4j/logs:/logs: Mount the $HOME/neo4j/logs directory in the host to the /logs directory in the container.
- -e NEO4J_AUTH=neo4j/password: Set the Neo4j username to neo4j and the password to password.
docker-compose
General configuration
Using Docker Compose makes it easier and more manageable to run multiple services such as MySQL, Redis, MongoDB, and Neo4j. Below is a sample docker-compose.yml file that defines these four services and connects them to a custom network so that the services communicate using the custom network.
Here is an example docker-compose.yml file:
version: '3' services: mysql: image: mysql:latest environment: MYSQL_ROOT_PASSWORD: rootpassword ports: - "3306:3306" volumes: - ./mysql-data:/var/lib/mysql networks: - my_network redis: image: redis:latest ports: - "6379:6379" volumes: - ./redis-data:/data command: redis-server --appendonly yes networks: - my_network mongo: image: mongo:latest ports: - "27017:27017" volumes: - ./mongo-data:/data/db networks: -my_network neo4j: image: neo4j:latest ports: - "7474:7474" - "7687:7687" environment: NEO4J_AUTH: neo4j/password volumes: - ./neo4j-data:/data - ./neo4j-logs:/logs networks: -my_network networks: my_network: driver:bridge
Command: redis-server –appendonly The yes line specifies the command to be run when the Redis container starts in the Redis service definition of the docker-compose.yml file.
- redis-server: This is the command to start the Redis server.
- –appendonly yes: This flag enables “append-only file” (AOF) persistence mode in Redis.
Use .env files to configure environment variables
docker-compose.yml files and .env files are often used together in Docker Compose to define and run containers in a more flexible and configurable way. Here are some key points about their relationship:
- .env file
- The env file is usually located in the same directory as the docker-compose.yml file.
- It contains key-value pairs that define environment variables, such as MYSQL_ROOT_PASSWORD=root_password.
- These environment variables are available in the docker-compose.yml file.
- docker-compose.yml file
- The docker-compose.yml file defines the application’s services, networks, and volumes.
- In this file, you can use the ${VARIABLE_NAME} syntax to reference environment variables defined in the .env file.
- The relationship between the two files
- When running docker-compose up or other Docker Compose commands, Docker Compose automatically looks for .env files in the same directory.
- Docker Compose reads the .env file and replaces the corresponding variables in the docker-compose.yml file with the values defined in it.
- This enables adjustments to settings by simply changing the .env file without modifying the docker-compose.yml file.
.env file case
#MySQL MYSQL_ROOT_PASSWORD=root_password MYSQL_DATABASE=mydatabase MYSQL_USER=myuser MYSQL_PASSWORD=mypassword #Redis REDIS_PASSWORD=redis_password #MongoDB MONGO_INITDB_ROOT_USERNAME=root MONGO_INITDB_ROOT_PASSWORD=root_password #Neo4J NEO4J_AUTH=neo4j/neo4j_password
docker-compose.yml file case
version: '3' services: mysql: image: mysql:latest environment: MYSQL_ROOT_PASSWORD: ${<!-- -->MYSQL_ROOT_PASSWORD} MYSQL_DATABASE: ${<!-- -->MYSQL_DATABASE} MYSQL_USER: ${<!-- -->MYSQL_USER} MYSQL_PASSWORD: ${<!-- -->MYSQL_PASSWORD} ports: - "3306:3306" volumes: - mysql_data:/var/lib/mysql networks: - my_custom_network redis: image: redis:latest environment: REDIS_PASSWORD: ${<!-- -->REDIS_PASSWORD} ports: - "6379:6379" command: redis-server --requirepass ${<!-- -->REDIS_PASSWORD} volumes: - redis_data:/data networks: - my_custom_network mongo: image: mongo:latest environment: MONGO_INITDB_ROOT_USERNAME: ${<!-- -->MONGO_INITDB_ROOT_USERNAME} MONGO_INITDB_ROOT_PASSWORD: ${<!-- -->MONGO_INITDB_ROOT_PASSWORD} ports: - "27017:27017" volumes: - mongo_data:/data/db networks: - my_custom_network neo4j: image: neo4j:latest environment: NEO4J_AUTH: ${<!-- -->NEO4J_AUTH} ports: - "7474:7474" - "7687:7687" volumes: - neo4j_data:/data - neo4j_logs:/logs networks: - my_custom_network networks: my_custom_network: driver:bridge volumes: mysql_data: redis_data: mongo_data: neo4j_data: neo4j_logs:
Command: redis-server –requirepass ${REDIS_PASSWORD}
- redis-server: This is the command to start the Redis server.
- –requirepass: This is an option to specify the password required to access the Redis server.
- ${REDIS_PASSWORD}: This is an environment variable whose value should be set in the .env file or Docker Compose command line. The value of this environment variable will be used as an argument to the –requirepass option, which is the password required by the Redis server.
Bug description
When creating a mysql container, if the connection fails, it is best to use anonymous data volumes. Taking the docker-compose model as an example, perform up -d and down twice, and add external: true under the anonymous data volume the second time. At the same time, connecting with a docker container connected to the same network card is equivalent to initializing the anonymous data volume. This method can solve most connection failure problems, but the specific reason is unknown.
Summary
Through this article, you have learned how to use Docker to quickly deploy and manage multiple databases. The commands and code examples are not only easy to execute, but also highly reusable. Docker’s flexibility and convenience mean you can apply this knowledge to a variety of different projects and environments, greatly increasing your productivity.
Hopefully this article will help you better understand the usefulness of Docker for database management and encourage you to try and apply these methods in your future work.