结合Docker从零搭建服务器Java开发环境及开发工具(持续更新系列)

1.购买服务器,设置密码登录,开放Linux远程SSH访问端口22

2.开启防火墙

后面需要端口的服务(两层服务器开放端口的同时服务器平台的安全规则也要开放)
# 防火墙启动
systemctl start firewalld       ##启动Firewall
systemctl enable firewalld.service  ##设置开机自启动
firewall-cmd --state                           ##查看防火墙状态,是否是running
# 开启需要的端口(自行定义):SSH 22 ,https 443 ,tomcat 8080,nginx 8081,mysql 3306 ,redis 6379 
firewall-cmd --add-port=80/tcp --permanent     ##永久添加80端口 -----------重点
firewall-cmd --add-port=80-81/tcp --permanent # 一次最多两个
firewall-cmd --reload   # 重新加载上开放的端口生效
chown -R 1000:1000 /home/docker

3.本地安装

1.JDK1.8

# 安装目录
mkdir -p /usr/local/java
# 下载jdk1.8
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/jdk-8u141-linux-x64.tar.gz
# ls 查看目录
[root@VM-8-15-centos java]# ls
jdk-8u191-linux-x64.tar.gz
# 解压
[root@VM-8-15-centos java]# tar -xzvf jdk-8u141-linux-x64.tar.gz
[root@VM-8-15-centos java]# ls
jdk1.8.0_141  jdk-8u141-linux-x64.tar.gz
# 配置环境变量
vim /etc/profile 
内容(放置最后即可)【
    export  JAVA_HOME=/usr/local/java/jdk1.8.0_141
    export  CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
    export  PATH=$PATH:$JAVA_HOME/bin
】
# 追加后,保存退出,然后立即刷新环境变量,使修改立即生效:
[root@VM-8-15-centos java]# source /etc/profile
[root@VM-8-15-centos java]#
[root@VM-8-15-centos java]# java -version
java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)

# 安装成功

2.Maven

# 安装wegt
yum -y install wget
# 下载安装maven
wget https://archive.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz --no-check-certificate
# 解压
[root@VM-8-15-centos maven]# tar -zxvf apache-maven-3.6.3-bin.tar.gz
[root@VM-8-15-centos maven]# ls
apache-maven-3.6.3  apache-maven-3.6.3-bin.tar.gz
# 配置本地仓库和阿里云镜像
[root@VM-8-15-centos maven]# ls
apache-maven-3.6.3  apache-maven-3.6.3-bin.tar.gz
[root@VM-8-15-centos maven]# cd apache-maven-3.6.3
[root@VM-8-15-centos apache-maven-3.6.3]# ls
bin  boot  conf  lib  LICENSE  NOTICE  README.txt
[root@VM-8-15-centos apache-maven-3.6.3]# mkdir repo
[root@VM-8-15-centos apache-maven-3.6.3]# ls
bin  boot  conf  lib  LICENSE  NOTICE  README.txt  repo
[root@VM-8-15-centos apache-maven-3.6.3]# vim conf/settings.xml
配置本地仓库内容【
<localRepository>/usr/local/maven/apache-maven-3.6.3/repo</localRepository>
】
配置阿里云镜像内容【
<mirror>
    <id>alimaven</id>
    <name>aliyun maven</name>
    <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    <mirrorOf>central</mirrorOf>
</mirror>
】
:wq
退出
# 配置环境变量(注意路径)
[root@VM-8-15-centos java]# vim /etc/profile
内容【export MAVEN_HOME=/usr/local/maven/apache-maven-3.6.3
export PATH=$MAVEN_HOME/bin:$PATH# 追加后,保存退出,然后立即刷新环境变量,使修改立即生效:
[root@VM-8-15-centos java]# source /etc/profile
[root@VM-8-15-centos apache-maven-3.6.3]# mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/maven/apache-maven-3.6.3
Java version: 1.8.0_141, vendor: Oracle Corporation, runtime: /usr/local/java/jdk1.8.0_141/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.11.1.el7.x86_64", arch: "amd64", family: "unix"
# 安装成功

3.安装Docker

​ 1. yum包更新到最新

yum update

​ 2. 安装需要的软件包,yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的

yum install -y yum-utils device-mapper-persistent-data lvm2

​ 3. 设置yum源

yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

​ 4. 安装docker,出现输入的界面都按 y

yum install -y docker-ce

​ 5. 查看docker版本,验证是否成功

[root@VM-8-15-centos docker]# docker -v
Docker version 20.10.5, build 55c4c88

4.安装Git

# 安装git
[root@VM-8-15-centos usr]# yum -y install git
[root@VM-8-15-centos usr]# git version
git version 1.8.3.1

4.Docker搭建相关的环境

