105.【Docker】(七)

简介: 105.【Docker】

2.基础知识:

1、每个保留关键字(指令)都是必须是----大写字母!!!!!
2、执行从上到下顺序执行
3、#表示注释
4、每一个指令都会创建提交一个新的镜像层,并提交

  • dockerfile是面向开发的,我们以后要发布项目,做镜像,就需要编写dockerfile文件,这个文件十分简单!
  • Docker镜像逐渐成为了企业交付的标准,必须要掌握!!!

步骤:开发,部署,运维…缺一不可

  1. DockerFile:构建文件,定义了一切的步骤,源代码
  2. DockerImages通过DockerFile构建生成的镜像,最终发布和运行的产品
  3. Docker容器容器就是镜像运行起来提供服务的

3.DockerFile 指令

我们可以通过这些指令进行自定义基础镜像

FROM      # 基础镜像,一切从这里开始
 MAINTAINER  # 镜像是谁写的,姓名+邮箱
 RUN      # 镜像构建的时候需要运行的命令
 ADD      # 步骤:往里面加东西:tomcat镜像,这个tomcat压缩包,添加内容
 WORKDIR  # 镜像的工作目录
 VOLUME   # 挂载的目录
 EXPOSE     # 暴露端口配置
 CMD      # 指定容器运行时的shell命令,只有最后一个生效,可被替代
 ENTRYPOINT   # 指定容器运行时的shell命令,可以追加命令
 ONBUILD    # 当构建一个被继承DockerFile这个时候就会运行ONBUILD的指令,触发指令
 COPY     # 类似ADD,将我们文件拷贝到镜像中,但不会自动解压
 ENV      # 构建的时候设置环境变量

4.实战测试

Docker Hub中99%镜像都是从这个

Github-centos-7源码

FROM scratch
ADD centos-7-x86_64-docker.tar.xz /
LABEL \
    org.label-schema.schema-version="1.0" \
    org.label-schema.name="CentOS Base Image" \
    org.label-schema.vendor="CentOS" \
    org.label-schema.license="GPLv2" \
    org.label-schema.build-date="20201113" \
    org.opencontainers.image.title="CentOS Base Image" \
    org.opencontainers.image.vendor="CentOS" \
    org.opencontainers.image.licenses="GPL-2.0-only" \
    org.opencontainers.image.created="2020-11-13 00:00:00+00:00"
CMD ["/bin/bash"]
(1). 创建一个自己的centos
1. 编写一个DockerFile配置文件
FROM centos:7    #来源于哪?
MAINTAINER jsxs<2261203961@qq.com>  #作者信息
ENV MYPATH /usr/local  #设置默认pwd路径
WORKDIR $MYPATH  #工作目录
RUN yum -y install vim  #安装vim镜像
RUN yum -y install net-tools  #安装工具镜像
EXPOSE 80  #暴漏端口80
CMD echo $MYPATH  #输出文件
CMD echo "-----end---"
CMD /bin/bash  #运行中执行这个命令
-------------------------------
2.  构建镜像
docker build -f mysockerfile -t jsxs-centos:1.0 .
3.测试运行 
 docker run -it --name jsxs-centos01 jsxs-centos:1.0
 4. 研究怎么做的
 docker history 0ab761aa5334
IMAGE          CREATED          CREATED BY                                      SIZE      COMMENT
0ab761aa5334   11 minutes ago   CMD ["/bin/sh" "-c" "/bin/bash"]                0B        buildkit.dockerfile.v0
<missing>      11 minutes ago   CMD ["/bin/sh" "-c" "echo \"-----end---\""]     0B        buildkit.dockerfile.v0
<missing>      11 minutes ago   CMD ["/bin/sh" "-c" "echo $MYPATH"]             0B        buildkit.dockerfile.v0
<missing>      11 minutes ago   EXPOSE map[80/tcp:{}]                           0B        buildkit.dockerfile.v0
<missing>      11 minutes ago   RUN /bin/sh -c yum -y install net-tools # bu…   25MB      buildkit.dockerfile.v0
<missing>      11 minutes ago   RUN /bin/sh -c yum -y install vim # buildkit    259MB     buildkit.dockerfile.v0
<missing>      11 minutes ago   WORKDIR /                                       0B        buildkit.dockerfile.v0
<missing>      11 minutes ago   ENV YPATH=/usr/local                            0B        buildkit.dockerfile.v0
<missing>      11 minutes ago   MAINTAINER jsxs<2261203961@qq.com>              0B        buildkit.dockerfile.v0
<missing>      18 months ago    /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B
<missing>      18 months ago    /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B
<missing>      18 months ago    /bin/sh -c #(nop) ADD file:b3ebbe8bd304723d4…   204MB

