Docker for DevOps Engineers
Table of contents
- Docker
- Task 1:Use the docker run command to start a new container and interact with it through the command line. [Hint: docker run hello-world]
- Task 2:Use the docker inspect command to view detailed information about a container or image.
- Task 3:Use the docker port command to list the port mappings for a container.
- Task 4:Use the docker stats command to view resource usage statistics for one or more containers.
- Task 5:Use the docker top command to view the processes running inside a container.
- Task 6:Use the docker save command to save an image to a tar archive.
- Task 7:Use the docker load command to load an image from a tar archive.
Docker
Docker is a platform for building, shipping, and running software applications using containerization technology. It allows developers to package their applications and dependencies into a portable container that can be easily deployed and run on any platform that supports Docker, including cloud servers, laptops, and even IoT devices.
Docker containers provide a lightweight, efficient, and isolated environment for running applications, enabling developers to easily build, test, and deploy their software. Docker containers can be easily shared and reused, allowing developers to collaborate and deploy their applications faster and more reliably.
Overall, Docker simplifies the process of building and deploying applications, making it an essential tool for modern software development and deployment.
Task 1:Use the docker run
command to start a new container and interact with it through the command line. [Hint: docker run hello-world]
🐳Open a terminal window and ensure that Docker is installed and running.
🐳Type the following command:docker run
[pavan@localhost ~]$ docker --version
Docker version 23.0.2, build 569dd73
[pavan@localhost ~]$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:ffb13da98453e0f04d33a6eee5bb8e46ee50d08ebe17735fc0779d0349e889e9
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
[pavan@localhost ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
499ae670567d hello-world "/hello" 11 minutes ago Exited (0) 11 minutes ago romantic_shannon
c25404aaea20 gcr.io/k8s-minikube/kicbase:v0.0.37 "/usr/local/bin/entr…" 10 days ago Exited (137) 10 days ago minikube
2a0373f1ec6f alpine "/bin/sh" 2 weeks ago Exited (0) 2 weeks ago tender_leakey
🐳Docker will first check if the hello-world
image is available on your system. If it is not available locally, Docker will download it from the Docker Hub, which is the default registry for Docker images.
🐳Once the image is downloaded, Docker will create a new container from that image and start it. The hello-world
container will print a message that confirms that your Docker installation is working correctly.
Task 2:Use the docker inspect
command to view detailed information about a container or image.
🐳Type the following command:docker run <container id>
[pavan@localhost ~]$ docker inspect 499ae670567d
[
{
"Id": "499ae670567de7b648192806ef6ff92552e876ad2fd0136262b32a9ef741e828",
"Created": "2023-04-11T11:47:44.426016493Z",
"Path": "/hello",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-04-11T11:47:46.109524258Z",
"FinishedAt": "2023-04-11T11:47:46.102328587Z"
},
"Image": "sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412",
"ResolvConfPath": "/var/lib/docker/containers/499ae670567de7b648192806ef6ff92552e876ad2fd0136262b32a9ef741e828/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/499ae670567de7b648192806ef6ff92552e876ad2fd0136262b32a9ef741e828/hostname",
"HostsPath": "/var/lib/docker/containers/499ae670567de7b648192806ef6ff92552e876ad2fd0136262b32a9ef741e828/hosts",
"LogPath": "/var/lib/docker/containers/499ae670567de7b648192806ef6ff92552e876ad2fd0136262b32a9ef741e828/499ae670567de7b648192806ef6ff92552e876ad2fd0136262b32a9ef741e828-json.log",
"Name": "/romantic_shannon",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"ConsoleSize": [
24,
80
],
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "private",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": [],
"BlkioDeviceWriteBps": [],
"BlkioDeviceReadIOps": [],
"BlkioDeviceWriteIOps": [],
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": null,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/3f009f538092d7fee2e4face481f2c0841b4cc46c7285df4a9b5cb867da7eb4c-init/diff:/var/lib/docker/overlay2/2ad8ffbf7a67e5d235b0234dfa785be2eed61f55dd72aaca6611a3526601749c/diff",
"MergedDir": "/var/lib/docker/overlay2/3f009f538092d7fee2e4face481f2c0841b4cc46c7285df4a9b5cb867da7eb4c/merged",
"UpperDir": "/var/lib/docker/overlay2/3f009f538092d7fee2e4face481f2c0841b4cc46c7285df4a9b5cb867da7eb4c/diff",
"WorkDir": "/var/lib/docker/overlay2/3f009f538092d7fee2e4face481f2c0841b4cc46c7285df4a9b5cb867da7eb4c/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "499ae670567d",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": true,
"AttachStderr": true,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/hello"
],
"Image": "hello-world",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "02e967e60698a05e9f8df813f5a5caaae8292d9f9cf2c60ffc1bed4d71962855",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/02e967e60698",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "b5e46ea08557e25aa76a1e112a890e7fedd2b0db96dccbcdff852bfbdcaa1a6a",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
🐳Docker will output a large amount of JSON-formatted data that describes the container's configuration and runtime state. This includes information such as the container's network settings, environment variables, mounted volumes, and more.
Task 3:Use the docker port
command to list the port mappings for a container.
🐳Type the following command:docker port <container id>
docker port <container-id>
Note that <container-id>
should be replaced with the actual container ID that you want to list port mappings for.
🐳Docker will output a list of port mappings for the container, including the container's internal port number and the corresponding external port number that is mapped to it. This can be useful for determining how to access a container's services from outside the container.
Task 4:Use the docker stats
command to view resource usage statistics for one or more containers.
Sure, here's how to use the docker stats
command to view resource usage statistics for one or more containers:
🐳To view resource usage statistics for a single container, find the container ID by typing the command docker ps -a
. Choose the container ID that you want to inspect.To view resource usage statistics for the container, type the following command:
docker stats <container-id>
Note that <container-id>
should be replaced with the actual container ID that you want to inspect.
🐳Docker will continuously output resource usage statistics for the container, including CPU usage, memory usage, network I/O, and more. This can be useful for monitoring the performance of a container and identifying potential performance bottlenecks.To view resource usage statistics for multiple containers at once, simply list the container IDs separated by spaces:
docker stats <container-id-1> <container-id-2> <container-id-3> ...
Task 5:Use the docker top
command to view the processes running inside a container.
🐳Find the container ID for the container you want to view processes for by typing the command docker ps -a
.To view the processes running inside the container, type the following command:
docker top <container-id>
Note that <container-id>
should be replaced with the actual container ID that you want to view processes for.
🐳Docker will output a table showing the processes running inside the container, including the process ID (PID), user, CPU usage, memory usage, and more.You can use the docker top
command to identify any processes that are consuming excessive resources or causing issues inside the container. You can also use it to monitor the health and performance of the container's processes over time.
Task 6:Use the docker save
command to save an image to a tar archive.
🐳Find the image ID for the image you want to save by typing the command docker images
.
🐳To save the image to a tar archive, type the following command:
docker save -o <output-file>.tar <image-id>
Note that <output-file>
should be replaced with the name you want to give to the tar archive, and <image-id>
should be replaced with the actual image ID that you want to save.
🐳Docker will output the tar archive to the file specified in the command, which will contain all the image layers and metadata.You can then use the tar archive to load the image onto another Docker host by using the docker load
command.
Task 7:Use the docker load
command to load an image from a tar archive.
🐳Obtain a tar archive containing the Docker image you want to load.
🐳To load the image from the tar archive, navigate to the directory where the tar archive is located and type the following command:
docker load -i <input-file>.tar
Note that <input-file>
should be replaced with the name of the tar archive file containing the Docker image.
🐳Docker will read the tar archive and load the image onto your local Docker host.You can verify that the image has been loaded by typing the command docker images
, which should display a list of all images on your local Docker host.
#Docker #Devops
Please free to write your thoughts and can connect with me on LinkedIn