关闭防火墙
# 关闭防火墙
systemctl stop firewalld.service
# 禁止防火墙开机启动
systemctl disable firewalld.service
启动或重启docker
# 启动docker
systemctl start docker
# 重启docker
systemctl restart docker
redis
加载镜像
docker load < redis-docker-image.gz
启动容器
docker run -d --name redis7000 -p 7000:6379 redis
jedis 连接测试
新建测试项目
pom.xml
添加 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.0</modelVersion>
<groupId>cn.tedu</groupId>
<artifactId>docker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<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>
</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 容器
启动三个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
jedis 分片测试
编写测试方法
@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虚拟网络
docker network create --subnet=172.18.0.0/24 dockernet
ifconfig
mysql
加载 mariadb 镜像
docker load < mariadb-docker-image.gz
创建数据卷
docker volume create mysql-data
启动 mariadb 容器,并挂载数据卷
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
进入容器,登录mysql测试
docker exec -it mysql bash
mysql -uroot -p
连接数据库,导入数据
选择 资料/docker/EasyMall/easymall.sql
tomcat
加载 tomcat 镜像
docker load < tomcat7-docker-image.gz
docker image ls
启动 tomcat 容器
docker run -d --name tomcat --net dockernet --ip 172.18.0.12 -v /opt/webapps:/usr/tomcat/webapps -p 8080:8080 tomcat:7
访问测试
Q.E.D.