[英]Create a Docker image in command line linux from an existing directory
我有一个包含文件和子目录的目录,并且我现在想在不使用Dockerfile的情况下通过命令行从该文件创建Docker映像,请不要问我为什么。
This can be done with docker commit
. 这可以通过docker commit
来完成。 The simplest example would look something like the following: 最简单的示例如下所示:
docker run -d --name my-container <base-image>
docker cp . my-container:<path-where-files-go>
docker commit my-container <new-image-name>
The accepted answer surely works but is prone to producing inconsistent images. 接受的答案肯定有效,但易于产生不一致的图像。 I would recommend using a dockerfile
. 我建议使用dockerfile
。 This gives you a single point to make changes to your images. 这使您可以单点更改图像。 See example below... 请参见下面的示例...
Assuming your working directory has a single folder foo
which you need in your image
So next to it, create a file named dockerfile
then edit the file to look as below 假设您的工作目录中有一个需要在image
包含的文件夹foo
,因此在它旁边,创建一个名为dockerfile
的文件,然后编辑该文件,如下所示
FROM <your_base_image:latest>
COPY foo /path/of/folder/in/image/foo
Now run the command below in your terminal
. 现在,在terminal
运行以下命令。 Ensure your working directory has both dockerfile
and the folder foo
确保您的工作目录同时具有dockerfile
和文件夹foo
docker build . -t <your_image_name>
This dockerfile
can be used to generate the exact same image every other time. 此dockerfile
可用于每隔两次生成完全相同的映像。 Any changes added to it means that all new images generated from that dockerfile
will contain the changes. 添加到其中的任何更改意味着从该dockerfile
生成的所有新映像dockerfile
将包含更改。 Below are other commands you can add in your dockerfile
to enrich the resulting images
with nano
editor 您可以在dockerfile
添加以下其他命令,以使用nano
编辑器丰富生成的images
RUN apt-get update
RUN apt-get install nano -y
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.