目录
前置:(41条消息) 【docker】容器应用_每天都要有成长的博客-CSDN博客
基于docker安装jdk
# 这里拉取的jdk镜像 主要是为了后面打包springboot应用镜像
# 搜索镜像
docker search jdk
# 拉取镜像
docker pull java
Using default tag: latest
latest: Pulling from library/java
5040bd298390: Pull complete
fce5728aad85: Pull complete
76610ec20bf5: Pull complete
60170fec2151: Pull complete
e98f73de8f0d: Pull complete
11f7af24ed9c: Pull complete
49e2d6393f32: Pull complete
bb9cdec9c7f3: Pull complete
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
Status: Downloaded newer image for java:latest
# 验证下jdk 镜像是否拉取成功
docker images
基于docker安装nacos
yum install -y docker
docker search nacos
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nacos/nacos-server This project contains a Docker image meant t… 295 [OK]
zhusaidong/nacos-server-m1 Nacos Server for Apple MacOS M1 8
nacos/nacos-mysql-master nacos-mysql-master 6
docker pull nacos/nacos-server
Using default tag: latest
latest: Pulling from nacos/nacos-server
2d473b07cdd5: Pull complete
fba7fe11b144: Pull complete
4f94aa2ac436: Pull complete
6edcaff5b244: Pull complete
250947560fba: Pull complete
1c8ba434fa9d: Pull complete
7782d9e429a6: Pull complete
0420ed3501eb: Pull complete
a93f4cb07596: Pull complete
4f4fb700ef54: Pull complete
Digest: sha256:21743e0ecae6b5b72890467f4b9d34a1cc73951d01073c63fa1af844fc07aa35
Status: Downloaded newer image for nacos/nacos-server:latest
systemctl stop docker
systemctl start docker
systemctl stop firewalld
systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
mkdir -p /root/nacos/init.d /root/nacos/logs
touch /root/nacos/init.d/custom.properties
#在文件中写入以下配置
management.endpoints.web.exposure.include=*
# 创建容器并启动
docker run -d -p 8848:8848 -e MODE=standalone -e PREFER_HOST_MODE=hostname -v /root/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties -v /root/nacos/logs:/home/nacos/logs --restart always --name nacos nacos/nacos-server
# 如果启动遇到问题,重启docker后再执行
systemctl restart docker
docker start nacos
访问http://localhost:8848/nacos/ 账号默认nacos、密码默认nacos
单机模式nacos默认使用嵌入式数据库来实现数据的存储,若想要使用外部mysql存储nacos数据,单独配置(或者需要给nacos配置多数据源)
docker run -d -p 8848:8848 \
-p 9848:9848 \
-p 9849:9849 \
-e MODE=standalone \
-e PREFER_HOST_MODE=hostname \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=192.168.208.80 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_DB_NAME=nacosconfig \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=123456 \
-e MYSQL_DATABASE_NUM=1 \
-v /root/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties \
-v /root/nacos/logs:/home/nacos/logs \
--restart=always --name nacos nacos/nacos-server:2.0.3
基于docker安装Redis
docker pull redis
mkdir -p /data/redis
docker run --restart=always --log-opt max-size=100m --log-opt max-file=2 -p 6379:6379 --name redis -v /data/redis/redis.conf:/etc/redis/redis.conf -v /data/redis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes
# 配置项说明
–restart=always 总是开机启动
–log是日志方面的
-p 6379:6379 将6379端口挂载出去
–name 给这个容器取一个名字
-v 数据卷挂载
/data/redis/redis.conf:/etc/redis/redis.conf 这里是将 liunx 路径下的myredis.conf 和redis下的redis.conf 挂载在一起。
/data/redis/data:/data
-d redis 表示后台启动redis
redis-server /etc/redis/redis.conf 以配置文件启动redis,加载容器内的conf文件,最终找到的是挂载的目录 /etc/redis/redis.conf 也就是liunx下的/data/redis/redis.conf
–appendonly yes 开启redis 持久化max-size:指定日志文件大小上限
max-file:指定日志文件个数
# 配置项redis.conf
protected-mode no
port 6379
tcp-backlog 511
requirepass 123456
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-max-len 128
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
基于docker 安装mongo
docker pull mongo:latest
docker run -itd --name mongo -p 27017:27017 mongo --auth
$ docker exec -it mongo mongo admin
# 创建一个名为 admin,密码为 123456 的用户。
> db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});
# 尝试使用上面创建的用户信息进行连接。
> db.auth('admin', '123456')
基于docker安装MySQL
mkdir -p /mnt/mysql/data /etc/mysql/conf
touch /etc/mysql/conf/my.cnf
docker pull mysql
docker run --restart=always --name=mysql -p 3306:3306 \
-v /etc/mysql/conf/my.cnf:/etc/my.cnf \
-v /mnt/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql \
--lower_case_table_names=1
注:8.0 的版本不能在my.cnf中配置忽略大小写,容器初始化的时候,增加配置
如果数据库中有数据,需要将数据导出sql.删除容器挂在文件后,执行docker run ,再导入数据。
原有的数据文件不删除,docker run 依然会有错误
docker exec -it mysql /bin/bash
mysql -uroot -p123456
alter user 'root'@'%' identified by '123456' password expire never;
alter user 'root'@'%' identified with mysql_native_password by '123456';
flush privileges;
基于docker 安装neo4j
docker run --name=neo4j \
--volume=/opt/neo4j/data:/data:rw \
--volume=/opt/neo4j/conf:/conf:rw \
--volume=/opt/neo4j/logs:/logs:rw \
--volume=/opt/neo4j/plugins:/plugins:rw \
-p 7474:7474 -p 7687:7687 \
--restart=always \
neo4j:3.5.1
docker run -d --name container_name \ //-d表示容器后台运行 --name指定容器名字
-p 7474:7474 -p 7687:7687 \ //映射容器的端口号到宿主机的端口号
-v /home/neo4j/data:/data \ //把容器内的数据目录挂载到宿主机的对应目录下
-v /home/neo4j/logs:/logs \ //挂载日志目录
-v /home/neo4j/conf:/var/lib/neo4j/conf //挂载配置目录
-v /home/neo4j/import:/var/lib/neo4j/import \ //挂载数据导入目录
--env NEO4J_AUTH=neo4j/password \ //设定数据库的名字的访问密码
neo4(:版本号) //指定使用的镜像版本,括号内版本号可省略
http://localhost:7474/browser/
基于docker 安装elasticsearch
sudo docker pull elasticsearch:7.12.0
sudo mkdir -p /opt/elasticsearch/config
sudo mkdir -p /opt/elasticsearch/data
sudo mkdir -p /opt/elasticsearch/plugins
echo "http.host: 0.0.0.0" >> /opt/elasticsearch/config/elasticsearch.yml
sudo chmod -R 777 /opt/elasticsearch/
sudo docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms84m -Xmx512m" \
-v /opt/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /opt/elasticsearch/data:/usr/share/elasticsearch/data \
-v /opt/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-d elasticsearch:7.12.0
docker ps 查看是否启动
docker logs elasticsearch 启动日志查询
docker restart elasticsearch 重启
docker exec -it elasticsearch bash 进入
基于docker 安装kibana
docker pull kibana:7.7.0
#ELASTICSEARCH_HOSTS为启动的ES服务器
docker run --name kibana \
-p 5601:5601 \
-e ELASTICSEARCH_HOSTS=http://192.168.208.80:9200 \
-d kibana:7.7.0
curl http://ip:5601/
离线安装docker
Index of linux/static/stable/x86_64/ (docker.com)
tar -zxvf docker-26.1.0.tgz
sudo cp docker/* /usr/bin/
# 在后台运行
sudo nohup dockerd &
docker run hello-world
Releases · docker/compose (github.com)
docker 离线安装镜像
docker save -o mysql.tar mysql:latest
docker load -i mysql.tar
docker run --restart=always --name=mysql -p 3306:3306 \
-v /data/mysql/conf:/etc/mysql \
-v /data/mysql/data:/var/lib/mysql \
-v /data/mysql/log:/var/log/mysql \
-v /etc/localtime:/etc/localtime:ro \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:5.7.22 \
--lower_case_table_names=1
--skip-name-resolve
服务正常启动后,访问问题汇总
systemctl stop firewalld
sysctl net.ipv4.ip_forward
# 显示net.ipv4.ip_forward=0则表示未打开
service iptables stop
# 关于未转发,也可直接重启
service docker restart