Skip to content

Commit b31f3af

Browse files
author
vcoisne
committed
Merge pull request docker#5 from ManoMarks/master
Adding in beginner tutorials
2 parents e71ace1 + 20882eb commit b31f3af

20 files changed

+1147
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ Index:
66
* Docker for [ASP.NET and Windows containers](windows/readme.md)
77
* [Docker for Java Developers](java/readme.adoc)
88
* [Docker Tutorials from the Community](community-tutorials.md)
9-
* [Guide to submitting your own tutorial](contriubtions.md)
9+
* [Guide to submitting your own tutorial](contribute.md)
1010
* [Docker documentation](https://docs.docker.com)

beginner/chapters/alpine.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
## 1.0 Running your first container
2+
Now that you have everything setup, it's time to get our hands dirty. In this section, you are going to run a [Alpine Linux](http://www.alpinelinux.org/) container (a lightweight linux distribution) on our system and get a taste of the `docker run` command.
3+
4+
To get started, let's run the following in our terminal:
5+
```
6+
$ docker pull alpine
7+
```
8+
9+
> Note: Depending on how you've installed docker on your system, you might see a `permission denied` error after running the above command. If you're on a Mac, [verify your installation](https://docs.docker.com/mac/step_one/). If you're on Linux, you may need to prefix your `docker` commands with `sudo`. Alternatively you can [create a docker group](http://docs.docker.com/engine/installation/ubuntulinux/#create-a-docker-group) to get rid of this issue.
10+
11+
The `pull` command fetches the alpine **image** from the **Docker registry** and saves it in our system. You can use the `docker images` command to see a list of all images on your system.
12+
```
13+
$ docker images
14+
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
15+
alpine latest c51f86c28340 4 weeks ago 1.109 MB
16+
hello-world latest 690ed74de00f 5 months ago 960 B
17+
```
18+
19+
### 1.1 Docker Run
20+
Great! Let's now run a Docker **container** based on this image. To do that you are going to use the `docker run` command.
21+
22+
```
23+
$ docker run alpine ls -l
24+
total 48
25+
drwxr-xr-x 2 root root 4096 Mar 2 16:20 bin
26+
drwxr-xr-x 5 root root 360 Mar 18 09:47 dev
27+
drwxr-xr-x 13 root root 4096 Mar 18 09:47 etc
28+
drwxr-xr-x 2 root root 4096 Mar 2 16:20 home
29+
drwxr-xr-x 5 root root 4096 Mar 2 16:20 lib
30+
......
31+
......
32+
```
33+
What happened? Behind the scenes, a lot of stuff happened. When you call `run`, the Docker client finds the image (alpine in this case), creates the container and then runs a command in that container. When you run `docker run alpine`, you provided a command (`ls -l`), so Docker started the command specified and you saw the listing.
34+
35+
Let's try something more exciting.
36+
37+
```
38+
$ docker run alpine echo "hello from alpine"
39+
hello from alpine
40+
```
41+
OK, that's some actual output. In this case, the Docker client dutifully ran the `echo` command in our alpine container and then exited it. If you've noticed, all of that happened pretty quickly. Imagine booting up a virtual machine, running a command and then killing it. Now you know why they say containers are fast!
42+
43+
Try another command.
44+
```
45+
$ docker run alpine uptime
46+
00:16:48 up 1:48, 0 users, load average: 0.00, 0.01, 0.04
47+
```
48+
49+
Try another command.
50+
```
51+
$ docker run alpine /bin/sh
52+
```
53+
54+
Wait, nothing happened! Is that a bug? Well, no. These interactive shells will exit after running any scripted commands, unless they are run in an interactive terminal - so for this example to not exit, you need to `docker run -it alpine /bin/sh`.
55+
56+
You are now inside the container shell and you can try out a few commands like `ls -l`, `uptime` and others. Exit out of the container by giving the `exit` command.
57+
58+
59+
Ok, now it's time to see the `docker ps` command. The `docker ps` command shows you all containers that are currently running.
60+
61+
```
62+
$ docker ps
63+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
64+
```
65+
66+
Since no containers are running, you see a blank line. Let's try a more useful variant: `docker ps -a`
67+
68+
```
69+
$ docker ps -a
70+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
71+
36171a5da744 alpine "/bin/sh" 5 minutes ago Exited (0) 2 minutes ago fervent_newton
72+
305297d7a235 alpine "uptime" 5 minutes ago Exited (0) 4 minutes ago distracted_goldstine
73+
a6a9d46d0b2f alpine "echo 'hello from alp" 6 minutes ago Exited (0) 6 minutes ago lonely_kilby
74+
ff0a5c3750b9 alpine "ls -l" 8 minutes ago Exited (0) 8 minutes ago elated_ramanujan
75+
c317d0a9e3d2 hello-world "/hello" 34 seconds ago Exited (0) 12 minutes ago stupefied_mcclintock
76+
```
77+
78+
What you see above is a list of all containers that you ran. Notice that the `STATUS` column shows that these containers exited a few minutes ago. You're probably wondering if there is a way to run more than just one command in a container. Let's try that now:
79+
80+
```
81+
$ docker run -it alpine /bin/sh
82+
/ # ls
83+
bin dev etc home lib linuxrc media mnt proc root run sbin sys tmp usr var
84+
/ # uptime
85+
05:45:21 up 5:58, 0 users, load average: 0.00, 0.01, 0.04
86+
```
87+
Running the `run` command with the `-it` flags attaches us to an interactive tty in the container. Now you can run as many commands in the container as you want. Take some time to run your favorite commands.
88+
89+
> **Danger Zone**: If you're feeling particularly adventurous you can try `rm -rf /bin` in the container. Make sure you run this command in the container and **not** in your laptop. Doing this will not make any other commands like `ls` and `grep` work. Once everything stops working, you can exit the container and then start it up again with the `docker run -it alpine sh` command. Since Docker creates a new container every time, everything should start working again.
90+
91+
That concludes a whirlwind tour of the `docker run` command which would most likely be the command you'll use most often. It makes sense to spend some time getting comfortable with it. To find out more about `run`, use `docker run --help` to see a list of all flags it supports. As you proceed further, we'll see a few more variants of `docker run`.
92+
### 1.2 Terminology
93+
In the last section, you saw a lot of Docker-specific jargon which might be confusing to some. So before you go further, let's clarify some terminology that is used frequently in the Docker ecosystem.
94+
95+
- *Images* - The Filesystem and configuration of our application which are used to create containers. To find out more about a Docker image, run `docker inspect alpine`. In the demo above, you used the `docker pull` command to download the **alpine** image. When you executed the command `docker run hello-world`, it also did a `docker pull` behind the scenes to download the **hello-world** image.
96+
- *Containers* - Created using Docker images and run the actual application. You created a container using `docker run` which you did using the alpine image that you downloaded. A list of running containers can be seen using the `docker ps` command.
97+
- *Docker daemon* - The background service running on the host that manages building, running and distributing Docker containers.
98+
- *Docker client* - The command line tool that allows the user to interact with the Docker daemon.
99+
- *Docker Hub* - A [registry](https://hub.docker.com/explore/) of Docker images. You can think of the registry as a directory of all available Docker images. You'll be using this later in this tutorial.
100+
101+
## Next Steps
102+
For the next step in the tutorial, head over to [2.0 Webapps with Docker](./webapps.md)

beginner/chapters/setup.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
### Prerequisites
2+
There are no specific skills needed for this tutorial beyond a basic comfort with the command line and using a text editor. Prior experience in developing web applications will be helpful but is not required. As you proceed further along the tutorial, we'll make use of [Docker Hub](https://hub.docker.com/).
3+
4+
### Setting up your computer
5+
Getting all the tooling setup on your computer can be a daunting task, but thankfully getting Docker up and running on your favorite OS has become very easy.
6+
7+
The *getting started* guide on Docker has detailed instructions for setting up Docker on [Mac](http://docs.docker.com/mac/step_one/), [Linux](http://docs.docker.com/linux/step_one/) and [Windows](http://docs.docker.com/windows/step_one/).
8+
9+
Once you are done installing Docker, test your Docker installation by running the following:
10+
```
11+
$ docker run hello-world
12+
Unable to find image 'hello-world:latest' locally
13+
latest: Pulling from library/hello-world
14+
03f4658f8b78: Pull complete
15+
a3ed95caeb02: Pull complete
16+
Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca36966a7
17+
Status: Downloaded newer image for hello-world:latest
18+
19+
Hello from Docker.
20+
This message shows that your installation appears to be working correctly.
21+
...
22+
```
23+
## Next Steps
24+
For the next step in the tutorial, head over to [1.0 Running your first container](alpine.md)

beginner/chapters/votingapp.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
## 3.0 Run a multi-container app with Docker Compose
2+
This portion of the tutorial will guide you through the creation and customization of a voting app. It's important that you follow the steps in order, and make sure to customize the portions that are customizable.
3+
4+
**Important.**
5+
To complete the submission, you will need to have Docker and Docker Compose installed on your machine as mentioned in the [Setup](./setup.md) section. You'll also need to have a [Docker Id](https://hub.docker.com/register/). Once you do run login from the commandline:
6+
7+
```
8+
$ docker login
9+
```
10+
11+
And follow the login directions. Now you can push images to Docker Hub.
12+
13+
> Note: If you encounter an error response from daemon while attempting to login, you may need to restart your machine by running `docker-machine restart <YOUR_DOCKER_MACHINE_NAME>`.
14+
15+
16+
### 3.1 Get the voting-app
17+
You now know how to build your own Docker image, so let's take it to the next level and glue things together. For this app you have to run multiple containers and Docker Compose is the best way to do that.
18+
19+
Start by quickly reading the documentation available [here](https://docs.docker.com/compose/overview/).
20+
21+
Clone the voting-app repository already available at [Github Repo](https://github.com/docker/example-voting-app.git).
22+
23+
```
24+
git clone https://github.com/docker/example-voting-app.git
25+
```
26+
27+
### 3.2 Customize the app
28+
29+
#### 3.2.1 Modify app.py
30+
31+
In the folder ```example-voting-app/voting-app``` you need to edit the app.py and change the two options for the programming languages you chose.
32+
33+
Edit the following lines:
34+
35+
```
36+
option_a = os.getenv('OPTION_A', "Java")
37+
option_b = os.getenv('OPTION_B', "Python")
38+
```
39+
40+
substituting two options of your choice. For instance:
41+
42+
```
43+
option_a = os.getenv('OPTION_A', "Cats")
44+
option_b = os.getenv('OPTION_B', "Dogs")
45+
```
46+
#### 3.2.2 Running your app
47+
Now, run your application. To do that, we'll use [Docker Compose](https://docs.docker.com/compose). Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you define a `.yml` file that describes all the containers and volumes that you want, and the networks between them. In the example-voting-app directory, you'll see a `docker-compose.yml file`:
48+
49+
```yml
50+
version: "2"
51+
52+
services:
53+
voting-app:
54+
build: ./voting-app/.
55+
volumes:
56+
- ./voting-app:/app
57+
ports:
58+
- "5000:80"
59+
networks:
60+
- front-tier
61+
- back-tier
62+
63+
result-app:
64+
build: ./result-app/.
65+
volumes:
66+
- ./result-app:/app
67+
ports:
68+
- "5001:80"
69+
networks:
70+
- front-tier
71+
- back-tier
72+
73+
worker:
74+
image: manomarks/worker
75+
networks:
76+
- back-tier
77+
78+
redis:
79+
image: redis:alpine
80+
container_name: redis
81+
ports: ["6379"]
82+
networks:
83+
- back-tier
84+
85+
db:
86+
image: postgres:9.4
87+
container_name: db
88+
volumes:
89+
- "db-data:/var/lib/postgresql/data"
90+
networks:
91+
- back-tier
92+
93+
volumes:
94+
db-data:
95+
96+
networks:
97+
front-tier:
98+
back-tier:
99+
```
100+
101+
This Compose file defines
102+
103+
- A voting-app container based on a Python image
104+
- A result-app container based on a Node.js image
105+
- A redis container based on a redis image, to temporarily store the data.
106+
- A Java based worker app based on a Java image
107+
- A Postgres container based on a postgres image
108+
109+
Note that three of the containers are built from Dockerfiles, while the other two are images on Docker Hub. To learn more about how they're built, you can examine each of the Dockerfiles in the two directories: `voting-app`, `result-app`. We included the code for the Java worker in `worker` but pre-built the image to save on downloads.
110+
111+
The Compose file also defines two networks, front-tier and back-tier. Each container is placed on one or two networks. Once on those networks, they can access other services on that network in code just by using the name of the service. To learn more about networking check out the [Networking with Compose documentation](https://docs.docker.com/compose/networking/).
112+
113+
To launch your app navigate to the example-voting-app directory and run the following command:
114+
115+
```
116+
$ docker-compose up -d
117+
```
118+
119+
This tells Compose to start all the containers specified in the `docker-compose.yml` file. The `-d` tells it to run them in daemon mode, in the background.
120+
121+
Last you'll need to figure out the ip address of your Docker host. If you're running Linux, it's just localhost, or 127.0.0.1. If you're using Docker Machine on Mac or Windows, you'll need to run:
122+
123+
```
124+
$ docker-machine ip default
125+
```
126+
127+
It'll return an IP address. If you only have one Docker Machine running, most likely, that's 192.168.99.100. We'll call that `<YOUR_IP_ADDRESS>`. Navigate to `http://<YOUR_IP_ADDRESS>:5000` in your browser, and you'll see the voting app, something like this:
128+
129+
<img src="../images/vote.png" title="vote">
130+
131+
Click on one to vote. You can check the results at `http://<YOUR_IP_ADDRESS:5001>`.
132+
133+
**NOTE**: If you are running this tutorial in a cloud environment like AWS, Azure, Digital Ocean, or GCE you will not have direct access to localhost or 127.0.0.1 via a browser. A work around for this is to leverage ssh port forwarding. Below is an example for Mac OS. Similarly this can be done for Windows and Putty users.
134+
135+
```
136+
$ ssh -L 5000:localhost:5000 <ssh-user>@<CLOUD_INSTANCE_IP_ADDRESS>
137+
```
138+
139+
#### 3.2.3 Build and tag images
140+
141+
You are all set now. Navigate to each of the directories where you have a Dockerfile to build and tag your images that you want to submit.
142+
143+
In order to build the images, make sure to replace `<YOUR_DOCKER_ID>` with your *Docker Hub username* in the following commands:
144+
145+
```
146+
$ docker build --no-cache -t <YOUR_DOCKER_ID>/votingapp_voting-app .
147+
...
148+
$ docker build --no-cache -t <YOUR_DOCKER_ID>/votingapp_result-app .
149+
...
150+
$ docker build --no-cache -t <YOUR_DOCKER_ID>/votingapp_worker .
151+
```
152+
153+
#### 3.2.4 Push images to Docker Hub
154+
155+
Push the images to Docker hub. Remember, you must have run `docker login` before you can push.
156+
157+
```
158+
$ docker push <YOUR_DOCKER_ID>/votingapp_voting-app
159+
...
160+
$ docker push <YOUR_DOCKER_ID>/votingapp_result-app
161+
...
162+
$ docker push <YOUR_DOCKER_ID>/votingapp_worker
163+
```
164+
165+
Now you can access these images anywhere by running
166+
167+
```
168+
$ docker pull <YOUR_DOCKER_ID>/votingapp_voting-app
169+
$ docker pull <YOUR_DOCKER_ID>/votingapp_result-app
170+
$ docker pull <YOUR_DOCKER_ID>/votingapp_worker
171+
```
172+
173+
### 3.3 Next steps
174+
Now that you've built some images and pushed them to hub, and learned about Docker Compose, you can explore more of Docker by checking out [the documentation](https://docs.docker.com). And if you need any help, check out the [Docker Forums](forums.docker.com) or [StackOverflow](https://stackoverflow.com/tags/docker/).

0 commit comments

Comments
 (0)