top of page

Docker Lab Exercise 

Exercise

These steps should give you an understanding of checking
if Docker is installed,
installing it if necessary,
starting the Docker service,
pulling and running the "Hello World" image,
and checking the images and container status on your CentOS instance


1. To check if Docker is installed, run the command:


```bash

docker --version

```


Output (if Docker is not installed):

```

bash: docker: command not found

```


2. Install Docker using `yum`:


```bash

sudo yum update

sudo yum install -y docker

```


Output:

```

[...]

Complete!

```


3. Start the Docker service:


```bash

sudo systemctl start docker

```


Output (no output indicates successful execution):


4. Enable Docker to start on system boot:


```bash

sudo systemctl enable docker

```


Output (no output indicates successful execution):


5. Check the Docker version again:


```bash

docker --version

```


Output (example):

```

Docker version 20.10.7, build f0df350

```


6. List the Docker images available:


```bash

sudo docker images

```


Output (if Docker is just installed and no images have been pulled yet):

```

REPOSITORY   TAG       IMAGE ID       CREATED      SIZE

```


7. Run the "Hello World" Docker image:


```bash

sudo docker run hello-world

```


Output (example):

```

Hello from Docker!

This message shows that your installation appears to be working correctly.

[...]

```


8. List the Docker images again:


```bash

sudo docker images

```


Output (after running the "Hello World" image):

```

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE

hello-world  latest    fce289e99eb9   7 months ago   13.3kB

```


9. List the running containers:


```bash

sudo docker ps

```


Output (if no containers are running):

```

CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

```


10. List all containers (including stopped containers):


```bash

sudo docker ps -a

```


Output (after running the "Hello World" image):

```

CONTAINER ID   IMAGE         COMMAND       CREATED          STATUS                      PORTS     NAMES

eef4b5f60df9   hello-world   "/hello"      2 minutes ago    Exited (0) 2 minutes ago              hungry_wescoff

```


11. Run the "Hello World" image again:


```bash

sudo docker run hello-world

```


Output (example):

```

Hello from Docker!

This message shows that your installation appears to be working correctly.

[...]

```


docker run --name , docker serach, docker pull, docker tag, docker login, docker push



1. Run the "Hello World" Docker image with the container name `C1`:


```bash

sudo docker run --name C1 hello-world

```


Output:

```

Hello from Docker!

This message shows that your installation appears to be working correctly.

[...]

```


This command creates and runs a container named `C1` from the "Hello World" image.


2. List the Docker images available:


```bash

docker images

```


Output (example):

```

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE

hello-world  latest    fce289e99eb9   7 months ago   13.3kB

```


This command lists the Docker images currently available on your system.


3. Search for the Ubuntu image on Docker Hub:


```bash

sudo docker search ubuntu

```


Output (example):

```

NAME              DESCRIPTION             STARS    OFFICIAL   AUTOMATED

ubuntu            Ubuntu is a Debian-b...  13127    [OK]

[...]

```


This command searches for the Ubuntu image on Docker Hub and displays a list of related images.


4. Pull the Ubuntu image from Docker Hub:


```bash

docker pull ubuntu

```


Output (example):

```

Using default tag: latest

latest: Pulling from library/ubuntu

[...]

```


This command downloads the latest version of the Ubuntu image from Docker Hub to your local system.


5. Difference between `pull` and `run` commands:

   - `docker pull` is used to download Docker images from a registry (such as Docker Hub) to your local system. It retrieves the image and stores it locally, making it available for future use.

   - `docker run` is used to create and run a container from a Docker image. It pulls the image if it is not already available locally and starts a new container based on that image.


6. Pull a specific version (tag) of a custom image:


```bash

docker pull devopstrainer/myimage:4

```


Output (example):

```

Using default tag: latest

latest: Pulling from devopstrainer/myimage

[...]

```


This command pulls the version `4` of a custom image named `devopstrainer/myimage` from Docker Hub.


7. List the Docker images available:


```bash

docker images

```


Output (example):