构建镜像

我们发现我们自定义的镜像增加了好多命令

5.CMD 与 ENTRYPOINT 区别

(1).CMD

覆盖

CMD

------------------------
[root@Jsxs dockerfile]# vim dockerfile-cmd-test  #编写文件
FROM centos:7
CMD ["ls","-a"]
-------
# 构建文件 -f 打包的文件 -t 打包后的镜像标题
[root@Jsxs dockerfile]# docker build -f dockerfile-cmd-test -t cmdtest .
[+] Building 0.4s (5/5) FINISHED
 => [internal] load .dockerignore                                                                                                                                    0.1s
 => => transferring context: 2B                                                                                                                                      0.0s
 => [internal] load build definition from dockerfile-cmd-test                                                                                                        0.1s
 => => transferring dockerfile: 76B                                                                                                                                  0.0s
 => [internal] load metadata for docker.io/library/centos:7                                                                                                          0.4s
 => CACHED [1/1] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987                                             0.0s
 => exporting to image                                                                                                                                               0.0s
 => => exporting layers                                                                                                                                              0.0s
 => => writing image sha256:d53776618e7ca21294bf0d76a1042fa5976f2b0992727c1122ede2900cd8ba93                                                                         0.0s
 => => naming to docker.io/library/cmdtest                                                                                                                           0.0s
 -----
 # 创建容器并执行
[root@Jsxs dockerfile]# docker run -it --name testcmd1 cmdtest
.  ..  .dockerenv  anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

我们像追加一个命令 -l "ls -al"显示全部数据,-l;不是命令所以报错
[root@Jsxs dockerfile]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED          SIZE
jsxs-centos           1.0       0ab761aa5334   31 minutes ago   488MB
nginx                 latest    605c77e624dd   14 months ago    141MB
tomcat                9.0       b8e65a4d736d   15 months ago    680MB
mysql                 5.7       c20987f18b13   15 months ago    448MB
cmdtest               latest    d53776618e7c   18 months ago    204MB
centos                latest    5d0da3dc9764   18 months ago    231MB
centos-test           1.0       0282d836c4f5   18 months ago    231MB
portainer/portainer   latest    580c0e4e98b0   2 years ago      79.1MB
[root@Jsxs dockerfile]# docker run d53776618e7c -l
docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "-l": executable file not found in $PATH: unknown.
我们需要编写一个完整的命令进行替换-
[root@Jsxs ~]# docker run d53776618e7c ls -al
total 64
drwxr-xr-x   1 root root  4096 Mar 22 14:38 .
drwxr-xr-x   1 root root  4096 Mar 22 14:38 ..
-rwxr-xr-x   1 root root     0 Mar 22 14:38 .dockerenv
-rw-r--r--   1 root root 12114 Nov 13  2020 anaconda-post.log
lrwxrwxrwx   1 root root     7 Nov 13  2020 bin -> usr/bin
drwxr-xr-x   5 root root   340 Mar 22 14:38 dev
drwxr-xr-x   1 root root  4096 Mar 22 14:38 etc
drwxr-xr-x   2 root root  4096 Apr 11  2018 home
lrwxrwxrwx   1 root root     7 Nov 13  2020 lib -> usr/lib
lrwxrwxrwx   1 root root     9 Nov 13  2020 lib64 -> usr/lib64
drwxr-xr-x   2 root root  4096 Apr 11  2018 media
drwxr-xr-x   2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x   2 root root  4096 Apr 11  2018 opt
dr-xr-xr-x 225 root root     0 Mar 22 14:38 proc
dr-xr-x---   2 root root  4096 Nov 13  2020 root
drwxr-xr-x  11 root root  4096 Nov 13  2020 run
lrwxrwxrwx   1 root root     8 Nov 13  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x  13 root root     0 Mar 22 14:38 sys
drwxrwxrwt   7 root root  4096 Nov 13  2020 tmp
drwxr-xr-x  13 root root  4096 Nov 13  2020 usr
drwxr-xr-x  18 root root  4096 Nov 13  2020 var
(2).ENTRYPOINT

追加

