I need to find the IP Address of the docker instance.
Can I do it via a shell command?
Simpler
docker inspect -f '{{ .NetworkSettings.IPAddress }}' $(docker ps -q)
as
docker ps -q
gives the ids of all running containers
Related
It's possible to list the running docker containers with docker ps, but how can I list the images used by them?
Listing running Docker images with their tag (registry/namespace/name:tag)
docker inspect --format '{{.Config.Image}}' $(docker ps --format='{{.ID}}')
List the IDs of the running containers with docker ps --format='{{.ID}}'
List the images associated with the containers with docker inspect --format '{{.Config.Image}}' <containers_ids>
Listing running Docker images with their digest (registry/namespace/name#digest)
docker image inspect --format '{{index .RepoDigests 0}}' $(docker inspect --format '{{.Image}}' $(docker ps --format='{{.ID}}'))
List the IDs of the running containers with docker ps --format='{{.ID}}'
List the images IDs associated with the containers with docker inspect --format '{{.Image}}' <containers_ids>
List the images associated with the images IDs with docker image inspect --format '{{index .RepoDigests 0}}' <images_ids>
I want to get a container ID of docker service.
Is there any command available for this ?
I tried
docker service ps MyService
but this one only gives the service ID, I am interested in the container id in which the service is running
try combination of docker process filtering and formatting:
docker ps -f name=YOUR_SERVICE_NAME --format "{{.ID}}"
UPDATE
thanks to ahivert for even shorter solution:
# same behavior with
docker ps -f name=YOUR_SERVICE_NAME --quiet
try from
https://github.com/moby/moby/issues/31369
for f in $(docker service ps -q $service);do docker inspect --format '{{.NodeID}} {{.Status.ContainerStatus.ContainerID}}' $f; done
and
docker network inspect --verbose
from https://github.com/moby/moby/pull/31710
docker service ps -q -f desired-state=running SERVICE_NAME | xargs docker inspect --format '{{.Status.ContainerStatus.ContainerID}}'
docker ps | grep "<service-name>\." | awk '{print $1}'
I tried this, it gives me a list of running services:
docker container ls
Is it possible to get MAC address of the host machine from Docker container and write it in a text file?
docker inspect <container name or id> |grep MacAddress|tr -d ' ,"'|sort -u
or inside the container:
ifconfig -a
ifconfig is part of the 'net-tools' linux pkg and this is good way to enter the running container:
nsenter -t $(docker inspect --format '{{ .State.Pid }}' <container name or id> ) -m -u -i -n -p -w
Use docker inspect to pull MacAddress and redirect the results to a file. For example, try this on a container named my-container. This uses range (from the Go template package) to find MacAddress:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}' my-container > /path/file.txt
If that doesn't work, first try viewing the metadata available for my-container:
docker inspect my-container
Find MacAddress in those results. Then create a docker inspect command that uses the docker json template function to pull the value from that specific json path. The path to MacAddress may vary, so here's an example that instead uses Status:
docker inspect -f "{{json .State.Health.Status}}" my-container > /path/file.txt
In docker I can make docker inspect -f "{{ .NetworkSettings.IPAddress }}" CONTAINER to run command on a particular container.
What I need is to do something like
docker inspect -f "{{ .NetworkSettings.IPAddress }}" all or
docker inspect -f "{{ .NetworkSettings.IPAddress }}" *
to run a command on all containers, not mentioning their names explicitly?
It relates to other docker commands as well. Is there such a way without bash scripting?
You can list multiple container to a docker inspect.
docker inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]
But the only way to list those container is through bash $(sudo docker ps -aq).
For example:
docker inspect --format='{{.Name}}' $(sudo docker ps -aq)
docker inspect -f "{{ .NetworkSettings.IPAddress }}" $(sudo docker ps -aq)
The OP f1yegor proposes in the comments:
all=$(sudo docker ps -aq) docker inspect -f "{{ .NetworkSettings.IPAddress }}" $all
What is the Docker way to clean up all stopped Docker containers but retain data-only containers?
docker rm $(docker ps -qa -f status=exited) removes these too!
How to clean up the according images?
In general there is no definitive way to distinguish data-only from other containers. If you wish them to survive your cleansing, you could probably design a certain name scheme and have more elaborate scripts that wouldn't remote containers with name, say, starting with data-.
Following Mykolas proposal I introduced a naming convention requiring all data-only containers to be suffixed by -data.
To remove all stopped containers, except those named *-data:
docker ps -a -f status=exited | grep -v '\-data *$'| awk '{if(NR>1) print $1}' | xargs -r docker rm
To remove all unused images afterwards:
docker rmi $(docker images -qa -f dangling=true)
(the images used by the data-only containers are retained)
May be you can, in the docker run command of all your data-only container add a -e "type=data-only", and then filter based on this criteria, either with a grep or with a docker inspect example, I start a container with sudo docker run -it -e type=data-only ubuntu bash
root#f7e9ea4efbd9:/# and then sudo docker inspect -f "{{ .Config.Env }}" f7e shows
[type=data-only PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin]