dockerfile

Dockerfile是用来构建docker镜像的文件

构建步骤:

编写一个dockerfile文件,随后运行命令:

docker build -f 文件路径 -t 镜像名 .  # 文件名为Dockerfile时可省略且最后的.不要忽略
docker run     # 运行镜像
docker push    # 发布镜像

dockerfile构建过程

每个保留关键字(指令)必须是大写字母

执行顺序从上到下顺序执行

#号表示注释

每一行命令都会创建一个镜像层,并提交。

 Dockerfile命令

命令    效果
FROM    基础镜像:Centos/Ubuntu
MAINTAINER    镜像作者+邮箱
RUN    镜像构建的时候需要运行的命令
ADD    为镜像添加内容(压缩包)
WORKDIR    镜像工作目录(进入容器时的目录)
VOLUME    挂载的目录
EXPOSE    暴露端口配置
CMD/ENTRYPOINT    指定这个容器启动时要运行的命令(CMD替代先前命令,ENTRYPOINT在先前命令后追加,前者替换 后者追加 如:> 和>>)

ONBUILD      当运行一个被继承 dockerfile 这个时候就会运行ONBUILD,是一个触发指令
COPY    类似于ADD,将文件拷贝到镜像中
ENV    构建时设置环境变量   docker run 时的 -e


构建一个自己的centos

编写一个dockerfile文件

FROM centos:7             
MAINTAINER feng<2039777404@qq.com
ENV MYPATH /usr/local  #构建变量
WORKDIR $MYPATH  #进入容器时的目录
RUN yum -y install vim
RUN yum -y install net-tools   #下载ifconfig相关
EXPOSE 80            暴漏端口
CMD echo $MYRATH
CMD echo ".....end...."
CMD /bin/bash   

docker build -f 文件路径 -t 镜像名 . # 文件名为Dockerfile时可省略且最后的.不要忽略

docker build -f mydockerfile-centos -t mycentos:0.1 .  #在自己编写的文件路径下执行

#注意 如果用的是centos8 且yum源用的是官方或腾讯(我的是腾讯源报错) 后边会报错

Error: Failed to download metadata for repo 'appstream': Cannot prepare internal mirrorlist: No URLs in mirrorlist 

yum源改成阿里的

也可以修改CentOS-Linux-BaseOS.repo  CentOS-Linux-AppStream.repo

使用vault.centos.org代替mirror.centos.org

构建成功

Successfully built 5b99f75014d2
Successfully tagged mycentos:0.1

创建运行容器

