阿里云部署Docker(9)----Dockerfile脚本定制镜像

本文详细介绍Dockerfile的编写方法及各指令的使用技巧,包括如何使用FROM、RUN、CMD、EXPOSE等指令来构建自己的Docker镜像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文为原创文章,转载需注明转自:http://blog.youkuaiyun.com/minimicall?viewmode=contents

技术爱好者都是比较懒的。而docker又是开发者支持起来的。所以,它肯定是有比较懒的方式供我们定制自己需要的东西。

docker build

docker 用build指令来执行dockerfile脚本。

具体的用法:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. sudo docker build .  
小心后面那个点,表示当前目录。当前目录有一个Dockerfile的文件。

当然,你可以指定你建立的镜像的名字。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. sudo docker build -t shykes/myapp .  

然后你就可以看到执行过程,或许,会非常的漫长,取决于要下的东西的大小和你的网速。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. sudo docker build -t SvenDowideit/ambassador .  
  2. Uploading context 10.24 kB  
  3. Uploading context  
  4. Step 1 : FROM docker-ut  
  5.  ---> cbba202fe96b  
  6. Step 2 : MAINTAINER SvenDowideit@home.org.au  
  7.  ---> Using cache  
  8.  ---> 51182097be13  
  9. Step 3 : CMD env | grep _TCP= | sed 's/.*_PORT_\([0-9]*\)_TCP=tcp:\/\/\(.*\):\(.*\)/socat TCP4-LISTEN:\1,fork,reuseaddr TCP4:\2:\3 \&/'  | sh && top  
  10.  ---> Using cache  
  11.  ---> 1a5ffc17324d  
  12. Successfully built 1a5ffc17324d  
当你下完之后,你可以用:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. docker images  

你会发现多了你下载的镜像。

好接下来我们讲讲Dockerfile本身如何编写。

格式:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. # Comment  
  2. INSTRUCTION arguments  
命令是大写的。

FROM

所有的镜像都应该是基于某一个现有的镜像。

所以,就有了FROM 指令

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. FROM <image>  
或者,更加具体点说明它的Tag。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. FROM <Image>:<TAG>  

FROM语句必须是第一句“非注释”语句,在Dockerfile中。

我们总是会想在一个脚本里面添加些注释,来说明一些想说的话。


注释

那就是注释:#开头的行。

但是#在行中,则却表示是一个参数。

维护

接下来,需要说明维护人。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. MAINTAINER <name>  
填上你的NICK NAME。表示你做的。

RUN指令

RUN指令应该是用的最多的指令。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. RUN <command> (the command is run in a shell - /bin/sh -c - shell form)  

另一种方式是:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. RUN ["executable""param1""param2"] (exec form)  

RUN语句会在当前镜像的基础上执行该条指令,同时执行完就成了一个新的镜像一样,即数据和影响都是会保存的,然后用这个新的镜像去执行下一条指令,这样上一条的结果镜像是下一条指令的基础,如此不断推进。

CMD指令

格式:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. CMD ["executable","param1","param2"] (exec form, this is the preferred form)  
  2. CMD ["param1","param2"] (as default parameters to ENTRYPOINT)  
  3. CMD command param1 param2 (shell form)  

有三种形式。

CMD在DOckerfile里面只能用一次,如果你写了很多条,那么只有最后一条是有效的。


CMD有什么用呢,可以理解为Main函数一样吧,作为一个入口。具体见英文

The main purpose of a CMD is to provide defaults for an executing container. These defaults can include an executable, or they can omit the executable, in which case you must specify an ENTRYPOINT instruction as well.

EXPOSE

这个单词的中文叫什么,暴露。对,他就是暴露镜像的某个接口出来。例如,我的镜像是用来做http服务的,那么我就理应暴露我镜像的80端口。然后-p 主机端口:80 ,还记得吧。端口映射。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. EXPOSE <port> [<port>...]  

ENV

环境变量的设置

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. ENV <key> <value>  

环境变量一旦设定,对整个Dockerfile都是有效的。

