# Introduction to Docker #2

***originally published July 31th***

## Need for Mounting Data Outside of Container
The data or files created in a container basically exist in the container as default. 
Since the container is disposable, once the container is renewed (meaning trash the old container, and replace it with a new one), the data will disappear. However, putting the data or files on the host side, they are evacuated and are not deleted even though the container is suddenly stopped or trashed. The way of evacuating data on the host side is two ways, ***volumes***, and ***bind mounts***.

<div style="text-align: right;">
[docker reference](https://docs.docker.com/storage/)
<div>

## Volumes (Might be best practice)


![Volume.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659190804582/XusDMILCw.png align="left")

Volumes are created on the host side and managed by Docker. All of them are controlled through Docker. Volumes are created with the command `docker volume create`.

### Advantages of Volumes
1. Sharing the data among multiple containers safely
2. Not relying on File System or File structure
3. Using the Non-local Data (Can store the data in the cloud)

### Example of Mounting with Volumes with Nginx
Before running the container, need to create volume first. Volumes can be created with `docker volume create`. This example creates a volume and names it `docker-nigex`.
```docker
❯ docker volume create docker-nigex
docker-nigex
```

This example mounts volume `docker-nigex`, names the container `volumenginx`, and connect 8080 to 80. 

```docker
❯ docker run --name volumenginx -d -p 8080:80 -v docker-nigex:/usr/share/nginx/html nginx
```
With `docker inspect [OPTIONS] NAME|ID [NAME|ID...]`, can see the object detail information (either container or volume).

**Volume detail information**
![dockerinspectdocker-nigex.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659230313163/92aiFcZ92.png align="left")

With the command `docker inspect volumenginx`and seeing the section of `"Mounts":`, the detail container information exactly matches what you see `docker inspect docker-nginx`.

**Container detail information**
![firstvolumenginx.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659231172325/y3rkx7OUS.png align="left")
![secondnigex.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659231195453/0E-8Sm3uJ.png align="left")

Creating the directory `docker-nginx` and placing the files below:<br>
1.[index.html](https://github.com/KojiroAsano/Eye_Exercise/blob/main/index.html)<br>
2.[eye.js](https://github.com/KojiroAsano/Eye_Exercise/blob/main/eyes.js)<br>
3.[styles.css](https://github.com/KojiroAsano/Eye_Exercise/blob/main/styles.css)<br>
Copy the directory to the container.

```docker
docker cp C:\Users\JOK32\docker\docker-nigex volumenginx:/usr/share/nginx/html
```
Now, go to http://localhost:8080/ again and see a display below:
![volumenigex.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659273920847/AjCe3JZNN.png align="left")



<hr>
## Bind mounts


![Bindmount.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659190854740/p_eyhxaUN.png align="left")

Bind mounts can be created anywhere on the host side. Bind mounts can be controlled with ease, but they rely on the host side much. Bind mounts can be utilized with the command `docker run` and option `-v` (short command of `--volume`). If the directory does not exist, the docker automatically creates the directory.

### Advantages of Volumes
1. Can use existing files or data
2. Change can easily be seen 



### Example of Mounting with Bind Mounts with nginx
In this example, mounting existing directory `C:\Users\JOK32\docker\docker-nigex` (this directory is empty), named container as `bindmountnginx`, and connect port `80` to `8080`.
[Nginx reference(Dockerhub)](https://hub.docker.com/_/nginx)
```docker
> docker run --name bindmountnginx -d -p 8080:80 -v C:\Users\JOK32\docker\docker-nigex:/usr/share/nginx/html nginx
```
go to http://localhost:8080/. <br>
Since there is no file in the directory, nigex just displays like below:

![bindmountnigex.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659227063071/SsVY61DRR.png align="left")

Putting files below under `\docker\docker-nigex`<br>
1.[index.html](https://github.com/KojiroAsano/Eye_Exercise/blob/main/index.html)<br>
2.[eye.js](https://github.com/KojiroAsano/Eye_Exercise/blob/main/eyes.js)<br>
3.[styles.css](https://github.com/KojiroAsano/Eye_Exercise/blob/main/styles.css)<br>
nigex automatically displays what you want.

![eye-nigex.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1659227630419/uLSrjs7Sp.png align="left")

<hr>
Any feedback always welcome!!
