CMD 和 ENTRYPOINT区别
CMD # 指定这个容器启动的时候要运行的命令,只有最后一个会生效,可被替代。 ENTRYPOINT # 指定这个容器启动的时候要运行的命令,可以追加命令
测试cmd
# 编写dockerfile文件 [root@centos-7 dockerfile]# vim dockerfile-cmd-test FROM centos:7.9.2009 CMD ["ls","-a"] # 启动后执行 ls -a 命令 # 构建镜像 [root@centos-7 dockerfile]# docker build -f dockerfile-cmd-test -t cmd-test:0.1 . Sending build context to Docker daemon 3.072kB Step 1/2 : FROM centos:7.9.2009 ---> eeb6ee3f44bd Step 2/2 : CMD ["ls","-a"] ---> Running in 185e06e96419 Removing intermediate container 185e06e96419 ---> 93138f328e47 Successfully built 93138f328e47 Successfully tagged cmd-test:0.1
# 运行镜像 [root@centos-7 dockerfile]# docker run 93138f328e47 . .. .dockerenv anaconda-post.log bin ... var
# 想追加一个命令 -l 成为ls -al:展示列表详细数据 [root@centos-7 dockerfile]# docker run 93138f328e47 -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. ERRO[0000] error waiting for container: context canceled # cmd的情况下 -l 替换了CMD["ls","-l"] 而 -l 不是命令所以报错 [root@centos-7 dockerfile]# docker run 93138f328e47 ls -al total 12 drwxr-xr-x. 1 root root 6 May 22 05:45 . ... drwxr-xr-x. 18 root root 238 Nov 13 2020 var
测试entrypoint
[root@centos-7 dockerfile]# vim dockerfile-cmd-entrypoint FROM centos:7.9.2009 ENTRYPOINT ["ls","-a"] [root@centos-7 dockerfile]# docker build -f dockerfile-cmd-entrypoint -t entrypoint-test:0.1 . Sending build context to Docker daemon 4.096kB Step 1/2 : FROM centos:7.9.2009 ---> eeb6ee3f44bd Step 2/2 : ENTRYPOINT ["ls","-a"] ---> Running in 9ab90f566fc3 Removing intermediate container 9ab90f566fc3 ---> c96e368b33ab Successfully built c96e368b33ab Successfully tagged entrypoint-test:0.1
[root@centos-7 dockerfile]# docker run c96e368b33ab . .. .dockerenv anaconda-post.log ... var
# 我们的命令,是直接拼接在我们得ENTRYPOINT命令后面的 [root@centos-7 dockerfile]# docker run c96e368b33ab -l total 12 drwxr-xr-x. 1 root root 6 May 22 05:45 . ... drwxr-xr-x. 18 root root 238 Nov 13 2020 var
实战:Tomcat镜像
下载
jdk-8u231-linux-x64.rpm/jdk-8u231-linux-x64.tar.gz 文件
1、准备镜像文件
准备tomcat 和 jdk 到当前目录,编写好README
[root@centos-7 tomcat]# ll 总用量 185532 -rwxrw-rw-. 1 root root 11560971 5月 8 18:39 apache-tomcat-9.0.62.tar.gz -rwxrw-rw-. 1 root root 178418154 5月 8 19:00 jdk-8u202-linux-x64.rpm -rw-r--r--. 1 root root 0 5月 22 14:00 README
2、编写Dokerfile,官方命名Dokerfile,build会自动寻找这个文件,就不需要-f指定了
$ vim Dockerfile FROM centos:7.9.2009 # 基础镜像centos MAINTAINER jsss<3063494684@qq.com> # 作者 COPY README /usr/local/README # 复制README文件 ADD jdk-8u231-linux-x64.tar.gz /usr/local/ # 添加jdk,ADD 命令会自动解压 ADD apache-tomcat-9.0.62.tar.gz /usr/local/ # 添加tomcat,ADD 命令会自动解压 RUN yum -y install vim # 安装 vim 命令 ENV MYPATH /usr/local # 环境变量设置 工作目录 WORKDIR $MYPATH ENV JAVA_HOME /usr/local/jdk1.8.0_231 # 环境变量: JAVA_HOME环境变量 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.62 # 环境变量: tomcat环境变量 ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.62 # 设置环境变量 分隔符是: ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin EXPOSE 8080 # 设置暴露的端口 CMD /usr/local/apache-tomcat-9.0.62/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.62/logs/catalina.out # 设置默认命令
3、构建镜像
# 因为dockerfile命名使用默认命名 因此不用使用-f 指定文件 [root@centos-7 tomcat]# docker build -t diytomcat . Sending build context to Docker daemon 205.5MB Successfully built cfec22eba496 Successfully tagged diytomcat:latest
[root@centos-7 tomcat]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE diytomcat latest cfec22eba496 About a minute ago 849MB
4、run镜像
# -d:后台运行 -p:暴露端口 --name:别名 -v:绑定路径 # docker run -d -p 9090:8080 --name mytomcat -v /home/kuangstudy/tomcat/test://usr/local/apache-tomcat-9.0.62/webapps/test -v /home/kuangstudy/tomcat/tomcatlogs/:/usr/local/apache-tomcat-9.0.62/logs diytomcat e25884de29385c03bff3e4ac7f7a2e9b5baea9ced24a4b0aa8518817150f3fb8
# ls apache-tomcat-9.0.62.tar.gz jdk-8u231-linux-x64.tar.gz test Dockerfile README tomcatlogs
5、访问测试
$ docker exec -it 自定义容器的id /bin/bash
[root@centos-7 tomcat]# docker exec -it e25884de293 /bin/bash [root@e25884de2938 local]# ls -l total 0 -rw-r--r--. 1 root root 0 May 22 06:00 README drwxr-xr-x. 1 root root 45 May 22 06:54 apache-tomcat-9.0.62 drwxr-xr-x. 2 root root 6 Apr 11 2018 bin drwxr-xr-x. 2 root root 6 Apr 11 2018 etc drwxr-xr-x. 2 root root 6 Apr 11 2018 games drwxr-xr-x. 2 root root 6 Apr 11 2018 include drwxr-xr-x. 7 501 games 245 Oct 5 2019 jdk1.8.0_231 drwxr-xr-x. 2 root root 6 Apr 11 2018 lib drwxr-xr-x. 2 root root 6 Apr 11 2018 lib64 drwxr-xr-x. 2 root root 6 Apr 11 2018 libexec drwxr-xr-x. 2 root root 6 Apr 11 2018 sbin drwxr-xr-x. 5 root root 49 Nov 13 2020 share drwxr-xr-x. 2 root root 6 Apr 11 2018 src [root@e25884de2938 local]# cd apache-tomcat-9.0.62/ [root@e25884de2938 apache-tomcat-9.0.62]# ls BUILDING.txt LICENSE README.md RUNNING.txt conf logs webapps CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib temp work [root@e25884de2938 apache-tomcat-9.0.62]# ls -l total 128 -rw-r-----. 1 root root 18980 Mar 31 14:34 BUILDING.txt -rw-r-----. 1 root root 6210 Mar 31 14:34 CONTRIBUTING.md -rw-r-----. 1 root root 57092 Mar 31 14:34 LICENSE -rw-r-----. 1 root root 2333 Mar 31 14:34 NOTICE -rw-r-----. 1 root root 3378 Mar 31 14:34 README.md -rw-r-----. 1 root root 6898 Mar 31 14:34 RELEASE-NOTES -rw-r-----. 1 root root 16497 Mar 31 14:34 RUNNING.txt drwxr-x---. 2 root root 4096 Mar 31 14:34 bin drwx------. 1 root root 22 May 22 07:03 conf drwxr-x---. 2 root root 4096 Mar 31 14:34 lib drwxr-xr-x. 2 root root 197 May 22 07:03 logs drwxr-x---. 2 root root 30 Mar 31 14:34 temp drwxr-x---. 1 root root 18 May 22 07:03 webapps drwxr-x---. 1 root root 22 May 22 07:03 work [root@e25884de2938 apache-tomcat-9.0.62]# exit exit
$ cul localhost:8080
[root@centos-7 tomcat]# curl localhost:9090 <!DOCTYPE html> <html lang="en"> ... </html> [root@centos-7 tomcat]#
6、发布项目
(由于做了卷挂载,我们直接在本地编写项目就可以发布了!)
发现:项目部署成功,可以直接访问!
我们以后开发的步骤:需要掌握Dockerfile的编写!我们之后的一切都是使用docker镜像来发布运行!
[root@centos-7 tomcat]# cd test/ [root@centos-7 test]# mkdir WEB_INF [root@centos-7 test]# cd WEB_INF/ [root@centos-7 WEB_INF]# vim web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> </web-app>
[root@centos-7 test]# vim index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta charset="utf-8"%> <title>Hello.jsss</title> </head> <body> <h2>Hello World!</h2> <% System.out.println("----my test web logs----"); %> </body> </html>
[root@centos-7 test]# docker run -d -p 9090:8080 diytomcat bef08adfae1f7d0f6efa020ec263fec60c303b28b3b4e3208bbb95dc455b57e7 [root@centos-7 test]# curl localhost:9090/test <!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> The requested resource [/test] is not available</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/9.0.62</h3></body></html>[root@centos-7 test]#
[root@centos-7 test]# cd .. [root@centos-7 tomcat]# cd tomcatlogs/ [root@centos-7 tomcatlogs]# ll 总用量 40 -rw-r-----. 1 root root 15827 5月 22 15:28 catalina.2022-05-22.log -rw-r-----. 1 root root 15827 5月 22 15:28 catalina.out -rw-r-----. 1 root root 0 5月 22 15:03 host-manager.2022-05-22.log -rw-r-----. 1 root root 408 5月 22 15:03 localhost.2022-05-22.log -rw-r-----. 1 root root 827 5月 22 15:09 localhost_access_log.2022-05-22.txt -rw-r-----. 1 root root 0 5月 22 15:03 manager.2022-05-22.log [root@centos-7 tomcatlogs]#
发布自己的镜像
发布到 Docker Hub
2、确定这个账号可以登录
3、登录
$ docker login --help Usage: docker login [OPTIONS] [SERVER] Log in to a Docker registry. If no server is specified, the default is defined by the daemon. Options: -p, --password string Password --password-stdin Take the password from stdin -u, --username string Username
$ docker login -u 你的用户名 -p 你的密码 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
4、提交 push镜像
# 会发现push不上去,因为如果没有前缀的话默认是push到 官方的library # 解决方法: # 第一种 build的时候添加你的dockerhub用户名,然后在push就可以放到自己的仓库了 $ docker build -t kuangshen/mytomcat:0.1 . # 第二种 使用docker tag #然后再次push $ docker tag 容器id kuangshen/mytomcat:1.0 #然后再次push $ docker push kuangshen/mytomcat:1.0
退登
[root@centos-7 ~]# docker logout Removing login credentials for https://index.docker.io/v1/
发布到 阿里云镜像服务上
1.登录阿里云
https://cr.console.aliyun.com/repository/
2.找到容器镜像服务
第一次
3.创建命名空间
4.创建容器镜像
5.浏览阿里云
安装官网提示的来
$ docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/jsss-study/kuangshen-test:[镜像版本号] $ docker push registry.cn-hangzhou.aliyuncs.com/jsss-study/kuangshen-test:[镜像版本号] # 修改id 和 版本 $ docker tag cfec22eba496 registry.cn-hangzhou.aliyuncs.com/jsss-study/kuangshen-test:1.0 # 提交 docker push registry.cn-hangzhou.aliyuncs.com/jsss-study/kuangshen-test:1.0
小结
Docker网络
理解Docker 0
学习之前清空下前面的docker 镜像、容器
# 删除全部容器 $ docker rm -f $(docker ps -aq) # 删除全部镜像 $ docker rmi -f $(docker images -aq)
测试
三个网络
问题: docker 是如果处理容器网络访问的?
# 测试 运行一个tomcat $ docker run -d -P --name tomcat01 tomcat # 查看容器内部网络地址 $ docker exec -it 容器id ip addr # 发现容器启动的时候会得到一个 eth0@if91 ip地址,docker分配! $ ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 261: eth0@if91: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 172.18.0.2/16 brd 172.18.255.255 scope global eth0 valid_lft forever preferred_lft forever # 思考? linux能不能ping通容器内部! 可以 容器内部可以ping通外界吗? 可以! $ ping 172.18.0.2 PING 172.18.0.2 (172.18.0.2) 56(84) bytes of data. 64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.069 ms 64 bytes from 172.18.0.2: icmp_seq=2 ttl=64 time=0.074 ms