目錄
前言:
公開悄悄話:
技術堆疊:
1.Docker圖解
2.Docker的安裝以及使用
2.1.Docker的安裝
2.1.1.卸載舊版本
2.1.2安裝Docker
2.2.Docker的使用
2.2.1.Docker常用命令
2.2.2搞個容器玩玩
3.Spring Boot專案制作鏡像
3.1.創建Springboot專案
3.1.1.liquibase解釋
3.2.制作Dockerfile檔案
3.3.制作鏡像并運行
前言:
最近在搞一個單機版專案,我要在linux環境中搞一套開發(DEV)環境,還要再搞一套測驗(PRE)環境,上線后當然還要部署一套生成(PRD)環境,專案中要用到工程Jar、Redis、Mysql等等
較蠢的辦法當然是安裝下載源檔案、環境配置、工程打包、上傳服務器、一個個啟動有的行程啟動還要有先后順序,一套環境安裝下來就得消耗半個運維,如果不止一個jar包而是微服務多個jar包,如果產品火了市場大賣,那公司得多準備些運維用來消耗
當然牛一點的運維為了保命學會了寫sheel腳本,通過shell腳本去執行,效率直接翻倍,
不會shell腳本的運維默默的遞交了辭職書

開發偷偷的跑到領導辦公室

老板走到連shell都會寫的運維面前

公開悄悄話:
別問我怎么知道的,問我我就說

文末有工程git地址,也有微信二維碼,歡迎點贊收藏
技術堆疊:
本文涉及到的技術堆疊包括:Docker的常規操作、SpringBoot專案制作鏡像(DockerFile)、Liquibase的使用
1.Docker圖解
我剛接觸也一臉悶逼,什么叫鏡像,什么叫容器,什么叫資料卷

用一張圖來給大家形象的比劃一下,當然我們只需要理解一個每個名詞的意思是什么,就沒必要去搞懂什么一個鏡像有多少層,每一層代表什么意思,如果有不準確的地方還望大家指出來,

2.Docker的安裝以及使用
2.1.Docker的安裝
大家可以參考官網的安裝,官網的安裝步驟還算詳細,也可以直接看下面的步驟,只是對官網做了簡單點翻譯和刪減,
直接復制、粘貼、回車,就搞定了,
2.1.1.卸載舊版本
較舊的Docker版本稱為docker或docker-engine,如果已安裝這些程式,請卸載它們以及相關的依賴項,
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
2.1.2安裝Docker
在新主機上首次安裝Docker Engine之前,需要設定Docker存盤庫,之后,您可以從存盤庫安裝和更新Docker,
設定存盤庫
安裝yum-utils軟體包(提供yum-config-manager 實用程式)并設定穩定的存盤庫
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
安裝最新版本的Docker Engine和容器
sudo yum install docker-ce docker-ce-cli containerd.io
設定一下開機啟動
systemctl enable docker
安裝完成,啟動Docker
sudo systemctl start docker
添加鏡像加速,因為docker的鏡像倉庫在國外,很多鏡像下起來賊慢,這里推薦使用阿里的鏡像加速
- 訪問 https://cr.console.aliyun.com/cn-hangzhou/mirrors,然后點擊鏡像加速器,會看到如下資訊