```

REPOSITORY                 TAG       IMAGE ID       CREATED        SIZE

hello-world                latest    fce289e99eb9   7 months ago   13.3kB

ubuntu                     latest    1318b700e415   7 months ago   72.7MB

devopstrainer/myimage      4         b39ea256c708   12 months ago  450MB

```


This command lists the Docker images available on your system, including the ones you pulled.


8. Tag an existing image with a new name and version:


```bash

sudo docker tag devopstrainer/myimage:4 devopstrainer/myimage:5

```


Output (no output indicates successful execution):


This command creates a new tag (`5`) for the existing image `devopstrainer/myimage:4`.


9. Push the tagged image to a Docker registry:


```bash

sudo docker push devopstrainer/myimage:5

```


Output (example):

```

The push refers to repository [docker.io/devopstrainer/myimage]

[...]

```


This command pushes the tagged image `devopstrainer/myimage:5` to a Docker registry, such as Docker Hub.


10. Login to a Docker registry:


```bash

docker login <username>

```


Replace `<username>` with your Docker Hub username or the appropriate registry. You will be prompted to enter your password.


11. View the Docker configuration file:


```bash

sudo cat /root/.docker/config.json

```


Output (example):

```

{

  "auths": {

    "https://index.docker.io/v1/": {

      "auth": "<base64-encoded-authentication-details>"

    }

  }

}

```


This command displays the contents of the Docker configuration file that stores the authentication details for Docker registries.


12. Push the tagged image to a Docker registry after logging in:


```bash

sudo docker push devopstrainer/myimage:5

```


Output (example):

```

The push refers to repository [docker.io/devopstrainer/myimage]

[...]

```


This command pushes the tagged image `devopstrainer/myimage:5` to a Docker registry after logging in.


docker run -itd, docker stop, docker run -it, docker start, docker attach, docker stats

1. Run an Ubuntu container in detached mode:


```bash

docker run -itd ubuntu

```


Output (example):

```

c6c6a3fba661f35ad4d4a5080619c028a66edc0a648b6c1821695e9fe98dbb32

```


This command creates and starts a new Ubuntu container in detached mode. The container ID is displayed as the output.


2. List the running containers:


```bash

docker ps

```


Output (example):

```

CONTAINER ID   IMAGE    COMMAND   CREATED         STATUS         PORTS   NAMES

c6c6a3fba661   ubuntu   "/bin/bash"   2 minutes ago   Up 2 minutes          zen_archimedes

```


This command lists the currently running containers, including the newly created Ubuntu container.


3. Stop a running container:


```bash

docker stop zen_archimedes

```


Output (no output indicates successful execution):


This command stops the container with the name `zen_archimedes`.


4. List the running containers again:


```bash

docker ps

```


Output (example):

```

CONTAINER ID   IMAGE    COMMAND   CREATED      STATUS        PORTS   NAMES

```


This command displays an empty list since the previously running container has been stopped.


5. List all containers (including stopped containers):


```bash

docker ps -a

```


Output (example):

```

CONTAINER ID   IMAGE    COMMAND   CREATED         STATUS                      PORTS   NAMES

c6c6a3fba661   ubuntu   "/bin/bash"   10 minutes ago   Exited (0) 2 minutes ago          zen_archimedes

```


This command lists all containers, including the stopped container `zen_archimedes`.


6. Difference between `run` and `start` commands:

   - `docker run` creates and starts a new container from an image. It is used to create and start a container in one step.

   - `docker start` starts one or more stopped containers. It is used to restart a container that was previously created.


7. Run an interactive Ubuntu container:


```bash

docker run -it ubuntu

```


Output:

```

root@39a88861c09e:/#

```


This command starts a new interactive session inside an Ubuntu container, allowing you to execute commands within the container.


8. Check for the `yum` package manager inside the container:


```bash

yum

```


Output (example):

```

bash: yum: command not found

```


The `yum` package manager is not available in Ubuntu, so it returns a "command not found" error.


9. Check for the `apt-get` package manager inside the container:


```bash

apt-get

```


Output (example):

```

bash: apt-get: command not found

```


The `apt-get` package manager is available in Ubuntu and returns a "command not found" error only if the package is not installed.


