Develop Intelligence - Docker Build Mounts and Volumes
Develop Intelligence - Docker Build Mounts and Volumes
Dockerfile:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o out
No matter which type of mount you choose to use, the data looks the same from within the container.
An easy way to visualize the difference is to think about where the data lives on the Docker host.
Bind mounts have been around since the early days of Docker. Bind mounts have limited functionality compared to
volumes. When you use a bind mount, a file or directory on the host machine is mounted into a container. The file
or directory is referenced by its full or relative path on the host machine. By contrast, when you use a volume, a
new directory is created within Docker’s storage directory on the host machine, and Docker manages that
directory’s contents.
Develop Intelligence – Docker Build Mounts and Volumes
Volumes are the preferred mechanism for persisting data generated by and used by Docker containers.
Volumes have several advantages over bind mounts:
Volumes are easier to back up or migrate than bind mounts.
You can manage volumes using Docker CLI commands or the Docker API.
Volumes work similary on both Linux and Windows containers.
Volumes can be more safely shared among multiple containers.
New volumes can have their content pre-populated by a container (demo below).
Volume drivers* let you store volumes on remote hosts or cloud providers, to encrypt the contents of
volumes.
*https://docs.docker.com/storage/volumes/#use-a-volume-driver
Save the data on node2 using the identity of user test. If anything is saved to /home/text of container, it should
be saved on Node2. Volume name=sshvolume
$ docker volume create --driver vieux/sshfs \
-o sshcmd=username@node2:/home/test \
-o password=userpassword \
sshvolume
Working with Docker Bind Mount
Syntax for attaching bind mount or volume to container
We can use either
1. -v or --volume
2. --mount
Syntax for Linux Containers:
docker run --rm --volume /data:/app/mydata sandeepsoni/counterdemo
OR
docker run --rm --mount type=bind,source=/data,destination=/app/mydata sandeepsoni/counterdemo
/data is the physical folder on host machine.
This command can be used for checking root directory of host machine
docker run --rm -it -v /:/root ubuntu sh
# cd /root
Note:
The biggest difference is that the -v syntax combines all the options together in one field, while the --mount syntax
separates them.
New users should try --mount syntax which is simpler than --volume syntax.
Create a volume
docker volume create my-vol
Volume Commands:
docker volume create my-vol
docker volume ls
docker volume inspect my-vol
docker volume rm my-vol
MSSQL Server
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=Password_*123" --name "sql-demo" -e
"MSSQL_PID=Developer" -d -p 1412:1433 -v db-data-sqlsrv:/var/opt/mssql mcr.microsoft.com/mssql/server:2019-
latest
MySQL Server
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=Password_*123 -d -p 3306:3306 -v db-data-
mysql:/var/lib/mysql mysql:latest
Demo for "New volumes can have their content pre-populated by a container"
When the container is created, files in container volume folder will be also visible in volume folder on host.
Dockerfile
FROM ubuntu
RUN mkdir /myvol
RUN echo "hello world" > /myvol/greeting.txt
Note:
Command: docker run -it --rm -v v1:/myvol demo bash
Develop Intelligence – Docker Build Mounts and Volumes
Observation: The file greeting.txt will be present in volume v1 (/var/lib/docker/volumes/v1/_data/) on host and
also in /myvol folder in container.
If Linux VM is used – Filesystem of the container can be used using the following command
docker run --rm -it -v /:/host alpine
# cd host
Anonymous Volumes
When you mount a volume, it may be named or anonymous. Anonymous volumes are not given an explicit name
when they are first mounted into a container, so Docker gives them a random name that is guaranteed to be
unique within a given Docker host. Besides the name, named and anonymous volumes behave in the same ways.
docker run -it -v /data ubuntu /bin/sh
docker run -it --mount type=volume,source=,destination=/data ubuntu /bin/sh
Deleting Volumes:
Unused Volumes (no running container is using a volume) can be removed using following command
docker volume prune