测试环境:centos7.6,docker 18.09.1
首先要确保docker环境已经安装成功

  1. [root@localhost ~]# docker --version
  2. Docker version 18.09.2, build 6247962

Docker 安装 Mysql

  • 安装镜像

直接跳过docker search mysql这一步,我们直接安装官方mysql5.5的镜像

  1. [root@localhost ~]# docker pull mysql:5.5

安装完成查看本地镜像,标签名为5.5

  1. [root@localhost ~]# docker images mysql
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. mysql 5.5 50ba61f07d0c 7 days ago 205MB
  • 运行容器
  1. [root@localhost ~]# docker run -p 3306:3306 -d --name mymysql -v /docker/mysql/data:/usr/share/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.5
  2. 979542f0c201ed229a548a260cd3bf0d8fbafb99ecb3e75e74cdc302358575f6

-p 3306:3306:将容器的 3306 端口映射到主机的 3306 端口。
-v /docker/mysql/data:/usr/share/mysql:将主机当前目录下的data目录挂载到容器的 /var/lib/mysql 。
-e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码。

  • 查看容器运行情况
  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 979542f0c201 mysql:5.5 "docker-entrypoint.sh" 2 minutes ago Up 2 minutes 0.0.0.0:3306->3306/tcp mymysql
  • 使用mysql
  1. [root@localhost ~]# docker exec -it mymysql bash
  2. root@979542f0c201:/# mysql -u root -p
  3. Enter password:
  4. Welcome to the MySQL monitor. Commands end with ; or \g.
  5. Your MySQL connection id is 3
  6. Server version: 5.5.62 MySQL Community Server (GPL)
  7. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. mysql>

Docker 安装 Redis

  • 安装镜像

这里安装redis3.2的官方镜像版本

  1. [root@localhost ~]# docker pull redis:3.2

安装完成查看本地镜像,标签名为3.2

  1. [root@localhost ~]# docker images redis
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. redis 3.2 87856cc39862 4 months ago 76MB
  • 运行容器
  1. [root@localhost ~]# docker run -p 6379:6379 -d --name myredis -v /docker/redis/data:/data redis:3.2 redis-server --appendonly yes
  2. 84631392a9fab37c08976673c75a701687bfe6a3443209db92a19bab8e107b6b

-p 6379:6379 将容器的6379端口映射到主机的6379端口
-v /docker/redis/data:/data: 将主机中当前目录下的data挂载到容器的/data
redis-server —appendonly yes: 在容器执行redis-server启动命令,并打开redis持久化配置

  • 查看容器运行情况
  1. [root@localhost ~]# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 84631392a9fa redis:3.2 "docker-entrypoint.s猞" 16 seconds ago Up 13 seconds 0.0.0.0:6379->6379/tcp myredis
  4. 979542f0c201 mysql:5.5 "docker-entrypoint.s猞" 14 minutes ago Up 14 minutes 0.0.0.0:3306->3306/tcp mymysql
  • 使用redis
  1. [root@localhost ~]# docker exec -it myredis redis-cli
  2. 127.0.0.1:6379> ping
  3. PONG