10. Exit the interactive session:


```bash

exit

```


Output (no output indicates successful execution):


This command exits the interactive session inside the container and returns you to the host machine's terminal.


11. List all containers (including stopped containers):


```bash

docker ps -a

```


Output (example):

```

CONTAINER ID   IMAGE    COMMAND   CREATED        STATUS                      PORTS   NAMES

39a88861c09e   ubuntu   "/bin/bash"   2 minutes ago   Exited (0) 20 seconds ago          thirsty_davinci

```


This command displays the stopped container `thirsty_davinci`.


12. Run an interactive Ubuntu container again:


```bash

docker run -it ubuntu

```


Output:

```

root@ca7987d7c124:/#

```


This command starts a new interactive session inside another Ubuntu container.


13. Detach from the interactive session using keyboard shortcuts:


Press `Ctrl+p` followed by `Ctrl+q`.


Output (no output indicates successful execution):


This command detaches the interactive session without stopping the container.


14. List the running containers:


```bash

docker ps

```


Output (example):

```

CONTAINER ID   IMAGE    COMMAND   CREATED         STATUS         PORTS   NAMES

ca7987d7c124   ubuntu   "/bin/bash"   2 minutes ago   Up 2 minutes          musing_curie

```


This command shows the running container `musing_curie`.


15. Stop a running container:


```bash

docker stop musing_curie

```


Output (no output indicates successful execution):


This command stops the container `musing_curie`.


16. List all containers (including stopped containers):


```bash

docker ps -a

```


Output (example):

```

CONTAINER ID   IMAGE    COMMAND   CREATED         STATUS                      PORTS   NAMES

ca7987d7c124   ubuntu   "/bin/bash"   6 minutes ago   Exited (0) 3 seconds ago           musing_curie

```


This command displays the stopped container `musing_curie`.


17. Start a stopped container:


```bash

docker start musing_curie

```


Output (no output indicates successful execution):


This command starts the stopped container `musing_curie`.


18. List the running containers:


```bash

docker ps

```


Output (example):

```

CONTAINER ID   IMAGE    COMMAND   CREATED          STATUS         PORTS   NAMES

ca7987d7c124   ubuntu   "/bin/bash"   6 minutes ago    Up 3 seconds          musing_curie

```


This command shows the running container `musing_curie`.


19. Attach to a running container:


```bash

docker attach musing_curie

```


Output:

```

root@ca7987d7c124:/#

```


This command attaches to the running container `musing_curie` and provides an interactive session inside the container.


20. Difference between `docker attach` and `docker run -it`:

   - `docker attach` attaches the host's standard input, output, and error streams to a running container, allowing you to interact with the container's terminal.

   - `docker run -it` starts a new container and provides an interactive session with a terminal.


21. List the running containers:


```bash

docker ps

```


Output (example):

```

CONTAINER ID   IMAGE    COMMAND   CREATED         STATUS          PORTS   NAMES

ca7987d7c124   ubuntu   "/bin/bash"   10 minutes ago   Up 2 minutes           musing_curie

```


This command shows the running container `musing_curie`.


22. List all containers (including stopped containers):


```bash

docker ps -a

```


Output (example):

```

CONTAINER ID   IMAGE    COMMAND   CREATED         STATUS                      PORTS   NAMES

ca7987d7c124   ubuntu   "/bin/bash"   10 minutes ago   Exited (0) 1 second ago           musing_curie

```


This command displays the stopped container `musing_curie`.


23. Detach from the interactive session using keyboard shortcuts:


Press `Ctrl+p` followed by `Ctrl+q`.


Output (no output indicates successful execution


):


This command detaches the interactive session without stopping the container.


24. Display resource usage statistics of running containers:


```bash

docker stats

```


Output (example):

```

CONTAINER ID   NAME            CPU %     MEM USAGE / LIMIT     MEM %     NET I/O       BLOCK I/O     PIDS

ca7987d7c124   musing_curie    0.00%     3.898MiB / 7.632GiB   0.05%     648B / 648B   0B / 0B       1

```