[root@shuju dockerfile]# docker run -it mycentos:0.1
[root@5ef5a5019030 local]# pwd  #默认工作目录
/usr/local
[root@5ef5a5019030 local]# ifconfig    #有了ifconfig等命令 vim也能用了
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 656 (656.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

列出本地镜像的变更历史

docker history 镜像id/名

CMD和ENTRYPOINT区别

CMD

 cat dockerfile-cnetos
FROM centos:7
CMD ["ls","-a"]

docker build -f dockerfile-cnetos -t text .

docker run 3486cc7f575e
.
..
.dockerenv
anaconda-post.log
bin
dev
。。。。。。

ls -a 生效

想追加一个命令 -l替换了ls -a 而-l 不是命令所以报错 

docker run 3486cc7f575e -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

ENTRYPOINT

cat dockerfile-text
FROM centos:7
ENTRYPOINT ["ls","-a"]

docker build -f dockerfile-text -t text1 .

 docker run 3e6b761e58f4 -l  #-l 直接追加到ls -a后 变更成 ls -a -l
total 12
drwxr-xr-x   1 root root     6 May 14 21:21 .
drwxr-xr-x   1 root root     6 May 14 21:21 ..
-rwxr-xr-x   1 root root     0 May 14 21:21 .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

tomcat镜像制作

1、准备tomcat jdk 压缩包 (jdk linux版)

apache-tomcat-9.0.62.tar.gz  jdk-18_linux-x64_bin.tar.gz

2、编写dockerfile文件 (如果文件命名为Dockerfile 启动时就不用加-f了 其会自动寻找Dockerfile文件)

FROM centos:7
MAINTAINER feng<2039777404@qq.com
COPY readme.txt /usr/local/readme.txt
ADD jdk-18_linux-x64_bin.tar.gz /usr/local/
ADD apache-tomcat-9.0.62.tar.gz /usr/local/
RUN yum -y install vim
ENV mypath /usr/local
WORKDIR $mypath
ENV JAVA_HOME /usr/local/jdk-18.0.1.1
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.62
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/bin/logs/catalina.out

3、构建镜像  两个压缩包要与Dockerfile文件在同一目录

docker build -t mytomcat .  #因为文件名为Dockerfile 省略-f

4、创建运行容器 

run -d -p 9090:8080 --name fengtomcat -v /root/tomcat/test:/usr/local/apache-tomcat-9.0.62/webapps/test -v /root/tomcat/tomcatlogs:/urs/local/apache-tomcat-9.0.62/logs mytomcat

5、发布项目可以直接在本机test里发布

test 文件里 

[root@shuju test]# ls
index.jsp  WEB-INF(文件夹放xml文件)

[root@shuju 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>菜鸟教程(runoob.com)</title>
</head>
<body>
Hello World!<br/>
<%
System.out.println(" my web logs");
%>
</body>
</html>

[root@shuju test]# cd WEB-INF/
[root@shuju WEB-INF]# ls
web.xml
[root@shuju WEB-INF]# cat 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>

6、日志查看

在tomcatlog文件里的

[root@shuju tomcat]# cd tomcatlogs1
[root@shuju tomcatlogs]# ls
catalina.2022-05-15.log  host-manager.2022-05-15.log  localhost_access_log.2022-05-15.txt
catalina.out             localhost.2022-05-15.log     manager.2022-05-15.log

我们设的日志文件为catalina.out  查看日志

发布镜像到Dockerhub

1、地址:https://hub.docker.com 注册登录自己的账号

2、在服务器上提交自己的镜像

[root@shuju tomcatlogs1]# 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 登录名  #输入密码显示登录成功或失败

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

提交镜像

docker push 账户名/镜像名:版本号

[root@shuju tomcatlogs1]# docker push fengjiashuai/mytomcat:1.0
The push refers to repository [docker.io/fengjiashuai/mytomcat]
An image does not exist locally with the tag: fengjiashuai/mytomcat

要带上版本号 新建一个标签

 docker tag fa14d228c2fd fengjiashuai/tomcat:1.0  

[root@shuju tomcatlogs1]# docker images
REPOSITORY            TAG       IMAGE ID       CREATED             SIZE
fengjiashuai/tomcat   1.0       fa14d228c2fd   About an hour ago   760MB
mytomcat              latest    fa14d228c2fd   About an hour ago   760MB

 docker push fengjiashuai/tomcat:1.0   #成功发布

[root@shuju tomcatlogs1]# docker logout   #退出登录
Removing login credentials for https://index.docker.io/v1/

阿里云镜像服务上

登录阿里云

找到容器镜像服务

在个人实例 镜像仓库里创建镜像仓库

选本地仓库

操作指南有如何发布的命令

登录

[root@shuju tomcatlogs1]# docker login --username=aliyun9727599389 registry.cn-hangzhou.aliyuncs.com
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

然后 设置一个有版本号的镜像push

docker tag 镜像id registry.cn-hangzhou.aliyuncs.com/命名空间名/镜像名:版本

docker tag fa14d228c2fd registry.cn-hangzhou.aliyuncs.com/fengjiashuai/mytomcat:1.0 #registry.cn-hangzhou.aliyuncs.com 路径  fengjiashuai 镜像空间名字 

docker push registry.cn-hangzhou.aliyuncs.com/fengjiashuai/mytomcat:1.0 

 

posted @   忆笙歌  阅读(78)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示