当然,key = value这样直接说,其实效果是一样的。

ADD指令

有点像拷贝指令,至少它就是完成文件的拷贝工作的。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. ADD <src> <dst>  

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. ADD hom* /mydir/        # adds all files starting with "hom"  
  2. ADD hom?.txt /mydir/    # ? is replaced with any single character  

COPY指令

和ADD一样,是拷贝


ENTRYPOINT

入口点

真正的MAIN函数

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. ENTRYPOINT ["executable""param1""param2"] (exec form, the preferred form)  
  2. ENTRYPOINT command param1 param2 (shell form)  
CMD和这个指令的区别,是应用的场景不一样。

这里,我给大家贴原文会比较好。

An ENTRYPOINT helps you to configure a container that you can run as an executable. That is, when you specify an ENTRYPOINT, then the whole container runs as if it was just that executable.


Unlike the behavior of the CMD instruction, The ENTRYPOINT instruction adds an entry command that will not be overwritten when arguments are passed to docker run. This allows arguments to be passed to the entry point, i.e. docker run <image> -d will pass the -d argument to the entry point.


You can specify parameters either in the ENTRYPOINT JSON array (as in "like an exec" above), or by using a CMD instruction. Parameters in the ENTRYPOINT instruction will not be overridden by the docker run arguments, but parameters specified via a CMD instruction will be overridden by docker run arguments.


Like a CMD, you can specify a plain string for the ENTRYPOINT and it will execute in /bin/sh -c:


FROM ubuntu
ENTRYPOINT ls -l
For example, that Dockerfile's image will always take a directory as an input and return a directory listing. If you wanted to make this optional but default, you could use a CMD instruction:


FROM ubuntu
CMD ["-l"]
ENTRYPOINT ["ls"]

WORKDIR 工作目录

RUN ENTERPOINT带的指令在哪里执行的设置。


此外,还有一些指令,例如

USER ,ONBUILD,等就不想说了。

最后给出一个示例

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. # Nginx  
  2. #  
  3. # VERSION               0.0.1  
  4.   
  5. FROM      ubuntu  
  6. MAINTAINER Victor Vieux <victor@docker.com>  
  7.   
  8. RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server  
  9.   
  10. # Firefox over VNC  
  11. #  
  12. # VERSION               0.3  
  13.   
  14. FROM ubuntu  
  15.   
  16. # Install vnc, xvfb in order to create a 'fake' display and firefox  
  17. RUN apt-get update && apt-get install -y x11vnc xvfb firefox  
  18. RUN mkdir /.vnc  
  19. # Setup a password  
  20. RUN x11vnc -storepasswd 1234 ~/.vnc/passwd  
  21. # Autostart firefox (might not be the best way, but it does the trick)  
  22. RUN bash -c 'echo "firefox" >> /.bashrc'  
  23.   
  24. EXPOSE 5900  
  25. CMD    ["x11vnc""-forever""-usepw""-create"]  
  26.   
  27. # Multiple images example  
  28. #  
  29. # VERSION               0.1  
  30.   
  31. FROM ubuntu  
  32. RUN echo foo > bar  
  33. # Will output something like ===> 907ad6c2736f  
  34.   
  35. FROM ubuntu  
  36. RUN echo moo > oink  
  37. # Will output something like ===> 695d7793cbe4  
  38.   
  39. # You᾿ll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with  
  40. # /oink.  





