用dockerfile配置生成docker image并实现容器部署

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介:

前言:

       docker有个dockerfile 是什么东西,为什么我们要用他? 


简单的说:  以前咱们docker run创建一个docker容器,有时候会附带不少的参数。 


比如:

1
docker run -d -p 22 -p 8080:8080 ruifengyun /ubunt-redis  "redis-server redis.conf && /usr/sbin/sshd -D"


会发现很是麻烦。  这个时候咱们可以创建编辑一个dockerfile文件,生成新的image,然后从这个新的镜像创建容器,容器里面相关联的启动项和端口,目录都是提前定义好的。

原文: http://rfyiamcool.blog.51cto.com/1030776/1541081 

cat Dockerfile

1
2
3
4
5
6
7
8
9
10
11
#配置redis
FROM ubuntu
MAINTAINER ruifengyun  "ruifengyun@qq.com"
ADD . /start .sh   /root/start .sh
RUN apt-get update
RUN apt-get  install  -y redis-server
RUN apt-get  install  -y openssh-server
#CMD redis-server /etc/redis/redis.conf && /usr/sbin/sshd -D
CMD [ "redis-server" , "/etc/redis/redis.conf" ]
EXPOSE 6379
EXPOSE 22


FROM   是作为镜像的基础

RUN    可以理解为在FROM下来的镜像做一些环境的部署。

CMD    是创建容器后,会运行的命令

EXPOSE 是暴露的端口

MAINTAINER 通知的邮件

ADD    相当于把主机的start.sh脚本传递给了容器里面。

VOLUME  是本地的路径的映射

WORKDIR 是执行的路径,也就是cmd entrypoint执行的路径。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
root@dev-ops: /var/4 # docker build -t rui Dockerfile
Uploading context 2.048 kB
Uploading context
2014 /08/16  09:50:59 Error:  open  /tmp/docker-build829651796/Dockerfile : not a directory
root@dev-ops: /var/4 # docker build -t rui/redis .
Uploading context 2.56 kB
Uploading context
Step 0 : FROM ubuntu
  ---> c4ff7513909d
Step 1 : MAINTAINER Victor Coisne victor.coisne@dotcloud.com
  ---> Using cache
  ---> bbe0c91632f1
Step 2 : RUN apt-get update
  ---> Running  in  b10a1a60dcb3
Ign http: //archive .ubuntu.com trusty InRelease
Ign http: //archive .ubuntu.com trusty-updates InRelease
Ign http: //archive .ubuntu.com trusty-security InRelease
Get:1 http: //archive .ubuntu.com trusty Release.gpg [933 B]
Get:2 http: //archive .ubuntu.com trusty-updates Release.gpg [933 B]
Get:3 http: //archive .ubuntu.com trusty-security Release.gpg [933 B]
Get:4 http: //archive .ubuntu.com trusty Release [58.5 kB]
Get:5 http: //archive .ubuntu.com trusty-updates Release [59.7 kB]
Get:6 http: //archive .ubuntu.com trusty-security Release [59.7 kB]
Get:7 http: //archive .ubuntu.com trusty /main  Sources [1335 kB]
Get:8 http: //archive .ubuntu.com trusty /restricted  Sources [5335 B]
Get:9 http: //archive .ubuntu.com trusty /universe  Sources [7926 kB]
Get:10 http: //archive .ubuntu.com trusty /main  amd64 Packages [1743 kB]
Get:11 http: //archive .ubuntu.com trusty /restricted  amd64 Packages [16.0 kB]
Get:12 http: //archive .ubuntu.com trusty /universe  amd64 Packages [7589 kB]
Get:13 http: //archive .ubuntu.com trusty-updates /main  Sources [138 kB]
Get:14 http: //archive .ubuntu.com trusty-updates /restricted  Sources [1250 B]
Get:15 http: //archive .ubuntu.com trusty-updates /universe  Sources [91.7 kB]
Get:16 http: //archive .ubuntu.com trusty-updates /main  amd64 Packages [375 kB]
Get:17 http: //archive .ubuntu.com trusty-updates /restricted  amd64 Packages [6341 B]
Get:18 http: //archive .ubuntu.com trusty-updates /universe  amd64 Packages [235 kB]
Get:19 http: //archive .ubuntu.com trusty-security /main  Sources [47.4 kB]
Get:20 http: //archive .ubuntu.com trusty-security /restricted  Sources [40 B]
Get:21 http: //archive .ubuntu.com trusty-security /universe  Sources [11.9 kB]
Get:22 http: //archive .ubuntu.com trusty-security /main  amd64 Packages [167 kB]
Get:23 http: //archive .ubuntu.com trusty-security /restricted  amd64 Packages [40 B]
Get:24 http: //archive .ubuntu.com trusty-security /universe  amd64 Packages [57.0 kB]
Fetched 19.9 MB  in  11min 48s (28.1 kB /s )
Reading package lists...
  ---> 9ce87ae24eeb
