forked from docs/doc-exports
Reviewed-by: Eotvos, Oliver <oliver.eotvos@t-systems.com> Reviewed-by: Hasko, Vladimir <vladimir.hasko@t-systems.com> Co-authored-by: Dong, Qiu Jian <qiujiandong1@huawei.com> Co-committed-by: Dong, Qiu Jian <qiujiandong1@huawei.com>
104 lines
16 KiB
HTML
104 lines
16 KiB
HTML
<a name="swr_faq_0012"></a><a name="swr_faq_0012"></a>
|
|
|
|
<h1 class="topictitle1">How Do I Create a Container Image?</h1>
|
|
<div id="body1535018272825"><p id="swr_faq_0012__p161283561506">The following two approaches are for you to consider. Approach 1 is for images that will only be updated occasionally whereas approach 2 is for images that will be frequently updated.</p>
|
|
<ul id="swr_faq_0012__ul9505205920012"><li id="swr_faq_0012__li550515591805">Approach 1: creating a snapshot. This approach involves three key steps: (1) Start a base container by running a base image (for example, Ubuntu image); (2) install the container engine software inside the base container; (3) create a snapshot of the container.</li><li id="swr_faq_0012__li712901410">Approach 2: creating a Dockerfile to build an image. This approach involves two key steps: (1) Write software installation instructions into a Dockerfile; (2) run the <strong id="swr_faq_0012__b1997852871114">docker build</strong> command to build an image from the Dockerfile.</li></ul>
|
|
<div class="section" id="swr_faq_0012__section1017412550210"><a name="swr_faq_0012__section1017412550210"></a><a name="section1017412550210"></a><h4 class="sectiontitle">Approach 1: Creating a Snapshot</h4><p id="swr_faq_0012__p19402104151011">This approach is suitable for images that will only be updated occasionally.</p>
|
|
<div class="fignone" id="swr_faq_0012__fig61321912124615"><span class="figcap"><b>Figure 1 </b>Creating a snapshot</span><br><span><img class="imgResize" id="swr_faq_0012__image135232026175316" src="en-us_image_0165153802.png" width="522.69" height="277.65143175000003" title="Click to enlarge"></span></div>
|
|
<p id="swr_faq_0012__p1771713815316">Procedure:</p>
|
|
<ol id="swr_faq_0012__ol19709554936"><li id="swr_faq_0012__li470919544318">Install the container engine software on a host.</li><li id="swr_faq_0012__li118181720511"><a name="swr_faq_0012__li118181720511"></a><a name="li118181720511"></a>Start an empty base container in the interactive mode.<p id="swr_faq_0012__p1845119176514"><a name="swr_faq_0012__li118181720511"></a><a name="li118181720511"></a>For example, start a CentOS container in the interactive mode.</p>
|
|
<p id="swr_faq_0012__p43929288612"><strong id="swr_faq_0012__b1523781312340">docker run -it centos</strong></p>
|
|
</li><li id="swr_faq_0012__li1844517587518">Run the following commands to install the target software:<p id="swr_faq_0012__p1070892913813"><a name="swr_faq_0012__li1844517587518"></a><a name="li1844517587518"></a><strong id="swr_faq_0012__b231654103415">yum install XXX</strong></p>
|
|
<p id="swr_faq_0012__p192795461486"><strong id="swr_faq_0012__b131716416341">git clone https://github.com/lh3/bwa.git</strong></p>
|
|
<p id="swr_faq_0012__p152418311915"><strong id="swr_faq_0012__b1731774153413">cd bwa;make</strong></p>
|
|
<div class="note" id="swr_faq_0012__note93501350443"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="swr_faq_0012__p7350452447">Install Git in advance and check whether an SSH key is set on the local host. </p>
|
|
</div></div>
|
|
</li><li id="swr_faq_0012__li187481532662">Run the <strong id="swr_faq_0012__b7234754151115">exit</strong> command to exit the container.</li></ol><ol start="5" id="swr_faq_0012__ol017278769"><li id="swr_faq_0012__li11813152220613">Create a snapshot.<div class="p" id="swr_faq_0012__p214963514618"><a name="swr_faq_0012__li11813152220613"></a><a name="li11813152220613"></a><strong id="swr_faq_0012__b10385122955815">docker commit -m "xx" -a "test" container-id test/image:tag</strong><ul id="swr_faq_0012__ul6717188238"><li id="swr_faq_0012__li37171381438"><strong id="swr_faq_0012__b6671826124">-a</strong>: indicates the author of the base image.</li><li id="swr_faq_0012__li207181681938"><strong id="swr_faq_0012__b194991685127">container-id</strong>: indicates the ID of the container you have started in step <a href="#swr_faq_0012__li118181720511">2</a>. You can run the <strong id="swr_faq_0012__b1816761410124">docker ps -a</strong> command to query the container ID.</li><li id="swr_faq_0012__li47181081315"><strong id="swr_faq_0012__b16425121711126">-m</strong>: indicates the commit message.</li><li id="swr_faq_0012__li1718148637"><strong id="swr_faq_0012__b11971111911210">test/image:tag</strong>: indicates the repository name/image name:tag name.</li></ul>
|
|
</div>
|
|
</li></ol><ol start="6" id="swr_faq_0012__ol113581114181114"><li id="swr_faq_0012__li13358141414115">Run the <strong id="swr_faq_0012__b4222132241213">docker images</strong> command to list the built container image.</li></ol>
|
|
</div>
|
|
<div class="section" id="swr_faq_0012__section1690134131216"><a name="swr_faq_0012__section1690134131216"></a><a name="section1690134131216"></a><h4 class="sectiontitle">Approach 2: Creating a Dockerfile to Build an Image</h4><p id="swr_faq_0012__p68391374108">This approach is suitable for images that will be frequently updated. In <a href="#swr_faq_0012__section1017412550210">Approach 1</a>, you create a snapshot of the whole container. This could be demanding if you need to frequently update your images. In this case, <a href="#swr_faq_0012__section1690134131216">Approach 2</a> is put forward to automate the image build process.</p>
|
|
<p id="swr_faq_0012__p1954145242410">The idea behind <a href="#swr_faq_0012__section1690134131216">Approach 2</a> is to write the process of <a href="#swr_faq_0012__section1017412550210">Approach 1</a> into a Dockerfile and then run the <strong id="swr_faq_0012__b10213102814125">docker build -t test/image:tag.</strong> command to automatically build an image from the Dockerfile. In the preceding command, <strong id="swr_faq_0012__b6214152819126">.</strong> indicates the path to the Dockerfile.</p>
|
|
<div class="fignone" id="swr_faq_0012__fig5947936184615"><span class="figcap"><b>Figure 2 </b>Creating a Dockerfile to build an image</span><br><span><img class="imgResize" id="swr_faq_0012__image529642210512" src="en-us_image_0165153805.png" width="522.69" height="196.50750000000002" title="Click to enlarge"></span></div>
|
|
<p id="swr_faq_0012__p15189452181213">Example Dockerfile:</p>
|
|
<div class="note" id="swr_faq_0012__note1922523450"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="swr_faq_0012__p83175220459">If an external network is required, ensure that network connectivity is available.</p>
|
|
</div></div>
|
|
<pre class="screen" id="swr_faq_0012__screen6273104510277">#Version 1.0.1
|
|
FROM centos:latest
|
|
|
|
# Setting the root user as the executor of subsequent commands
|
|
USER root
|
|
|
|
# Performing operations
|
|
RUN yum update -y
|
|
RUN yum install -y java
|
|
|
|
# Using <strong id="swr_faq_0012__b13316142917135">&&</strong> to concatenate commands
|
|
RUN touch test.txt && echo "abc" >>abc.txt
|
|
|
|
# Setting an externally exposed port
|
|
EXPOSE 80 8080 1038
|
|
|
|
# Adding a network file
|
|
ADD https://www.baidu.com/img/bd_logo1.png /opt/
|
|
|
|
# Setting an environment variable
|
|
ENV WEBAPP_PORT=9090
|
|
|
|
# Setting a work directory
|
|
WORKDIR /opt/
|
|
|
|
# Setting a start command
|
|
ENTRYPOINT ["ls"]
|
|
|
|
# Setting start parameters
|
|
CMD ["-a", "-l"]
|
|
|
|
# Setting a volume
|
|
VOLUME ["/data", "/var/www"]
|
|
|
|
# Setting the trigger operation for a sub-image
|
|
ONBUILD ADD . /app/src
|
|
ONBUILD RUN echo "on build excuted" >> onbuild.txt</pre>
|
|
</div>
|
|
<div class="section" id="swr_faq_0012__section157841197357"><h4 class="sectiontitle">Basic Syntax of Dockerfile</h4><ul id="swr_faq_0012__ul10151144443713"><li id="swr_faq_0012__li101511044113711">FROM:<p id="swr_faq_0012__p194981226123519"><a name="swr_faq_0012__li101511044113711"></a><a name="li101511044113711"></a>It is used to specify the parent image (base image) from which you are building a new image. Except annotations, a Dockerfile must start with a FROM instruction. Subsequent instructions run in this parent image environment until the next FROM instruction appears. You can create multiple images in the same Dockerfile by adding multiple FROM instructions.</p>
|
|
</li><li id="swr_faq_0012__li930815018377">MAINTAINER:<p id="swr_faq_0012__p144988266359"><a name="swr_faq_0012__li930815018377"></a><a name="li930815018377"></a>It is used to specify the information about the author who creates an image, including the username and email address. This parameter is optional.</p>
|
|
</li><li id="swr_faq_0012__li20546125713715">RUN:<p id="swr_faq_0012__p44991626153519"><a name="swr_faq_0012__li20546125713715"></a><a name="li20546125713715"></a>It is used to modify an image. Generally, RUN commands are executed to install libraries, and install and configure programs. After a RUN command is executed, an image layer will be created on the current image. The next command will be executed on the new image. The RUN statement can be in one of the following formats:</p>
|
|
<ul id="swr_faq_0012__ul188922180382"><li id="swr_faq_0012__li1251391483810"><strong id="swr_faq_0012__b123901201636">RUN yum update</strong>: Command that is executed in the <strong id="swr_faq_0012__b192977414317">/bin/sh</strong> directory.</li><li id="swr_faq_0012__li3761220169"><strong id="swr_faq_0012__b143057539317">RUN ["yum", "update"]</strong>: Directly invoke <strong id="swr_faq_0012__b34012101544">exec</strong>.</li><li id="swr_faq_0012__li17472125131717"><strong id="swr_faq_0012__b18717184122510">RUN yum update && yum install nginx</strong>: Use <strong id="swr_faq_0012__b17982134572511">&&</strong> to connect multiple commands to a RUN statement.</li></ul>
|
|
</li><li id="swr_faq_0012__li854822715384">EXPOSE:<p id="swr_faq_0012__p1243663484118"><a name="swr_faq_0012__li854822715384"></a><a name="li854822715384"></a>It is used to specify one or more network ports that will be exposed on a container. If there are multiple ports, separate them by spaces.</p>
|
|
<p id="swr_faq_0012__p71151133194118">When running a container, you can set <strong id="swr_faq_0012__b89413135346">-P</strong> (uppercase) to map the ports specified in EXPOSE to random ports on a host. Other containers or hosts can communicate with the container through the ports on the host.</p>
|
|
<p id="swr_faq_0012__p195005260355">You can also use <strong id="swr_faq_0012__b11996153216370">-p</strong> (lowercase) to expose the ports that are not listed in EXPOSE.</p>
|
|
</li><li id="swr_faq_0012__li0592123263819">ADD:<p id="swr_faq_0012__p5500182633516"><a name="swr_faq_0012__li0592123263819"></a><a name="li0592123263819"></a>It is used to add a file to a new image. The file can be a host file, a network file, or a folder.</p>
|
|
<ul id="swr_faq_0012__ul22918213398"><li id="swr_faq_0012__li17001812112013">First parameter: source file (folder)<ul id="swr_faq_0012__ul16766183922019"><li id="swr_faq_0012__li61323582018">If a relative path is used, this path must correspond to the directory where the Dockerfile is located.</li><li id="swr_faq_0012__li191483514208">If a URL is used, the file needs to be downloaded first and then added to the image.</li></ul>
|
|
</li><li id="swr_faq_0012__li113012922119">Second parameter: target path<ul id="swr_faq_0012__ul5442418132115"><li id="swr_faq_0012__li0784161492111">If the source file is in the .zip or .tar file, the container engine decompresses the file and then adds it to the specified location of the image.</li><li id="swr_faq_0012__li1678781462110">If the source file is a compressed network file specified by a URL, the file will not be decompressed.</li></ul>
|
|
</li></ul>
|
|
</li><li id="swr_faq_0012__li196210613394">VOLUME:<p id="swr_faq_0012__p85011926183511"><a name="swr_faq_0012__li196210613394"></a><a name="li196210613394"></a>It is used to create a mount point for a specified path (file or folder) in the image. Multiple containers can share data through the same mount point. Even if one of the containers is stopped, the mount point can still be accessed.</p>
|
|
</li><li id="swr_faq_0012__li339519113394">WORKDIR:<p id="swr_faq_0012__p11501152643514"><a name="swr_faq_0012__li339519113394"></a><a name="li339519113394"></a>It is used to specify a new work directory for the next command. The directory can be an absolute or a relative directory. WORKDIR can be specified multiple times as required. When a container is started, the directory specified by the last WORKDIR command is used as the current work directory of the container.</p>
|
|
</li><li id="swr_faq_0012__li199101716143910">ENV:<p id="swr_faq_0012__p2050313266359"><a name="swr_faq_0012__li199101716143910"></a><a name="li199101716143910"></a>It is used to set an environment variable for running the container. When running the container, you can set <strong id="swr_faq_0012__b19245184352">-e</strong> to modify the environment variable or add other environment variables.</p>
|
|
<p id="swr_faq_0012__p183171428182319">Example:</p>
|
|
<p id="swr_faq_0012__p1850317261355"><strong id="swr_faq_0012__b1745723153717">docker run -e WEBAPP_PORT=8000 -e WEBAPP_HOST=www.example.com ...</strong></p>
|
|
</li><li id="swr_faq_0012__li732213367393">CMD:<p id="swr_faq_0012__p10503162673518"><a name="swr_faq_0012__li732213367393"></a><a name="li732213367393"></a>It is used to specify the default command for starting a container.</p>
|
|
</li><li id="swr_faq_0012__li17414243113917">ENTRYPOINT:<p id="swr_faq_0012__p1591912339011"><a name="swr_faq_0012__li17414243113917"></a><a name="li17414243113917"></a>It is used to specify the default command for starting a container. Difference: For ENTRYPOINT, parameters added to the image during container running will be spliced. For CMD, these parameters will be overwritten.</p>
|
|
<ul id="swr_faq_0012__ul1053791410014"><li id="swr_faq_0012__li81119321008">If the Dockerfile specifies that the default command for starting a container is <strong id="swr_faq_0012__b123865321319">ls -l</strong>, the default command <strong id="swr_faq_0012__b4392163201320">ls -l</strong> will be run accordingly. For example:<ul id="swr_faq_0012__ul14111113219011"><li id="swr_faq_0012__li131102323012"><strong id="swr_faq_0012__b616431620138">ENTRYPOINT [ "ls", "-l"]</strong>: The program and parameter for starting a container are set to be <strong id="swr_faq_0012__b1516481691319">ls</strong> and <strong id="swr_faq_0012__b0164101615136">-l</strong> respectively.</li><li id="swr_faq_0012__li1111110326018"><strong id="swr_faq_0012__b78035293116">docker run centos</strong>: The <strong id="swr_faq_0012__b136092031636">docker run centos ls -l</strong> command is run by default for starting a CentOS container.</li><li id="swr_faq_0012__li1311113321502"><strong id="swr_faq_0012__b18194113321315">docker run centos -a</strong>: When the <strong id="swr_faq_0012__b6195143361317">-a</strong> parameter is added for starting a CentOS container, the <strong id="swr_faq_0012__b1195153313139">docker run centos ls -l -a</strong> command is run by default.</li></ul>
|
|
</li><li id="swr_faq_0012__li352921710014">If the Dockerfile specifies that the default command for starting a container is <strong id="swr_faq_0012__b1626011501133">--entrypoint</strong> but you need to replace the default command, you can add <strong id="swr_faq_0012__b4260105061310">--entrypoint</strong> parameters to replace the configuration specified in Dockerfile. Example:<p id="swr_faq_0012__p125412179011"><strong id="swr_faq_0012__b19923102212371">docker run gutianlangyu/test --entrypoint echo "hello world"</strong></p>
|
|
</li></ul>
|
|
</li><li id="swr_faq_0012__li11986445134012">USER:<p id="swr_faq_0012__p13503426193511"><a name="swr_faq_0012__li11986445134012"></a><a name="li11986445134012"></a>It is used to specify the user or UID for running the container, and running the RUN, CMD, or ENTRYPOINT command.</p>
|
|
</li><li id="swr_faq_0012__li235985164012">ONBUILD:<p id="swr_faq_0012__p3505102619353"><a name="swr_faq_0012__li235985164012"></a><a name="li235985164012"></a>Trigger command. During image build, the image builder of the container engine saves all commands specified by the ONBUILD command to the image metadata. These commands will not be executed in the process of building the current image. These commands will be executed only when a new image uses the FROM instruction to specify the parent image as the current image.</p>
|
|
<p id="swr_faq_0012__p1238219572917">Using the FROM instruction to build a child image based on the parent image created by the Dockerfile:</p>
|
|
<p id="swr_faq_0012__p35187142911"><strong id="swr_faq_0012__b2965175615132">ONBUILD ADD. /app/src</strong>: The <strong id="swr_faq_0012__b4971155613134">ADD. /app/src</strong> command is automatically executed.</p>
|
|
</li></ul>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="swr_faq_1001.html">General FAQs</a></div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
<script language="JavaScript">
|
|
<!--
|
|
image_size('.imgResize');
|
|
var msg_imageMax = "view original image";
|
|
var msg_imageClose = "close";
|
|
//--></script> |