# 关闭防火墙
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
添加 redis 和 junit 依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>cn.tedugroupId>
<artifactId>dockerartifactId>
<version>0.0.1-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>redis.clientsgroupId>
<artifactId>jedisartifactId>
<version>2.9.0version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
project>
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端口
# 如果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
docker network create --subnet=172.18.0.0/24 dockernet
ifconfig
docker load < mariadb-docker-image.gz
docker volume create mysql-data
docker run -d --name mysql --net dockernet --ip 172.18.0.11 -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 mariadb
docker ps -a
docker inspect mysql
docker exec -it mysql bash
mysql -uroot -p
选择 资料/docker/EasyMall/easymall.sql
docker load < tomcat7-docker-image.gz
docker image ls
docker run -d --name tomcat --net dockernet --ip 172.18.0.12 -v /opt/webapps:/usr/tomcat/webapps -p 8080:8080 tomcat:7