I am not able to exec into container which uses containerd as runtime.
I need to check some processes running inside the container. Is there any way to exec into container?
I am able to list containers using ctr cli.
Any help would be appreciated.
exec is a sub-command of command task,try
ctr -a $CONTAINERD_HOST -n $NAMESPACE t exec -t --exec-id $exec_id --fifo-dir /tmp $executor_id bash
An alternative to the ctr command that K D Singh wrote about is crictl, so you can use it like so
crictl exec -it 9b47589560c6d /bin/sh
where 9b47589560c6d would be replaced by your container id (doesn't work with names as far as I know).
Firstly run to learn container id:
sudo crictl ps -a
Be careful to use container id (first three letters enough):
sudo crictl exec -it 27e /bin/sh
With ctrctl, we can give seperate commands for images, container and pod (like rmi: remove image, rmp: remove pod, rm: remove container)
Commands: https://github.com/kubernetes-sigs/cri-tools/blob/master/docs/crictl.md
You can use exec command
docker exec -it <container name> /bin/sh
The -i option keeps the STDIN open and -t allocates a pseudo-tty
This will open the shell and you can execute any command inside the running container.
Use exec command for containerd
ctr containers exec -h
Reference:
https://chromium.googlesource.com/external/github.com/docker/containerd/+/refs/tags/v0.2.7/docs/cli.md#exec-another-process-into-a-container
Related
I have a brief script to connect to a docker container and login:
docker exec -it mysql mysql --password=password
The first mysql is the name of the container, the second is the first command I'd like to run.
This works fine, I end up with a mysql prompt inside the container, but then I have to run something like:
use mydatabase
I'd rather do this as a one-liner, just to save time. Is this possible?
To run multiple commands in one line on a container you can write something like this:
docker exec -it <container> <bash|sh> -c "<command1> && <command2> && <command3>"
In your case, for instance:
docker exec -it mysql sh -c "mysql --password=password && use mydatabase"
Using && delimiter instead of ; will execute the next command only if the previous one has completed successfully (0 exit code)
I am trying to achieve something like
docker run -it <image_name> bash
I want to specify the image to run and do not care about anything else.
crictl requires config files for both a container and a pod for the run command, if I am not mistaken.
[hbaba#ip-XX-XX-XXX misc]$ sudo crictl -r /run/crio/crio.sock run -h
....
USAGE:
crictl run [command options] container-config.[json|yaml] pod-config.[json|yaml]
I am looking for the simplest way of starting a container, possibly with only a specified image.
I have a MySQL instance running on a docker container. I am trying to access the bash terminal by running "docker exec -t myContainerID /bin/bash" for the container so that I can check into my MySQL and see if the setup is correct. Although after accessing the bash terminal, any command I run is irresponsive. Even something as simple as ls. Is there any way to resolve this or know what might be causing the problem? Thanks.
You seem to be missing the -i option, try running: docker exec -ti CONTAINER_ID /bin/bash
And just FYI:
--interactive , -i Keep STDIN open even if not attached
--tty , -t Allocate a pseudo-TTY
So I'm writing an install script and because I haven't been able to find a solid MySQL replacement on armfh (the db must be MySQL compatible), I'm using a community on that works, however it does not initiate the db as it should. it requires me to pass the following argument.
mysql -h"db" -u"root" -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" < /docker-entrypoint-initdb.d/1_db.sql
From inside the docker. Problem is I want this to flow naturally as a smooth install script. I've tried using the following command to pass the document and get a password prompt:
docker exec -it db bash -c "mysql -h"db" -u"root" -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" < /docker-entrypoint-initdb.d/1_db.sql"
If also tried:
docker exec -it db bash -c "mysql -h'db' -u'root' -p'$MYSQL_ROOT_PASSWORD' '$MYSQL_DATABASE' < /docker-entrypoint-initdb.d/1_db.sql
FwIW: I used the MYSQL_ROOT_PASSWORD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1) to define the password. And if I manually enter the docker send command 1 (in quotations) the db initiates.
So to summarize my question: Is it possible to pass a command like the above to activate the 1_db.sql file from outside docker?
Any help would be amazing! Thanks in advance!
Is it possible to pass a command like the above to activate the
1_db.sql file from outside docker?
you can try something like
cat 1_db.sql | docker exec -i test bash -c 'mysql -uroot -p$MYSQL_ROOT_PASSWORD $MYSQL_DATABASE'
also, remember when you try exec bash -c "mysql -uroot -p$MYSQL_ROOT_PASSWORD"it will for MYSQL_ROOT_PASSWORD in the host, not inside container, use single quotes.
determined $MYSQL_ROOT_PASS to MySQL docker from outside docker? If so
how?
docker exec -i test bash -c 'echo mysql docker password is $MYSQL_ROOT_PASSWORD'
Goal: Currently trying to connect to a MySQL database on the host from the container.
Action: I have seen several answers to this problem, one being to run the container in host only mode with the --net=host option, so I can access the host using localhost/127.blah.
Result: Normally, when I execute the command
docker run -it [image_id] bash
I get a shell prompt with [user]#[container_id] (i.e., username#12345abcdef). However, when I execute the command
docker run --net=host -it [image_id] bash
it drops me to the same prompt as the host, but no longer in bold (specifically the prompt [user]#[container_id] is no longer in bold). I can tell I am in the container because of the file structure.
Question: What is happening? Am I one the right track to connecting to the database on the host? Why is it the same prompt and what is the significance of the change in font type from bold (host) to not bold (container)?
If it matters, the docker container is being created/ran inside a Vagrant machine.
The container id you normally see in your shell prompt is your container hostname. When you use --net=host, your container hostname is the same as your host's hostname. You have removed the normal network namespace that containers have. So the bash prompt with your container will look similar to the prompt on the host of you display the same fields on each. You can see the formatting of the prompt by checking the value of $PS1.
Edit: here's a comparison of different values of $PS1 from different base images (my host happens to be Debian):
$ echo $PS1
${debian_chroot:+($debian_chroot)}\u#\h:\w\$
$ docker run -it --rm debian:latest
root#4aca692dc29d:/# echo $PS1
${debian_chroot:+($debian_chroot)}\u#\h:\w\$
root#4aca692dc29d:/# exit
exit
$ docker run -it --rm ubuntu
root#b1eb8e51d672:/# echo $PS1
\[\e]0;\u#\h: \w\a\]${debian_chroot:+($debian_chroot)}\u#\h:\w\$
root#b1eb8e51d672:/# exit
exit
$ docker run -it --rm busybox
/ # echo $PS1
\w \$
/ # exit
$ docker run -it --rm centos
[root#abbfa9aa6968 /]# echo $PS1
[\u#\h \W]\$
[root#abbfa9aa6968 /]# exit
exit