summaryrefslogtreecommitdiff
path: root/docs/gettingstarted.md
blob: ff944177b60f2f8c685ae0a15471e20b1732277d (plain)
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
<!--[metadata]>
+++
title = "Getting Started"
description = "Getting started with Docker Compose"
keywords = ["documentation, docs,  docker, compose, orchestration, containers"]
[menu.main]
parent="workw_compose"
weight=-85
+++
<![end-metadata]-->


# Getting Started

On this page you build a simple Python web application running on Docker Compose. The
application uses the Flask framework and increments a value in Redis. While the
sample uses Python, the concepts demonstrated here should be understandable even
if you're not familiar with it.

## Prerequisites

Make sure you have already
[installed both Docker Engine and Docker Compose](install.md). You
don't need to install Python, it is provided by a Docker image.

## Step 1: Setup

1. Create a directory for the project:

        $ mkdir composetest
        $ cd composetest

2. With your favorite text editor create a file called `app.py` in your project
   directory.

        from flask import Flask
        from redis import Redis

        app = Flask(__name__)
        redis = Redis(host='redis', port=6379)

        @app.route('/')
        def hello():
            redis.incr('hits')
            return 'Hello World! I have been seen %s times.' % redis.get('hits')

        if __name__ == "__main__":
            app.run(host="0.0.0.0", debug=True)

3. Create another file called `requirements.txt` in your project directory and
   add the following:

        flask
        redis

   These define the applications dependencies.

## Step 2: Create a Docker image

In this step, you build a new Docker image. The image contains all the
dependencies the Python application requires, including Python itself.

1. In your project directory create a file named `Dockerfile` and add the
   following:

        FROM python:2.7
        ADD . /code
        WORKDIR /code
        RUN pip install -r requirements.txt
        CMD python app.py

  This tells Docker to:

  * Build an image starting with the Python 2.7 image.
  * Add the current directory `.` into the path `/code` in the image.
  * Set the working directory to `/code`.
  * Install the Python dependencies.
  * Set the default command for the container to `python app.py`

  For more information on how to write Dockerfiles, see the [Docker user guide](https://docs.docker.com/engine/userguide/dockerimages/#building-an-image-from-a-dockerfile) and the [Dockerfile reference](http://docs.docker.com/reference/builder/).

2. Build the image.

        $ docker build -t web .

  This command builds an image named `web` from the contents of the current
  directory. The command automatically locates the `Dockerfile`, `app.py`, and
  `requirements.txt` files.


## Step 3: Define services

Define a set of services using `docker-compose.yml`:

1. Create a file called docker-compose.yml in your project directory and add
   the following:


        version: '2'
        services:
          web:
            build: .
            ports:
             - "5000:5000"
            volumes:
             - .:/code
            depends_on:
             - redis
          redis:
            image: redis

This Compose file defines two services, `web` and `redis`. The web service:

* Builds from the `Dockerfile` in the current directory.
* Forwards the exposed port 5000 on the container to port 5000 on the host machine.
* Mounts the project directory on the host to `/code` inside the container allowing you to modify the code without having to rebuild the image.
* Links the web service to the Redis service.

The `redis` service uses the latest public [Redis](https://registry.hub.docker.com/_/redis/) image pulled from the Docker Hub registry.

## Step 4: Build and run your app with Compose

1. From your project directory, start up your application.

        $ docker-compose up
        Pulling image redis...
        Building web...
        Starting composetest_redis_1...
        Starting composetest_web_1...
        redis_1 | [8] 02 Jan 18:43:35.576 # Server started, Redis version 2.8.3
        web_1   |  * Running on http://0.0.0.0:5000/
        web_1   |  * Restarting with stat

   Compose pulls a Redis image, builds an image for your code, and start the
   services you defined.

2. Enter `http://0.0.0.0:5000/` in a browser to see the application running.

   If you're using Docker on Linux natively, then the web app should now be
   listening on port 5000 on your Docker daemon host. If `http://0.0.0.0:5000`
   doesn't resolve, you can also try `http://localhost:5000`.

   If you're using Docker Machine on a Mac, use `docker-machine ip MACHINE_VM` to get
   the IP address of your Docker host. Then, `open http://MACHINE_VM_IP:5000` in a
   browser.

   You should see a message in your browser saying:

   `Hello World! I have been seen 1 times.`

3. Refresh the page.

   The number should increment.

## Step 5: Experiment with some other commands

If you want to run your services in the background, you can pass the `-d` flag
(for "detached" mode) to `docker-compose up` and use `docker-compose ps` to
see what is currently running:

        $ docker-compose up -d
        Starting composetest_redis_1...
        Starting composetest_web_1...
        $ docker-compose ps
        Name                 Command            State       Ports
        -------------------------------------------------------------------
        composetest_redis_1   /usr/local/bin/run         Up
        composetest_web_1     /bin/sh -c python app.py   Up      5000->5000/tcp

The `docker-compose run` command allows you to run one-off commands for your
services. For example, to see what environment variables are available to the
`web` service:

        $ docker-compose run web env

See `docker-compose --help` to see other available commands. You can also install [command completion](completion.md) for the bash and zsh shell, which will also show you available commands.

If you started Compose with `docker-compose up -d`, you'll probably want to stop
your services once you've finished with them:

        $ docker-compose stop

At this point, you have seen the basics of how Compose works.


## Where to go next

- Next, try the quick start guide for [Django](django.md),
  [Rails](rails.md), or [WordPress](wordpress.md).
- [Explore the full list of Compose commands](./reference/index.md)
- [Compose configuration file reference](compose-file.md)