简体   繁体   English

Dockerfile 与 shell

[英]Dockerfile vs shell

I am trying to create an ubuntu image with sqlite and postgreSQL installed.我正在尝试创建一个安装了 sqlite 和 postgreSQL 的 ubuntu 映像。 I am confused if what I'm doing is the best practice.我很困惑我所做的是否是最佳实践。

I found out that I can build a very simple dockerfile and install sqlite and postgresql via shell (using docker run -it [image_name] ).我发现我可以构建一个非常简单的 dockerfile 并通过 shell 安装 sqlite 和 postgresql(使用 docker run -it docker run -it [image_name] )。

So I'll have a dockerfile like such所以我会有一个像这样的 dockerfile

FROM ubuntu:18.04
ENV TZ=[time zone data]
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

and run docker build some_container:1.0 .并运行docker build some_container:1.0 . Then run docker run -it some_container:1.0 , which will give me a shell, and then run install commands like apt-get install postgres-12 inside the shell.然后运行docker run -it some_container:1.0 ,它会给我一个shell,然后在shell中运行像apt-get install postgres-12这样的安装命令。

Is this even the right way to build images?这甚至是构建图像的正确方法吗? I am confused if this is a good way because many online materials don't use shell but use dockerfile to specify what to install and run.我很困惑这是否是一个好方法,因为许多在线资料不使用 shell,而是使用 dockerfile 来指定要安装和运行的内容。

What benefit does using dockerfile have over editing images via shell?与通过 shell 编辑图像相比,使用 dockerfile 有什么好处?

A Dockerfile is a set of instructions to build your image. Dockerfile 是一组构建镜像的指令。 You should use it to prepare everything you need to work so next time you spin up the container you won't have to do any prep work.您应该使用它来准备工作所需的一切,以便下次启动容器时无需进行任何准备工作。

Not have the postgres installation in the Dockerfile means that every time you want to use your container you'll have to open a shell and manually install it instead of just running the container and having a database ready for you to use.没有在 Dockerfile 中安装 postgres 意味着每次你想使用你的容器时,你都必须打开一个 shell 并手动安装它,而不是仅仅运行容器并准备好一个数据库供你使用。

As soon as you quit that machine you're running, it's gone and you need to re-create it.一旦您退出正在运行的机器,它就消失了,您需要重新创建它。 Every command you entered manually is gone.您手动输入的每个命令都消失了。 Dockerfiles are a recipe for creating images reproducibly from scratch. Dockerfiles 是一种从头开始可重复地创建图像的方法。

Taking a bigger picture view: virtual machines are useful, we can agree on that yes?从更大的角度来看:虚拟机很有用,我们同意这一点,是吗? They allow you to run complex services in a simple environment, shielded from the complexities of other stuff which you may want to run in parallel.它们允许您在一个简单的环境中运行复杂的服务,而不受您可能希望并行运行的其他东西的复杂性影响。 Setting up a virtual machine can be painful though;不过,设置虚拟机可能会很痛苦; you need to start the machine itself, and then log into it to install all the stuff you need.你需要启动机器本身,然后登录它来安装你需要的所有东西。 That can be complex and time consuming.这可能既复杂又耗时。

Docker simplifies getting virtual machines up and running in a known good state: you define in a Dockerfile what steps are necessary to create exactly the virtual machine you want, and that's basically it. Docker 简化了在已知良好状态下启动和运行虚拟机的过程:您在 Dockerfile 中定义了创建所需虚拟机所需的步骤,基本上就是这样。 Docker will build the machine and run it with a simple command, and it does so efficiently (with layer caching and tight OS integration), and all you need is a single text file which you can check into your version control system (as opposed to a multi-GB virtual machine image). Docker 将构建机器并使用一个简单的命令运行它,并且它非常高效(具有层缓存和紧密的操作系统集成),您只需要一个文本文件,您可以将其签入您的版本控制系统(而不是一个多 GB 的虚拟机映像)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM