Docker

docker container 설정 수정하기

docker run 이후 policy의 수정을 위해서는 다음 2가지 방법이 있음

  • docker update
docker update --restart=always <container>
  • /var/lib/docker/containers/CONTAINER_ID/hostconfig.json 파일 edit
{"Binds":["/nexus-data:/nexus-data"],"ContainerIDFile":"","LogConfig":{"Type":"journald","Config":{}},"NetworkMode":"default","PortBindings":{"8081/tcp":[{"HostIp":"","HostPort":"8081"}]},"RestartPolicy":{"Name":"always","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"CapAdd":null,"CapDrop":null,"Dns":[],"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"","Cgroup":"","Links":[],"OomScoreAdj":0,"PidMode":"","Privileged":true,"PublishAllPorts":false,"ReadonlyRootfs":false,"SecurityOpt":["label=disable"],"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"docker-runc","ConsoleSize":[0,0],"Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"CgroupParent":"","BlkioWeight":0,"BlkioWeightDevice":null,"BlkioDeviceReadBps":null,"BlkioDeviceWriteBps":null,"BlkioDeviceReadIOps":null,"BlkioDeviceWriteIOps":null,"CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpusetCpus":"","CpusetMems":"","Devices":[],"DiskQuota":0,"KernelMemory":0,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":-1,"OomKillDisable":false,"PidsLimit":0,"Ulimits":null,"CpuCount":0,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0}

docker log 보기

  • docker logs -f <> : 실행중인 로그 확인 할 경우
  • docker events& : event 발생 로그 확인 (터미널을 2개 여는게 좋음)
  • docker start -a <> : 실행시 일어나는 오류 확인 가능

docker inspect

container의 상세 정보를 확인하는 명령어 이다.

docker inspect <<containerid>>

해당 명령어를 실행 하면 아래와 같이 모든 container low level 정보를 return 한다.

[devops@localhost ~]$ docker inspect nexus
[
{
"Id": "67826ba9e0179b6009c9c7a177fe1815fd320cdac65ea91d479413f7c4e5e81a",
"Created": "2019-06-26T03:19:12.069213873Z",
"Path": "sh",
"Args": [
"-c",
"${SONATYPE_DIR}/start-nexus-repository-manager.sh"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 19383,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-07-02T07:49:00.930407162Z",
"FinishedAt": "2019-07-02T07:48:08.216626691Z"
},
"Image": "sha256:36b0df681a47e5948f2088a446652766132141175dece2d6d47ef6e749385c05",
"ResolvConfPath": "/var/lib/docker/containers/67826ba9e0179b6009c9c7a177fe1815fd320cdac65ea91d479413f7c4e5e81a/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/67826ba9e0179b6009c9c7a177fe1815fd320cdac65ea91d479413f7c4e5e81a/hostname",
"HostsPath": "/var/lib/docker/containers/67826ba9e0179b6009c9c7a177fe1815fd320cdac65ea91d479413f7c4e5e81a/hosts",
"LogPath": "",
"Name": "/nexus",
"RestartCount": 0,
"Driver": "overlay2",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": [
"/nexus-data:/nexus-data"
],
"ContainerIDFile": "",
"LogConfig": {
"Type": "journald",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {
"8081/tcp": [
{
"HostIp": "",
"HostPort": "8081"
}
]
},
"RestartPolicy": {
"Name": "always",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": true,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": [
"label=disable"
],
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "docker-runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": null,
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DiskQuota": 0,
"KernelMemory": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": -1,
"OomKillDisable": false,
"PidsLimit": 0,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0
},
"GraphDriver": {
"Name": "overlay2",
"Data": {
"LowerDir": "/var/lib/docker/overlay2/bdeae8fe832211ba5059562162cb4eb7098de60657f622a31f0115653c324301-init/diff:/var/lib/docker/overlay2/e3c7b24292cf5ea41f2d3ab9c3b91a3c3b22f09a1cb26cf6048b36802c30b036/diff:/var/lib/docker/overlay2/3f075ffc65dfb1a311d63c70842d0ba2c467ca9bf1289c5a2a45a21e77a8314c/diff:/var/lib/docker/overlay2/32e8540578f9058f575a9051bc28df2f0308da53a20469a3a91c1a6b60f9cb0e/diff",
"MergedDir": "/var/lib/docker/overlay2/bdeae8fe832211ba5059562162cb4eb7098de60657f622a31f0115653c324301/merged",
"UpperDir": "/var/lib/docker/overlay2/bdeae8fe832211ba5059562162cb4eb7098de60657f622a31f0115653c324301/diff",
"WorkDir": "/var/lib/docker/overlay2/bdeae8fe832211ba5059562162cb4eb7098de60657f622a31f0115653c324301/work"
}
},
"Mounts": [
{
"Type": "bind",
"Source": "/nexus-data",
"Destination": "/nexus-data",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
"Hostname": "67826ba9e017",
"Domainname": "",
"User": "nexus",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"8081/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"SONATYPE_DIR=/opt/sonatype",
"NEXUS_HOME=/opt/sonatype/nexus",
"NEXUS_DATA=/nexus-data",
"NEXUS_CONTEXT=",
"SONATYPE_WORK=/opt/sonatype/sonatype-work",
"DOCKER_TYPE=docker",
"INSTALL4J_ADD_VM_PARAMS=-Xms1200m -Xmx1200m -XX:MaxDirectMemorySize=2g -Djava.util.prefs.userRoot=/nexus-data/javaprefs"
],
"Cmd": [
"sh",
"-c",
"${SONATYPE_DIR}/start-nexus-repository-manager.sh"
],
"ArgsEscaped": true,
"Image": "sonatype/nexus3",
"Volumes": {
"/nexus-data": {}
},
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"com.sonatype.license": "Apache License, Version 2.0",
"com.sonatype.name": "Nexus Repository Manager base image",
"org.label-schema.build-date": "20190305",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS",
"vendor": "Sonatype"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "50b3a078a453332680ca6dd09d2cd10fc641fddc49c5b3b34db178c516f8475d",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"8081/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "8081"
}
]
},
"SandboxKey": "/var/run/docker/netns/50b3a078a453",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "b030c131340f1b86c3ce868dae109fee9814310503076f685ff274b42e76eeac",
"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": "e7bccaaf25cb4ba061b98aab2ef6470301c688b7d99db0f40e7e7bd7b6665379",
"EndpointID": "b030c131340f1b86c3ce868dae109fee9814310503076f685ff274b42e76eeac",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
}
}
}
}
]

