Basic commands: Diving into Docker-Part 4
Continuing from https://medium.com/@adari.girishkumar/installing-docker-diving-into-docker-part-3-1c3b239a59d5. In this part, I will explain what docker images and containers are and some important commands to start working with docker
What are the docker images?
If you’ve ever used virtual machines aside from Docker, you’ll be familiar with docker images. In other virtual machine environments, images would be called something like “snapshots.” They’re a picture of a Docker virtual machine at a specific point in time. Docker images are a little bit different from a virtual machine snapshot, though. For starters, Docker images can’t ever change. Once you’ve made one, you can delete it, but you can’t modify it. If you need a new version of the snapshot, you create an entirely new image.
This inability to change (called “immutability”) is a powerful tool for Docker images. An image can never change. So, if you get your Docker virtual machine into a working state and create an image, you know that image will always work, forever. This makes it easy to try out additions to your environment. You might experiment with new software packages, or try to reorganize your project files. When you do this, you can be sure that you won’t break your working instance, because you can’t. You will always be able to shut down your Docker virtual machine and restart it using your existing image, and it’ll be like nothing ever changed.
What are the docker containers?
If a Docker image is a digital photograph, a Docker container is like a printout of that photograph. In technical terms, we call it an “instance” of the image. Each Docker container runs separately, and you can modify the container while it’s running. Modifications to a Docker container aren’t saved unless you create another image, as we noted. Most Docker images include full operating systems to allow you to do whatever you need on them. This makes it easy to start up a program — as a command line — on the running container. Inside that command line, you can do some work like installing a new software package or configuring the system’s security. Then you can save another image and upload it to somewhere like Docker Hub to share it with people who can make use of your work.
Docker Commands
1. Finding the version
docker -v
docker --version
2. Downloading Images
When we need to pull the docker image from dockerhub (docker repository).
docker pull image_name
3. Images
List all the docker images pulled on the system with image details such as TAG/IMAGE ID/SIZE etc.
docker images
4. Run
Run the docker image mentioned in the command. This command will create a docker container.
docker run -dit image_name
5. What’s running?
To see all the containers that are running now
docker ps
to see all the docker container which are still running and exited also
docker ps -a
6. exec
Access the docker container and run commands inside the container. I am accessing the apache server container in this example. (We can just type part of container id)
docker exec -it container_id bash
Type exit and press enter to come out of the container.
7. Removing Container
Remove the docker container with container id mentioned in the command.
docker rm container_id
8. Removing Image
Remove the docker image with the docker image id mentioned in the command
docker rmi image_id
To remove the image, we should remove the container(either running/stopped).
9. Stopping Docker container
Stop a container with container id mentioned in the command
docker stop container_id/name
10. Restart Docker container
Restart the docker container with container id mentioned in the command.
docker restart container_id/name
11. Commit
Save a new docker image with container id mentioned in the command on the local system. In the example below, geekflare is the username, and httpd_image is the image name.
new image name should be in the format of {{username}}/{{imagename}}:{{version}}
docker commit container_id new_image_name
12. Login to DockerHub
Login into the docker hub. You will be asked your docker hub credentials to log in.
docker login
13. Push to DockerHub
Upload a docker image with the image name mentioned in the command on the dockerhub.
docker push new_image_name
14. Docker Network
The following command in docker lists the details of all the network in the cluster.
docker network ls
There are several other docker network commands.
15. Docker Info
Get detailed information about docker installed on the system including the kernel version, number of containers and images, etc.
root@girishVBOX:/home/girish# docker info
Client:
Debug Mode: falseServer:
Containers: 2
Running: 0
Paused: 0
Stopped: 2
Images: 3
Server Version: 19.03.6
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version:
runc version:
init version:
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 4.15.0-72-generic
Operating System: Ubuntu 18.04.2 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 3.853GiB
Name: girishVBOX
ID: X6VR:KGPM:2PER:KU5E:SZHX:ETAJ:JKB4:6NNB:X52K:HUNT:MCS6:MGRP
Docker Root Dir: /var/lib/docker
Debug Mode: false
Username: adarigirishkumar
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: falseWARNING: No swap limit support
16. Checking History
Shows the history of a docker image with the image name mentioned in the command.
docker history image_name
17. Search
Search for a docker image on Dockerhub with the name mentioned in the command.
docker search image_name
18. Creating Volume
Create a volume which docker container will use to store data.
docker volume create
docker volume ls
19. Updating configuration
Update container configurations. This shows all the update options.
docker update --helpUsage: docker update [OPTIONS] CONTAINER [CONTAINER...]Update configuration of one or more containersOptions:
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit the CPU real-time period in microseconds
--cpu-rt-runtime int Limit the CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
--kernel-memory bytes Kernel memory limit
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--pids-limit int Tune container pids limit (set -1 for unlimited)
--restart string Restart policy to apply when a container exits
Run the below command to update the CPU configuration of docker container with container id mentioned in the command.
docker update -c 1 d4b
20. Info of container
For getting all the details of a container
docker inspect container_name/idroot@girishVBOX:/home/girish# docker inspect d4b
[
{
"Id": "d4b900c798aa6b13d7c9fd06d8d847d8b359cefd5d5a9183111416a4acde66a5",
"Created": "2020-05-04T09:37:11.022530033Z",
"Path": "httpd-foreground",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt": "2020-05-04T10:08:13.430152929Z",
"FinishedAt": "2020-05-04T10:25:39.079090417Z"
},
"Image": "sha256:b2c2ab6dcf2e526597d0a5fc506f123088e6572a8a656f04cea86d4f559c66e9",
"ResolvConfPath": "/var/lib/docker/containers/d4b900c798aa6b13d7c9fd06d8d847d8b359cefd5d5a9183111416a4acde66a5/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/d4b900c798aa6b13d7c9fd06d8d847d8b359cefd5d5a9183111416a4acde66a5/hostname",
"HostsPath": "/var/lib/docker/containers/d4b900c798aa6b13d7c9fd06d8d847d8b359cefd5d5a9183111416a4acde66a5/hosts",
"LogPath": "/var/lib/docker/containers/d4b900c798aa6b13d7c9fd06d8d847d8b359cefd5d5a9183111416a4acde66a5/d4b900c798aa6b13d7c9fd06d8d847d8b359cefd5d5a9183111416a4acde66a5-json.log",
"Name": "/interesting_antonelli",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "docker-default",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Capabilities": null,
"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",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 1,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"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/10338c8e66236152666754bae9e4de6362cbfe690331509d2e87101741994d03-init/diff:/var/lib/docker/overlay2/d9c668c2e2bcaba8b82dc99c0aeef6963dfd5a6f9f2a62ecdc8e789f1aefc241/diff:/var/lib/docker/overlay2/eaa3ac511418280bb4b4a6c51c4034d2f852bec9388da967abba953c5b4607af/diff:/var/lib/docker/overlay2/3456121f116d005269746e968c2204b31b2251d36b159bf0de175452e871693f/diff:/var/lib/docker/overlay2/2a754c8fd02bb0d633bc3f65189ec9736690e0b62c021adc08775b543431500c/diff:/var/lib/docker/overlay2/60951e1bb8b54bd333b3d52f644e76a4a57d43a8ecf36feb86ba57a122d0a5cf/diff",
"MergedDir": "/var/lib/docker/overlay2/10338c8e66236152666754bae9e4de6362cbfe690331509d2e87101741994d03/merged",
"UpperDir": "/var/lib/docker/overlay2/10338c8e66236152666754bae9e4de6362cbfe690331509d2e87101741994d03/diff",
"WorkDir": "/var/lib/docker/overlay2/10338c8e66236152666754bae9e4de6362cbfe690331509d2e87101741994d03/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "d4b900c798aa",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"80/tcp": {}
},
"Tty": true,
"OpenStdin": true,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/apache2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"HTTPD_PREFIX=/usr/local/apache2",
"HTTPD_VERSION=2.4.43",
"HTTPD_SHA256=a497652ab3fc81318cdc2a203090a999150d86461acff97c1065dc910fe10f43",
"HTTPD_PATCHES="
],
"Cmd": [
"httpd-foreground"
],
"Image": "httpd",
"Volumes": null,
"WorkingDir": "/usr/local/apache2",
"Entrypoint": null,
"OnBuild": null,
"Labels": {},
"StopSignal": "SIGWINCH"
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "dd175ad22c232f37629b40d2007b63c4cb27ac99be0613e8238d6950ffabf7a9",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/dd175ad22c23",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "c37740ece5c9b9351aff8755a445b5731eed58f2647d64c33604a8879922a2c4",
"EndpointID": "",
"Gateway": "",
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "",
"DriverOpts": null
}
}
}
}
]
21. Logging out from Dockerhub
docker logout
Dockerfiles and best practices to write a Dockerfile: https://medium.com/@adari.girishkumar/dockerfile-and-best-practices-for-writing-dockerfile-diving-into-docker-part-5-5154d81edca4