- 修改/etc/docker/daemon.json 檔案,沒有的話新增一個如下:
-
[root@adobe2 docker]# touch daemon.json [root@adobe2 docker]# ls daemon.json key.json [root@adobe2 docker]# nano daemon.json [root@adobe2 docker]# cat daemon.json { "registry-mirrors": ["https://******.mirror.aliyuncs.com"] }
通過運行hello-world 映像來驗證是否正確安裝了Docker Engine
sudo docker run hello-world
出現如下則說明安裝成功,可以看出首先沒有找到image(鏡像),沒找到的情況下會去鏡像倉庫pull(下載),
[root@adobe2 ~]# sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Already exists
Digest: sha256:f2266cbfc127c960fd30e76b7c792dc23b588c0db76233517e1891a4e357d519
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
這時候輸入命令>docker images就可以看到我們本地的鏡像了
[root@adobe2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 7 weeks ago 13.3kB
[root@adobe2 ~]#
2.2.Docker的使用
2.2.1.Docker常用命令
下面列一些docker常用的命令,本文的重點不在這,想學的明白一點的可以自行去官網或者菜鳥教程學習
- docker images 查看本地鏡像
- docker ps 查看已經啟動的容器
- docker ps -a 查看所有容器
- docker ps -l 查看最近一次啟動的容器
- docker run -d -p 80:80 --name "redis_name" --restart=always redis 啟動redis,-d表示后臺啟動,-p標識埠映射(前是對外埠,后是容器內的埠),--name自定義容器名,--restart=always掛了自動重啟,還有一個常用的-v命令資料庫的掛載,
- docker exec -it redis_name bash 進入容器
- docker stop redis_name 停止容器
- docker rm -f redis_name 洗掉容器
2.2.2搞個容器玩玩
下載鏡像可以到Docker Hub上去查找,拉取鏡像,
地址:https://hub.docker.com/
我們來安裝個Nginx試試,
[root@adobe2 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
f7ec5a41d630: Pull complete
aa1efa14b3bf: Pull complete
b78b95af9b17: Pull complete
c7d6bca2b8dc: Pull complete
cf16cd8e71e0: Pull complete
0241c68333ef: Pull complete
Digest: sha256:75a55d33ecc73c2a242450a9f1cc858499d468f077ea942867e662c247b5e412
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@adobe2 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 62d49f9bab67 13 days ago 133MB
hello-world latest d1165f221234 7 weeks ago 13.3kB
可以看到鏡像已經拉下來,我們啟動一下,Nginx默認埠80,-d 后臺運行,-p 80:80 后一個80就是nginx容器中默認訪問Nginx的埠,前一個80埠就是通過宿主機暴露出來的埠,
[root@adobe2 ~]# docker run -d -p 80:80 --name nginx01 nginx
7ded1c1ba12ffce86a90153e7ecd3c024aa526c79fa86ec46337f0d8121a359e
[root@adobe2 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7ded1c1ba12f nginx "/docker-entrypoint.…" 8 seconds ago Up 5 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp nginx01
這時我們打開瀏覽器輸入: <宿主機Ip>:80

可以看到已經啟動成功,正好這里我們來將一下 -v 引數的使用
我們現在訪問Nginx的這個頁面是在Nginx容器中的index.html頁面,我們可以通過docker inspect nginx01 查看這個容器的所有資訊,里面有一個MergedDir引數就是對應的宿主機中的地址,當然我們正常通過進入到容器中查看
[root@adobe2 html]# docker exec -it nginx01 bash
root@7ded1c1ba12f:/# cd /usr/share/nginx/html/
root@7ded1c1ba12f:/usr/share/nginx/html# ls
50x.html index.html
root@7ded1c1ba12f:/usr/share/nginx/html# cat index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
這時候我們在本地本地根路徑下,創建一個/nginx/index.html檔案,內容為<h1>adobe 666</h1>,然后通過 -v 命令掛在到容器中/usr/share/nginx/html/這個檔案夾下,然后看效果
[root@adobe2 html]# cd ~
[root@adobe2 ~]# ls
anaconda-ks.cfg original-ks.cfg
[root@adobe2 ~]# mkdir nginx
[root@adobe2 ~]# ls
anaconda-ks.cfg nginx original-ks.cfg
[root@adobe2 ~]# cd nginx/
[root@adobe2 nginx]# echo "<h1>di li re ba ai shang wo</h1>" > index.html
[root@adobe2 nginx]# ls
index.html
[root@adobe2 nginx]# cat index.html
<h1>adobe 666</h1>
啟動容器
[root@adobe2 nginx]# docker run -d --name nginx02 -p 81:80 -v /root/nginx/:/usr/share/nginx/html/ nginx
377685c7747b242d3eeba796421992fc14b9c633b7a60d31d2419304fbda06b2
[root@adobe2 nginx]#
打開瀏覽器輸入: <宿主機Ip>:81
理解-v的意思了么?再生產中常用到的例如組態檔的掛載,日志的掛載等,

3.Spring Boot專案制作鏡像
3.1.創建Springboot專案

該專案中用到的技術包括:mybatis-plus(加上代碼生成器)、liquibase
Git代碼奉上,請笑納:
mybatis-plus這個講就有點麻煩了,大家想用的話可以去學一學,賊好用
大家可能對liquibase有點陌生,這里就針對這個liquibase做個簡單的解釋
3.1.1.liquibase解釋
引入pom
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.3.1</version>
</dependency>
application.yml配置,可以看到
liquibase:
change-log: classpath:db/changelog/master.xml
server:
port: 8080
servlet:
context-path: /dockertest
spring:
application:
name: dockertest
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/dockertest?serverTimezone=GMT%2B8&characterEncoding=utf8&useUnicode=true&useSSL=false
username: root
password: root
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
serialization:
write-dates-as-timestamps: false
liquibase:
change-log: classpath:db/changelog/master.xml
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
auto-mapping-behavior: full
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:mapper/**/*Mapper.xml
global-config:
# 邏輯洗掉配置
db-config:
# 洗掉前
logic-not-delete-value: 1
# 洗掉后
logic-delete-value: 0
主目錄是master.xml
然后你要有腳本的話就自己創建回應的XML檔案,然后添加到master.xml中
例如我要初始化一個表tb_docker,我建一個檔案 2021-04-24-PZF.xml
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet id="add table tb_docker" author="pengzf">
<sql>
CREATE TABLE `tb_docker` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`name` varchar(16) DEFAULT NULL,
`age` int(4) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
</sql>
</changeSet>
<changeSet id="add data to tb_docker" author="pengzf">
<sql>
INSERT INTO tb_docker VALUES (1,"張三",32)
</sql>
</changeSet>
</databaseChangeLog>
然后將檔案2021-04-24-PZF.xml添加到master.xml中
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<include file="2021-04-24-PZF.xml" relativeToChangelogFile="true"></include>
</databaseChangeLog>
搞定,就是這么easy

先看一下資料庫原來的樣子,沒有任何表

啟動專案之后,可以看到出現三個表,當然tb_docker里面有沒有那條張三的記錄呢?你猜

大家應該對liquibase有那么點了解了吧?例如我有多個環境,我就不需要每個環境手動執行初始化腳本了,還能保證手動執行帶來的不確定性,
我敢打賭不會有問題,如果有問題的話,掃描下方二維碼

3.2.制作Dockerfile檔案
直接創建一個檔案名為Dockerfile
# 指定父鏡像,基于java:8運行,
FROM java:8
# 作者
MAINTAINER 15705116160@163.com
VOLUME /tmp
# 本地jar包拷貝到容器中,命名為mydockertest.jar
ADD dockertest.jar mydockertest.jar
# 容器創建時執行一段命令,構建鏡像時執行的命令
RUN bash -c 'touch /mydockertest.jar'
ENTRYPOINT ["java","-Djava.-Djava.security.egd=file:/dev/./urandom","-jar","/mydockertest.jar"]
# 暴露出去的埠
EXPOSE 8080
3.3.制作鏡像并運行
注意我們linux上面現在沒有安裝mysql,我們可以先將我們工程里面與資料源鏈接的地方都注釋掉,修改啟動檔案
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})排除spring自動加載的資料源配置
我在Controller里面寫了一個方法
@RestController
@RequestMapping("/tb-docker")
public class TbDockerController {
@GetMapping("/testTwo")
public String testTwo(){
return "Hello World";
}
}
打包,在linux的根路徑下創建檔案夾springboot,并將jar包和Dockerfile上傳進去
[root@adobe2 nginx]# cd ~
[root@adobe2 ~]# ls
anaconda-ks.cfg nginx original-ks.cfg
[root@adobe2 ~]# mkdir springboot
[root@adobe2 ~]# cd springboot/
[root@adobe2 springboot]# rz -y
[root@adobe2 springboot]# rz -y
[root@adobe2 springboot]# ls
Dockerfile dockertest.jar
[root@adobe2 springboot]#
在該檔案夾中執行命令docker build -t dockertest:01 .
因為Dockerfile檔案就在該檔案夾下,所以可以直接運行,否則的話需要-f命令指定Doclerfile檔案,容器命名為dockertest,tag為01
[root@adobe2 springboot]# docker build -t dockertest:01 .
Sending build context to Docker daemon 32.67MB
Step 1/7 : FROM java:8
8: 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:8
---> d23bdf5b1b1b
Step 2/7 : MAINTAINER 15705116160@163.com
---> Running in ef6e7fd12ded
Removing intermediate container ef6e7fd12ded
---> 432a60519029
Step 3/7 : VOLUME /tmp
---> Running in 5dceb082dd7f
Removing intermediate container 5dceb082dd7f
---> 73ea411a2dba
Step 4/7 : ADD dockertest.jar mydockertest.jar
---> c9fb086ece70
Step 5/7 : RUN bash -c 'touch /mydockertest.jar'
---> Running in 25048e46dbe0
Removing intermediate container 25048e46dbe0
---> 68cb87e03377
Step 6/7 : ENTRYPOINT ["java","-Djava.-Djava.security.egd=file:/dev/./urandom","-jar","/mydockertest.jar"]
---> Running in da5ccbf7075b
Removing intermediate container da5ccbf7075b
---> dcc12fefa0a5
Step 7/7 : EXPOSE 8080
---> Running in 8d1abf6bc4e3
Removing intermediate container 8d1abf6bc4e3
---> 2c8c676d582e
Successfully built 2c8c676d582e
Successfully tagged dockertest:01
[root@adobe2 springboot]#
我們可以看到先去下載java8的鏡像,后面制作鏡像dockertest:01,通過命令docker images查看是否制作成功
[root@adobe2 springboot]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockertest 01 2c8c676d582e 2 minutes ago 709MB
nginx latest 62d49f9bab67 13 days ago 133MB
hello-world latest d1165f221234 7 weeks ago 13.3kB
java 8 d23bdf5b1b1b 4 years ago 643MB
[root@adobe2 springboot]#
看到了吧,鏡像dockertest:01已經制作成功,那么我現在看看能不能正常啟動
[root@adobe2 springboot]# docker run -d -p 8080:8080 dockertest:01
a0ca43ea5f3732cf4477156dec02465571c6a001b05ff2adbc416af4836af6d5
[root@adobe2 springboot]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a0ca43ea5f37 dockertest:01 "java -Djava.-Djava.…" 5 seconds ago Up 4 seconds 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp friendly_haibt
[root@adobe2 springboot]#
正常啟動,訪問頁面方法:<宿主機Ip>:8080/dockertest/tb-docker/testTwo,回傳正常

最后附上工程git地址,大家可以下載下來參考參考,
git地址:https://gitee.com/adobepzf/docker-test.git
微信:
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/286557.html
標籤:其他