vim dockerfile-cmd-entrypoint
FROM centos:7
ENTRYPOINT ["ls","-a"]
[root@Jsxs dockerfile]# docker build -f dockerfile-cmd-entrypoint -t entrypointtest .
[+] Building 15.4s (5/5) FINISHED
 => [internal] load build definition from dockerfile-cmd-entrypoint                                                                                                  0.0s
 => => transferring dockerfile: 89B                                                                                                                                  0.0s
 => [internal] load .dockerignore                                                                                                                                    0.0s
 => => transferring context: 2B                                                                                                                                      0.0s
 => [internal] load metadata for docker.io/library/centos:7                                                                                                         15.4s
 => CACHED [1/1] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987                                             0.0s
 => exporting to image                                                                                                                                               0.0s
 => => exporting layers                                                                                                                                              0.0s
 => => writing image sha256:dbcf4f99ed1cde79a9a4034b78ca79e0829f3e83bc9830a34065a1918f0dbcfa                                                                         0.0s
 => => naming to docker.io/library/entrypointtest                                                                                                                    0.0s
[root@Jsxs dockerfile]# docker run
"docker run" requires at least 1 argument.
See 'docker run --help'.
Usage:  docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Create and run a new container from an image
[root@Jsxs dockerfile]#
[root@Jsxs dockerfile]# docker run entrypointtest
.
..
.dockerenv
anaconda-post.log
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
#  拼接 追加
[root@Jsxs dockerfile]# docker run entrypointtest -l
total 64
drwxr-xr-x   1 root root  4096 Mar 22 14:46 .
drwxr-xr-x   1 root root  4096 Mar 22 14:46 ..
-rwxr-xr-x   1 root root     0 Mar 22 14:46 .dockerenv
-rw-r--r--   1 root root 12114 Nov 13  2020 anaconda-post.log
lrwxrwxrwx   1 root root     7 Nov 13  2020 bin -> usr/bin
drwxr-xr-x   5 root root   340 Mar 22 14:46 dev
drwxr-xr-x   1 root root  4096 Mar 22 14:46 etc
drwxr-xr-x   2 root root  4096 Apr 11  2018 home
lrwxrwxrwx   1 root root     7 Nov 13  2020 lib -> usr/lib
lrwxrwxrwx   1 root root     9 Nov 13  2020 lib64 -> usr/lib64
drwxr-xr-x   2 root root  4096 Apr 11  2018 media
drwxr-xr-x   2 root root  4096 Apr 11  2018 mnt
drwxr-xr-x   2 root root  4096 Apr 11  2018 opt
dr-xr-xr-x 223 root root     0 Mar 22 14:46 proc
dr-xr-x---   2 root root  4096 Nov 13  2020 root
drwxr-xr-x  11 root root  4096 Nov 13  2020 run
lrwxrwxrwx   1 root root     8 Nov 13  2020 sbin -> usr/sbin
drwxr-xr-x   2 root root  4096 Apr 11  2018 srv
dr-xr-xr-x  13 root root     0 Mar 22 14:46 sys
drwxrwxrwt   7 root root  4096 Nov 13  2020 tmp
drwxr-xr-x  13 root root  4096 Nov 13  2020 usr
drwxr-xr-x  18 root root  4096 Nov 13  2020 var

6.实战Tomcat镜像

(1).准备 tomcat 与 jdk 安装包

(2).编写Dockerfile文件

Dockerfile 是官方名字,我们启动的时候就会自动寻找这个名字。

[root@Jsxs jsxs]# touch readme.md
[root@Jsxs jsxs]# vim Dockerfile
FROM centos:7
MAINTAINER jsxs<2261203961@qq.com>
COPY readme.md /usr/local/readme.md
ADD jdk-8u202-linux-x64.tar.gz /usr/local
ADD apache-tomcat-9.0.73.tar.gz /usr/local
RUN yum -y install vim
ENV MYPATH /usr/local
WORKDIR $MYPATH
ENV JAVA_HOME /usr/local/jdk1.8.0_202
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.73
ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.73
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
EXPOSE 8080
CMD /usr/local/apache-tomcat-9.0.73/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.73/logs/catalina.out
(3).构建镜像
docker build -f Dockerfile -t diytomcat .
[+] Building 53.8s (11/11) FINISHED
 => [internal] load .dockerignore                                                                                                                                    0.1s
 => => transferring context: 2B                                                                                                                                      0.0s
 => [internal] load build definition from Dockerfile                                                                                                                 0.2s
 => => transferring dockerfile: 665B                                                                                                                                 0.0s
 => [internal] load metadata for docker.io/library/centos:7                                                                                                         15.4s
 => [internal] load build context                                                                                                                                    4.0s
 => => transferring context: 190.09MB                                                                                                                                3.3s
 => CACHED [1/6] FROM docker.io/library/centos:7@sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987                                             0.0s
 => [2/6] COPY readme.md /usr/local/readme.md                                                                                                                        0.2s
 => [3/6] ADD jdk-8u202-linux-x64.rpm /usr/local                                                                                                                     3.6s
 => [4/6] ADD apache-tomcat-9.0.73.tar.gz /usr/local                                                                                                                 1.1s
 => [5/6] RUN yum -y install vim                                                                                                                                    20.3s
 => [6/6] WORKDIR /usr/local                                                                                                                                         0.2s
 => exporting to image                                                                                                                                               8.1s
 => => exporting layers                                                                                                                                              7.8s
 => => writing image sha256:0e1f35086fef111eaeda3d551188b84844a446a49bd6d24ef4319b16f06458ce                                                                         0.0s
 => => naming to docker.io/library/diytomcat             