Step 3 : RUN apt-get  install  -y redis-server
  ---> Running  in  b28a88665c3f
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
   libjemalloc1 redis-tools
The following NEW packages will be installed:
   libjemalloc1 redis-server redis-tools
0 upgraded, 3 newly installed, 0 to remove and 3 not upgraded.
Need to get 410 kB of archives.
After this operation, 1272 kB of additional disk space will be used.
Get:1 http: //archive .ubuntu.com /ubuntu/  trusty /universe  libjemalloc1 amd64 3.5.1-2 [76.8 kB]
Get:2 http: //archive .ubuntu.com /ubuntu/  trusty /universe  redis-tools amd64 2:2.8.4-2 [65.7 kB]
Get:3 http: //archive .ubuntu.com /ubuntu/  trusty /universe  redis-server amd64 2:2.8.4-2 [267 kB]
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not  set , so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling  tty .)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re- open  stdin:
Fetched 410 kB  in  4s (91.4 kB /s )
Selecting previously unselected package libjemalloc1.
(Reading database ... 11518 files and directories currently installed.)
Preparing to unpack ... /libjemalloc1_3 .5.1-2_amd64.deb ...
Unpacking libjemalloc1 (3.5.1-2) ...
Selecting previously unselected package redis-tools.
Preparing to unpack ... /redis-tools_2 %3a2.8.4-2_amd64.deb ...
Unpacking redis-tools (2:2.8.4-2) ...
Selecting previously unselected package redis-server.
Preparing to unpack ... /redis-server_2 %3a2.8.4-2_amd64.deb ...
Unpacking redis-server (2:2.8.4-2) ...
Processing triggers  for  ureadahead (0.100.0-16) ...
Setting up libjemalloc1 (3.5.1-2) ...
Setting up redis-tools (2:2.8.4-2) ...
Setting up redis-server (2:2.8.4-2) ...
invoke-rc.d: policy-rc.d denied execution of start.
Processing triggers  for  libc-bin (2.19-0ubuntu6.1) ...
Processing triggers  for  ureadahead (0.100.0-16) ...
  ---> d37fb2bbe0b5
Step 4 : ENTRYPOINT redis-server  /etc/redis/redis .conf &&  /usr/sbin/sshd  -D
  ---> Running  in  f6c027ac643d
  ---> ec7fe19bdfed
Step 5 : USER daemon
  ---> Running  in  0e3b10d07a16
  ---> d16398d08a4a
Step 6 : EXPOSE 6379
  ---> Running  in  c8ca52dde189
  ---> e0a9bcb25972
Step 7 : EXPOSE 22
  ---> Running  in  22845a6abd90
  ---> 54bb130c7a44
Successfully built 54bb130c7a44
Removing intermediate container b10a1a60dcb3
Removing intermediate container b28a88665c3f
Removing intermediate container f6c027ac643d
Removing intermediate container 0e3b10d07a16
Removing intermediate container c8ca52dde189
Removing intermediate container 22845a6abd90
root@dev-ops: /var/4 #
原文: http: //rfyiamcool .blog.51cto.com /1030776/1541081