This command displays real-time statistics for the running container(s), including CPU usage, memory usage, network I/O, block I/O, and process ID statistics.


25. Suspend the current Docker command:


Press `Ctrl+z`.


Output (example):

```

[1]+  Stopped                 docker stats

```


This command suspends the current Docker command and returns you to the host machine's terminal.


26. List the running containers:


```bash

docker ps

```


Output (example):

```

CONTAINER ID   IMAGE    COMMAND   CREATED        STATUS                      PORTS   NAMES

ca7987d7c124   ubuntu   "/bin/bash"   13 minutes ago   Up 4 minutes                       musing_curie

```


This command shows the running container `musing_curie`.

docker rm, docker rm -f, docker rmi

1. List the running containers:


```bash

docker ps

```


Output (example):

```

CONTAINER ID   IMAGE    COMMAND   CREATED        STATUS         PORTS   NAMES

ca7987d7c124   ubuntu   "/bin/bash"   10 minutes ago   Up 4 minutes          musing_curie

```


This command lists the currently running containers.


2. Remove a container by its name:


```bash

docker rm musing_curie

```


Output (example):

```

musing_curie

```


This command removes the container with the name `musing_curie`.


3. Forcefully remove a running container:


```bash

docker rm -f musing_curie

```


Output (example):

```

musing_curie

```


This command forcefully removes the running container with the name `musing_curie`.


4. List the running containers again:


```bash

docker ps

```


Output (example):

```

CONTAINER ID   IMAGE    COMMAND   CREATED         STATUS         PORTS   NAMES

```


This command shows an empty list since the previously running container has been removed.


5. List the Docker images available:


```bash

docker images

```


Output (example):

```

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE

ubuntu       latest    1318b700e415   7 months ago   72.7MB

hello-world  latest    fce289e99eb9   7 months ago   13.3kB

```


This command lists the Docker images available on your system.


6. Remove an image:


```bash

docker rmi ubuntu

```


Output (example):

```

Untagged: ubuntu:latest

Deleted: sha256:1318b700e4154643f20de8ab4747b329ac0b54e87dcb24d62cb82e8b42a67114

Deleted: sha256:631ceee03dceb948f93a9b3316a313ee55e1d7cf30835a3d496f34b6aa6c7f65

```


This command removes the Ubuntu image.


7. Remove an image:


```bash

docker rmi hello-world

```


Output (example):

```

Untagged: hello-world:latest

Deleted: sha256:fce289e99eb9a84ed5af6b23b1a50fe16f32b3cbed6487b9daf18e8bcaf67d2f

```


This command removes the "Hello World" image.


8. List the Docker images again:


```bash

docker images

```


Output (example):

```

REPOSITORY   TAG       IMAGE ID       CREATED        SIZE

```


This command shows an empty list since both the Ubuntu and "Hello World" images have been removed.


9. List all containers (including stopped containers):


```bash

docker ps -a

```


Output (example):

```

CONTAINER ID   IMAGE    COMMAND   CREATED         STATUS                      PORTS   NAMES

ca7987d7c124   ubuntu   "/bin/bash"   20 minutes ago   Exited (0) 10 minutes ago          musing_curie

```


This command lists all containers, including the previously removed container `musing_curie`.



To create an Amazon Elastic Container Registry (ECR) repository named "repo1" using the AWS Management Console, follow these steps:

1. Open the AWS Management Console in your web browser and log in to your AWS account.

2. Navigate to the Amazon ECR service by searching for "ECR" in the AWS services search bar and selecting "Amazon Elastic Container Registry."

3. In the ECR console, click on the "Repositories" tab on the left-hand side.

4. Click on the "Create repository" button.

5. In the "Create repository" dialog box, provide the following details:

   - Repository name: Enter "repo1" as the name for your repository.

   - Tag immutability: Choose whether you want the tags on the images to be mutable or immutable.

   - Image scan setting: Specify whether you want images to be scanned for vulnerabilities.

   - Encryption: Choose whether you want the repository to be encrypted at rest using Amazon ECR managed keys or a custom key.

   - Lifecycle policy: If needed, configure a lifecycle policy to manage image lifecycle.

