# 关闭防火墙
systemctl stop firewalld.service
# 禁止防火墙开机启动
systemctl disable firewalld.service
# 启动docker
systemctl start docker
# 重启docker
systemctl restart docker
docker load < redis-docker-image.gz
docker run -d --name redis7000 -p 7000:6379 redis
添加pom.xml文件--添加jedis 和junit测试依赖
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
测试单台redis服务器
package docker;
import org.junit.Test;
import redis.clients.jedis.Jedis;
public class Test1 {
@Test
public void test1() {
Jedis j = new Jedis("192.168.64.150", 7000);
j.set("key1", "value1");
String v = j.get("key1");
System.out.println(v);
j.close();
}
}
# 进入容器
docker exec -it redis7000 bash
# 运行redis客户端工具
redis-cli
# 查询数据
127.0.0.1:6379> keys *
1) "key1"
127.0.0.1:6379> get key1
"value1"
启动三个redis容器,将端口分别映射到7000,7001和7002端口
只需要修改端口以及name的值即可
# 如果7000已经启动,不必重复启动
docker run -d --name redis7000 -p 7000:6379 redis
docker run -d --name redis7001 -p 7001:6379 redis
docker run -d --name redis7002 -p 7002:6379 redis
# 查看容器
docker ps -a
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
756f5227dd64 redis "docker-entrypoint.s…" 6 seconds ago Up 4 seconds 0.0.0.0:7001->6379/tcp redis7001
bc87e35664c8 redis "docker-entrypoint.s…" 13 seconds ago Up 11 seconds 0.0.0.0:7002->6379/tcp redis7002
c3700e3e4c73 redis "docker-entrypoint.s…" 2 hours ago Up 2 hours 0.0.0.0:7000->6379/tcp redis7000
编写测试方法
@Test
public void test2() {
JedisPoolConfig cfg = new JedisPoolConfig();
cfg.setMaxTotal(500);
cfg.setMaxIdle(20);
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo("192.168.64.150", 7000));
shards.add(new JedisShardInfo("192.168.64.150", 7001));
shards.add(new JedisShardInfo("192.168.64.150", 7002));
ShardedJedisPool pool = new ShardedJedisPool(cfg, shards);
ShardedJedis j = pool.getResource();
for (int i = 0; i < 100; i++) {
j.set("key"+i, "value"+i);
}
pool.close();
}
分别进入三个redis容器,执行 keys *
查看结果
docker exec -it redis7000 redis-cli
docker exec -it redis7001 redis-cli
docker exec -it redis7002 redis-cli
# 删除原先创建的虚拟网络(如果有), my-net为虚拟网络名
docker network rm my-net
# 创建新的虚拟网络
docker network create --subnet=172.18.0.0/24 dockernet
docker load < mariadb-docker-image.gz
docker volume create mysql-data
docker run -d --rm --name mysql -v mysql-data:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root --net dockernet --ip 172.18.0.11 mariadb
docker exec -it mysql bash
mysql -uroot -p
docker ps -a
docker inspect mysql
进入容器,登录mysql测试
# 进入容器
docker exec -it myql bash
# 登录MySQL
mysql -u root -p
root
连接数据库工具,导入数据
# 我是加载的本地的, 如果本地没有 可使用 docker pull tomcat 进行下载
docker load < tomcat7-docker-image.gz
docker image ls
# 或者
docker images
原文:https://www.cnblogs.com/zpKang/p/13673609.html