여기서 특정 영역에 대한 정보만을 얻고 싶다면 다음과 같이 format 설정을 해주면 된다.

docker inspect --format 또는

docker inspect -f

를 사용하면 된다. 사용 법은 다음과 같다.

[devops@localhost ~]$ docker inspect --format='{{.NetworkSettings.Networks}}' nexus
map[bridge:0xc420130300]

inspect 해서 나온 모든 정보 중에서 필요로 하는 정보의 Name을 .Name.Name 식으로 표현 하면 특정 값을 얻어 낼 수가 있다. 가령 IPAddress를 얻어 오고자 하면 다음과 같이 하면 된다.

[devops@localhost ~]$ docker inspect --format='{{.NetworkSettings.Networks.bridge.IPAddress}}' nexus
172.17.0.2

만약 해당 결과 값을 json 형태의 템플릿으로 받고자 하면 다음과 같이 하면 된다.

[devops@localhost ~]$ docker inspect --format='{{json .NetworkSettings.Networks.bridge.IPAddress}}' nexus                   "172.17.0.2"

"" 앞뒤로 이와 같은게 붙은거 빼고는 차이가 없어 보이지만,

[devops@localhost ~]$ docker inspect --format='{{json .NetworkSettings.Networks}}' nexus
{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"e7bccaaf25cb4ba061b98aab2ef6470301c688b7d99db0f40e7e7bd7b6665379","EndpointID":"b030c131340f1b86c3ce868dae109fee9814310503076f685ff274b42e76eeac","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02"}}

Networks만을 출력하면 다른걸 확인 할 수있다. 앞서 json없이 출력은 메모리 번지를 출력 했다면, json과 함께 출력 하면 이하 모든 값이 string으로 출력 되는걸 확인 할 수 있다.

이외에 index, range,$variableParm 정의, :=Assignment 등이 추가로 잇는듯 한데 어떻게 사용하는지는 잘 모르겠다.

docker shell 로 바로 진입하기

docker run -it --rm <<이미지id>> /bin/sh or

docker run -it --rm <<이미지id>> /bin/bash