### 实现阿里云云效流水线 RuoYi-SpringBoot 项目自动化部署 #### 准备工作 为了在阿里云云效流水线上实现RuoYi-SpringBoot项目的自动化部署,需先完成一些准备工作。确保已创建好阿里云账号并开通了云效服务;同时,在本地环境中准备好待部署的Spring Boot应用源码以及Dockerfile文件用于构建镜像[^1]。 #### 创建仓库与分支策略 将RuoYi-SpringBoot工程推送到GitLab或其他支持Webhook功能的版本控制系统中去,并设置合理的分支管理策略以便于后续触发CI/CD流程。对于主要开发工作的master/main分支建议开启保护模式防止误操作影响生产环境稳定性[^2]。 #### 构建自定义Maven命令 针对特定需求定制maven打包指令,比如指定JDK版本、跳过测试用例执行等参数优化编译效率。此部分可通过`.mvn/jvm.config` 和 `.mvn/mvn.config` 文件来配置全局变量或者通过pipeline脚本动态传递给maven插件使用[^3]。 ```bash # .mvn/jvm.config -Xms512m -Xmx1024m # .mvn/mvn.config -DskipTests=true clean package ``` #### 编写Pipeline YAML配置文件 编写适用于当前场景下的持续集成管道描述文档(通常是yaml格式),明确定义各个阶段的任务列表及其依赖关系。下面是一个简单的例子展示了如何利用官方提供的Java模板快速搭建起一套完整的CICD体系结构: ```yaml version: 1.0 stages: - compile - test - build_image - deploy jobs: maven_compile_job: stage: compile steps: - mvn clean install -Dmaven.test.skip=true unit_test_job: stage: test script: - mvn test docker_build_push_job: image: registry.cn-hangzhou.aliyuncs.com/cloudnative/docker:latest stage: build_image only: changes: - &#39;**/*.java&#39; services: - docker:dind before_script: - export DOCKER_BUILDKIT=1 script: - echo "$DOCKER_PASSWORD" | docker login --username="$DOCKER_USERNAME" --password-stdin $REGISTRY_URL - cd ./ruoyi-cloud/ - docker build -t ${IMAGE_NAME}:${CI_COMMIT_REF_SLUG} . - docker push ${IMAGE_NAME}:${CI_COMMIT_REF_SLUG} kubernetes_deploy_job: type: deploy environment: name: production url: http://${K8S_SERVICE_IP}/api/v1/namespaces/${NAMESPACE}/services/${SERVICE_NAME}:http/proxy/ when: manual dependencies: - docker_build_push_job script: - apk add curl - curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl - chmod +x ./kubectl - mv ./kubectl /usr/local/bin/ - mkdir -p ~/.kube - echo "${KUBECONFIG}" >~/.kube/config - kubectl apply -f deployment.yaml ``` 上述代码片段实现了从源码拉取到最终发布上线整个过程中涉及到的关键环节控制逻辑表达[^4]。 #### Kubernetes集群中的资源对象声明 最后一步是在目标Kubernetes集群内部署应用程序所需的各类资源配置清单,如Deployment、Service、Ingress等API对象的具体属性设定。这通常保存在一个名为`deployment.yaml` 的文本文件里供kubctl工具读取解析后提交至apiserver处理请求。 ```yaml apiVersion: apps/v1 kind: Deployment metadata: labels: app: ruoyi-springboot-app name: ruoyi-springboot-deployment spec: replicas: 3 selector: matchLabels: app: ruoyi-springboot-app template: metadata: labels: app: ruoyi-springboot-app spec: containers: - envFrom: - secretRef: name: db-secret image: &#39;registry.cn-hangzhou.aliyuncs.com/my-repo/ruoyi-springboot:${TAG}&#39; livenessProbe: failureThreshold: 3 initialDelaySeconds: 40 periodSeconds: 10 successThreshold: 1 tcpSocket: port: 9090 name: ruoyi-springboot-container ports: - containerPort: 9090 --- apiVersion: v1 kind: Service metadata: annotations: service.beta.kubernetes.io/alibaba-cloud-loadbalancer-id: lb-wz9hjg**** labels: app: ruoyi-springboot-service name: ruoyi-springboot-service spec: externalTrafficPolicy: Cluster healthCheckNodePort: 30727 loadBalancerID: lb-wz9hjg**** ports: - nodePort: 30727 port: 80 protocol: TCP targetPort: 9090 selector: app: ruoyi-springboot-app sessionAffinity: None type: LoadBalancer ``` 以上就是关于如何借助阿里云云效平台实施RuoYi-SpringBoot项目的自动部署方案介绍[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值