1.创建Docker目录-“–restart=always参数能够使我们在重启docker时,自动启动相关容器

mkdir -p /home/docker
# 赋权限
chown -R 1000:1000 /home/docker
# 解决版本:
# 重启docker:
systemctl restart docker
然后执行:
iptables-save
### 

2.Docker搭建tomcat8-8080

# 拉取镜像
docker pull tomcat:8
# 查看镜像
[root@VM-8-15-centos docker]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
tomcat       8         036850cd208d   11 days ago   474MB
# 运行镜像,获取镜像中的webapps等相关目录与本地进行挂载

docker run --name tomcat8-8080 -d -p 8080:8080 tomcat:8

[root@VM-8-15-centos tomcat]# docker ps -a
CONTAINER ID   IMAGE      COMMAND             CREATED              STATUS              PORTS                    NAMES
0d04555ac207   tomcat:8   "catalina.sh run"   About a minute ago   Up About a minute   0.0.0.0:8080->8080/tcp   tomcat8-8080
# 进入容器
[root@VM-8-15-centos tomcat]# docker exec -it tomcat8-8080 /bin/bash
root@0d04555ac207:/usr/local/tomcat#
root@0d04555ac207:/usr/local/tomcat# ls
bin           conf             lib      logs            NOTICE     RELEASE-NOTES  temp     webapps.dist
BUILDING.txt  CONTRIBUTING.md  LICENSE  native-jni-lib  README.md  RUNNING.txt    webapps  work
root@0d04555ac207:/usr/local/tomcat#
# 此时访问8080端口内容会出现404,webapps内容为空导致,而先挂载的话则会出现无法替换,则使用webapps.dist文件替换即可,所以先拷贝再重新挂载
rm -rf webapps
root@0d04555ac207:/usr/local/tomcat# ls
bin           conf             lib      logs            NOTICE     RELEASE-NOTES  temp          work
BUILDING.txt  CONTRIBUTING.md  LICENSE  native-jni-lib  README.md  RUNNING.txt    webapps.dist
root@0d04555ac207:/usr/local/tomcat# rm -rf webapps
cp: -r not specified; omitting directory 'webapps.dist'
root@0d04555ac207:/usr/local/tomcat# cp webapps.dist -r  webapps
root@0d04555ac207:/usr/local/tomcat# ls
bin           conf             lib      logs            NOTICE     RELEASE-NOTES  temp     webapps.dist
BUILDING.txt  CONTRIBUTING.md  LICENSE  native-jni-lib  README.md  RUNNING.txt    webapps  work
root@0d04555ac207:/usr/local/tomcat# exit
# 访问8080端口内容成功
# ** 挂载tomcat相关的文件
# tomcat默认访问webapps下的ROOT目录文件
1. /usr/local/tomcat/webapps/ROOT
# 配置文件
2. /usr/local/tomcat/conf
# 日志文件
3. /usr/local/tomcat/logs
# 拷贝文件-本地文件夹
docker cp tomcat8-8080:/usr/local/tomcat/webapps/ROOT /home/docker/tomcat/webapps/ROOT
docker cp tomcat8-8080:/usr/local/tomcat/conf /home/docker/tomcat
docker cp tomcat8-8080:/usr/local/tomcat/logs /home/docker/tomcat
[root@VM-8-15-centos tomcat]# ls
conf  logs  ROOT
# 删除容器,重新挂载
[root@VM-8-15-centos ROOT]# docker stop tomcat8-8080
tomcat8-8080
[root@VM-8-15-centos ROOT]# docker rm tomcat8-8080
tomcat8-8080
# 挂载运行
	docker run --restart=always -d \
	-v /home/docker/tomcat/webapps/ROOT:/usr/local/tomcat/webapps/ROOT \
	-v /home/docker/tomcat/conf:/usr/local/tomcat/conf \
	-v /home/docker/tomcat/logs:/usr/local/tomcat/logs \
	-e TZ="Asia/Shanghai" \
	--privileged=true \
	--name tomcat8-8080 \
	-p 8080:8080 \
	tomcat:8
# 创建测试资源
[root@VM-8-15-centos ROOT]# vim test.html
[root@VM-8-15-centos ROOT]# ls
asf-logo-wide.svg  bg-middle.png  bg-upper.png  index.jsp          test.html   tomcat.svg
bg-button.png      bg-nav.png     favicon.ico   RELEASE-NOTES.txt  tomcat.css  WEB-INF
# curl 访问资源
[root@VM-8-15-centos ROOT]# curl 127.0.0.1:8080/test.html
Tomcat8
# docker运行成功

3.Docker挂载搭建-Nginx-8081

先运行获取挂载文件再重新挂载文件目录

# 拉取镜像直接运行先
docker pull nginx:latest

