What is the elegant way to `watch docker ps --format`? - bash

Problem
I'm trying to watch my running containers, with command like this:
watch docker ps --format "table {{.Image}}\t{{.Names}}\t{{.Command}}"
but instead of nice output in the format of
IMAGE NAMES COMMAND
... ... ...
I get
"docker ps" accepts no arguments.
See 'docker ps --help'.
Usage: docker ps [OPTIONS]
List containers
Is there any elegant way to do watch running containers?

Solution
Shortly after posting this question I found solution.
Adding single-quotes '...' solves the problem.
watch 'docker ps --format "table {{.Image}}\t{{.Names}}\t{{.Command}}"'

Related

Programmatically stopping a docker container

I want to programmatically fetch the id of a running container and stop it. However, I'm a little lost
Here's the command I use to fetch the id of the running container:
docker ps -q --no-trunc --format="{{.ID}}" --filter "ancestor=<repo-name>"
How do I pass the output of this command to
docker stop <id>
I'm new to docker, thus the question. TIA
If you are using bash, you can use back ticks to evaluate a command and substitute in the command output, in your case:
docker stop `docker ps -q --no-trunc --format="{{.ID}}" --filter "ancestor=<repo-name>"`
Please, consider read this related Unix Stackexchange question, it may be of help as well.
As suggested in the linked question, you can use the $(...) syntax as well:
docker stop $(docker ps -q --no-trunc --format="{{.ID}}" --filter "ancestor=<repo-name>")
Although I don't know in this specific use case, the $(...) syntax has the further advantage of working with PowerShell too.

Docker exec -it <containerid> dir doesn't work. Why? How to get directory info?

I'm very new to docker.
Also I'm using Docker for Windows (ie Image and Container are for Windows OS).
I'm trying to get a list of all the folders and subfolders to resolve another issue I'm having. I read several post and blogs and seems like I should be able to run
docker exec -it <container id> dir
To get the info as it is suppose to allow me to run commands against the container.
I even ran
docker exec -it f83eb1533b67 help
which gave me a list of commands (because no one tells what are acceptable 'commands'...) and it is listed. however I get the following message when I run DIR command
PS P:\docker\tmp\SqlServerSetup> `docker exec -it f83eb1533b67 dir`
container f83eb1533b671b4462b8a1562da7343185b2dd27e94ff360e0230969d432ec37 encountered an error during CreateProcess: failure in a Windows system call: The system cannot find the file specified. (0x2)
[Event Detail: Provider: 00000000-0000-0000-0000-000000000000] extra info: {"CommandLine":"dir","WorkingDirectory":"C:\\","Environment":{"ACCEPT_EULA":"Y","attach_dbs":"[]","sa_password":"Pass1.4DBAs","sa_password_path":"C:\\ProgramData\\Docker\\secrets\\sa-password"},"EmulateConsole":true,"CreateStdInPipe":true,"CreateStdOutPipe":true,"ConsoleSize":[0,0]}
PS P:\docker\tmp\SqlServerSetup>
Please note: I don't want to persist a volume. Seems like that option is for people that are trying to reuse data.
UPDATE:
This is the statement that i'm using to create the container:
docker run -p 1433:1433 -e sa_password=Pass1.4DBAs -e ACCEPT_EULA=Y -p 11433:1433 --name sqlTraining --cap-add SYS_PTRACE -d microsoft/mssql-server-windows-developer
It works fine. Container is created, but I want to view the filesystem within that container.
For Windows containers, prefix the command with the command shell (cmd) and the /c parameter. For example:
docker exec <container id> cmd /c dir
This will execute the dir command on the specified container and terminate.
Try running:
docker exec -it <container id> sh
to start the interactive shell console. This should help you with debugging.

Docker embed commands usage on Windows [duplicate]

This question already has answers here:
Docker unknown shorthand flag: 'a' in -aq)
(2 answers)
Closed 2 years ago.
I'm running Docker v 17.09.0-ce on Windows 10.
When I try to run embed commands like this:
docker restart $(docker ps -a) it throws me the error:
unknown shorthand flag: 'a' in -a). Without the -a flag the error looks like this:
Error response from daemon: No such container: $(docker
Error response from daemon: No such container: ps)
These kind of embed commands suggestions are widespread across the web. How do I make it working?
The $(docker ps -a) is a bash syntax (along with other common shells on Linux). To use this syntax on a Windows system, you need to use a bash shell rather than the powershell or command prompt.
docker restart $(docker ps -a) wouldn't work on Linux either. I think you're looking for docker restart $(docker ps -aq) (with "quiet" flag) - this works great in Powershell too.

How to check if the docker image has all the files?

Is there a way to check if the docker image has all of the files that the Dockerfile copies over and to understand if the image is built as configured in the Dockerfile? My situation is that the image is built successfully, however when I try running it, docker complains that it cant find some file or other and the container fails to run, so I cant exec on it.
Doing docker inspect is not helping since it does not report on the files in the image. Is there some method?
You can run a shell based on that image:
docker run -it <image-name> bash
Use sh instead if there is no bash available. There you can search for files as any shell.
But maybe you have not bash in the image, so use sh:
docker run -it <image-name> sh
But maybe you have an odd entrypoint, so override it:
docker run -it --entrypoint sh <image-name>
You can see the history of file and check if all the required files are present at the time of image creation
docker image history --no-trunc [image_name] > [file_name]

Dockerfile: why doesn't the bash command work?

I'd like to learn the Dockerfile from a very simple start, so here's my Dockfile:
FROM ubuntu
ENTRYPOINT /bin/bash
however, after building the image and run the container, I find that I can't run the bash commands. For example, if I type:
# clear
The container seems to get stuck running in an infinite loop.
So why does that happen? How can I fix it?
How are you running the container? Note that you have to pass the options -i in order to keep STDIN open and the -t to allocate a pseudo-TTY.
Below you can find an example:
docker run -i -t my-image
docker run -it ubuntu
Post this command, you will have a prompt like:
root#26f9e7a42517:/#

Resources