目录
- 课程目标
- Image核心知识回顾
- 制作Docker Image的两种方式
- Dockerfile常用指令
- Image实战篇
- 通过Dockerfile制作Image
- 通过Docker Commit创建Image
- 附录
- Image常用操作
- Container常用操作
- 项目Demo Github
Docker已经上市很多年,不是什么新鲜事物了,很多企业或者开发同学以前也不多不少有所接触,但是有实操经验的人不多,本系列教程主要偏重实战,尽量讲干货,会根据本人理解去做阐述,具体官方概念可以查阅官方教程,因为本系列教程对前一章节有一定依赖,建议先学习前面章节内容。
本系列教程导航:
Docker深入浅出 | 容器初体验
课程目标
- 回顾Image核心知识
- 了解如何制作自定义Image的两种方式
- 了解Dockerfile的一些常用指令
- 了解Image一些常用命令
- 了解如何运行一个Springboot容器
Image核心知识回顾
在上一篇已经提到过,Docker Image是有多层结构,实际上由一层一层的文件系统组成,底层都是共享宿主Linux内核,Image的分层结构是是为了提高复用性。Image可以看作是Java的class文件,容器可以看成是JAVA的对象去理解,下层的每一层镜像可以是JAVA中的父类,上层镜像可以共享底层镜像的组件,类似JAVA中的继承规则。
Docker Image 基于 Union file systems做镜像和容器分层,避免在每次以新容器运行图像时复制一组完整的文件。将更改分隔为其自身层中的容器文件系统,允许将同一容器置于从已知内容重新启动(因为在删除容器时,更改的图层将被关闭)。
面向用户的是Container层,所有用户新增环境依赖和数据都会保存在容器层,下层Image只可读、不可修改。
Container是一种轻量级的虚拟技术,不需要模拟硬件创建虚拟机启动内核空间,因此启动速度很快
Docker是基于Linux Kernel的Namespace、CGroups、UnionFileSystem等技术封装成的一种自
定义容器格式,从而提供一套虚拟运行环境。
- Namespace:对全局系统资源的一种封装隔离,使得处于不同namespace的进程拥有独立的全局系统资源,改变一个namespace中的系统资源只会影响当前namespace里的进程,对其他namespace中的进程没有影响,比如pid[进程]、net[网络]、mnt[挂载点]等
- CGroups: cgroup和namespace类似,也是将进程进行分组,但它的目的和namespace不一样,namespace是为了隔离进程组之间的资源,而cgroup是为了对一组进程进行统一的资源监控和限制,比如内存、CPU、进程数等
- Union file systems:用来做image和container分层
制作Docker Image的两种方式
- 通过Dockerfile制作(推荐) - Dockerfile其内部包含了一条条的指令,每一条指令构建一层,因此每一条指令的内容,就是描述该层应当如何构建。
- 通过
Docker commit
操作 - 通过docker commit命令反向基于容器副本创建一个新的镜像文件。但是使用docker commit
看不到Image的创建过程,因此对排查问题不友好。
Dockerfile常用指令
- FROM
指定基础镜像,比如FROM centos:6
FROM centos:6
- RUN
在镜像内部执行一些命令,比如安装软件,配置环境等,换行可以使用
groupadd -r mysql && useradd -r -g mysql mysql
- ENV
设置变量的值,ENV MYSQL_MAJOR 5.7,可以通过docker run --e key=value修改,后面可以直接使
用${MYSQL_MAJOR}
ENV MYSQL_MAJOR 5.7
- LABEL
设置镜像标签
LABEL email="[email protected]"
LABEL name="evan"
- VOLUME
定义匿名数据卷。在启动容器时忘记挂载数据卷,会自动挂载到匿名卷,在启动容器 docker run 的时候,我们可以通过 -v 参数修改挂载点。
作用:- 避免重要的数据,因容器重启而丢失,这是非常致命的。
- 避免容器不断变大。
VOLUME /var/lib/mysql
- COPY
将主机的文件复制到镜像内,如果目录不存在,会自动创建所需要的目录,注意只是复制,不会提取和
解压
COPY demo-api.jar /usr/loacl/app/
- ADD
将主机的文件复制到镜像内,和COPY类似,只是ADD会对压缩文件提取和解压
ADD demo-api.jar /usr/loacl/app/
- WORKDIR
指定镜像的工作目录,之后的命令都是基于此目录工作,若不存在则创建
WORKDIR /usr/local
WORKDIR tomcat
RUN touch test.txt
会在/usr/local/tomcat下创建test.txt文件
- CMD
容器启动的时候默认会执行的命令,若有多个CMD命令,则最后一个生效
CMD ["mysqld"] 或CMD mysqld
- ENTRYPOINT
和CMD的使用类似,但docker run
执行时,会覆盖CMD的命令,而ENTRYPOINT
不会
ENTRYPOINT ["docker-entrypoint.sh"]
ENTRYPOINT ["/bin/bash", "-C","/start.sh"]
- EXPOSE
声明容器运行的服务端口,启动镜像时,可以使用-p将该端口映射给宿主机
EXPOSE 3306 3307
更多操作可以查看菜鸟教程
Image实战篇
通过Dockerfile制作Image
(1)创建一个Springboot项目
指定镜像的工作目录,之后的命令都是基于此目录工作,若不存在则创建
(2)创建一个Rest Api
@RestController
public class DemoController {
@GetMapping("/hello")
public String hello() {
return "Hello";
}
}
(3)通过maven命令 mvn clean package
打包应用
target目录会生成了一个demo-api-0.0.1-SNAPSHOT.jar
(4)进入上一章创建的虚拟机Centos7服务器中创建一个目录demo-dockerfile
目录
- 使用前一章创建的密码
evan123
去登陆Centos7服务器
Connection to 192.168.100.9 closed.
192:centos7 evan$ ssh [email protected]
[email protected]'s password:
Last login: Thu Jan 30 03:22:59 2020 from 192.168.100.7
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
[[email protected] ~]#
- 创建文件夹
[[email protected] /]# mkdir /usr/local/demo-dockerfile
(4)回到宿主机器,上传demo-api-0.0.1-SNAPSHOT.jar
到Centos7服务器demo-dockerfile
目录,我这里用使用命令行操作,其他童鞋可以使用其他sfpt工具上传
192:centos7 evan$ scp Users/evan/development/repository/eshare-docker-in-action/demo-api/target/demo-api-0.0.1-SNAPSHOT.jar [email protected]:/usr/local/demo-dockerfile
[email protected]'s password:
/etc/profile.d/lang.sh: line 19: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
Users/evan/development/repository/eshare-docker-in-action/demo-api/target/demo-api-0.0.1-SNAPSHOT.jar: No such file or directory
192:centos7 evan$ scp /Users/evan/development/repository/eshare-docker-in-action/demo-api/target/demo-api-0.0.1-SNAPSHOT.jar [email protected]:/usr/local/demo-dockerfile
[email protected]'s password:
/etc/profile.d/lang.sh: line 19: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
demo-api-0.0.1-SNAPSHOT.jar 100% 18MB 105.8MB/s 00:00
192:centos7 evan$
(5)回到虚拟机,在刚才创建的目录下创建一个Dockerfile
文件
192:centos7 evan$ ssh [email protected]
[email protected]'s password:
Last login: Thu Jan 30 03:25:43 2020 from 192.168.100.7
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
[[email protected] ~]# cd /usr/local/demo-dockerfile
[[email protected] demo-dockerfile]# mkdir Dockerfile
[[email protected] demo-dockerfile]# touch Dockerfile
[[email protected] demo-dockerfile]# ls
Dockerfile demo-api-0.0.1-SNAPSHOT.jar
(6)填入以下内容到Dockerfile
FROM openjdk:8
MAINTAINER evan
LABEL name="demo-dockerfile" version="1.0"author="evan" COPY demo-api-0.0.1-SNAPSHOT.jar demo-api-image.jar CMD ["java","-jar","demo-api-image.jar"]
(7)先启动Docker服务,假如不存在
[[email protected] demo-dockerfile]# sudo systemctl start docker
(8)在该目录下基于Dockerfile
构建自定义的镜像
命令:docker build -t demo-api-image .
运行结果如下:
[[email protected] demo-dockerfile]# docker build -t demo-api-image .
Sending build context to Docker daemon 19.32MB
Step 1/5 : FROM openjdk:8
8: Pulling from library/openjdk
146bd6a88618: Already exists
9935d0c62ace: Already exists
db0efb86e806: Already exists
e705a4c4fd31: Already exists
3d3bf7f7e874: Already exists
49371c5b9ff6: Already exists
3f7eaaf7ad75: Already exists
Digest: sha256:7b7408b997615b4d6aaf6c1f0de8a32182497250288ee0a27b4e98cf14a52fb3
Status: Downloaded newer image for openjdk:8
---> 8c6851b1fc09
Step 2/5 : MAINTAINER evan
---> Running in 8fb93afccef8
Removing intermediate container 8fb93afccef8
---> 1f516267494a
Step 3/5 : LABEL name="demo-dockerfile" version="1.0"author="evan"
---> Running in 12cf6c64acf8
Removing intermediate container 12cf6c64acf8
---> 5ab38f113669
Step 4/5 : COPY demo-api-0.0.1-SNAPSHOT.jar demo-api-image.jar
---> d752dc61c8a9
Step 5/5 : CMD ["java","-jar","demo-api-image.jar"]
---> Running in d8bb64f014ee
Removing intermediate container d8bb64f014ee
---> cd026463e853
Successfully built cd026463e853
Successfully tagged demo-api-image:latest
(9)基于我们自定义的image启动容器,容器命名为demo-api
docker run -d --name demo-api -p 8999:8080 demo-api-image
(10)查看容器的启动日志
[[email protected] demo-dockerfile]# docker logs demo-api
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.4.RELEASE)
2020-01-30 04:32:29.537 INFO 1 --- [ main] com.example.demo.api.DemoApiApplication : Starting DemoApiApplication v0.0.1-SNAPSHOT on 4b90fdd0dd97 with PID 1 (/demo-api-image.jar started by root in /)
2020-01-30 04:32:29.540 INFO 1 --- [ main] com.example.demo.api.DemoApiApplication : No active profile set, falling back to default profiles: default
2020-01-30 04:32:30.478 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-01-30 04:32:30.488 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-01-30 04:32:30.489 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.30]
2020-01-30 04:32:30.540 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-01-30 04:32:30.540 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 895 ms
2020-01-30 04:32:30.712 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-30 04:32:30.888 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-01-30 04:32:30.890 INFO 1 --- [ main] com.example.demo.api.DemoApiApplication : Started DemoApiApplication in 1.756 seconds (JVM running for 2.25)
(11)在宿主机器上访问容器服务,测试Api是否成功部署
[[email protected] demo-dockerfile]# curl localhost:8999/hello
Hello
(12)再启动多一个容器实例测试
[[email protected] demo-dockerfile]# docker run -d --name demo-api02 -p 8998:8080 demo-api-image
3029d6b4325a6773ddcacca8d39917b1abfe12d6004cc8a765608f7f2d64edb3
[[email protected] demo-dockerfile]# curl localhost:8998/hello
Hello
(12)登陆Docker镜像仓库,没有账号的童鞋提前到hub.docker.com
注册
[[email protected] demo-dockerfile]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: 10856214
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
(13)推送定制的Docker镜像到远程仓库,镜像名称要跟Docker id一致
[[email protected] demo-dockerfile]# docker tag demo-api-image 10856214/demo-api-image
[[email protected] demo-dockerfile]# docker push 10856214/demo-api-image
The push refers to repository [docker.io/10856214/demo-api-image]
9fb0d7d193ef: Pushed
a6ded049566a: Mounted from library/openjdk
e7fe5541de5f: Mounted from library/openjdk
03ff63c55220: Mounted from library/openjdk
bee1e39d7c3a: Mounted from library/openjdk
1f59a4b2e206: Mounted from library/openjdk
0ca7f54856c0: Mounted from library/openjdk
ebb9ae013834: Mounted from library/openjdk
latest: digest: sha256:32afd9d8ca8205d6e667543a66163330c5067c5a37ebd80d53e9563b809e8bb4 size: 2007
(14)查看Image是否已经成功推送到远程仓库
(15)从远程仓库把Image拉到本地
[[email protected] demo-dockerfile]# docker pull 10856214/demo-api-image
Using default tag: latest
latest: Pulling from 10856214/demo-api-image
Digest: sha256:32afd9d8ca8205d6e667543a66163330c5067c5a37ebd80d53e9563b809e8bb4
Status: Image is up to date for 10856214/demo-api-image:latest
docker.io/10856214/demo-api-image:latest
(16)基于远程10856214/demo-api-image
启动容器
[[email protected] demo-dockerfile]# docker run -d --name demo-api03 -p 8997:8080 10856214/demo-api-image
f6907cc3f8b7c21e26111c748de23d923f96f5b2ffef54478ff49d8c03416651
[[email protected] demo-dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6907cc3f8b7 10856214/demo-api-image "java -jar demo-api-…" 4 seconds ago Up 3 seconds 0.0.0.0:8997->8080/tcp demo-api03
3029d6b4325a demo-api-image "java -jar demo-api-…" About an hour ago Up About an hour 0.0.0.0:8998->8080/tcp demo-api02
4b90fdd0dd97 demo-api-image "java -jar demo-api-…" About an hour ago Up About an hour 0.0.0.0:8999->8080/tcp demo-api
[[email protected] demo-dockerfile]#
通过Docker Commit创建Image
(1)查看运行中的容器
[[email protected] demo-dockerfile]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f6907cc3f8b7 10856214/demo-api-image "java -jar demo-api-…" 2 hours ago Up 2 hours 0.0.0.0:8997->8080/tcp demo-api03
3029d6b4325a demo-api-image "java -jar demo-api-…" 3 hours ago Up 3 hours 0.0.0.0:8998->8080/tcp demo-api02
4b90fdd0dd97 demo-api-image "java -jar demo-api-…" 3 hours ago Up 3 hours 0.0.0.0:8999->8080/tcp demo-api
[[email protected] demo-dockerfile]#
(2)进入我们在上一节从远程仓库拉回来的自定义镜像内部
[[email protected] demo-dockerfile]# docker exec -it demo-api03 bash
[email protected]:/#
(3)修改容器demo-api03,在容器内部安装yum工具
- 尝试运行yum指令,默认是不带yum工具,因为容器里的linux操作系统只保留基本指令
[email protected]:/# yum
bash: yum: command not found
[email protected]:/#
- 查看下容器内部的linux操作系统版本
[email protected]:/# cat /etc/issue
Debian GNU/Linux 9 \n \l
内置的操作系统用的Debian
- 使用apt-get更新系统组件
[email protected]:/# apt-get update
Ign:1 http://deb.debian.org/debian stretch InRelease
Get:2 http://security.debian.org/debian-security stretch/updates InRelease [94.3 kB]
Get:3 http://deb.debian.org/debian stretch-updates InRelease [91.0 kB]
Get:4 http://deb.debian.org/debian stretch Release [118 kB]
Get:5 http://security.debian.org/debian-security stretch/updates/main amd64 Packages [516 kB]
Get:6 http://deb.debian.org/debian stretch-updates/main amd64 Packages [27.9 kB]
Get:7 http://deb.debian.org/debian stretch Release.gpg [2365 B]
Get:8 http://deb.debian.org/debian stretch/main amd64 Packages [7086 kB]
Fetched 7936 kB in 1min 18s (101 kB/s)
Reading package lists... Done
- 安装容器内置操作系统必要依赖
apt-get install build-essential
- 下载安装yum组件
apt-get install yum
- 测试
yum
是否安装成功
[email protected]:/# yum --help
Usage: yum [options] COMMAND
List of Commands:
check Check for problems in the rpmdb
check-update Check for available package updates
clean Remove cached data
deplist List a package's dependencies
...
(4)通过以上步骤,我们已经成功修改了容器,接下来我们推出容器,基于当前容器版本创建一个新的Image,命名为demo-api-image-v1
[[email protected] demo-dockerfile]# docker commit demo-api03 demo-api-image-v1
sha256:7887668690a3e53c27535669252f67b0391d54443776f1a431f91fe5800958be
[[email protected] demo-dockerfile]#
这时候创建出来的Image是默认已经携带了yum
组件
(5)基于上面更新的新镜像demo-api-image-v1
创建一个容器
[[email protected] demo-dockerfile]# docker run -d -it --name demo-api-v1 demo-api-image-v1
6363450e8f7af1f53f935e345d50f7bbfd3ff82bb2409fd5fd75a0f253ed1de8
(6)进入demo-api-v2
容器验证是否已经存在yum
组件
[[email protected] demo-dockerfile]# docker exec -it demo-api-v1 bash
[email protected]:/# yum
You need to give some command
Usage: yum [options] COMMAND
List of Commands:
check Check for problems in the rpmdb
check-update Check for available package updates
clean Remove cached data
deplist List a package's dependencie
...
附录
Image常用操作
- 查看本地image列表
docker images docker image ls
- 获取远端镜像
docker pull
- 删除镜像[注意此镜像如果正在使用,或者有关联的镜像,则需要先处理完]
docker image rm imageid docker rmi -f imageid docker rmi -f $(docker image ls)
删除所有镜像 - 运行镜像
docker run image
- 发布镜像
docker push
Container常用操作
- 根据镜像创建容器
docker run -d --name -p 9090:8080 my-tomcat tomcat
- 查看运行中的container
docker ps
- 查看所有的container[包含退出的]
docker ps -a
- 删除container
docker rm containerid docker rm -f $(docker ps -a)
删除所有container - 进入到一个container中
docker exec -it container bash
- 根据container生成image
docker commit demo-api03 demo-api-image-v1
- 查看某个container的日志
docker logs container
- 查看容器资源使用情况
docker stats
- 查看容器详情信息
docker inspect container
- 停止/启动容器
docker stop/start container
项目Demo Github
https://github.com/EvanLeung08/eshare-docker-in-action.git
原文地址:https://www.cnblogs.com/evan-liang/p/12244304.html