1.docker开启远程访问

1.1 打开/usr/lib/systemd/system/docker.service文件

1.2 在[Service]标签下添加

ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock

1.3 加载docker配置

systemctl daemon-reload

1.4 重启docker

systemctl restart docker

1.5 开启端口号

firewall-cmd --zone=public --add-port=2375/tcp --permanent
firewall-cmd --reload

1.6 测试是否开启成功

http://ip:2375/version
{
  Platform: {
    Name: "Docker Engine - Community"
  },
  Components: [
    {
      Name: "Engine",
      Version: "20.10.7",
      Details: {
        ApiVersion: "1.41",
        Arch: "amd64",
        BuildTime: "2021-06-02T11:54:58.000000000+00:00",
        Experimental: "false",
        GitCommit: "b0f5bc3",
        GoVersion: "go1.13.15",
        KernelVersion: "5.10.16.3-microsoft-standard-WSL2",
        MinAPIVersion: "1.12",
        Os: "linux"
      }
    },
...

2.springboot项目配置

2.1 编写Dockerfile

在项目根目录下创建Dockerfile文件,内容如下:

FROM ascdc/jdk8
VOLUME /tmp
ADD target/hgt-admin.jar /app.jar
EXPOSE 12001
ENTRYPOINT ["java","-jar","/app.jar"]

2.2 配置 Maven 插件

在pom.xml的中添加:

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>1.2.0</version>
    <executions>
        <execution>
            <id>build-image</id>
            <phase>package</phase>
            <goals>
                <goal>build</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <dockerHost>http://192.168.3.10:2375</dockerHost>
        <imageName>${project.artifactId}</imageName>
        <imageTags>
            <imageTag>${project.version}</imageTag>
        </imageTags>
        <forceTags>true</forceTags>
        <dockerDirectory>${project.basedir}</dockerDirectory>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>
说明:
1. 首先在 execution 节点中配置当执行 mvn package 的时候,顺便也执行一下 docker:build
2. 然后在 configuration 中分别配置 Docker 的主机地址,镜像的名称,镜像的 tags,其中 dockerDirectory 表示指定 Dockerfile 的位置。
3. 最后 resource 节点中再配置一下 jar 的位置和名称即可。

2.3 打包成docker镜像

3.创建docker网桥

3.1 查看docker网卡

[root@test ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
38d35e955dbe        bridge              bridge              local
b6fbc0a4cc2f        host                host                local
dab39019e575        none                null                local

3.2 查看docker网卡详细信息

[root@test ~]# docker network inspect 38d35e955dbe
[
    {
        "Name": "bridge",
        "Id": "38d35e955dbe6d85dfa6f1affbe9145feb885b889282cec86e25d55e52ccf569",
        "Created": "2020-12-05T17:25:58.521649522+08:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "36ca3045d984bd932134b20cf455087e73be8755863680bf175ad929652bd1e6": {
                "Name": "registry",
                "EndpointID": "0895f28039fab6e52c1ce357fea8afb3393329093722eaeba3903b0b579a3f52",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            },
            "dafde5a374019bf633a83eefb5255a55b35ec12d25de96cd04df12a35e5ec35d": {
                "Name": "postgresql",
                "EndpointID": "72368429ef9684a98e03a0e9df404f02bf087809395b93aff970832840f461fc",
                "MacAddress": "02:42:ac:11:00:03",
                "IPv4Address": "172.17.0.3/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

3.3 创建自定义网卡

[root@test ~]# docker network create -d bridge --subnet 172.18.0.0/16 hgt_net

3.4 查看自定义网卡

[root@test ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
38d35e955dbe        bridge              bridge              local
431627603ff2        hgt_net             bridge              local
b6fbc0a4cc2f        host                host                local
dab39019e575        none                null                local

4.运行docker镜像

4.1查看docker镜像

[root@test ~]# docker images
REPOSITORY     TAG              IMAGE ID       CREATED          SIZE
hgt-admin      1.0.0            e36fe6387b72   4 hours ago      720MB
mysql          5.7              9f1d21c1025a   5 weeks ago      448MB
rabbitmq       3.8-management   85e83aca5d60   6 weeks ago      249MB
redis          6.0.6            1319b1eaa0b7   12 months ago    104MB
ascdc/jdk8     latest           9f04bbc44406   3 years ago      644MB

4.2运行镜像

4.2.1 后端镜像

1>.运行mysql
docker run --name hgt-mysql -p 3306:3306 --network=hgt_net --ip=172.18.1.1 -e MYSQL_ROOT_PASSWORD=admin123,./ -d mysql:5.7
2>.运行redis
docker run --name hgt-redis -d -p 6379:6379  --network=hgt_net --ip=172.18.1.2  redis:6.0.6 --requirepass "admin123,./" --appendonly yes
3>.运行hgt-admin
docker run -d --name hgt-admin -p 12001:12001 --network=hgt_net --ip=172.18.1.3  hgt-admin:1.0.0

4.2.2 前端镜像

1>.文件拷贝

将vue生成的dist文件夹拷贝到/home/soft/文件夹下,并将dist重命名为hgt-admin-ui

2>.准备nginx.conf文件

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  192.168.3.10;

		location / {
		    # 前端文件路径必须和DockerFile文件中的路径保持一致
            root   /usr/share/nginx/html;
			try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }
		
		location /prod-api/{
			proxy_set_header Host $http_host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header REMOTE-HOST $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_pass http://192.168.3.10:12001/;
		}

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

3>.编写DockerFile文件

vim /home/soft/hgt-admin-ui/DockerFile

FROM nginx:latest
MAINTAINER liuqiang
COPY hgt-admin-ui/ /usr/share/nginx/html/
COPY ./nginx.conf /etc/nginx/nginx.conf   

4>.生成镜像

将hgt-admin-ui文件夹,DockerFile文件及nginx.conf文件拷贝到同一目录下,运行以下命令

docker build -t dataserver -f ./DockerFile .

5>.查看镜像

[root@test ~]# docker images
REPOSITORY     TAG              IMAGE ID       CREATED          SIZE
hgt-admin-ui   latest           acb596ff8056   14 minutes ago   138MB
hgt-admin      1.0.0            e36fe6387b72   4 hours ago      720MB
mysql          5.7              9f1d21c1025a   5 weeks ago      448MB
rabbitmq       3.8-management   85e83aca5d60   6 weeks ago      249MB
redis          6.0.6            1319b1eaa0b7   12 months ago    104MB
ascdc/jdk8     latest           9f04bbc44406   3 years ago      644MB

6>.运行镜像

docker run -d -p 80:80 --network=hgt_net --ip=172.18.1.4 --name hgt-admin-ui hgt-admin-ui:latest
Logo

前往低代码交流专区

更多推荐