等折腾完了后,他会生成一个镜像 。 这个镜像是由咱们的dockerfile搞的。

wKioL1PvF17B2sFBAAE_w3QKOcY684.jpg

原文: http://rfyiamcool.blog.51cto.com/1030776/1541081 

这次咱们再创建容器,不用再加那么多参数了。

1
2
3
4
5
6
7
root@dev-ops:~ # docker run -d -P rui
116b30b056493237caca158849ae687c9beb4f8656be485c2a3cc71a27d8e951
root@dev-ops:~
root@dev-ops:~
root@dev-ops:~ # docker ps -a
CONTAINER ID        IMAGE                           COMMAND                CREATED             STATUS              PORTS                      NAMES
116b30b05649        rui:latest                      redis-server  /etc/redis/redis .conf    4 seconds ago       Up 3 seconds        0.0.0.0:49153->6379 /tcp    nostalgic_lumiere


咱们再来一个比较全的dockerfile例子:

功能是用来部署lnmp和wordpress  ,配置看起来多 ,其实还是比较规范的。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
原文: http: //rfyiamcool .blog.51cto.com /1030776/1541081 
FROM ubuntu:14.04
MAINTAINER liudehua <liudehua@xxx.com>
 
# Keep upstart from complaining
RUN dpkg-divert -- local  --rename --add  /sbin/initctl
RUN  ln  -sf  /bin/true  /sbin/initctl
 
# Let the conatiner know that there is no tty
ENV DEBIAN_FRONTEND noninteractive
 
RUN apt-get update
RUN apt-get -y upgrade
 
# Basic Requirements
RUN apt-get -y  install  mysql-server mysql-client nginx php5-fpm php5-mysql php-apc pwgen python-setuptools curl git unzip
 
# Wordpress Requirements
RUN apt-get -y  install  php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5- ps  php5-pspell php5-recode php5-sqlite php5-tidy php5-xmlrpc php5-xsl
 
# mysql config
RUN  sed  -i -e "s/^bind-address\s*=\s*127.0.0.1/bind-address = 0.0.0.0/"  /etc/mysql/my .cnf
 
# nginx config
RUN  sed  -i -e "s/keepalive_timeout\s*65/keepalive_timeout 2/"  /etc/nginx/nginx .conf
RUN  sed  -i -e "s/keepalive_timeout 2/keepalive_timeout 2;\n\tclient_max_body_size 100m/"  /etc/nginx/nginx .conf
RUN  echo  "daemon off;"  >>  /etc/nginx/nginx .conf
 
# php-fpm config
RUN  sed  -i -e  "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g"  /etc/php5/fpm/php .ini
RUN  sed  -i -e  "s/upload_max_filesize\s*=\s*2M/upload_max_filesize = 100M/g"  /etc/php5/fpm/php .ini
RUN  sed  -i -e  "s/post_max_size\s*=\s*8M/post_max_size = 100M/g"  /etc/php5/fpm/php .ini
RUN  sed  -i -e  "s/;daemonize\s*=\s*yes/daemonize = no/g"  /etc/php5/fpm/php-fpm .conf
RUN  sed  -i -e  "s/;catch_workers_output\s*=\s*yes/catch_workers_output = yes/g"  /etc/php5/fpm/pool .d /www .conf
RUN  find  /etc/php5/cli/conf .d/ -name  "*.ini"  - exec  sed  -i -re  's/^(\s*)#(.*)/\1;\2/g'  {} \;
 
# nginx site conf
ADD . /nginx-site .conf  /etc/nginx/sites-available/default
 
# Supervisor Config
RUN  /usr/bin/easy_install  supervisor
RUN  /usr/bin/easy_install  supervisor-stdout
ADD . /supervisord .conf  /etc/supervisord .conf
 
