Docker Lab Exercises
Day-2
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.