github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/docs/getstarted/step_four.md (about)

     1  <!--[metadata]>
     2  +++
     3  aliases = [
     4  "/mac/step_four/",
     5  "/windows/step_four/",
     6  "/linux/step_four/",
     7  ]
     8  title = "Build your own image"
     9  description = "Getting started with Docker"
    10  keywords = ["beginner, getting started, Docker"]
    11  [menu.main]
    12  identifier = "getstart_build_image"
    13  parent = "tutorial_getstart_menu"
    14  weight = 4
    15  +++
    16  <![end-metadata]-->
    17  
    18  # Build your own image
    19  
    20  The `whalesay` image could be improved. It would be nice if you didn't have to
    21  think of something to say. And you type a lot to get `whalesay` to talk.
    22  
    23      docker run docker/whalesay cowsay boo-boo
    24  
    25  In this next section, you will improve the `whalesay` image by building a new version that "talks on its own" and requires fewer words to run.
    26  
    27  ## Step 1: Write a Dockerfile
    28  
    29  In this step, you use your favorite text editor to write a short Dockerfile.  A
    30  Dockerfile describes the software that is "baked" into an image. It isn't just
    31  ingredients tho, it can tell the software what environment to use or what
    32  commands to run. Your recipe is going to be very short.
    33  
    34  1. Go back to your command terminal window.
    35  
    36  2. Make a new directory by typing `mkdir mydockerbuild` and pressing RETURN.
    37  
    38          $ mkdir mydockerbuild
    39  
    40      This directory serves as the "context" for your build. The context just means it contains all the things you need to build your image.
    41  
    42  3. Change to your new directory.
    43  
    44          $ cd mydockerbuild
    45  
    46      Right now the directory is empty.
    47  
    48  4. Create a Dockerfile in the directory by typing `touch Dockerfile` and pressing RETURN.
    49  
    50          $ touch Dockerfile
    51  
    52      The command appears to do nothing but it actually creates the Dockerfile in the current directory.  Just type `ls Dockerfile` to see it.
    53  
    54          $ ls Dockerfile
    55          Dockerfile
    56  
    57  5. Open the `Dockerfile` in a visual text editor like <a href="https://atom.io/" target="_blank">Atom</a> or <a href="https://www.sublimetext.com/" target="_blank">Sublime</a>, or a text based editor like `vi`, or `nano` (https://www.nano-editor.org/).
    58  
    59  6. Add a line to the file like this:
    60  
    61          FROM docker/whalesay:latest
    62  
    63        The `FROM` keyword tells Docker which image your image is based on. Whalesay is cute and has the `cowsay` program already, so we'll start there.
    64  
    65  7. Now, add the `fortunes` program to the image.
    66  
    67          RUN apt-get -y update && apt-get install -y fortunes
    68  
    69        The `fortunes` program has a command that prints out wise sayings for our whale to say. So, the first step is to install it. This line installs the software into the image.
    70  
    71  8. Once the image has the software it needs, you instruct the software to run
    72      when the image is loaded.
    73  
    74            CMD /usr/games/fortune -a | cowsay
    75  
    76      This line tells the `fortune` program to pass a nifty quote to the `cowsay` program.
    77  
    78  9. Check your work, your file should look like this:
    79  
    80          FROM docker/whalesay:latest
    81          RUN apt-get -y update && apt-get install -y fortunes
    82          CMD /usr/games/fortune -a | cowsay
    83  
    84  10. Save and close your Dockerfile.
    85  
    86      At this point, you have all your software ingredients and behaviors described in a Dockerfile. You are ready to build a new image.
    87  
    88  ## Step 2: Build an image from your Dockerfile
    89  
    90  1. At the command line, make sure the Dockerfile is in the current directory by typing `cat Dockerfile`
    91  
    92          $ cat Dockerfile
    93          FROM docker/whalesay:latest
    94  
    95          RUN apt-get -y update && apt-get install -y fortunes
    96  
    97          CMD /usr/games/fortune -a | cowsay
    98  
    99  2. Now, build your new image by typing the `docker build -t docker-whale .` command in your terminal (don't forget the . period).
   100  
   101          $ docker build -t docker-whale .
   102          Sending build context to Docker daemon 2.048 kB
   103          ...snip...
   104          Removing intermediate container a8e6faa88df3
   105          Successfully built 7d9495d03763
   106  
   107  	  The command takes several seconds to run and reports its outcome. Before
   108      you do anything with the new image, take a minute to learn about the
   109      Dockerfile build process.
   110  
   111  ## Step 3: Learn about the build process
   112  
   113  The `docker build -t docker-whale .` command takes the `Dockerfile` in the
   114  current directory, and builds an image called `docker-whale` on your local
   115  machine. The command takes about a minute and its output looks really long and
   116  complex. In this section, you learn what each message means.
   117  
   118  First Docker checks to make sure it has everything it needs to build.
   119  
   120      Sending build context to Docker daemon 2.048 kB
   121  
   122  Then, Docker loads with the `whalesay` image.	It already has this image
   123  locally as you might recall from the last page. So, Docker doesn't need to
   124  download it.
   125  
   126      Step 1 : FROM docker/whalesay:latest
   127       ---> fb434121fc77
   128  
   129  Docker moves onto the next step which is to update the `apt-get` package
   130  manager. This takes a lot of lines, no need to list them all again here.
   131  
   132      Step 2 : RUN apt-get -y update && apt-get install -y fortunes
   133       ---> Running in 27d224dfa5b2
   134      Ign http://archive.ubuntu.com trusty InRelease
   135      Ign http://archive.ubuntu.com trusty-updates InRelease
   136      Ign http://archive.ubuntu.com trusty-security InRelease
   137      Hit http://archive.ubuntu.com trusty Release.gpg
   138      ....snip...
   139      Get:15 http://archive.ubuntu.com trusty-security/restricted amd64 Packages [14.8 kB]
   140      Get:16 http://archive.ubuntu.com trusty-security/universe amd64 Packages [134 kB]
   141      Reading package lists...
   142      ---> eb06e47a01d2
   143  
   144  Then, Docker installs the new `fortunes` software.
   145  
   146      Reading package lists...
   147      Building dependency tree...
   148      Reading state information...
   149      The following extra packages will be installed:
   150        fortune-mod fortunes-min librecode0
   151      Suggested packages:
   152        x11-utils bsdmainutils
   153      The following NEW packages will be installed:
   154        fortune-mod fortunes fortunes-min librecode0
   155      0 upgraded, 4 newly installed, 0 to remove and 3 not upgraded.
   156      Need to get 1961 kB of archives.
   157      After this operation, 4817 kB of additional disk space will be used.
   158      Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main librecode0 amd64 3.6-21 [771 kB]
   159      ...snip......
   160      Setting up fortunes (1:1.99.1-7) ...
   161      Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
   162       ---> c81071adeeb5
   163      Removing intermediate container 23aa52c1897c
   164  
   165  Finally, Docker finishes the build and reports its outcome.		
   166  
   167      Step 3 : CMD /usr/games/fortune -a | cowsay
   168       ---> Running in a8e6faa88df3
   169       ---> 7d9495d03763
   170      Removing intermediate container a8e6faa88df3
   171      Successfully built 7d9495d03763
   172  
   173  
   174  ## Step 4: Run your new docker-whale
   175  
   176  In this step, you verify the new images is on your computer and then you run your new image.
   177  
   178  1. Open a command line terminal.
   179  
   180  2. Type `docker images` and press RETURN.
   181  
   182      This command, you might remember, lists the images you have locally.
   183  
   184          $ docker images
   185          REPOSITORY           TAG          IMAGE ID          CREATED             SIZE
   186          docker-whale         latest       7d9495d03763      4 minutes ago       273.7 MB
   187          docker/whalesay      latest       fb434121fc77      4 hours ago         247 MB
   188          hello-world          latest       91c95931e552      5 weeks ago         910 B
   189  
   190  3. Run your new image by typing `docker run docker-whale` and pressing RETURN.
   191  
   192          $ docker run docker-whale
   193           _________________________________________
   194          / "He was a modest, good-humored boy. It  \
   195          \ was Oxford that made him insufferable." /
   196           -----------------------------------------
   197                    \
   198                     \
   199                      \     
   200                                    ##        .            
   201                              ## ## ##       ==            
   202                           ## ## ## ##      ===            
   203                       /""""""""""""""""___/ ===        
   204                  ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ /  ===- ~~~   
   205                       \______ o          __/            
   206                        \    \        __/             
   207                          \____\______/   
   208  
   209  As you can see, you've made the whale a lot smarter. It finds its own
   210  things to say and the command line is a lot shorter!  You may also notice
   211  that Docker didn't have to download anything.  That is because the image was
   212  built locally and is already available.
   213  
   214  ## Where to go next
   215  
   216  On this page, you learned to build an image by writing your own Dockerfile.
   217  You ran your image in a container. You also just used Linux from your Mac yet
   218  again. In the next section, you take the first step in sharing your image by
   219  [creating a Docker Hub account](step_five.md).
   220  
   221  
   222  &nbsp;