Docker入门 - 006 Docker 多种数据库的安装
Docker 安装 MySQL
查找Docker Hub上的mysql镜像
root@VM_16_14_centos ~# docker search mysql INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/mysql MySQL is a widely used, open-source relati... 5757 [OK] docker.io docker.io/mariadb MariaDB is a community-developed fork of M... 1837 [OK] docker.io docker.io/mysql/mysql-server Optimized MySQL Server Docker images. Crea... 397 [OK] docker.io docker.io/zabbix/zabbix-server-mysql Zabbix Server with MySQL database support 91 [OK] docker.io docker.io/hypriot/rpi-mysql RPi-compatible Docker Image with Mysql 79 docker.io docker.io/centurylink/mysql Image containing mysql. Optimized to be li... 59 [OK] docker.io docker.io/zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server ... 47 [OK] docker.io docker.io/tutum/mysql Base docker image to run a MySQL database ... 31 docker.io docker.io/1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 29 [OK] docker.io docker.io/centos/mysql-57-centos7 MySQL 5.7 SQL database server 21 docker.io docker.io/mysql/mysql-cluster Experimental MySQL Cluster Docker images. ... 19 docker.io docker.io/schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic back... 16 [OK] docker.io docker.io/linuxserver/mysql A Mysql container, brought to you by Linux... 14 docker.io docker.io/bitnami/mysql Bitnami MySQL Docker Image 13 [OK] docker.io docker.io/zabbix/zabbix-proxy-mysql Zabbix proxy with MySQL database support 11 [OK] docker.io docker.io/centos/mysql-56-centos7 MySQL 5.6 SQL database server 8 docker.io docker.io/openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6 docker.io docker.io/circleci/mysql MySQL is a widely used, open-source relati... 4 docker.io docker.io/dsteinkopf/backup-all-mysql backup all DBs in a mysql server 3 [OK] docker.io docker.io/frodenas/mysql A Docker Image for MySQL 2 [OK] docker.io docker.io/ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 0 [OK] docker.io docker.io/cloudfoundry/cf-mysql-ci Image used in CI of cf-mysql-release 0 docker.io docker.io/cloudposse/mysql Improved `mysql` service with support for ... 0 [OK] docker.io docker.io/inferlink/landmark-mysql landmark-mysql 0 [OK] docker.io docker.io/openzipkin/zipkin-mysql Mirror of https://quay.io/repository/openz... 0
这里我们拉取官方的镜像:
root@VM_16_14_centos ~# docker pull mysql Using default tag: latest Trying to pull repository docker.io/library/mysql ... latest: Pulling from docker.io/library/mysql 2a72cbf407d6: Pull complete 38680a9b47a8: Pull complete 4c732aa0eb1b: Pull complete c5317a34eddd: Pull complete f92be680366c: Pull complete e8ecd8bec5ab: Pull complete 2a650284a6a8: Pull complete 5b5108d08c6d: Pull complete beaff1261757: Pull complete c1a55c6375b5: Pull complete 8181cde51c65: Pull complete Digest: sha256:691c55aabb3c4e3b89b953dd2f022f7ea845e5443954767d321d5f5fa394e28c Status: Downloaded newer image for docker.io/mysql:latest
等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为mysql,标签为latest(最新)的镜像。
使用mysql镜像
运行容器
root@VM_16_14_centos ~# docker run -p 3306:3306 --name mymysql -v $PWD/conf/my.cnf:/etc/my.cnf.d/ -v $PWD/logs:/logs -v $PWD/data:/data -e MYSQL_ROOT_PASSWORD=123456 -d mysql 5645591944b0d8273e9df8a38abba36d64c64d4284f026265d373f6a2a1e8248
命令说明:
-
-p 3306:3306:将容器的3306端口映射到主机的3306端口
- --name mymysql:容器的名字
-
-v $PWD/conf/my.cnf:/etc/my.cnf.d/:将主机当前目录下的conf/my.cnf挂载到容器的/etc/my.cnf.d/
-
-v $PWD/logs:/logs:将主机当前目录下的logs目录挂载到容器的/logs
-
-v $PWD/data:/data:将主机当前目录下的data目录挂载到容器的/data
-
-e MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码
- -d mysql:后台运行容器,并返回容器ID,可以是mysql,也可以是mysql镜像ID
查看容器启动情况
root@VM_16_14_centos ~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5645591944b0 mysql "docker-entrypoint..." 5 minutes ago Up 5 minutes 0.0.0.0:3306->3306/tcp mymysql
Docker 安装 Redis
查找Docker Hub上的redis镜像:
root@VM_16_14_centos ~# docker search redis INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/redis Redis is an open source key-value store th... 4879 [OK] docker.io docker.io/bitnami/redis Bitnami Redis Docker Image 70 [OK] docker.io docker.io/sameersbn/redis 64 [OK] docker.io docker.io/tenstartups/redis-commander 32 [OK] docker.io docker.io/hypriot/rpi-redis Raspberry Pi compatible redis image 30 docker.io docker.io/joshula/redis-sentinel A container for Redis Sentinel 21 docker.io docker.io/kubeguide/redis-master redis-master with "Hello World!" 19 docker.io docker.io/kubeguide/guestbook-redis-slave Guestbook redis slave 16 docker.io docker.io/webhippie/redis Docker images for redis 8 [OK] docker.io docker.io/arm32v7/redis Redis is an open source key-value store th... 5 docker.io docker.io/rtoma/logspout-redis-logstash Logspout including Redis adapter for sendi... 5 docker.io docker.io/oliver006/redis_exporter Prometheus Exporter for Redis Metrics. Su... 3 docker.io docker.io/centos/redis-32-centos7 Redis in-memory data structure store, used... 2 docker.io docker.io/dynomitedb/redis Redis backend for DynomiteDB. 2 [OK] docker.io docker.io/rediscommander/redis-commander Alpine image for redis-commander - Redis m... 2 [OK] docker.io docker.io/tomesar/redis-arm Redis for ARM! 2 [OK] docker.io docker.io/frodenas/redis A Docker Image for Redis 1 [OK] docker.io docker.io/google/guestbook-python-redis A simple guestbook example written in Pyth... 1 docker.io docker.io/kilsoo75/redis-master This image is for the redis master of SK C... 1 docker.io docker.io/tiredofit/redis Redis Server w/ Zabbix monitoring and S6 O... 1 [OK] docker.io docker.io/ajmath/fluentd-redis Use fluentd logs to send docker logs to re... 0 [OK] docker.io docker.io/anchorfree/redis redis cache server for logging 0 docker.io docker.io/brendangibat/docker-logspout-redis Docker Logspout container with Logspout-Re... 0 [OK] docker.io docker.io/iadvize/redis 0 docker.io docker.io/sstarcher/fluent-redis-aws https://github.com/sstarcher/docker-fluent... 0 [OK]
这里我们拉取官方的镜像:
root@VM_16_14_centos ~# docker search redis INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/redis Redis is an open source key-value store th... 4879 [OK] docker.io docker.io/bitnami/redis Bitnami Redis Docker Image 70 [OK] docker.io docker.io/sameersbn/redis 64 [OK] docker.io docker.io/tenstartups/redis-commander 32 [OK] docker.io docker.io/hypriot/rpi-redis Raspberry Pi compatible redis image 30 docker.io docker.io/joshula/redis-sentinel A container for Redis Sentinel 21 docker.io docker.io/kubeguide/redis-master redis-master with "Hello World!" 19 docker.io docker.io/kubeguide/guestbook-redis-slave Guestbook redis slave 16 docker.io docker.io/webhippie/redis Docker images for redis 8 [OK] docker.io docker.io/arm32v7/redis Redis is an open source key-value store th... 5 docker.io docker.io/rtoma/logspout-redis-logstash Logspout including Redis adapter for sendi... 5 docker.io docker.io/oliver006/redis_exporter Prometheus Exporter for Redis Metrics. Su... 3 docker.io docker.io/centos/redis-32-centos7 Redis in-memory data structure store, used... 2 docker.io docker.io/dynomitedb/redis Redis backend for DynomiteDB. 2 [OK] docker.io docker.io/rediscommander/redis-commander Alpine image for redis-commander - Redis m... 2 [OK] docker.io docker.io/tomesar/redis-arm Redis for ARM! 2 [OK] docker.io docker.io/frodenas/redis A Docker Image for Redis 1 [OK] docker.io docker.io/google/guestbook-python-redis A simple guestbook example written in Pyth... 1 docker.io docker.io/kilsoo75/redis-master This image is for the redis master of SK C... 1 docker.io docker.io/tiredofit/redis Redis Server w/ Zabbix monitoring and S6 O... 1 [OK] docker.io docker.io/ajmath/fluentd-redis Use fluentd logs to send docker logs to re... 0 [OK] docker.io docker.io/anchorfree/redis redis cache server for logging 0 docker.io docker.io/brendangibat/docker-logspout-redis Docker Logspout container with Logspout-Re... 0 [OK] docker.io docker.io/iadvize/redis 0 docker.io docker.io/sstarcher/fluent-redis-aws https://github.com/sstarcher/docker-fluent... 0 [OK] root@VM_16_14_centos ~# docker pull redis Using default tag: latest Trying to pull repository docker.io/library/redis ... latest: Pulling from docker.io/library/redis b0568b191983: Pull complete 6637dc5b29fe: Pull complete 7b4314315f15: Pull complete 67b22db27e51: Pull complete 350dbcc91819: Pull complete eee5ee716895: Pull complete Digest: sha256:26c93c5b06eaa323bb1089500f42b0dd158138772348b865e364127f1d554982 Status: Downloaded newer image for docker.io/redis:latest
等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为redis,标签为latest(不选择版本默认最新)的镜像。
使用redis镜像
运行容器
root@VM_16_14_centos /# docker run -p 6379:6379 -v $PWD/data:/data -d redis redis-server --appendonly yes 47884407c8065335b4bf413e28e44e5afe41280e1eb1913ce828cf30bf61bd1b
命令说明:
-p 6379:6379 : 将容器的6379端口映射到主机的6379端口
-v $PWD/data:/data : 将主机中当前目录下的data挂载到容器的/data
redis-server --appendonly yes : 在容器执行redis-server启动命令,并打开redis持久化配置
查看容器启动情况
root@VM_16_14_centos /# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5b8bd6476df7 mysql "docker-entrypoint..." 32 minutes ago Up 32 minutes 0.0.0.0:3306->3306/tcp mymysql
连接、查看容器
使用redis镜像执行redis-cli命令连接到刚启动的容器,主机IP为118.89.65.22
root@VM_16_14_centos /# docker exec -it 47884407c8065335b4bf413e28e44e5afe41280e1eb1913ce828cf30bf61bd1b redis-cli -h 118.89.65.22 118.89.65.22:6379> set abc 123 OK 118.89.65.22:6379> get abc "123" 118.89.65.22:6379> quit root@VM_16_14_centos /#
Docker 安装 MongoDB
查找Docker Hub上的mongo镜像:
root@VM_16_14_centos ~# docker search mongo INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED docker.io docker.io/mongo MongoDB document databases provide high av... 4257 [OK] docker.io docker.io/mongo-express Web-based MongoDB admin interface, written... 226 [OK] docker.io docker.io/tutum/mongodb MongoDB Docker image – listens in port 270... 218 [OK] docker.io docker.io/mvertes/alpine-mongo light MongoDB container 71 [OK] docker.io docker.io/mongoclient/mongoclient Official docker image for Mongoclient, fea... 48 [OK] docker.io docker.io/bitnami/mongodb Bitnami MongoDB Docker Image 30 [OK] docker.io docker.io/frodenas/mongodb A Docker Image for MongoDB 17 [OK] docker.io docker.io/mongooseim/mongooseim Small docker image for MongooseIM - robust... 15 docker.io docker.io/mongooseim/mongooseim-docker MongooseIM server the latest stable version 10 [OK] docker.io docker.io/centos/mongodb-26-centos7 MongoDB NoSQL database server 5 docker.io docker.io/istepanov/mongodump Docker image with mongodump running as a c... 5 [OK] docker.io docker.io/centos/mongodb-32-centos7 MongoDB NoSQL database server 4 docker.io docker.io/eses/mongodb_exporter mongodb exporter for prometheus 4 [OK] docker.io docker.io/khezen/mongo MongoDB Docker image supporting RocksDB st... 4 [OK] docker.io docker.io/cvallance/mongo-k8s-sidecar Kubernetes side car to setup and maintain ... 3 [OK] docker.io docker.io/19hz/mongo-container Mongodb replicaset for coreos 1 [OK] docker.io docker.io/alino/coreos-mongodb-cluster mongo 3.0 1 [OK] docker.io docker.io/circleci/mongo MongoDB document databases provide high av... 1 docker.io docker.io/ekesken/mongo docker image for mongo that is configurabl... 1 [OK] docker.io docker.io/extremeprog/mongo-sites-api mongo-sites-api automated build 1 [OK] docker.io docker.io/openshift/mongodb-24-centos7 DEPRECATED: A Centos7 based MongoDB v2.4 i... 1 docker.io docker.io/gebele/mongodb mongodb 0 [OK] docker.io docker.io/os33/go-mongo go and mongo package for testing 0 docker.io docker.io/quilt/mongo MongoDB container for quilt.io 0 [OK] docker.io docker.io/targetprocess/mongodb_exporter MongoDB exporter for prometheus 0 [OK]
这里我们拉取官方的镜像:
root@VM_16_14_centos ~# docker pull mongo Using default tag: latest Trying to pull repository docker.io/library/mongo ... latest: Pulling from docker.io/library/mongo b0568b191983: Pull complete 1e8b5b4e67a0: Pull complete a87b29dbb553: Pull complete 908c259a6a99: Pull complete 2fdec16e62a3: Pull complete e233c6c476cb: Pull complete 671c302f3b40: Pull complete 207ff3c88601: Pull complete 3a7998bea9fd: Pull complete 18d605d67f26: Pull complete Digest: sha256:d969194a7c4dcd817cae5dc726b1493547a3ad3f1b30f7f58857adc9ae6a4483 Status: Downloaded newer image for docker.io/mongo:latest
等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为mongo,标签为latest(不选择版本默认最新)的镜像。
使用mongo镜像
运行容器
root@VM_16_14_centos ~# docker run -p 27017:27017 -v $PWD/db:/data/db -d mongo fea379da42de9414cda126d51377a72240a95c7b12703db9072bf484f5182901
命令说明:
-p 27017:27017 :将容器的27017 端口映射到主机的27017 端口。
-v $PWD/db:/data/db :将主机中当前目录下的db挂载到容器的/data/db,作为mongo数据存储目录。
查看容器启动情况
root@VM_16_14_centos ~# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fea379da42de mongo "docker-entrypoint..." 12 seconds ago Up 11 seconds 0.0.0.0:27017->27017/tcp gracious_wiles
使用mongo镜像执行mongo 命令连接到刚启动的容器,主机IP为:118.89.65.22
root@VM_16_14_centos ~# docker run -it mongo mongo --host 118.89.65.22 MongoDB shell version v3.6.3 connecting to: mongodb://118.89.65.22:27017/ MongoDB server version: 3.6.3 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user 2018-03-21T09:03:44.305+0000 I STORAGE [main] In File::open(), ::open for '/home/mongodb/.mongorc.js' failed with No such file or directory Server has startup warnings: 2018-03-21T08:55:49.043+0000 I STORAGE [initandlisten] 2018-03-21T08:55:49.043+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2018-03-21T08:55:49.043+0000 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem 2018-03-21T08:55:50.761+0000 I CONTROL [initandlisten] 2018-03-21T08:55:50.761+0000 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2018-03-21T08:55:50.761+0000 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2018-03-21T08:55:50.761+0000 I CONTROL [initandlisten] 2018-03-21T08:55:50.761+0000 I CONTROL [initandlisten] 2018-03-21T08:55:50.761+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2018-03-21T08:55:50.761+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2018-03-21T08:55:50.761+0000 I CONTROL [initandlisten] 2018-03-21T08:55:50.761+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2018-03-21T08:55:50.761+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2018-03-21T08:55:50.761+0000 I CONTROL [initandlisten] >