[root@VM-8-15-centos ROOT]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    88736fe82739   3 days ago    142MB

docker run -d -p 8081:80 --name nginx-8081 nginx:latest
 
# 创建挂载的目录  /home/docker/nginx
mkdir -p /home/docker/nginx
# 进入容器-查看容器目录
[root@VM-8-15-centos nginx]# docker exec -it nginx-8081 /bin/bash
root@ddb2e0fa28c5:/#
root@ddb2e0fa28c5:/# ls
bin   dev                  docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc                   lib   media  opt  root  sbin  sys  usr
# ** 需要拷贝目录
# 配置文件目录
1. /etc/nginx
# 访问目录
2. /usr/share/nginx/data
# 拷贝镜像内容
docker cp nginx-8081:/etc/nginx /home/docker/nginx
docker cp nginx-8081:/usr/share/nginx/html /home/docker/nginx/data
[root@VM-8-15-centos nginx]# ls
nginx
[root@VM-8-15-centos nginx]# cd nginx
[root@VM-8-15-centos nginx]# ls
conf.d  fastcgi_params  mime.types  modules  nginx.conf  scgi_params  uwsgi_params
# 修改配置文件中的访问目录可在这里配置-默认访问index文件
vim conf.d/default.conf
# 删除镜像
[root@VM-8-15-centos nginx]# docker stop nginx-8081
nginx-8081
[root@VM-8-15-centos nginx]# docker rm nginx-8081
nginx-8081
# Docker运行
docker run --restart=always -d -p 8081:80 --name nginx-8081 \
-v /home/docker/nginx/nginx:/etc/nginx \
-v /home/docker/nginx/data:/usr/share/nginx/html \
nginx:latest
# 运行成功
[root@VM-8-15-centos data]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                    NAMES
9d1ab1d5ec20   nginx:latest   "/docker-entrypoint.…"   6 seconds ago    Up 5 seconds    0.0.0.0:8081->80/tcp     nginx-8081
# curl命令,像浏览器访问一样
[root@VM-8-15-centos data]# curl 127.0.0.1:8081
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>


在这里插入图片描述

4.Docker搭建MySQL

# 创建数据库挂载的文件夹
[root@VM-8-15-centos docker]# mkdir -p /home/docker/mysql/conf
[root@VM-8-15-centos docker]# mkdir -p /home/docker/mysql/data
[root@VM-8-15-centos docker]# mkdir -p /home/docker/mysql/logs
[root@VM-8-15-centos docker]# ls
mysql  nginx  tomcat
[root@VM-8-15-centos docker]# cd mysql
[root@VM-8-15-centos mysql]# ls
conf  data  logs
[root@VM-8-15-centos mysql]#
# 赋权限
chown -R 1000:1000 /home/docker/mysql
# 拉取镜像运行
docker pull mysql:5.7
# 要加权限
sudo docker run --privileged=true \
-p 3306:3306 \
 --restart=always \
--name mysql5.7-3306 \
-v /home/docker/mysql/logs:/var/log/mysql \
-v /home/docker/mysql/data:/var/lib/mysql \
-v /home/docker/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=root  \
-d mysql:5.7
#
[root@VM-8-15-centos conf]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                               NAMES
341a0b52e3d0   mysql:5.7      "docker-entrypoint.s…"   9 seconds ago   Up 8 seconds   0.0.0.0:3306->3306/tcp, 33060/tcp   mysql5.7-3306

######  进入容器
docker exec -it mysql5.7-3306 /bin/bash
# 账号 root 密码 root
bash-4.2# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.40 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
##  修改密码并赋予远程访问权限
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
# 设置远程访问密码
Database changed
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mysql!@#root' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
# 刷新权限
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
# 退出
mysql> exit
Bye

# 数据即可连接成功
ip
端口 3306
账号 root
#本地密码
密码 root
# 

在这里插入图片描述

5.Docker 搭建Redis

# 创建挂载的文件夹
[root@VM-8-15-centos docker]# mkdir -p /home/docker/redis/data
[root@VM-8-15-centos docker]# mkdir -p /home/docker/redis/conf
[root@VM-8-15-centos docker]# mkdir -p /home/docker/redis/logs
[root@VM-8-15-centos docker]# ls
mysql  nginx  redis  tomcat
[root@VM-8-15-centos docker]# cd redis
[root@VM-8-15-centos redis]# ls
conf  data  logs
# 拉取镜像
docker pull redis:latest
# 在conf文件夹下载配置文件并编辑
wget http://download.redis.io/redis-stable/redis.conf
# 编辑conf
vim redis.conf
内容:[
bind 127.0.0.1 #注释掉这部分,这是限制redis只能本地访问
protected-mode no #默认yes,开启保护模式,限制为本地访问
appendonly yes  
requirepass 123456 
]