# Install Wordpress
ADD http: //wordpress .org /latest . tar .gz  /usr/share/nginx/latest . tar .gz
RUN  cd  /usr/share/nginx/  &&  tar  xvf latest. tar .gz &&  rm  latest. tar .gz
RUN  mv  /usr/share/nginx/html/5 /usr/share/nginx/wordpress
RUN  rm  -rf  /usr/share/nginx/www
RUN  mv  /usr/share/nginx/wordpress  /usr/share/nginx/www
RUN  chown  -R www-data:www-data  /usr/share/nginx/www
 
# Wordpress Initialization and Startup Script
ADD . /start .sh  /start .sh
RUN  chmod  755  /start .sh
 
# private expose
EXPOSE 3306
EXPOSE 80
 
CMD [ "/bin/bash" "/start.sh" ]


再来一个mognodb的例子:

注: 可以用 \  标识换行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
FROM dockerfile /ubuntu
 
# Install MongoDB.
RUN \
   apt-key adv --keyserver hkp: //keyserver .ubuntu.com:80 --recv 7F0CEB10 && \
   echo  'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen'  tee  /etc/apt/sources .list.d /mongodb .list && \
   apt-get update && \
   apt-get  install  -y mongodb-org && \
   mkdir  -p  /data/db
 
VOLUME [ "/data" ]
 
WORKDIR  /data
 
CMD [ "mongod" ]
 
EXPOSE 27017
EXPOSE 28017



官网有不少的例子,有兴趣的朋友可以到 https://github.com/dockerfile 查看下。 


在dockerfile使用cmd、entrypoint 需要注意:


cmd 是可以写成shell的模式, 也就是 咱们平时写语句那样

CMD  redis-server redis.conf && service sshd restart


docker调用它的时候是用/bin/sh -c 调用的。  这个时候有些少许的问题,大家再测试的时候,最好在自己的本机也测一般。  sh -c 这东西挺奇妙的 ,貌似他的参数断句有问题,有些蛋疼。


一般来说,在用cmd启动的时候  用exec的模式多点 ,也就是 ['redis-server','/etc/redis/redis.conf'] 他自己会用空格组成一条命令。  