6. After providing the necessary information, click on the "Create repository" button.

7. The ECR repository named "repo1" will be created, and you will be redirected to the repository details page.

8. From the repository details page, you can find the repository URI, ARN, and other information related to the repository.

9. You can now use this repository to push and pull Docker images using the provided repository URI.


By following these steps in the AWS Management Console, you can create an ECR repository named "repo1" and manage your container images within it.

docker tag, docker configure, docker login --username AWS --password-stdin, docker push

2. Tag an existing Docker image with the ECR repository URI:



docker tag ubuntu 312709392218.dkr.ecr.eu-north-1.amazonaws.com/repo1:latest



Output (no output indicates successful execution):


This command tags the locally available "ubuntu" image with the ECR repository URI and the `latest` tag.


3. List the Docker images available:


docker images


Output (example):

```

REPOSITORY                                                  TAG                 IMAGE ID            CREATED             SIZE

ubuntu                                                      latest              1318b700e415        7 months ago        72.7MB

312709392218.dkr.ecr.eu-north-1.amazonaws.com/repo1         latest              1318b700e415        7 months ago        72.7MB

```


This command lists the Docker images available on your system, including the tagged image with the ECR repository URI.


4. Push the tagged image to the ECR repository:


docker push 312709392218.dkr.ecr.eu-north-1.amazonaws.com/repo1:latest


Output (example):


The push refers to repository [312709392218.dkr.ecr.eu-north-1.amazonaws.com/repo1]

[...]



This command pushes the locally tagged image to the specified ECR repository.


5. Check the AWS CLI version:


aws --version


Output (example):

```

aws-cli/2.2.3 Python/3.9.7 Linux/5.4.0-88-generic exe/x86_64.ubuntu.20 prompt/off

```


This command displays the version of the AWS CLI installed on your system.


6. Configure AWS CLI with your Access Key ID and Secret Access Key:


aws configure


Output (no output indicates successful execution):


This command prompts you to enter your Access Key ID, Secret Access Key, default region, and output format. Follow the prompts and provide the appropriate values.


7. Retrieve the login password for ECR and authenticate Docker:


aws ecr get-login-password --region eu-north-1 | docker login --username AWS --password-stdin 312709392218.dkr.ecr.eu-north-1.amazonaws.com


Output (no output indicates successful execution):


This command retrieves the login password for the specified ECR repository in the specified region and uses it to authenticate Docker with the repository.


8. View the Docker configuration file:


cat /root/.docker/config.json


Output (example):

```json

{

    "auths": {

        "https://312709392218.dkr.ecr.eu-north-1.amazonaws.com": {

            "username": "AWS",

            "password": "<base64-encoded-password>",

            "email": "null"

        }

    },

    "HttpHeaders": {

        "User-Agent": "Docker-Client/20.10.7 (linux)"

    }

}

```


This command displays the contents of the Docker configuration file that stores the authentication details for the ECR repository.


9. List the Docker images available:


docker images


Output (example):

```

REPOSITORY                                                  TAG                 IMAGE ID            CREATED             SIZE

ubuntu                                                      latest              1318b700e415        7 months ago        72.7MB

312709392218.dkr.ecr.eu-north-1.amazonaws.com/repo1         latest              1318b700e415        7 months ago        72.7MB

```


This command lists the Docker images available on your system, including the image with the ECR repository URI.


10. Push the tagged image to the ECR repository again:


docker push 312709392218.dkr.ecr.eu-north-1.amazonaws.com/repo1:latest



Output (example):

```

The push refers to repository [312709392218.dkr.ecr.eu-north-1.amazonaws.com/repo1]

[...]

```


This command pushes the locally tagged image to the specified ECR repository.


These commands and their outputs explain the process of creating an ECR repository on AWS using the AWS Management Console, tagging and pushing Docker images to the repository, configuring AWS CLI, retrieving login passwords, and interacting with ECR using Docker commands.

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.

exercise - 1
ex-2
ex-3
ex-4
ex-5
ex-6
ex-7
bottom of page