This commit is contained in:
Aaron Arney 2018-02-07 21:20:12 -05:00 коммит произвёл GitHub
Родитель 2abddfee59
Коммит 911a27645f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 35 добавлений и 20 удалений

Просмотреть файл

@ -8,7 +8,7 @@ Let's start by taking baby-steps. First, we'll use Docker to run a static websit
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.
```
```bash
$ docker run -d dockersamples/static-site
```
@ -28,14 +28,15 @@ First, stop the container that you have just launched. In order to do this, we n
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.
```
```bash
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a7a0e504ca3e dockersamples/static-site "/bin/sh -c 'cd /usr/" 28 seconds ago Up 26 seconds 80/tcp, 443/tcp stupefied_mahavira
```
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.
```
```bash
$ docker stop a7a0e504ca3e
$ docker rm a7a0e504ca3e
```
@ -44,7 +45,7 @@ $ docker rm a7a0e504ca3e
Now, let's launch a container in **detached** mode as shown below:
```
```bash
$ docker run --name static-site -e AUTHOR="Your Name" -d -P dockersamples/static-site
e61d12292d69556eabe2a44c16cbd54486b2527e2ce4f95438e504afb7b02810
```
@ -59,7 +60,7 @@ In the above command:
Now you can see the ports by running the `docker port` command.
```
```bash
$ docker port static-site
443/tcp -> 0.0.0.0:32772
80/tcp -> 0.0.0.0:32773
@ -69,7 +70,7 @@ If you are running [Docker for Mac](https://docs.docker.com/docker-for-mac/), [D
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).
```
```bash
$ docker-machine ip default
192.168.99.100
```
@ -77,9 +78,10 @@ You can now open `http://<YOUR_IPADDRESS>:[YOUR_PORT_FOR 80/tcp]` to see your si
You can also run a second webserver at the same time, specifying a custom host port mapping to the container's webserver.
```
```bash
$ docker run --name static-site-2 -e AUTHOR="Your Name" -d -p 8888:80 dockersamples/static-site
```
<img src="../images/static.png" title="static">
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
But first, let's stop and remove the containers since you won't be using them anymore.
```
```bash
$ docker stop static-site
$ docker rm static-site
```
Let's use a shortcut to remove the second site:
```
```bash
$ docker rm -f static-site-2
```
Run `docker ps` to make sure the containers are gone.
```
```bash
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
```
@ -111,7 +114,7 @@ In this section, let's dive deeper into what Docker images are. You will build y
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.
```
```bash
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockersamples/static-site latest 92a386b6e686 2 hours ago 190.5 MB
@ -132,7 +135,7 @@ For simplicity, you can think of an image akin to a git repository - images can
For example you could pull a specific version of `ubuntu` image as follows:
```
```bash
$ docker pull ubuntu:12.04
```
@ -140,7 +143,7 @@ If you do not specify the version number of the image then, as mentioned, the Do
So for example, the `docker pull` command given below will pull an image named `ubuntu:latest`:
```
```bash
$ docker pull ubuntu
```
@ -159,6 +162,7 @@ Another key concept is the idea of _official images_ and _user images_. (Both of
- **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.
### 2.3 Create your first image
>**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.
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
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.
#### app.py
Create the **app.py** with the following content:
```
```python
from flask import Flask, render_template
import random
@ -219,16 +224,20 @@ def index():
if __name__ == "__main__":
app.run(host="0.0.0.0")
```
#### requirements.txt
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:
```
Flask==0.10.1
```
#### templates/index.html
Create a directory called `templates` and create an **index.html** file in that directory with the following content in it:
```
```html
<html>
<head>
<style type="text/css">
@ -257,7 +266,9 @@ Create a directory called `templates` and create an **index.html** file in that
</body>
</html>
```
### 2.3.2 Write a Dockerfile
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**.
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
```
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:
```
RUN apk add --update py2-pip
```
@ -293,6 +305,7 @@ A [Dockerfile](https://docs.docker.com/engine/reference/builder/) is a text file
```
4. Specify the port number which needs to be exposed. Since our flask app is running on `5000` that's what we'll expose.
```
EXPOSE 5000
```
@ -410,7 +423,7 @@ If you don't have the `alpine:3.5` image, the client will first pull the image a
### 2.3.4 Run your image
The next step in this section is to run the image and see if it actually works.
```
```bash
$ docker run -p 8888:5000 --name myfirstapp YOUR_USERNAME/myfirstapp
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
```
@ -426,7 +439,7 @@ Now that you've created and tested your image, you can push it to [Docker Cloud]
First you have to login to your Docker Cloud account, to do that:
```
```bash
docker login
```
@ -434,21 +447,22 @@ Enter `YOUR_USERNAME` and `password` when prompted.
Now all you have to do is:
```
```bash
docker push YOUR_USERNAME/myfirstapp
```
Now that you are done with this container, stop and remove it since you won't be using it again.
Open another terminal window and execute the following commands:
```
```bash
$ docker stop myfirstapp
$ docker rm myfirstapp
```
or
```
```bash
$ docker rm -f myfirstapp
```
@ -463,6 +477,7 @@ Here's a quick summary of the few basic commands we used in our Dockerfile.
* `COPY` copies local files into the container.
* `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:
```
CMD ["python", "./app.py"]