(4).创建容器并开启
docker run -d -p 9090:8080 --name jsxstomcat -v /home/jsxs/test:/usr/local/apache-tomcat-9.0.73/webapps/test -v /home/jsxs/testlog/:/usr/local/apache-tomcat-9.0.73/logs diytomcat
----------------
# 解释,第一个挂载是将webapps下的test映射到本地tomcat下的test
-v /home/eric/build/tomcat/test:/usr/local/apache-tomcat-9.0.8/webapps/test
# 解释,第二个挂载是将apache-tomcat-9.0.8下的日志映射到本地tomcat下的tomcatlogs
-v /home/eric/build/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.8/logs 
----------------------
(5).开启容器
[root@Jsxs ~]# docker exec -it 52300f8c0167b2d23726c7acb135081462a1a31cf31d1756654176f6f43c7f32 /bin/bash
[root@52300f8c0167 local]# pwd
/usr/local
[root@52300f8c0167 local]# ls
apache-tomcat-9.0.73  bin  etc  games  include  jdk1.8.0_202  lib  lib64  libexec  readme.md  sbin  share  src

外网访问测试成功!!

(6).发布项目

(由于做了卷挂载,我们直接在本地编写项目就可以发布了)

[root@Jsxs test]# mkdir WEB-INF
[root@Jsxs test]# ls
WEB-INF
[root@Jsxs test]# cd WEB-INF/
[root@Jsxs WEB-INF]# vim web.xml
[root@Jsxs WEB-INF]# cat web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
</web-app>
[root@Jsxs WEB-INF]# cd ..
[root@Jsxs test]# vim index.jsp
[root@Jsxs test]# cat index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hello. xiaofan</title>
</head>
<body>
Hello World!<br/>
<%
System.out.println("-----my test web logs------");
%>
</body>
</html>
-----------------------------------------------------------------------------------
# 这是web.xml内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
</web-app>
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
# 这是index.jsp内容
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>hello. xiaofan</title>
</head>
<body>
Hello World!<br/>
<%
System.out.println("-----my test web logs------");
%>
</body>
</html>
------------------------------------------------------------------

ls -tail -f   #可以动态查看列表

发现:项目部署成功, 可以直接访问ok!

我们以后开发的步骤:需要掌握Dockerfile的编写! 我们之后的一切都是使用docker进行来发布运行的!

相关文章
|
2月前
|
运维 安全 开发者
Docker
Docker
30 1
|
运维 Java 程序员
【docker】对docker的理解
【docker】对docker的理解
|
6月前
|
存储 运维 Linux
|
运维 持续交付 开发者
Docker 的前世今生
Docker 的前世今生
59 0
|
Docker 容器 Kubernetes
docker
docker
122 0
|
Docker 容器
Docker 初识
Docker 初识
57 0
|
安全 Linux Docker
认识 Docker 的世界
Docker 简介:Docker 是一种运行于 Linux 和 Windows 上的软件,用于创建、管理和编排容器。 Docker 是在 GitHub 上开发的 Moby 开源项目的一部分。 Docker 公司,位于旧金山,是整个 Moby 开源项目的维护者。Docker 公司还提供商业版和社区版。Docker运行时与编排引擎:Docker 引擎是用于运行和...
248 1
认识 Docker 的世界
|
Linux 测试技术 API
【Docker】什么是Docker,它用来干什么
【Docker】什么是Docker,它用来干什么
339 0
|
存储 运维 关系型数据库
Docker(2)
Docker(2)
|
存储 安全 测试技术
Docker初步了解
Docker初步了解
76 0