Docker Lab Exercises
Day-4
Create Docker Volumes | docker volume ls, docker volume create, docker volume inspect, docker run --name C1 -it -v vol1:/mylogs ubuntu
1. List Docker volumes:
docker volume ls
Output (example):
DRIVER VOLUME NAME
local vol1
This command lists the Docker volumes available on your system. In this case, there is a volume named "vol1" created using the default local driver.
2. Create a Docker volume named "vol1":
docker volume create vol1
Output (no output indicates successful execution):
This command creates a Docker volume named "vol1" using the default local driver.
3. Inspect the details of the "vol1" volume:
docker volume inspect vol1
Output (example):
json
[
{
"CreatedAt": "2023-07-18T12:34:56Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/vol1/_data",
"Name": "vol1",
"Options": {},
"Scope": "local"
}
]
This command displays detailed information about the "vol1" volume, including its creation time, driver, mountpoint, and other metadata.
4. List the content of the volume directory on the host system:
ls -l /var/lib/docker/volumes
Output (example):
total 4
drwxr-xr-x 3 root root 4096 Jul 18 12:34 vol1
This command lists the contents of the Docker volumes directory (`/var/lib/docker/volumes`), showing the "vol1" volume directory.
5. List the content of the "vol1" volume's data directory on the host system:
ls -l /var/lib/docker/volumes/vol1/_data
Output (example):
total 0
This command lists the contents of the data directory for the "vol1" volume. Since the volume was just created and is empty, no files or directories are present.
6. Run a container named "C1" with an interactive session and mount the "vol1" volume:
docker run --name C1 -it -v vol1:/mylogs ubuntu
Output (inside the container):
root@containerid:/#
This command starts a new container named "C1" using the "ubuntu" image, opens an interactive session inside the container, and mounts the "vol1" volume to the `/mylogs` directory within the container.
7. Display disk space usage:
df -h
Output (example):
Filesystem Size Used Avail Use% Mounted on
overlay 64G 5.5G 59G 9% /
tmpfs 64M 0 64M 0% /dev
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
shm 64M 0 64M 0% /dev/shm
/dev/xvda1 64G 5.5G 59G 9% /mylogs
tmpfs 1.9G 0 1.9G 0% /proc/asound
tmpfs 1.9G 0 1.9G 0% /proc/acpi
tmpfs 1.9G 0 1.9G 0% /proc/scsi
tmpfs 1.9G 0 1.9G 0% /sys/firmware
This command displays disk space usage within the container. It shows the filesystems and their sizes, used space, available space, and mount points.
8. Change directory to `/mylogs` inside the container:
cd /mylogs
Output (inside the container):
root@containerid:/mylogs#
This command changes the current directory to `/mylogs` inside the container.
9. Create a new file named "file1.txt":
touch file1.txt
Output (no output indicates successful execution):
This command creates an empty file named "file1.txt" in the current directory (`/mylogs`).
10. Append text to "file1.txt":
echo "I am on C1" >> file1.txt
Output (no output indicates successful execution):
This command appends the text "I am on C1" to the "file1.txt" file.
11. Exit the container:
exit
Output (no output indicates successful execution):
This command exits the interactive session within the container and returns to the host system's terminal.
12. List the running containers:
docker ps
Output (example):
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
This command shows an empty list of running containers since the container "C1" has been stopped.
13. List the content of the "vol1" volume's data directory on the host system:
ls -l /var/lib/docker/volumes/vol1/_data
Output (example):
total 4
-rw-r--r-- 1 root root 16 Jul 18 12:34 file1.txt
This command lists the contents of the "vol1" volume's data directory. It displays the "file1.txt" file that was created inside the container.
14. List Docker volumes:
docker volume ls
Output (example):
DRIVER VOLUME NAME
local vol1
This command lists the Docker volumes available on your system. The "vol1" volume is listed.
15. Run a new container named "C2" with an interactive session and mount the "vol1" volume:
docker run --name C2 -it -v vol1:/newlogs ubuntu
Output (inside the container):
root@containerid:/#
This command starts a new container named "C2" using the "ubuntu" image, opens an interactive session inside the container, and mounts the "vol1" volume to the `/newlogs` directory within the container.
16. Display disk space usage:
df -h
Output (example):
Filesystem Size Used Avail Use% Mounted on
overlay 64G 5.5G 59G 9% /
tmpfs 64M 0 64M 0% /dev
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
shm 64M 0 64M 0% /dev/shm
/dev/xvda1 64G 5.5G 59G 9% /newlogs
tmpfs 1.9G 0 1.9G 0% /
proc/asound
tmpfs 1.9G 0 1.9G 0% /proc/acpi
tmpfs 1.9G 0 1.9G 0% /proc/scsi
tmpfs 1.9G 0 1.9G 0% /sys/firmware
This command displays disk space usage within the container. It shows the filesystems and their sizes, used space, available space, and mount points.
17. Change directory to `/newlogs` inside the container:
cd /newlogs/
Output (inside the container):
root@containerid:/newlogs#
This command changes the current directory to `/newlogs` inside the container.
18. List the content of the `/newlogs` directory:
ls -l
Output (inside the container):
total 4
-rw-r--r-- 1 root root 16 Jul 18 12:34 file1.txt
-rw-r--r-- 1 root root 16 Jul 18 12:34 file2.txt
This command lists the files in the `/newlogs` directory inside the container. It shows both "file1.txt" (created in step 9) and "file2.txt" (created in step 19).
19. Append text to "file2.txt":
echo "I am on C2" >> file2.txt
Output (no output indicates successful execution):
This command appends the text "I am on C2" to the "file2.txt" file.
20. View the contents of "file2.txt":
cat file2.txt
Output (inside the container):
I am on C2
This command displays the contents of the "file2.txt" file, which should contain the text "I am on C2".
These commands and their outputs explain the process of creating and managing Docker volumes, mounting volumes in containers, manipulating files within containers, and inspecting the volume's data on the host system.
bind volumes , change default, Docker stores volumes on the host system in the /var/lib/docker/volumes directory
1. Run a container named "C3" with an interactive session and mount the "vol2" volume to "/newdir":
Note:- if the volumeis not created with docker create Volume , it will create automatically
docker run --name C3 -it -v vol2:/newdir ubuntu
Output (inside the container):
root@containerid:/#
This command starts a new container named "C3" using the "ubuntu" image, opens an interactive session inside the container, and mounts the "vol2" volume to the `/newdir` directory within the container.
2. Display disk space usage:
df -h
Output (example):
Filesystem Size Used Avail Use% Mounted on
overlay 64G 5.5G 59G 9% /
tmpfs 64M 0 64M 0% /dev
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
shm 64M 0 64M 0% /dev/shm
/dev/xvda1 64G 5.5G 59G 9% /newdir
tmpfs 1.9G 0 1.9G 0% /proc/asound
tmpfs 1.9G 0 1.9G 0% /proc/acpi
tmpfs 1.9G 0 1.9G 0% /proc/scsi
tmpfs 1.9G 0 1.9G 0% /sys/firmware
This command displays disk space usage within the container. It shows the filesystems and their sizes, used space, available space, and mount points.
3. Change directory to `/newdir` inside the container:
cd /newdir
Output (inside the container):
root@containerid:/newdir#
This command changes the current directory to `/newdir` inside the container.
4. Exit the container:
exit
Output (no output indicates successful execution):
This command exits the interactive session within the container and returns to the host system's terminal.
5. List Docker volumes:
docker volume ls
Output (example):
DRIVER VOLUME NAME
local vol2
This command lists the Docker volumes available on your system. The "vol2" volume is listed.
6. Run a container named "C4" with an interactive session and mount the `/tmp/logs` directory to `/newdir1`:
Note: By default, Docker stores volumes on the host system in the /var/lib/docker/volumes
directory. we can change that with other source location Example /tmp/logs
docker run --name C4 -it -v /tmp/logs:/newdir1 ubuntu
Output (inside the container):
root@containerid:/#
This command starts a new container named "C4" using the "ubuntu" image, opens an interactive session inside the container, and mounts the `/tmp/logs` directory on the host system to the `/newdir1` directory within the container.
7. Append text to "file1.txt" inside the container:
echo "I am on container C4" >> /newdir1/file1.txt
Output (no output indicates successful execution):
This command appends the text "I am on container C4" to the "file1.txt" file inside the container.
8. Exit the container:
exit
Output (no output indicates successful execution):
This command exits the interactive session within the container and returns to the host system's terminal.
9. List Docker volumes:
docker volume ls
Output (example):
DRIVER VOLUME NAME
local vol2
This command lists the Docker volumes available on your system. The "vol2" volume is still listed.
10. Inspect the "C4" container and display the mounts:
docker inspect C4 | grep -C 5 -i mounts
Output (example):
"Mounts": [
{
"Type": "volume",
"Name": "vol2",
"Source": "/tmp/logs",
"Destination": "/newdir",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
},
{
"Type": "bind",
"Source": "/tmp/logs",
"Destination": "/newdir1",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
]
This command inspects the "C4" container and uses `grep` to search for the keyword "mounts" (case-insensitive) in the output. The `-C 5` option displays 5 lines of context before and after each matched line.
The output shows the mounts of the "C4" container, indicating the "vol2" volume and the bind mount from the host `/tmp/logs` directory to `/newdir1` within the container.
11. Change directory to `/tmp/logs` on the host system:
cd /tmp/logs/
Output (no output indicates successful execution):
This command
changes the current directory to `/tmp/logs` on the host system.
12. Display the contents of "file1.txt":
cat file1.txt
Output:
I am on container C4
This command displays the contents of the "file1.txt" file located in the `/tmp/logs` directory on the host system. It shows the appended text "I am on container C4".
These commands and their outputs explain the process of running containers with mounted volumes, manipulating files within containers, inspecting mounts, and accessing files in bind-mounted directories on the host system.
copy Data from container to docker Host , and copy data from host to container
docker cp , docker start, docker attach
1. Run a container named "c5" interactively with the Ubuntu image:
docker run --name c5 -it ubuntu
Output (inside the container):
root@c5:/#
This command starts a new container named "c5" using the "ubuntu" image and opens an interactive session inside the container.
2. Append text to "file3.txt" inside the container:
echo "I am on C5" >> file3.txt
Output (no output indicates successful execution):
This command appends the text "I am on C5" to the "file3.txt" file inside the container.
3. Exit the container:
exit
Output (no output indicates successful execution):
This command exits the interactive session within the container and returns to the host system's terminal.
4. Copy "file3.txt" from the container "c5" to the "/tmp" directory on the host:
docker cp c5:/file3.txt /tmp
Output (no output indicates successful execution):
This command copies the "file3.txt" from the "c5" container to the "/tmp" directory on the host system.
5. List the files in the "/tmp" directory on the host:
ls -l /tmp
Output (example):
total 4
-rw-r--r-- 1 root root 10 Jul 18 13:45 file3.txt
This command lists the files in the "/tmp" directory on the host system, and it shows that "file3.txt" has been successfully copied from the container.
6. View the contents of "file3.txt" from the host:
cat /tmp/file3.txt
Output (example):
I am on C5
This command displays the contents of "file3.txt" from the "/tmp" directory on the host, which should contain the text "I am on C5".
7. Append text "I am from Docker Host" to "file4.txt" on the host:
echo "I am from Docker Host" >> /tmp/file4.txt
Output (no output indicates successful execution):
This command appends the text "I am from Docker Host" to the "file4.txt" on the host system in the "/tmp" directory.
8. Copy "file4.txt" from the host to the "c5" container:
docker cp /tmp/file4.txt c5:/
Output (no output indicates successful execution):
This command copies "file4.txt" from the "/tmp" directory on the host to the root directory ("/") inside the "c5" container.
9. Start the "c5" container:
docker start c5
Output (no output indicates successful execution):
This command starts the "c5" container.
10. Attach to the "c5" container:
docker attach c5
Output (inside the container):
root@c5:/#
This command attaches an interactive session to the "c5" container.
11. List the files in the container to check if "file4.txt" is present:
ls -l file4.txt
Output (inside the container):
-rw-r--r-- 1 root root 23 Jul 18 13:45 file4.txt
This command lists the "file4.txt" file in the root directory ("/") inside the container.
12. View the contents of "file4.txt" inside the container:
cat file4.txt
Output (inside the container):
I am from Docker Host
This command displays the contents of "file4.txt" inside the container, which should contain the text "I am from Docker Host".
The commands and their outputs demonstrate the process of copying files between the host and a Docker container using the "docker cp" command, as well as performing basic file operations inside the container.