一个dockerfile里面只能有一个CMD。 写多了没用。





 本文转自 rfyiamcool 51CTO博客,原文链接:http://blog.51cto.com/rfyiamcool/1541081 ,如需转载请自行联系原作者

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
6天前
|
Ubuntu API 网络虚拟化
ubuntu22 编译安装docker,和docker容器方式安装 deepseek
本脚本适用于Ubuntu 22.04,主要功能包括编译安装Docker和安装DeepSeek模型。首先通过Apt源配置安装Docker,确保网络稳定(建议使用VPN)。接着下载并配置Docker二进制文件,创建Docker用户组并设置守护进程。随后拉取Debian 12镜像,安装系统必备工具,配置Ollama模型管理器,并最终部署和运行DeepSeek模型,提供API接口进行交互测试。
129 15
|
1月前
|
Ubuntu NoSQL Linux
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
160 6
《docker基础篇:3.Docker常用命令》包括帮助启动类命令、镜像命令、有镜像才能创建容器,这是根本前提(下载一个CentOS或者ubuntu镜像演示)、容器命令、小总结
|
1月前
|
Kubernetes Linux 虚拟化
入门级容器技术解析:Docker和K8s的区别与关系
本文介绍了容器技术的发展历程及其重要组成部分Docker和Kubernetes。从传统物理机到虚拟机,再到容器化,每一步都旨在更高效地利用服务器资源并简化应用部署。容器技术通过隔离环境、减少依赖冲突和提高可移植性,解决了传统部署方式中的诸多问题。Docker作为容器化平台,专注于创建和管理容器;而Kubernetes则是一个强大的容器编排系统,用于自动化部署、扩展和管理容器化应用。两者相辅相成,共同推动了现代云原生应用的快速发展。
202 11
|
2月前
|
Ubuntu Linux 开发工具
docker 是什么?docker初认识之如何部署docker-优雅草后续将会把产品发布部署至docker容器中-因此会出相关系列文章-优雅草央千澈
Docker 是一个开源的容器化平台,允许开发者将应用程序及其依赖项打包成标准化单元(容器),确保在任何支持 Docker 的操作系统上一致运行。容器共享主机内核,提供轻量级、高效的执行环境。本文介绍如何在 Ubuntu 上安装 Docker,并通过简单步骤验证安装成功。后续文章将探讨使用 Docker 部署开源项目。优雅草央千澈 源、安装 Docker 包、验证安装 - 适用场景:开发、测试、生产环境 通过以上步骤,您可以在 Ubuntu 系统上成功安装并运行 Docker,为后续的应用部署打下基础。
96 8
docker 是什么?docker初认识之如何部署docker-优雅草后续将会把产品发布部署至docker容器中-因此会出相关系列文章-优雅草央千澈
|
2月前
|
存储 Kubernetes 开发者
容器化时代的领航者:Docker 和 Kubernetes 云原生时代的黄金搭档
Docker 是一种开源的应用容器引擎,允许开发者将应用程序及其依赖打包成可移植的镜像,并在任何支持 Docker 的平台上运行。其核心概念包括镜像、容器和仓库。镜像是只读的文件系统,容器是镜像的运行实例,仓库用于存储和分发镜像。Kubernetes(k8s)则是容器集群管理系统,提供自动化部署、扩展和维护等功能,支持服务发现、负载均衡、自动伸缩等特性。两者结合使用,可以实现高效的容器化应用管理和运维。Docker 主要用于单主机上的容器管理,而 Kubernetes 则专注于跨多主机的容器编排与调度。尽管 k8s 逐渐减少了对 Docker 作为容器运行时的支持,但 Doc
177 5
容器化时代的领航者:Docker 和 Kubernetes 云原生时代的黄金搭档
|
2月前
|
关系型数据库 应用服务中间件 PHP
实战~如何组织一个多容器项目docker-compose
本文介绍了如何使用Docker搭建Nginx、PHP和MySQL的环境。首先启动Nginx容器并查看IP地址,接着启动Alpine容器并安装curl测试连通性。通过`--link`方式或`docker-compose`配置文件实现服务间的通信。最后展示了Nginx配置文件和PHP代码示例,验证了各服务的正常运行。
84 3
实战~如何组织一个多容器项目docker-compose
|
2月前
|
监控 NoSQL 时序数据库
《docker高级篇(大厂进阶):7.Docker容器监控之CAdvisor+InfluxDB+Granfana》包括:原生命令、是什么、compose容器编排,一套带走
《docker高级篇(大厂进阶):7.Docker容器监控之CAdvisor+InfluxDB+Granfana》包括:原生命令、是什么、compose容器编排,一套带走
300 78
|
2月前
|
数据建模 应用服务中间件 nginx
docker替换宿主与容器的映射端口和文件路径
通过正确配置 Docker 的端口和文件路径映射,可以有效地管理容器化应用程序,确保其高效运行和数据持久性。在生产环境中,动态替换映射配置有助于灵活应对各种需求变化。以上方法和步骤提供了一种可靠且易于操作的方案,帮助您轻松管理 Docker 容器的端口和路径映射。
180 3
|
2月前
|
监控 Docker 容器
在Docker容器中运行打包好的应用程序
在Docker容器中运行打包好的应用程序
|
2月前
|
负载均衡 网络协议 算法
Docker容器环境中服务发现与负载均衡的技术与方法,涵盖环境变量、DNS、集中式服务发现系统等方式
本文探讨了Docker容器环境中服务发现与负载均衡的技术与方法,涵盖环境变量、DNS、集中式服务发现系统等方式,以及软件负载均衡器、云服务负载均衡、容器编排工具等实现手段,强调两者结合的重要性及面临挑战的应对措施。
126 3