Docker Compose contd..
Building image as part of docker-compose
- Refer Here for the docker compose file created for students course register example used in class yesterday
- Now lets add a persistent volume Refer Here
- Refer Here for the changes done in the docker compose file to include volumes
- Execute docker compose up
- Now generate some data and execute docker compose down.
- Bring the containers up by executing docker compose up -d and you should be able to see the data generated as docker compose down will not remove volumes, rather it removes containers and networks.
Docker container restrictions, logging etc
- Refer Here for the blog article.
- Run jenkins container
docker container run --name jenkins -d -P jenkins/jenkins:latest
- Inspect container
$ docker container inspect jenkins
[
{
"Id": "eabdb70aabc449e74ec84846bc77bd0140d1d84c6b9b3c5dfa760558a9126977",
"Created": "2022-11-27T03:19:18.918322685Z",
"Path": "/usr/bin/tini",
"Args": [
"--",
"/usr/local/bin/jenkins.sh"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 5755,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-11-27T03:19:19.313928199Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:fc282a1cac33030915a7b45a1c3ef3fc62f9a74549d9a53c2a5718747d697d6b",
"ResolvConfPath": "/var/lib/docker/containers/eabdb70aabc449e74ec84846bc77bd0140d1d84c6b9b3c5dfa760558a9126977/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/eabdb70aabc449e74ec84846bc77bd0140d1d84c6b9b3c5dfa760558a9126977/hostname",
"HostsPath": "/var/lib/docker/containers/eabdb70aabc449e74ec84846bc77bd0140d1d84c6b9b3c5dfa760558a9126977/hosts",
"LogPath": "/var/lib/docker/containers/eabdb70aabc449e74ec84846bc77bd0140d1d84c6b9b3c5dfa760558a9126977/eabdb70aabc449e74ec84846bc77bd0140d1d84c6b9b3c5dfa760558a9126977-json.log",
"Name": "/jenkins",
"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,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": true,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"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/71854daa56c3e6f862914e3e7dd13b5cd1251486b2e65a872a59be4f005cdf53-init/diff:/var/lib/docker/overlay2/e41539548c28c67d862f9e4081e78f6835810a96ef5677f348722d0bb723d20d/diff:/var/lib/docker/overlay2/25766e8a597c7619d7ab3cb8890b512f0690d0bf6dd6ea2b25a1afce71a01eb7/diff:/var/lib/docker/overlay2/7850ee0d0b58af7b58ae44a457fc8048d0f9c43a8350e0f3b315eb1910c45419/diff:/var/lib/docker/overlay2/dbc0215d3c715ff899ed562c56c9b36e0acf0941e1aae0047f9c512064e42b16/diff:/var/lib/docker/overlay2/5996be5561bc4d2232f16333ce930dc061462ddfb4087a938433fc47a381a2f0/diff:/var/lib/docker/overlay2/d3a5b1318932dd29aa7f5115c61a1cc375a8fd0157ebf214df30888aa28a42bb/diff:/var/lib/docker/overlay2/227e5eb46bf9cdb764e4096a142305f0ffb87cd290b6ed9676f3af6fbdb2c4a7/diff:/var/lib/docker/overlay2/40a0514f7f27620f684376c87748a682107ac6360668706de3640b1a7e2366c8/diff:/var/lib/docker/overlay2/2f0e642c3a01d20715cd87bb22d201354ecc14c69aca2510e9b85289b260b8f1/diff:/var/lib/docker/overlay2/e74b7eaf5e63fa6cf912ae866916c03bc92922b493802bd7532d0cdada3cea52/diff:/var/lib/docker/overlay2/79305f2abb966ed3c829c5291ad49a1b70481f30df9d713274c5a93f6e4bcc47/diff:/var/lib/docker/overlay2/d8485cfc6d5852a5cba727e1c77bda8f57f86e54f7194aa3d84ce1d0f0647d9b/diff:/var/lib/docker/overlay2/8860dcdc833b39493ca8b4db03c5da444114fd5b6f4f4f1603738c32d1c1538e/diff",
"MergedDir": "/var/lib/docker/overlay2/71854daa56c3e6f862914e3e7dd13b5cd1251486b2e65a872a59be4f005cdf53/merged",
"UpperDir": "/var/lib/docker/overlay2/71854daa56c3e6f862914e3e7dd13b5cd1251486b2e65a872a59be4f005cdf53/diff",
"WorkDir": "/var/lib/docker/overlay2/71854daa56c3e6f862914e3e7dd13b5cd1251486b2e65a872a59be4f005cdf53/work"
},
"Name": "overlay2"
},
"Mounts": [
{
"Type": "volume",
"Name": "39ffeae0a0b345dd644018a835ba31849f36259f23a508cf3a2ec0968beb6685",
"Source": "/var/lib/docker/volumes/39ffeae0a0b345dd644018a835ba31849f36259f23a508cf3a2ec0968beb6685/_data",
"Destination": "/var/jenkins_home",
"Driver": "local",
"Mode": "",
"RW": true,
"Propagation": ""
}
],
"Config": {
"Hostname": "eabdb70aabc4",
"Domainname": "",
"User": "jenkins",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"50000/tcp": {},
"8080/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"LANG=C.UTF-8",
"JENKINS_HOME=/var/jenkins_home",
"JENKINS_SLAVE_AGENT_PORT=50000",
"REF=/usr/share/jenkins/ref",
"JENKINS_VERSION=2.379",
"JENKINS_UC=https://updates.jenkins.io",
"JENKINS_UC_EXPERIMENTAL=https://updates.jenkins.io/experimental",
"JENKINS_INCREMENTALS_REPO_MIRROR=https://repo.jenkins-ci.org/incrementals",
"COPY_REFERENCE_FILE_LOG=/var/jenkins_home/copy_reference_file.log",
"JAVA_HOME=/opt/java/openjdk"
],
"Cmd": null,
"Image": "jenkins/jenkins:latest",
"Volumes": {
"/var/jenkins_home": {}
},
"WorkingDir": "",
"Entrypoint": [
"/usr/bin/tini",
"--",
"/usr/local/bin/jenkins.sh"
],
"OnBuild": null,
"Labels": {
"org.opencontainers.image.description": "The Jenkins Continuous Integration and Delivery server",
"org.opencontainers.image.licenses": "MIT",
"org.opencontainers.image.revision": "979b513098fcb4e37675fe19fe904fb6e07f1b05",
"org.opencontainers.image.source": "https://github.com/jenkinsci/docker",
"org.opencontainers.image.title": "Official Jenkins Docker image",
"org.opencontainers.image.url": "https://www.jenkins.io/",
"org.opencontainers.image.vendor": "Jenkins project",
"org.opencontainers.image.version": "2.379"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "d9b561e53663f0e213b7078f1466863a0cc5ce6f3d9169dfbcc3bea551d6e5ec",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"50000/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "49153"
},
{
"HostIp": "::",
"HostPort": "49153"
}
],
"8080/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "49154"
},
{
"HostIp": "::",
"HostPort": "49154"
}
]
},
"SandboxKey": "/var/run/docker/netns/d9b561e53663",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "d430c74f044d7dd019f6737e7b87cba75edaac9eadb168490670c07466b1e70e",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "11ac3cc1ec070c901195ef46bb93005a9c0889e3207766962992835c7de24fdd",
"EndpointID": "d430c74f044d7dd019f6737e7b87cba75edaac9eadb168490670c07466b1e70e",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]
- top command
- to know the stats resources consumed by containers
docker container stats
- to look at logs generated by container on STDOUT, STDERR
docker container logs
- Login into running container
docker container run -it <name> <shell>
- Containers can be paused and unpaused
- To stop the contianer
docker container stop
and to start it backdocker container start
- Lets run the portainer container which shows the information about networks, volumes, images, events about the containers running on a host
docker container run -d -p 8000:8000 -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest
- Run a container with restricted memory
docker container run --name jenkins2 --memory 1024m -d -P jenkins/jenkins:latest
- To run a container with restrictions refer
docker container run --help
Docker Registries
- Registry is used to store the docker images.
- Docker images are OCI (Open Container initiative) compliant
- Overview in Pipeline
- There are two types of registries
- public registry: Open to all for usage
- Example: Docker hub
- private registry: Registry for internal usage with in organization
- Examples:
- Docker hub (private)
- Azure Container Registry
- AWS Container Registry
- JFROG/Artifcatory
- Refer Here docker registry image
- Examples:
- public registry: Open to all for usage
- Lets build a student course registry
- Build the student course rest service with the following command
docker image build -t scr:1.0 .
- Generally registries will have repositories, each repository represents an application. The naming conventions slightly differ from registry to registry.
- Docker hub: Naming convention is
<account-name>/<image-name>:<tag>
- account name => shaikkhajaibrahim
- image name: scr
- tag: 1.0
- Now let me tag the already built image
scr:1.0
toshaikkhajaibrahim/scr:1.0
docker image tag scr:1.0 shaikkhajaibrahim/scr:1.0
* Now we need to login into docker hub docker login
* Now lets push the image
* Now let me use this image in docker playground