第一次實踐,方法比較笨,歡迎大神們指導。
1,準備web應用編譯文件
創建eval/web目錄,添加eval_linux.jar文件(將其替換爲自己的web應用jar即可),編寫Dockerfile,如下:
# Use an official java as a parent image
FROM java:8
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY evaluation_linux.jar /app/evaluation_linux.jar
# Make port 80 available to the world outside this container
EXPOSE 80
# Run java -jar when the container launches
CMD ["java", "-jar", "eval_linux.jar"]
2,準備數據庫內容
通過如下命令行運行容器將創建的數據庫保存在本地/home/luye/eval/data路徑中
docker run --name hi-eval -v /home/luye/eval/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
獲取上述容器IP地址:
docker inspect hi-eval|grep IPAddress
在Linux系統上遠程登錄容器,生成guns數據庫:
mysql -h 172.17.0.4 -u root -p
mysql> create database guns;
mysql> commit;
mysql> use guns
Database changed
mysql> source /home/luye/eval/db_data/eval-20190421.sql
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| guns |
| mysql |
| performance_schema |
| sys |
授權其他容器可以訪問該數據庫,否則後續數據庫無法鏈接。
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.17.0.*' IDENTIFIED BY 'root' WITH GRANT OPTION;flush privileges;
3,在eval目錄下編寫docker-compose.yml文件
version: '3.3'
services:
db:
container_name: db
image: mysql:5.7
volumes:
- "/home/luye/eval/data:/var/lib/mysql" ##這裏使用剛纔mount的本地地址
environment:
MYSQL_DATABASE: guns
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
ports:
- 3306:3306
web:
build:
./web
ports:
- 8080:8080
- 9990:9990
depends_on:
- db
4,運行上述腳本,可以看到容器正確運行起來了:
docker-compose up -d
Creating db ... done
Creating eval_web_1 ... done
5,驗證:
獲取上述容器eval_web_1的IP地址172.17.0.3,在瀏覽器中鍵入:http:172.17.0.3,就會發現我們的應用可以正常使用了。
關於docker-compose命令的具體含義參見:
https://github.com/docker/labs/blob/master/developer-tools/java/chapters/ch05-compose.adoc