Skip to content

Instantly share code, notes, and snippets.

@diegoquintanav
Last active March 31, 2018 19:42
Show Gist options
  • Save diegoquintanav/5e440badb209e668134c6eb03faab669 to your computer and use it in GitHub Desktop.
Save diegoquintanav/5e440badb209e668134c6eb03faab669 to your computer and use it in GitHub Desktop.
Docker gists

List the container port address

$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

where container_name_or_id can be a map of other docker methods, such as

$ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -q)

which returns the ip address of every container show by docker ps -q.

The logic behind this is that every container exposes descriptive metadata through inspect. If you do

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
a0fecb8dcb69        simple-flask-app    "python app.py"     9 minutes ago       Up 9 minutes        0.0.0.0:5000->5000/tcp   silly_turing

The long way is by inspecting the container using

docker inspect silly_turing | grep Network -C 4
            "LogConfig": {
                "Type": "json-file",
                "Config": {}
            },
            "NetworkMode": "default",
            "PortBindings": {
                "5000/tcp": [
                    {
                        "HostIp": "",
--
            ],
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "a1c8b69c0abc9c40f1cde11ff2e3aeb04cfaa479461a652d524247060bae7073",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
--
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "529e0148a79cefae50e75057214ad2e434fbf188727649c75e1a9ec096eb5ec9",
                    "EndpointID": "7ac697df892afe22811df5746917924a8c5e4da6444ea18836adc8b5d1548d43",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,

remove docker dangling images shown as <none> from docker registry

$ docker image rm $(docker image ls -q -f dangling=true)

aliased form

$ docker rmi $(docker images -q -f dangling=true)

add a -f parameter to rmi or image rm to force removal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment