Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

Docker_Served_Static_Website.html 17 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="generator" content="pandoc" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
  7. <title>Docker Served Static Website</title>
  8. <style type="text/css">
  9. code{white-space: pre-wrap;}
  10. span.smallcaps{font-variant: small-caps;}
  11. span.underline{text-decoration: underline;}
  12. div.column{display: inline-block; vertical-align: top; width: 50%;}
  13. </style>
  14. <style type="text/css">
  15. a.sourceLine { display: inline-block; line-height: 1.25; }
  16. a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
  17. a.sourceLine:empty { height: 1.2em; }
  18. .sourceCode { overflow: visible; }
  19. code.sourceCode { white-space: pre; position: relative; }
  20. div.sourceCode { margin: 1em 0; }
  21. pre.sourceCode { margin: 0; }
  22. @media screen {
  23. div.sourceCode { overflow: auto; }
  24. }
  25. @media print {
  26. code.sourceCode { white-space: pre-wrap; }
  27. a.sourceLine { text-indent: -1em; padding-left: 1em; }
  28. }
  29. pre.numberSource a.sourceLine
  30. { position: relative; left: -4em; }
  31. pre.numberSource a.sourceLine::before
  32. { content: attr(title);
  33. position: relative; left: -1em; text-align: right; vertical-align: baseline;
  34. border: none; pointer-events: all; display: inline-block;
  35. -webkit-touch-callout: none; -webkit-user-select: none;
  36. -khtml-user-select: none; -moz-user-select: none;
  37. -ms-user-select: none; user-select: none;
  38. padding: 0 4px; width: 4em;
  39. color: #aaaaaa;
  40. }
  41. pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
  42. div.sourceCode
  43. { }
  44. @media screen {
  45. a.sourceLine::before { text-decoration: underline; }
  46. }
  47. code span.al { color: #ff0000; font-weight: bold; } /* Alert */
  48. code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
  49. code span.at { color: #7d9029; } /* Attribute */
  50. code span.bn { color: #40a070; } /* BaseN */
  51. code span.bu { } /* BuiltIn */
  52. code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
  53. code span.ch { color: #4070a0; } /* Char */
  54. code span.cn { color: #880000; } /* Constant */
  55. code span.co { color: #60a0b0; font-style: italic; } /* Comment */
  56. code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
  57. code span.do { color: #ba2121; font-style: italic; } /* Documentation */
  58. code span.dt { color: #902000; } /* DataType */
  59. code span.dv { color: #40a070; } /* DecVal */
  60. code span.er { color: #ff0000; font-weight: bold; } /* Error */
  61. code span.ex { } /* Extension */
  62. code span.fl { color: #40a070; } /* Float */
  63. code span.fu { color: #06287e; } /* Function */
  64. code span.im { } /* Import */
  65. code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
  66. code span.kw { color: #007020; font-weight: bold; } /* Keyword */
  67. code span.op { color: #666666; } /* Operator */
  68. code span.ot { color: #007020; } /* Other */
  69. code span.pp { color: #bc7a00; } /* Preprocessor */
  70. code span.sc { color: #4070a0; } /* SpecialChar */
  71. code span.ss { color: #bb6688; } /* SpecialString */
  72. code span.st { color: #4070a0; } /* String */
  73. code span.va { color: #19177c; } /* Variable */
  74. code span.vs { color: #4070a0; } /* VerbatimString */
  75. code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
  76. </style>
  77. <link rel="stylesheet" href="style.css" />
  78. </head>
  79. <body>
  80. <h1 id="nginx-served-static-website-in-docker">NGINX Served Static Website In Docker</h1>
  81. <figure>
  82. <img src="Docker_Logo.png" alt="Docker Logo" /><figcaption>Docker Logo</figcaption>
  83. </figure>
  84. <p>Up to this point, we’ve gone over Docker installation and Docker concepts. In the last lesson, we also learned how to simply launch a Docker image straight from a downloaded image off the web in one command line command. Docker is very powerful and flexible. In this lesson, we want to continue the beginner lab and go to the next lesson. BUT we will also take that next lesson to the next logical level. Let’s start by going back to the <a href="https://docs.docker.com/">Docker Docs Site</a>.</p>
  85. <figure>
  86. <img src="../3_BEing_a_Docker_User_by_DOing/Docker_Docs_Site.png" alt="Docker Docs Site" /><figcaption>Docker Docs Site</figcaption>
  87. </figure>
  88. <p>Find this and all the other lessons in this Docker Mastery journey in my <a href="https://dagshub.com/ThomIves/Docker_Mastery">Docker Mastery</a> repo on <a href="https://dagshub.com/">DagsHub</a> - the Cloud Git Repo system for data scientists and machine learning engineers. Note that you can get a free account on DagsHub.</p>
  89. <p>Let’s return to the <strong>Get Started</strong> area again. Then we pick the <strong>Samples</strong> tab.</p>
  90. <figure>
  91. <img src="../3_BEing_a_Docker_User_by_DOing/Pick_Samples_Tab.png" alt="Samples" /><figcaption>Samples</figcaption>
  92. </figure>
  93. <p>That will take you to the Samples area of the site.</p>
  94. <figure>
  95. <img src="../3_BEing_a_Docker_User_by_DOing/Samples_Area.png" alt="Samples Area" /><figcaption>Samples Area</figcaption>
  96. </figure>
  97. <p>From there, let’s pick the top item - <a href="https://github.com/docker/labs/tree/master/beginner/">Docker for Beginners</a>. This will take us back to a GitHub page that has no cloning setup. The <strong>readme.md</strong> serves as our launch point for our second <em>learn</em> by <em>DOing</em> lesson.</p>
  98. <figure>
  99. <img src="../3_BEing_a_Docker_User_by_DOing/Docker_Labs_Beginner.png" alt="Beginner Lab" /><figcaption>Beginner Lab</figcaption>
  100. </figure>
  101. <p>In the previous lesson, we looked at my repo <a href="https://dagshub.com/ThomIves/Docker_Mastery/src/master/3_BEing_a_Docker_User_by_DOing">BEing a Docker User by DOing</a> in my <a href="https://dagshub.com/">DagsHub</a> repo for <a href="https://dagshub.com/ThomIves/Docker_Mastery">Docker Mastery</a>. We went over lesson 1.0 of the Beginner Lab for Docker.</p>
  102. <p>Today we will do the <strong>2.0 Webapps with Docker</strong> lesson. After that lesson, we will do a slightly more advanced step. We will start from our own HTML file and Dockerfile.</p>
  103. <h2 id="the-webapps-with-docker-lesson">The “Webapps with Docker” Lesson</h2>
  104. <p>The steps below are from the <strong>2.0 Webapps with Docker</strong> lesson. We only repeat them here for your convenience. It would be good to follow the steps from the Docker for Beginners repo though. This next command really does it all. We will want to modify it for our specific needs.</p>
  105. <p><strong>NOTE</strong> that I usually start my commands with <code>sudo</code> below, because I am working in Linux. Your OS likely won’t require this unless you are running in Linux. Just saying.</p>
  106. <p><code>sudo docker run --name static-site -e AUTHOR="Your Name" -d -P dockersamples/static-site</code></p>
  107. <p>In the above command: * -d will create a container with the process detached from our terminal * -P will publish all the exposed container ports to random ports on the Docker host * -e is how you pass environment variables to the container * –name allows you to specify a container name * AUTHOR is the environment variable name and Your Name is the value that you can pass</p>
  108. <p>We are passing in a rich number of options to the <code>run</code> command. To see all <code>run</code> options, use the following command at a command line for your OS.</p>
  109. <p><code>sudo docker run --help</code></p>
  110. <p>Now we want to modify the above example line to our specific needs.</p>
  111. <pre class="shell"><code>thom@thom-PT5610:~$ sudo docker run --name static-site -e AUTHOR=&quot;Thom Ives&quot; -d -P dockersamples/static-site
  112. [sudo] password for thom:
  113. Unable to find image &#39;dockersamples/static-site:latest&#39; locally
  114. latest: Pulling from dockersamples/static-site
  115. fdd5d7827f33: Pull complete
  116. a3ed95caeb02: Pull complete
  117. 716f7a5f3082: Pull complete
  118. 7b10f03a0309: Pull complete
  119. aff3ab7e9c39: Pull complete
  120. Digest: sha256:daa686c61d7d239b7977e72157997489db49f316b9b9af3909d9f10fd28b2dec
  121. Status: Downloaded newer image for dockersamples/static-site:latest
  122. f7fa837ea431def2c8720202d0d8b0233b5cdf7605c1848939674d9f378c6d25</code></pre>
  123. <p>With the <code>-d</code> option, the terminal will return to our control. We need to run the following to discover the port on our machine that our new Docker container is serving our static website to.</p>
  124. <pre class="shell"><code>thom@thom-PT5610:~$ sudo docker port static-site
  125. 443/tcp -&gt; 0.0.0.0:49159
  126. 443/tcp -&gt; :::49159
  127. 80/tcp -&gt; 0.0.0.0:49160
  128. 80/tcp -&gt; :::49160
  129. thom@thom-PT5610:~$ </code></pre>
  130. <p>You’ll see later that we can also use <code>sudo docker ps</code> to find the port number that we need. PLEASE do not expect to see the same port numbers for your run. Remember that the <code>-P</code> option created random port numbers for us. In a web browser, enter the following for a URL - <code>http://localhost:49160/</code>. Nice! We get our Docker served static website from NGINX running in our Docker container!</p>
  131. <figure>
  132. <img src="Docker_NGINX_Static_Website_from_Samples.png" alt="Docker NGINX Static Website from Samples" /><figcaption>Docker NGINX Static Website from Samples</figcaption>
  133. </figure>
  134. <h2 id="doing-more-ourselves">DOing MORE Ourselves</h2>
  135. <p>What I want to learn is how to do this WITHOUT automatically pulling <code>dockersamples/static-site</code> from Docker Hub. This will prepare us for knowing how to launch our own static websites AND MORE. So let’s write our own HTML. OK, I confess! The HTML5 Boilerplate package wrote most of this in VS Code. I added very little to it and deleted even more.</p>
  136. <div class="sourceCode" id="cb3"><pre class="sourceCode html"><code class="sourceCode html"><a class="sourceLine" id="cb3-1" title="1"><span class="dt">&lt;!DOCTYPE </span>html<span class="dt">&gt;</span></a>
  137. <a class="sourceLine" id="cb3-2" title="2"><span class="kw">&lt;html&gt;</span></a>
  138. <a class="sourceLine" id="cb3-3" title="3"> <span class="kw">&lt;head&gt;</span></a>
  139. <a class="sourceLine" id="cb3-4" title="4"> <span class="kw">&lt;meta</span><span class="ot"> charset=</span><span class="st">&quot;utf-8&quot;</span><span class="kw">&gt;</span></a>
  140. <a class="sourceLine" id="cb3-5" title="5"> <span class="kw">&lt;meta</span><span class="ot"> http-equiv=</span><span class="st">&quot;X-UA-Compatible&quot;</span><span class="ot"> content=</span><span class="st">&quot;IE=edge&quot;</span><span class="kw">&gt;</span></a>
  141. <a class="sourceLine" id="cb3-6" title="6"> <span class="kw">&lt;title&gt;</span>Docker NGINX Static Website<span class="kw">&lt;/title&gt;</span></a>
  142. <a class="sourceLine" id="cb3-7" title="7"> <span class="kw">&lt;meta</span><span class="ot"> name=</span><span class="st">&quot;description&quot;</span><span class="ot"> content=</span><span class="st">&quot;&quot;</span><span class="kw">&gt;</span></a>
  143. <a class="sourceLine" id="cb3-8" title="8"> <span class="kw">&lt;meta</span><span class="ot"> name=</span><span class="st">&quot;viewport&quot;</span><span class="ot"> content=</span><span class="st">&quot;width=device-width, initial-scale=1&quot;</span><span class="kw">&gt;</span></a>
  144. <a class="sourceLine" id="cb3-9" title="9"> <span class="kw">&lt;link</span><span class="ot"> rel=</span><span class="st">&quot;stylesheet&quot;</span><span class="ot"> href=</span><span class="st">&quot;&quot;</span><span class="kw">&gt;</span></a>
  145. <a class="sourceLine" id="cb3-10" title="10"> <span class="kw">&lt;/head&gt;</span></a>
  146. <a class="sourceLine" id="cb3-11" title="11"> <span class="kw">&lt;body&gt;</span></a>
  147. <a class="sourceLine" id="cb3-12" title="12"> <span class="kw">&lt;h1&gt;</span>Hi Friend and the Data Science Community<span class="kw">&lt;/h1&gt;</span></a>
  148. <a class="sourceLine" id="cb3-13" title="13"> <span class="kw">&lt;p&gt;</span>This is running in a Docker Container with a base NGINX image!<span class="kw">&lt;/p&gt;</span></a>
  149. <a class="sourceLine" id="cb3-14" title="14"> <span class="kw">&lt;p&gt;</span>More to come ...<span class="kw">&lt;/p&gt;</span></a>
  150. <a class="sourceLine" id="cb3-15" title="15"> <span class="kw">&lt;/body&gt;</span></a>
  151. <a class="sourceLine" id="cb3-16" title="16"><span class="kw">&lt;/html&gt;</span></a></code></pre></div>
  152. <p>Now we also need our own <code>Dockerfile</code>. I heavily plagiarized the one from the beginner lab. It’s worth understanding what each step of that <code>CMD</code> line is doing.</p>
  153. <div class="sourceCode" id="cb4"><pre class="sourceCode dockerfile"><code class="sourceCode dockerfile"><a class="sourceLine" id="cb4-1" title="1"><span class="kw">FROM</span> nginx</a>
  154. <a class="sourceLine" id="cb4-2" title="2"><span class="kw">ENV</span> FRIEND=<span class="st">&quot;Thom&quot;</span></a>
  155. <a class="sourceLine" id="cb4-3" title="3"></a>
  156. <a class="sourceLine" id="cb4-4" title="4"><span class="kw">WORKDIR</span> /usr/share/nginx/html</a>
  157. <a class="sourceLine" id="cb4-5" title="5"><span class="kw">COPY</span> Hello_Docker.html /usr/share/nginx/html</a>
  158. <a class="sourceLine" id="cb4-6" title="6"></a>
  159. <a class="sourceLine" id="cb4-7" title="7"><span class="kw">CMD</span> cd /usr/share/nginx/html &amp;&amp; sed -e s/Friend/<span class="st">&quot;$FRIEND&quot;</span>/ Hello_Docker.html &gt; index.html ; nginx -g <span class="st">&#39;daemon off;&#39;</span></a></code></pre></div>
  160. <p>For the CMD line: 1. change the directory to <code>/usr/share/nginx/html</code> in the NGINX server (NOTE that NGINX has many layers itself - it’s NGINX running on top of a Linux OS layer) 1. <code>&amp;&amp;</code> means to only do the next commands IF the previous command was successful 1. <code>sed -e s/Friend/"$FRIEND"/ Hello_Docker.html</code> is a fun command - you should study it with a web search - it essentially finds Friend in the html and replaces it with the value for the $FRIEND environment variable 1. direct the <code>Hello_Docker.html</code> file to a new file <code>index.html</code> 1. <code>;</code> indicates the next command 1. learning more about <code>nginx -g 'daemon off;'</code> is left as an exercise to the student (I hated reading that when I was an undergrad - NOW I love to write that when I am lazy!)</p>
  161. <p>Now, we can build our static website with our own modified HTML code and <code>Dockerfile</code>. Let’s break down this next command a bit. The <code>docker build</code> part is straight forward enough. The <code>-t</code> in case you haven’t seen the previous documents in the repo or a LinkedIn post means add the tag_name that follows.</p>
  162. <pre class="shell"><code>thom@thom-PT5610:~/Dir_with_Dockerfile$ sudo docker build -t my_static_website .
  163. Sending build context to Docker daemon 8.704kB
  164. Step 1/5 : FROM nginx
  165. latest: Pulling from library/nginx
  166. 42c077c10790: Pull complete
  167. 62c70f376f6a: Pull complete
  168. 915cc9bd79c2: Pull complete
  169. 75a963e94de0: Pull complete
  170. 7b1fab684d70: Pull complete
  171. db24d06d5af4: Pull complete
  172. Digest: sha256:2bcabc23b45489fb0885d69a06ba1d648aeda973fae7bb981bafbb884165e514
  173. Status: Downloaded newer image for nginx:latest
  174. ---&gt; 0e901e68141f
  175. Step 2/5 : ENV FRIEND=&quot;Thom&quot;
  176. ---&gt; Running in 73af5f3adedc
  177. Removing intermediate container 73af5f3adedc
  178. ---&gt; ad5daeb0c4b5
  179. Step 3/5 : WORKDIR /usr/share/nginx/html
  180. ---&gt; Running in e69b7a1cd003
  181. Removing intermediate container e69b7a1cd003
  182. ---&gt; f6e82d92a60d
  183. Step 4/5 : COPY Hello_Docker.html /usr/share/nginx/html
  184. ---&gt; 7827ed7d7bba
  185. Step 5/5 : CMD cd /usr/share/nginx/html &amp;&amp; sed -e s/Docker/&quot;$FRIEND&quot;/ Hello_Docker.html &gt; index.html ; nginx -g &#39;daemon off;&#39;
  186. ---&gt; Running in 9cc2c4c8c4ce
  187. Removing intermediate container 9cc2c4c8c4ce
  188. ---&gt; 5eb55ed8032c
  189. Successfully built 5eb55ed8032c
  190. Successfully tagged my_static_website:latest</code></pre>
  191. <p>GREAT! It built successfully. Now we need to run it. Remember, the <code>-d</code> means to detach the run from the terminal that it started it. The -P is a constructively lazy way to randomly create the port numbers needed to serve the page.</p>
  192. <pre class="shell"><code>thom@thom-PT5610:~/Dir_with_Dockerfile$ sudo docker run -d -P my_static_website
  193. 6b47bbcce7c43d4b1635d8862f36b4c499ab3cb823b54d218ada6baae275197b
  194. thom@thom-PT5610:~/Dir_with_Dockerfile$</code></pre>
  195. <p>Great! It’s running. Now we need to discover the port numbers that were created. We see that we need http://localhost:49158.</p>
  196. <pre class="shell"><code>thom@thom-PT5610:~/image.png$ sudo docker ps
  197. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  198. f0974b625626 my_static_website &quot;/docker-entrypoint.…&quot; 4 seconds ago Up 3 seconds 0.0.0.0:49158-&gt;80/tcp, :::49158-&gt;80/tcp elastic_herschel
  199. thom@thom-PT5610:~/image.png$ </code></pre>
  200. <p>BOOM! It worked. We are a step closer to building our own websites using Docker and NGINX. We are also a step closer to serving up our own websites using a cloud service such as Digital Ocean or one of those other 3 big cloud services.</p>
  201. <figure>
  202. <img src="Docker_NGINX_Static_Website.png" alt="My Docker NGINX Static Website" /><figcaption>My Docker NGINX Static Website</figcaption>
  203. </figure>
  204. <p>Book: The Linux Command Line</p>
  205. <h1 id="summary">Summary</h1>
  206. <p>That was MORE FUN than we should be allowed to have! But let’s have it anyway! Next step? Let’s continue in this lab next time. Then let’s learn enough to put this website up in the wild.</p>
  207. </body>
  208. </html>
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...