Skip to content

Commit 85b1998

Browse files
committed
Adding lesson 8, 0, 10
1 parent 3bae9ba commit 85b1998

3 files changed

Lines changed: 93 additions & 4 deletions

File tree

docker-for-beginners/notes/08-exposing-container-ports.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ If the argument was `-p 8080:80` then port 80 of the host machine would be mappe
1616
The container would be accessed using port 8080.
1717

1818
To see the exposed ports of a container run `docker port <CONTAINER_NAME>`. In this example run `docker port webserver`.
19+
This should return a similar output to the following:
20+
```bash
21+
80/tcp -> 0.0.0.0:80
22+
```
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1-
Notes coming soon.
1+
To change the hostname of a container use the `-h` or the `--hostname` argument when running a container.
22

3-
[Edit these notes](https://github.com/howToCodeWell/course-notes)
3+
For example to create a container with the hostname of `abc` run the following
4+
5+
```bash
6+
docker run -it -h abc ubuntu bash
7+
```
8+
This will create a Ubuntu container with the hostname `abc`. This container will be running bash and the terminal will have a similar output
9+
```bash
10+
$ root@abc:/#
11+
```
12+
13+
Setting a hostname of a container will also alter the containers host file.
14+
15+
To check this run the following `cat /etc/host`
16+
17+
```bash
18+
127.0.0.1 localhost
19+
20+
// Other entries
21+
22+
172.17.0.2 abc abc
23+
```
24+
25+
Please note, your container IP will be different.
Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,66 @@
1-
Notes coming soon.
1+
## Types of Docker volumes
2+
There are two type of volumes in Docker.
3+
The first is a Docker bind mount which is a path from the host machines directory structure to a mount point inside the container.
4+
The second is a Docker named volume. This is a managed mount point in Docker which allows the attachment from multiple containers.
25

3-
[Edit these notes](https://github.com/howToCodeWell/course-notes)
6+
## How to create a bind mount
7+
8+
To create a bind mount use the `-v` argument when running a container. This argument has two parts seperated by a `:`.
9+
10+
```bash
11+
-v <HOST_DIR>:<CONTAINER_DIR>
12+
```
13+
The left hand-side of the `:` is the directory structure from the host machine.
14+
The right hand-side of the `:` is the mount point of the container.
15+
16+
To mount the host machines home directory into a new directory called data on the container run the following
17+
```bash
18+
$ docker run -it -v ~/home/peterfisher:/data ubuntu bash
19+
```
20+
21+
## How to crate a Docker volume
22+
To create a Docker volume when running a container use the `-v` argument like the bind example but instead of supplying a path from the host machine pass a name of the volume that you wish to create,
23+
```bash
24+
-v <VOLUME_NAME>:<CONTAINER_DIR>
25+
```
26+
To create a Docker volume called `data` which is mounted to the container at the path `/data` run the following
27+
28+
```bash
29+
$ docker run --name test1 -it -v data:/data ubuntu bash
30+
```
31+
A named Docker volume can also be attached to another container at a different mount point.
32+
The following example attaches the same data Docker volume to a new container called `test2`. The Docker volume is mounted at the path `/data/store` in the container.
33+
```bash
34+
$ docker run --name test2 -it -v data:/data/store ubuntu bash
35+
```
36+
## To list Docker volumes
37+
To list all the docker volumes on the hostmachine run the following
38+
```bash
39+
$ docker volume list
40+
```
41+
42+
## To create a Docker volume
43+
To create a Docker volume without create or running a Docker container run the following
44+
```bash
45+
$ docker volume create <VOLUME_NAME>
46+
```
47+
48+
## How to attach volumes from another container
49+
To mount volumes that are attached from other containers pass the `--volumes-from` argument when creating a new container.
50+
The above command could be simplified by replacing the `-v` argument with the `--volumes-from` argument. Please note that both the name of the Docker volume and the original mount point inside the container will be copied.
51+
```bash
52+
$ docker run --name test2 -it --volumes-from test1 ubuntu bash
53+
```
54+
55+
## How to inspect volume configuration
56+
To inspect the mounts of a container run the following command and look for the mounts section in the JSON output.
57+
```bash
58+
$ docker inspect <CONTAINER_NAME_OR_ID>
59+
```
60+
61+
62+
To inspect the configuration of a Docker volume run the following
63+
```bash
64+
$ docker volume inspect <VOLUME_NAME>
65+
```
66+
This will show the name of the volume, the driver and the mount point of the container.

0 commit comments

Comments
 (0)