Docker基础操作篇-Dokerfile(含源码)
本文目录
第一步:安装Docker
第二步:安装第一个Mysql镜像
第三步:使用物理机中的Navicat连接虚拟机中的MySql镜像
第四步:下载小名的测试程序
第五步:我们通过物理机的浏览器访问一下容器里面的项目吧~
第一步:安装Docker
1. 执行命令:
[root@eamon ~]# curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
2. 安装成功后,查看Docker版本:
[root@eamon ~]# docker version
Client: Docker Engine - Community
Version: 20.10.3
API version: 1.41
Go version: go1.13.15
Git commit: 48d30b5
Built: Fri Jan 29 14:34:14 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
3. 注册阿里云账号 → 选择“容器镜像服务”(免费)
4. 选择最后一个“镜像加速器”,按照下面的教程,完成4步配置Dokcer的阿里云加速器
5. 启动Docker
[root@eamon ~]# systemctl start docker
第二步:安装第一个Mysql镜像
1. 方法一:查看mysql在DockerHub中有哪些镜像
[root@eamon ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10466 [OK]
mariadb MariaDB is a community-developed fork of MyS… 3891 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Create… 768 [OK]
percona Percona Server is a fork of the MySQL relati… 526 [OK]
centos/mysql-57-centos7 MySQL 5.7 SQL database server 87
mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 79
centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK]
bitnami/mysql Bitnami MySQL Docker Image 48 [OK]
deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK]
databack/mysql-backup Back up mysql databases to... anywhere! 37
prom/mysqld-exporter 37 [OK]
tutum/mysql Base docker image to run a MySQL database se… 35
schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK]
linuxserver/mysql A Mysql container, brought to you by LinuxSe… 27
centos/mysql-56-centos7 MySQL 5.6 SQL database server 20
circleci/mysql MySQL is a widely used, open-source relation… 20
arey/mysql-client Run a MySQL client from a docker container 17 [OK]
mysql/mysql-router MySQL Router provides transparent routing be… 17
fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 11 [OK]
openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6
devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3
ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK]
jelastic/mysql An image of the MySQL database server mainta… 1
monasca/mysql-init A minimal decoupled init container for mysql 0
方法二:在DockerHub官网查看(需要注册账号,且外网速度比较慢):
DockerHub官网
搜索MySql → 点击MySql
点击“Tags”查看都有哪些mysql镜像
2. 根据所需拉取相应的MySql版本(这里我使用的MySql5.7):
(1)拉取MySql镜像:
[root@eamon ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Pull complete
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
1831ac1245f4: Pull complete
37677b8c1f79: Downloading [======================> ] 48.79MB/108.4MB
27e4ac3b0f6e: Download complete
7227baa8c445: Download complete
(2)待到MySql每一层镜像都呈Pull complete即为拉取镜像完成
[root@eamon ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Pull complete
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
1831ac1245f4: Pull complete
37677b8c1f79: Pull complete
27e4ac3b0f6e: Pull complete
7227baa8c445: Pull complete
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
3. 运行MySql镜像
#参数含义
-d 后台启动
-p [小写p]端口映射(-p hostPort:containerPort)
-P [大写P]自动分配端口
-v 卷挂载
-e 环境配置
--name 容器名字
#执行命令
[root@eamon ~]# docker run -d -p 3306:3306 -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7
4.查看MySql容器是否运行起来了
[root@eamon ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a2058874e0b mysql:5.7 "docker.s…" 13 minutes 13 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql01
至此,MySql镜像安装成功🎉
第三步:使用物理机中的Navicat连接虚拟机中的MySql镜像
1.查看虚拟机ip(ifconfig命令) → 连接Navicat → 测试连接
2.这里我们通过Navicat新建一个数据库
3. 进入MySql容器内部,查看新创建的数据库“EamonMysql”
分别运行4个命令:
#1.获得mysql容器id
[root@eamon ~]# docker ps
#2.进入容器内部
[root@eamon ~]# docker exec -it 1a20 /bin/bash
#3.登录mysql
root@1a2058874e0b:/# mysql -uroot -p123456
#4.查看数据库
mysql> show databases;
运行过程:
#获得mysql容器id
[root@eamon ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1a2058874e0b mysql:5.7 "docker.s…" 13 minutes 13 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql01
#通过进入容器内部
[root@eamon ~]# docker exec -it 1a20 /bin/bash
#登录mysql
root@1a2058874e0b:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.33 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| EamonMysql |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql>
4.查看后,退出容器内部:
mysql> exit
Bye
root@1a2058874e0b:/# exit
exit
[root@eamon ~]#
这里呢,大家可以导入我的表结构和数据,小名后面的测试程序会用到
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`age` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '小名', 23);
INSERT INTO `user` VALUES (2, '哐cei', 60);
INSERT INTO `user` VALUES (3, '王麻子', 70);
SET FOREIGN_KEY_CHECKS = 1;
第四步:下载小名的测试程序https://github.com/EamonYin/DockerTest/tree/MySql
1.这里呢,小名用Spring boot+mybatis-plus为大家搭建了一个简单的测试程序
跟着小名教程一步步做的小伙伴,只需修改mysql的地址就可以了(如果不清楚如何设置虚拟机静态IP
PS:细心的小伙伴会注意到小名的这个测试程序比普通项目多了一个DockerFile文件
那么小名来解释一下这个DockerFile文件吧
#项目需要java8的镜像支持,待会部署到Docker上时,Docker如果没有找到jdk8的镜像话,会自动帮你下载
FROM java:8
#把本地的文件拷贝到容器镜像中,名字为eamon
COPY *.jar /eamon.jar
#在容器运行的时候要运行的命令
CMD ["--server.port=8080"]
#暴露容器端口
EXPOSE 8080
#追加命令到 --server.port=8080 之后追加 java -jar命令
ENTRYPOINT ["java","-jar","/eamon.jar"]
2.修改好url后,把小名的测试程序打成jar包,如下图:
3.上传jar包和Dockerfile到服务器
这里小名使用的Finalshell,直接将本地文件拖拽到服务器上的Demo目录里即可;用XShell的同学请自行处理,此处不做演示:
#进入 /home 目录
[root@eamon /]# cd /home
#创建 Demo 文件夹
[root@eamon home]# mkdir Demo
#进入 Demo 文件夹
[root@eamon home]# cd Demo
#查看上传好的两个文件
[root@eamon Demo]# ls
Dockerfile mysqltest-0.0.1-SNAPSHOT.jar
4.把刚刚上传的文件,构建成镜像
命令:
docker build -t eamonmysql .
#进入Demo目录
[root@eamon /]# cd /home/Demo/
#查看文件
[root@eamon Demo]# ll
总用量 33508
-rw-r--r--. 1 root root 124 2月 7 16:24 Dockerfile
-rw-r--r--. 1 root root 34305144 2月 7 16:24 mysqltest-0.0.1-SNAPSHOT.jar
#构建名为eamonmysql的镜像【错误】
[root@eamon Demo]# docker build -t eamonmysql
"docker build" requires exactly 1 argument.
See 'docker build --help'.
Usage: docker build [OPTIONS] PATH | URL | -
Build an image from a Dockerfile
#构建名为eamonmysql的镜像【正确】
[root@eamon Demo]# docker build -t eamonmysql .
Sending build context to Docker daemon 34.31MB
Step 1/5 : 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/5 : COPY *.jar /eamon.jar
---> 4659e16236b2
Step 3/5 : CMD ["--server.port=8080"]
---> Running in e9c72dda067f
Removing intermediate container e9c72dda067f
---> 94808e1967a8
Step 4/5 : EXPOSE 8080
---> Running in d18fb18812a1
Removing intermediate container d18fb18812a1
---> 82aa873f6a9d
Step 5/5 : ENTRYPOINT ["java","-jar","/eamon.jar"]
---> Running in 92355f08991e
Removing intermediate container 92355f08991e
---> 829877ea522f
Successfully built 829877ea522f
Successfully tagged eamonmysql:latest
看出docker build -t eamonmysql和docker build -t eamonmysql .的区别了吗?
对了!就是结尾的.!千万不要像小名这样粗心呦~
官方文档地址:
https://docs.docker.com/engine/reference/commandline/build/
This example specifies that the PATH is ., and so all the files in the local directory get tard and sent to the Docker daemon. The PATH specifies where to find the files for the “context” of the build on the Docker daemon. Remember that the daemon could be running on a remote machine and that no parsing of the Dockerfile happens at the client side (where you’re running docker build). That means that all the files at PATH get sent, not just the ones listed to ADD in the Dockerfile.
简单来说:Path设置成.的意思是:在当前目录下的多有文件都被发送进了Docker的守护进程
5.运行刚才的镜像
[root@eamon Demo]# docker run -d -P --name eamon-mysql-test eamonmysql
2fa54dcc77b8b9984400ee14cfd6690b77ab653c59241fcf3130c738328b184c
[root@eamon Demo]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2fa54dcc77b8 eamonmysql "java -jar /eamon.ja…" 10 s 8 s 0.0.0.0:49153->8080/tcp eamon-mysql-test
1a2058874e0b mysql:5.7 "docker-entrypoint.s…" 2 d 28 m 0.0.0.0:3306->3306/tcp, 33060/tcp mysql01
至此我们的eamonmysql镜像运行成功🎉
第五步:我们通过物理机的浏览器访问一下容器里面的项目吧~😁
大功告成!
细心的小伙伴会发现们这里的端口号怪怪的,这是因为,上面小名的命令里用的是-P[大写P]这一点,小名在第二步的第3小点里提到过,这里重温一下吧~
-p [小写p]端口映射(-p hostPort:containerPort)
-P [大写P]自动分配端口
作者:进阶的小名
出处:blog.csdn.net/Tianc666/article/details/113800210
关注GitHub今日热榜,专注挖掘好用的开发工具,致力于分享优质高效的工具、资源、插件等,助力开发者成长!
点个在看,你最好看