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
Copy file name to clipboardExpand all lines: beginner/chapters/webapps.md
+47-19
Original file line number
Diff line number
Diff line change
@@ -6,44 +6,56 @@ Great! So you have now looked at `docker run`, played with a docker container an
6
6
7
7
Let's start by taking baby-steps. First, we'll use Docker to run a dead-simple static website. You're going to pull a Docker image from the Docker Hub, run the container and see how easy it is to set up a web server.
8
8
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 Hub as [`seqvence/static-site`](https://hub.docker.com/r/seqvence/static-site/). You can download and run the image directly in one go using `docker run`.
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 Hub as [`seqvence/static-site`](https://hub.docker.com/r/seqvence/static-site/). You can download and run the image directly in one go using `docker run` as follows.
10
10
11
11
```
12
-
$ docker run seqvence/static-site
12
+
$ docker run -d seqvence/static-site
13
13
```
14
-
Since the image doesn't exist on your Docker host, the Docker daemon first fetches the image from the registry and then runs the image.
15
14
16
-
* Okay, now that the server is running, do you see the website?
17
-
* What port is it running on?
18
-
* And more importantly, how do you access the container directly from our host machine?
15
+
>**Note:** The current version of this image doesn't run without the `-d` flag, although it should. The `-d` flag enables **detached mode**, which detaches the running container from the terminal/shell and returns your prompt after the container starts. We are debugging the problem with this image but for now, use `-d` even for this first example.
19
16
20
-
In this case, the client didn't tell the Docker Engine to publish any of the ports, so you need to re-run the `docker run`command. We'll take the oportunity to publish ports and pass your name to the container to customize the message displayed. While we are at it, you should also find a way so that our terminal is not attached to the running container. So that you can happily close your terminal and keep the container running. This is called the **detached** mode.
17
+
So, what happens when you run this command?
21
18
22
-
Before we look at the **detached** mode, we should first find out a way to stop the container that you have just launched.
19
+
Since the image doesn't exist on your Docker host, the Docker daemon first fetches the it from the registry and then runs it as a container.
20
+
21
+
Now that the server is running, do you see the website? What port is it running on? And more importantly, how do you access the container directly from our host machine?
22
+
23
+
Actually, you probably won't be able to answer any of these questions yet! ☺ In this case, the client didn't tell the Docker Engine to publish any of the ports, so you need to re-run the `docker run` command to add this instruction.
24
+
25
+
Let's re-run the command with some new flags to publish ports and pass your name to the container to customize the message displayed. We'll use the *-d* option again to run the container in detached mode.
26
+
27
+
First, stop the container that you have just launched. In order to do this, we need the container ID.
28
+
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.
23
30
24
-
First up, launch another terminal (command window) and execute the following command. If you're using docker-machine you need to run `eval $(docker-machine env <YOUR_DOCKER_MACHINE_NAME>)` in each new terminal otherwise you'll get the error "Cannot connect to the Docker daemon. Is the docker daemon running on this host?".
25
31
```
26
32
$ docker ps
27
33
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
28
34
a7a0e504ca3e seqvence/static-site "/bin/sh -c 'cd /usr/" 28 seconds ago Up 26 seconds 80/tcp, 443/tcp stupefied_mahavira
29
35
```
30
36
31
-
Check out the `CONTAINER ID` column. You will need to use this `CONTAINER ID` value, a long sequence of characters and first stop the running container and then remove the running container as given below. The example below provides the `CONTAINER ID` on our system, you should use the value that you see in your terminal.
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.
32
38
```
33
39
$ docker stop a7a0e504ca3e
34
40
$ docker rm a7a0e504ca3e
35
41
```
36
42
37
-
Note: A cool feature is that you do not need to specify the entire `CONTAINER ID`. You can just specify a few starting characters and if it is unique among all the containers that you have launched, the Docker client will intelligently pick it up.
43
+
>**Note:** A cool feature is that you do not need to specify the entire `CONTAINER ID`. You can just specify a few starting characters and if it is unique among all the containers that you have launched, the Docker client will intelligently pick it up.
38
44
39
-
Now, let us launch a container in **detached** mode as shown below:
45
+
Now, let's launch a container in **detached** mode as shown below:
In the above command, `-d` will create a container with the process detached from our terminal, `-P` will publish all the exposed container ports to random ports on the Docker host, `-e` is how you pass environment variables to the container, and finally `--name` allows you to specify a container name. `AUTHOR` is the environment variable name and `Your Name` is the value that you can pass.
52
+
In the above command:
53
+
54
+
*`-d` will create a container with the process detached from our terminal
55
+
*`-P` will publish all the exposed container ports to random ports on the Docker host
56
+
*`-e` is how you pass environment variables to the container
57
+
*`--name` allows you to specify a container name
58
+
*`AUTHOR` is the environment variable name and `Your Name` is the value that you can pass
47
59
48
60
Now you can see the ports by running the `docker port` command.
49
61
@@ -53,13 +65,15 @@ $ docker port static-site
53
65
80/tcp -> 0.0.0.0:32773
54
66
```
55
67
56
-
If you're on Linux, you can open [http://localhost:32773](http://localhost:32773) (replace 32773 with your port for 80/tcp) in your browser. If you're on Windows or a Mac, you need to find the IP of the hostname.
68
+
If you are running [Docker for Mac](https://docs.docker.com/docker-for-mac/), [Docker for Windows](https://docs.docker.com/docker-for-windows/), or Docker on Linux, you can open `http://localhost:YOUR_PORT_FOR 80/tcp`. For our example this is `http://localhost:32773`.
69
+
70
+
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).
57
71
58
72
```
59
73
$ docker-machine ip default
60
74
192.168.99.100
61
75
```
62
-
You can now open [http://192.168.99.100:32773](http://192.168.99.100:32773) (replace 32773 with your port for 80/tcp) to see your site live!
76
+
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`.
63
77
64
78
You can also run a second webserver at the same time, specifying a custom host port mapping to the container's webserver.
I'm sure you agree that was super simple. To deploy this on a real server you would just need to install docker, and run the above docker command.
85
+
I'm sure you agree that was super simple. To deploy this on a real server you would just need to install Docker, and run the above `docker` command.
86
+
87
+
Now that you've seen how to run a webserver inside a Docker image, you must be wondering - how do I create my own Docker image? This is the question we'll explore in the next section.
72
88
73
-
Now that you've seen how to run a webserver inside a docker image, you must be wondering - how do I create my own docker image? This is the question we'll be exploring in the next section. But first, let's stop and remove the containers since you won't be using them anymore.
89
+
But first, let's stop and remove the containers since you won't be using them anymore.
74
90
75
91
```
76
-
$ docker stop static-site static-site-2
77
-
$ docker rm static-site static-site-2
92
+
$ docker stop static-site static-site
93
+
$ docker rm static-site static-site
94
+
```
95
+
96
+
Let's use a shortcut to remove the second site:
97
+
98
+
```
99
+
$ docker rm -f static-site static-site-2
100
+
```
101
+
102
+
Run `docker ps` to make sure the containers are gone.
103
+
```
104
+
$ docker ps
105
+
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0 commit comments