# 
docker run --restart=always \
--log-opt max-size=100m \
--log-opt max-file=2 \
-p 6379:6379 --name redis-6379 \
-v /home/docker/redis/conf/redis.conf:/etc/redis/redis.conf \
-v /home/docker/redis/data:/data \
-d redis redis-server \
/etc/redis/redis.conf  \
--appendonly yes  \
--requirepass 123456 

# 开启防火墙即可连接
docker exec -it redis-6379 /bin/bash
# 密码 123456
#  (注意不用用户名)

使用工具连接成功
在这里插入图片描述

6.Docker搭建nacos

# 拉取镜像并启动
docker pull nacos/nacos-server

[root@VM-8-15-centos ~]# docker images
REPOSITORY           TAG       IMAGE ID       CREATED       SIZE
redis                latest    3358aea34e8c   6 days ago    117MB
nginx                latest    88736fe82739   6 days ago    142MB
tomcat               8         036850cd208d   2 weeks ago   474MB
mysql                5.7       eef0fab001e8   2 weeks ago   495MB
nacos/nacos-server   latest    ff28177f850f   4 weeks ago   1.06GB
# 创建启动临时nacos容器
docker run -d --name nacos -p 8848:8848 -e PREFER_HOST_MODE=hostname -e MODE=standalone nacos/nacos-server
# 将配置文件复制出来
# 创建nacos文件夹 路径:/home/docker/nacos
[root@VM-8-15-centos ~]# cd /home
[root@VM-8-15-centos home]# ls
docker
[root@VM-8-15-centos home]# cd docker
[root@VM-8-15-centos docker]# ls
mysql  nginx  redis  tomcat
[root@VM-8-15-centos docker]# mkdir nacos
[root@VM-8-15-centos docker]# ls
mysql  nacos  nginx  redis  tomcat

#配置文件复制 出来
[root@VM-8-15-centos nacos]# docker ps -a
CONTAINER ID   IMAGE                COMMAND                  CREATED         STATUS         PORTS                               NAMES
e839ff5bd6f5   nacos/nacos-server   "bin/docker-startup.…"   2 minutes ago   Up 2 minutes   0.0.0.0:8848->8848/tcp              nacos
016cb3cd4868   redis                "docker-entrypoint.s…"   2 days ago      Up 2 days      0.0.0.0:6379->6379/tcp              redis-6379
5168e5f9549e   tomcat:8             "catalina.sh run"        2 days ago      Up 2 days      0.0.0.0:8080->8080/tcp              tomcat8-8080
bc6fbd79c610   mysql:5.7            "docker-entrypoint.s…"   2 days ago      Up 2 days      0.0.0.0:3306->3306/tcp, 33060/tcp   mysql5.7-3306
4ace5ce7473a   nginx:latest         "/docker-entrypoint.…"   2 days ago      Up 2 days      0.0.0.0:8081->80/tcp                nginx-8081
[root@VM-8-15-centos nacos]# docker cp nacos:/home/nacos/logs /home/docker/nacos
[root@VM-8-15-centos nacos]# docker cp nacos:/home/nacos/conf /home/docker/nacos
[root@VM-8-15-centos nacos]#
[root@VM-8-15-centos nacos]# ls
conf  logs
[root@VM-8-15-centos nacos]# cd conf
[root@VM-8-15-centos conf]# ls
1.4.0-ipv6_support-update.sql  application.properties  derby-schema.sql  mysql-schema.sql  nacos-logback.xml
# 删除临时容器
[root@VM-8-15-centos conf]# docker stop nacos
nacos
[root@VM-8-15-centos conf]# docker rm nacos
nacos
# 开放端口 9849 、9948、8848
[root@VM-8-15-centos conf]# firewall-cmd --add-port=9849/tcp --permanent
success
[root@VM-8-15-centos conf]# firewall-cmd --add-port=9948/tcp --permanent
success
[root@VM-8-15-centos conf]# firewall-cmd --add-port=8848/tcp --permanent
success
[root@VM-8-15-centos conf]# firewall-cmd --reload
success

# 重新运行容器
docker run -d \
-e PREFER_HOST_MODE=hostname \
-e MODE=standalone \
-e MYSQL_SERVICE_DB_NAME=nacos \
-e TIME_ZONE='Asia/Shanghai' \
-v /home/docker/nacos/logs:/home/nacos/logs \
-v /home/docker/nacos/conf:/home/nacos/conf \
-p 8848:8848 \
--name nacos-8848 \
--restart=always \
nacos/nacos-server:latest
# 本地访问地址
127.0.0.1:8848/nacos
账号:nacos
密码:nacos

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锐行织梦者

谢谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值