Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit f1ee16e

Browse files
author
Mano Marks
authored
Merge pull request #402 from ocularrhythm/patch-2
Apply minor formatting
2 parents d7f3430 + 911a276 commit f1ee16e

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

beginner/chapters/webapps.md

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Let's start by taking baby-steps. First, we'll use Docker to run a static websit
88

99
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.
1010

11-
```
11+
```bash
1212
$ docker run -d dockersamples/static-site
1313
```
1414

@@ -28,14 +28,15 @@ First, stop the container that you have just launched. In order to do this, we n
2828

2929
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.
3030

31-
```
31+
```bash
3232
$ docker ps
3333
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3434
a7a0e504ca3e dockersamples/static-site "/bin/sh -c 'cd /usr/" 28 seconds ago Up 26 seconds 80/tcp, 443/tcp stupefied_mahavira
3535
```
3636

3737
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
3940
$ docker stop a7a0e504ca3e
4041
$ docker rm a7a0e504ca3e
4142
```
@@ -44,7 +45,7 @@ $ docker rm a7a0e504ca3e
4445
4546
Now, let's launch a container in **detached** mode as shown below:
4647

47-
```
48+
```bash
4849
$ docker run --name static-site -e AUTHOR="Your Name" -d -P dockersamples/static-site
4950
e61d12292d69556eabe2a44c16cbd54486b2527e2ce4f95438e504afb7b02810
5051
```
@@ -59,7 +60,7 @@ In the above command:
5960

6061
Now you can see the ports by running the `docker port` command.
6162

62-
```
63+
```bash
6364
$ docker port static-site
6465
443/tcp -> 0.0.0.0:32772
6566
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
6970

7071
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).
7172

72-
```
73+
```bash
7374
$ docker-machine ip default
7475
192.168.99.100
7576
```
7677
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`.
7778

7879
You can also run a second webserver at the same time, specifying a custom host port mapping to the container's webserver.
7980

80-
```
81+
```bash
8182
$ docker run --name static-site-2 -e AUTHOR="Your Name" -d -p 8888:80 dockersamples/static-site
8283
```
84+
8385
<img src="../images/static.png" title="static">
8486

8587
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
8890

8991
But first, let's stop and remove the containers since you won't be using them anymore.
9092

91-
```
93+
```bash
9294
$ docker stop static-site
9395
$ docker rm static-site
9496
```
9597

9698
Let's use a shortcut to remove the second site:
9799

98-
```
100+
```bash
99101
$ docker rm -f static-site-2
100102
```
101103

102104
Run `docker ps` to make sure the containers are gone.
103-
```
105+
106+
```bash
104107
$ docker ps
105108
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
106109
```
@@ -111,7 +114,7 @@ In this section, let's dive deeper into what Docker images are. You will build y
111114

112115
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.
113116

114-
```
117+
```bash
115118
$ docker images
116119
REPOSITORY TAG IMAGE ID CREATED SIZE
117120
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
132135

133136
For example you could pull a specific version of `ubuntu` image as follows:
134137

135-
```
138+
```bash
136139
$ docker pull ubuntu:12.04
137140
```
138141

139142
If you do not specify the version number of the image then, as mentioned, the Docker client will default to a version named `latest`.
140143

141144
So for example, the `docker pull` command given below will pull an image named `ubuntu:latest`:
142145

143-
```
146+
```bash
144147
$ docker pull ubuntu
145148
```
146149

@@ -159,6 +162,7 @@ Another key concept is the idea of _official images_ and _user images_. (Both of
159162
- **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.
160163

161164
### 2.3 Create your first image
165+
162166
>**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.
163167
164168
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
187191
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.
188192

189193
#### app.py
194+
190195
Create the **app.py** with the following content:
191196

192-
```
197+
```python
193198
from flask import Flask, render_template
194199
import random
195200

@@ -219,16 +224,20 @@ def index():
219224
if __name__ == "__main__":
220225
app.run(host="0.0.0.0")
221226
```
227+
222228
#### requirements.txt
229+
223230
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:
224231

225232
```
226233
Flask==0.10.1
227234
```
235+
228236
#### templates/index.html
237+
229238
Create a directory called `templates` and create an **index.html** file in that directory with the following content in it:
230239

231-
```
240+
```html
232241
<html>
233242
<head>
234243
<style type="text/css">
@@ -257,7 +266,9 @@ Create a directory called `templates` and create an **index.html** file in that
257266
</body>
258267
</html>
259268
```
269+
260270
### 2.3.2 Write a Dockerfile
271+
261272
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**.
262273

263274
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 &#8212; 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
272283
```
273284

274285
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+
275287
```
276288
RUN apk add --update py2-pip
277289
```
@@ -293,6 +305,7 @@ A [Dockerfile](https://docs.docker.com/engine/reference/builder/) is a text file
293305
```
294306

295307
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+
296309
```
297310
EXPOSE 5000
298311
```
@@ -410,7 +423,7 @@ If you don't have the `alpine:3.5` image, the client will first pull the image a
410423
### 2.3.4 Run your image
411424
The next step in this section is to run the image and see if it actually works.
412425

413-
```
426+
```bash
414427
$ docker run -p 8888:5000 --name myfirstapp YOUR_USERNAME/myfirstapp
415428
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
416429
```
@@ -426,29 +439,30 @@ Now that you've created and tested your image, you can push it to [Docker Cloud]
426439

427440
First you have to login to your Docker Cloud account, to do that:
428441

429-
```
442+
```bash
430443
docker login
431444
```
432445

433446
Enter `YOUR_USERNAME` and `password` when prompted.
434447

435448
Now all you have to do is:
436449

437-
```
450+
```bash
438451
docker push YOUR_USERNAME/myfirstapp
439452
```
453+
440454
Now that you are done with this container, stop and remove it since you won't be using it again.
441455

442456
Open another terminal window and execute the following commands:
443457

444-
```
458+
```bash
445459
$ docker stop myfirstapp
446460
$ docker rm myfirstapp
447461
```
448462

449463
or
450464

451-
```
465+
```bash
452466
$ docker rm -f myfirstapp
453467
```
454468

@@ -463,6 +477,7 @@ Here's a quick summary of the few basic commands we used in our Dockerfile.
463477
* `COPY` copies local files into the container.
464478

465479
* `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:
480+
466481
```
467482
CMD ["python", "./app.py"]
468483

0 commit comments

Comments
 (0)