github.com/portworx/docker@v1.12.1/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 158.8 MB 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 158.8 MB 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 0 : 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 1 : 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 Removing intermediate container e2a84b5f390f 147 Step 2 : RUN apt-get install -y fortunes 148 ---> Running in 23aa52c1897c 149 Reading package lists... 150 Building dependency tree... 151 Reading state information... 152 The following extra packages will be installed: 153 fortune-mod fortunes-min librecode0 154 Suggested packages: 155 x11-utils bsdmainutils 156 The following NEW packages will be installed: 157 fortune-mod fortunes fortunes-min librecode0 158 0 upgraded, 4 newly installed, 0 to remove and 3 not upgraded. 159 Need to get 1961 kB of archives. 160 After this operation, 4817 kB of additional disk space will be used. 161 Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main librecode0 amd64 3.6-21 [771 kB] 162 ...snip...... 163 Setting up fortunes (1:1.99.1-7) ... 164 Processing triggers for libc-bin (2.19-0ubuntu6.6) ... 165 ---> c81071adeeb5 166 Removing intermediate container 23aa52c1897c 167 168 Finally, Docker finishes the build and reports its outcome. 169 170 Step 3 : CMD /usr/games/fortune -a | cowsay 171 ---> Running in a8e6faa88df3 172 ---> 7d9495d03763 173 Removing intermediate container a8e6faa88df3 174 Successfully built 7d9495d03763 175 176 177 ## Step 4: Run your new docker-whale 178 179 In this step, you verify the new images is on your computer and then you run your new image. 180 181 1. Open a command line terminal. 182 183 2. Type `docker images` and press RETURN. 184 185 This command, you might remember, lists the images you have locally. 186 187 $ docker images 188 REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE 189 docker-whale latest 7d9495d03763 4 minutes ago 273.7 MB 190 docker/whalesay latest fb434121fc77 4 hours ago 247 MB 191 hello-world latest 91c95931e552 5 weeks ago 910 B 192 193 3. Run your new image by typing `docker run docker-whale` and pressing RETURN. 194 195 $ docker run docker-whale 196 _________________________________________ 197 / "He was a modest, good-humored boy. It \ 198 \ was Oxford that made him insufferable." / 199 ----------------------------------------- 200 \ 201 \ 202 \ 203 ## . 204 ## ## ## == 205 ## ## ## ## === 206 /""""""""""""""""___/ === 207 ~~~ {~~ ~~~~ ~~~ ~~~~ ~~ ~ / ===- ~~~ 208 \______ o __/ 209 \ \ __/ 210 \____\______/ 211 212 As you can see, you've made the whale a lot smarter. It finds its own 213 things to say and the command line is a lot shorter! You may also notice 214 that Docker didn't have to download anything. That is because the image was 215 built locally and is already available. 216 217 ## Where to go next 218 219 On this page, you learned to build an image by writing your own Dockerfile. 220 You ran your image in a container. You also just used Linux from your Mac yet 221 again. In the next section, you take the first step in sharing your image by 222 [creating a Docker Hub account](step_five.md). 223 224 225