You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 18, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: beginner/chapters/webapps.md
+35-20Lines changed: 35 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Let's start by taking baby-steps. First, we'll use Docker to run a static websit
8
8
9
9
The image that you are going to use is a single-page website that was already created for this demo and is available on the Docker Store as [`dockersamples/static-site`](https://store.docker.com/community/images/dockersamples/static-site). You can download and run the image directly in one go using `docker run` as follows.
10
10
11
-
```
11
+
```bash
12
12
$ docker run -d dockersamples/static-site
13
13
```
14
14
@@ -28,14 +28,15 @@ First, stop the container that you have just launched. In order to do this, we n
28
28
29
29
Since we ran the container in detached mode, we don't have to launch another terminal to do this. Run `docker ps` to view the running containers.
30
30
31
-
```
31
+
```bash
32
32
$ docker ps
33
33
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34
34
a7a0e504ca3e dockersamples/static-site "/bin/sh -c 'cd /usr/" 28 seconds ago Up 26 seconds 80/tcp, 443/tcp stupefied_mahavira
35
35
```
36
36
37
37
Check out the `CONTAINER ID` column. You will need to use this `CONTAINER ID` value, a long sequence of characters, to identify the container you want to stop, and then to remove it. The example below provides the `CONTAINER ID` on our system; you should use the value that you see in your terminal.
38
-
```
38
+
39
+
```bash
39
40
$ docker stop a7a0e504ca3e
40
41
$ docker rm a7a0e504ca3e
41
42
```
@@ -44,7 +45,7 @@ $ docker rm a7a0e504ca3e
44
45
45
46
Now, let's launch a container in **detached** mode as shown below:
Now you can see the ports by running the `docker port` command.
61
62
62
-
```
63
+
```bash
63
64
$ docker port static-site
64
65
443/tcp -> 0.0.0.0:32772
65
66
80/tcp -> 0.0.0.0:32773
@@ -69,17 +70,18 @@ If you are running [Docker for Mac](https://docs.docker.com/docker-for-mac/), [D
69
70
70
71
If you are using Docker Machine on Mac or Windows, you can find the hostname on the command line using `docker-machine` as follows (assuming you are using the `default` machine).
71
72
72
-
```
73
+
```bash
73
74
$ docker-machine ip default
74
75
192.168.99.100
75
76
```
76
77
You can now open `http://<YOUR_IPADDRESS>:[YOUR_PORT_FOR 80/tcp]` to see your site live! For our example, this is: `http://192.168.99.100:32773`.
77
78
78
79
You can also run a second webserver at the same time, specifying a custom host port mapping to the container's webserver.
To deploy this on a real server you would just need to install Docker, and run the above `docker` command(as in this case you can see the `AUTHOR` is Docker which we passed as an environment variable).
@@ -88,19 +90,20 @@ Now that you've seen how to run a webserver inside a Docker container, how do yo
88
90
89
91
But first, let's stop and remove the containers since you won't be using them anymore.
90
92
91
-
```
93
+
```bash
92
94
$ docker stop static-site
93
95
$ docker rm static-site
94
96
```
95
97
96
98
Let's use a shortcut to remove the second site:
97
99
98
-
```
100
+
```bash
99
101
$ docker rm -f static-site-2
100
102
```
101
103
102
104
Run `docker ps` to make sure the containers are gone.
103
-
```
105
+
106
+
```bash
104
107
$ docker ps
105
108
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
106
109
```
@@ -111,7 +114,7 @@ In this section, let's dive deeper into what Docker images are. You will build y
111
114
112
115
Docker images are the basis of containers. In the previous example, you **pulled** the *dockersamples/static-site* image from the registry and asked the Docker client to run a container **based** on that image. To see the list of images that are available locally on your system, run the `docker images` command.
113
116
114
-
```
117
+
```bash
115
118
$ docker images
116
119
REPOSITORY TAG IMAGE ID CREATED SIZE
117
120
dockersamples/static-site latest 92a386b6e686 2 hours ago 190.5 MB
@@ -132,15 +135,15 @@ For simplicity, you can think of an image akin to a git repository - images can
132
135
133
136
For example you could pull a specific version of `ubuntu` image as follows:
134
137
135
-
```
138
+
```bash
136
139
$ docker pull ubuntu:12.04
137
140
```
138
141
139
142
If you do not specify the version number of the image then, as mentioned, the Docker client will default to a version named `latest`.
140
143
141
144
So for example, the `docker pull` command given below will pull an image named `ubuntu:latest`:
142
145
143
-
```
146
+
```bash
144
147
$ docker pull ubuntu
145
148
```
146
149
@@ -159,6 +162,7 @@ Another key concept is the idea of _official images_ and _user images_. (Both of
159
162
-**User images** are images created and shared by users like you. They build on base images and add additional functionality. Typically these are formatted as `user/image-name`. The `user` value in the image name is your Docker Store user or organization name.
160
163
161
164
### 2.3 Create your first image
165
+
162
166
>**Note:** The code for this section is in this repository in the [flask-app](https://github.com/docker/labs/tree/master/beginner/flask-app) directory.
163
167
164
168
Now that you have a better understanding of images, it's time to create your own. Our goal here is to create an image that sandboxes a small [Flask](http://flask.pocoo.org) application.
@@ -187,9 +191,10 @@ Start by creating a directory called ```flask-app``` where we'll create the foll
187
191
Make sure to ```cd flask-app``` before you start creating the files, because you don't want to start adding a whole bunch of other random files to your image.
188
192
189
193
#### app.py
194
+
190
195
Create the **app.py** with the following content:
191
196
192
-
```
197
+
```python
193
198
from flask import Flask, render_template
194
199
import random
195
200
@@ -219,16 +224,20 @@ def index():
219
224
if__name__=="__main__":
220
225
app.run(host="0.0.0.0")
221
226
```
227
+
222
228
#### requirements.txt
229
+
223
230
In order to install the Python modules required for our app, we need to create a file called **requirements.txt** and add the following line to that file:
224
231
225
232
```
226
233
Flask==0.10.1
227
234
```
235
+
228
236
#### templates/index.html
237
+
229
238
Create a directory called `templates` and create an **index.html** file in that directory with the following content in it:
230
239
231
-
```
240
+
```html
232
241
<html>
233
242
<head>
234
243
<styletype="text/css">
@@ -257,7 +266,9 @@ Create a directory called `templates` and create an **index.html** file in that
257
266
</body>
258
267
</html>
259
268
```
269
+
260
270
### 2.3.2 Write a Dockerfile
271
+
261
272
We want to create a Docker image with this web app. As mentioned above, all user images are based on a _base image_. Since our application is written in Python, we will build our own Python image based on [Alpine](https://store.docker.com/images/alpine). We'll do that using a **Dockerfile**.
262
273
263
274
A [Dockerfile](https://docs.docker.com/engine/reference/builder/) is a text file that contains a list of commands that the Docker daemon calls while creating an image. The Dockerfile contains all the information that Docker needs to know to run the app — a base Docker image to run from, ___location of your project code, any dependencies it has, and what commands to run at start-up. It is a simple way to automate the image creation process. The best part is that the [commands](https://docs.docker.com/engine/reference/builder/) you write in a Dockerfile are *almost* identical to their equivalent Linux commands. This means you don't really have to learn new syntax to create your own Dockerfiles.
@@ -272,6 +283,7 @@ A [Dockerfile](https://docs.docker.com/engine/reference/builder/) is a text file
272
283
```
273
284
274
285
2. The next step usually is to write the commands of copying the files and installing the dependencies. But first we will install the Python pip package to the alpine linux distribution. This will not just install the pip package but any other dependencies too, which includes the python interpreter. Add the following [RUN](https://docs.docker.com/engine/reference/builder/#run) command next:
286
+
275
287
```
276
288
RUN apk add --update py2-pip
277
289
```
@@ -293,6 +305,7 @@ A [Dockerfile](https://docs.docker.com/engine/reference/builder/) is a text file
293
305
```
294
306
295
307
4. Specify the port number which needs to be exposed. Since our flask app is running on `5000` that's what we'll expose.
308
+
296
309
```
297
310
EXPOSE 5000
298
311
```
@@ -410,7 +423,7 @@ If you don't have the `alpine:3.5` image, the client will first pull the image a
410
423
### 2.3.4 Run your image
411
424
The next step in this section is to run the image and see if it actually works.
412
425
413
-
```
426
+
```bash
414
427
$ docker run -p 8888:5000 --name myfirstapp YOUR_USERNAME/myfirstapp
415
428
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
416
429
```
@@ -426,29 +439,30 @@ Now that you've created and tested your image, you can push it to [Docker Cloud]
426
439
427
440
First you have to login to your Docker Cloud account, to do that:
428
441
429
-
```
442
+
```bash
430
443
docker login
431
444
```
432
445
433
446
Enter `YOUR_USERNAME` and `password` when prompted.
434
447
435
448
Now all you have to do is:
436
449
437
-
```
450
+
```bash
438
451
docker push YOUR_USERNAME/myfirstapp
439
452
```
453
+
440
454
Now that you are done with this container, stop and remove it since you won't be using it again.
441
455
442
456
Open another terminal window and execute the following commands:
443
457
444
-
```
458
+
```bash
445
459
$ docker stop myfirstapp
446
460
$ docker rm myfirstapp
447
461
```
448
462
449
463
or
450
464
451
-
```
465
+
```bash
452
466
$ docker rm -f myfirstapp
453
467
```
454
468
@@ -463,6 +477,7 @@ Here's a quick summary of the few basic commands we used in our Dockerfile.
463
477
*`COPY` copies local files into the container.
464
478
465
479
*`CMD` defines the commands that will run on the Image at start-up. Unlike a `RUN`, this does not create a new layer for the Image, but simply runs the command. There can only be one `CMD` per a Dockerfile/Image. If you need to run multiple commands, the best way to do that is to have the `CMD` run a script. `CMD` requires that you tell it where to run the command, unlike `RUN`. So example `CMD` commands would be:
0 commit comments