Check contents of folder in Docker container - docker

I'm trying to find out if /opt/refresh_key.sh exists in my docker container. I've tried the likes of docker container inspect container_name and also docker run -it image_name sh but neither seem to be what I need.

Docker run launch a new container.
If you want to dive inside your existing container you should do:
docker exec -it <container-name> /bin/bash
and then you will have access to the filesystem of the existing container.
You can find container-name by doing docker ps.

Try the following :
docker exec -it <contailer-id> [[ -f "/bin/sh" ]] && echo "exists"
Place your file name in place of /bin/sh

Related

while starting a docker container I have to execute a script inside docker container

while starting a docker container I have to execute a script inside docker container. Can I do it using docker run command or docker start command mentioning the path in docker? I know I have to use CMD in docker file but dockerfile is not present
Have you tried
docker run -it <image-name> bash "command-to-execute"
To enter a running Docker container (get a Bash prompt inside the container), please run the following:
docker container exec -it <container_id> /bin/bash
You can get the container_id by listing running Docker containers with:
docker container ps -a or docker ps -a
docker run --name TEST -d image sh -c " CMD "
in CMD section you can give the path of shell script

Where is the file I mounted at run time to Docker?

I mounted my secret file secret.json at runtime to a local docker, and while it works, I don't seems to find this volume anywhere.
My docker file looks like this and has no reference to secret:
RUN mkdir ./app
ADD src/python ./app/src/python
ENTRYPOINT ["python"]
Then I ran
docker build -t {MY_IMAGE_NAME} .
docker run -t -v $PATH_TO_SECRET_FILE/:/secrets/secret.json \
-e MY_CREDENTIALS=/secrets/secret.json \
{MY_IMAGE_NAME} ./app/src/python/runner.py
This runs successfully locally but when I do
docker run --entrypoint "ls" {MY_IMAGE_NAME}
I don't see the volume secrets.
Also, if I run
docker volume ls
it doesn't have anything that looks like secrets.
Without environment variable MY_CREDENTIALS the script won't run. So I am sure the secret file is mounted somewhere, but can't figure out where it is. Any idea?
You are actually creating two separate containers with the commands you are running. The first docker run command creates a container from the image you have built with the volume mounted and then the second command creates a new container from the same image but without any volumes (as you don't define any in your command)
I'd suggest you give your container a name like so
docker run -t -v $PATH_TO_SECRET_FILE/:/secrets/secret.json \
-e MY_CREDENTIALS=/secrets/secret.json \
--name my_container {MY_IMAGE_NAME} ./app/src/python/runner.py
and then run exec on that container
docker exec -it my_container sh

How to inspect the fs of a running Docker container

Is there a way to inspect a running Docker container? E.g., inspect the filesystem using a shell, etc?
To inspect an image, we could using docker run <tag> /bin/bash but I am looking to inspect a running container, not an image.
note that docker container inspect is not what I am looking for - that command just gives me metadata about the container.
Assuming that your container has a typical filesystem, you can just use docker exec to start a shell inside the container, as in:
docker exec -it mycontainer bash
Or if bash isn't available (for example, Alpine-based images):
docker exec -it mycontainer sh
Alternatively, you can export a container's filesystem as a tar archive using docker export. For example:
docker export -o mycontainer.tar mycontainer
And then you can inspect the archive or extract it as necessary. If
you just want to a file listing, then:
docker export mycontainer | tar tf -
You can use docker exec command
docker exec -it {container Id or name} command

Docker exec command without the container ID

How can do something like:
docker exec -it 06a0076fb4c0 install-smt
But use the name of the container instead
docker exec -it container/container install-smt
I am running a build on CI server so I can not manually input the container ID.
How can I achieve this?
Yes, you can do this by naming the container with --name. Note that your command with container/container is likely referencing an image name and not the container.
➜ ~ docker run --name my_nginx -p 80:80 -d nginx
d122acc37d5bc2a5e03bdb836ca7b9c69670de79063db995bfd6f66b9addfcac
➜ ~ docker exec my_nginx hostname
d122acc37d5b
Although it won't save any typing, you can do something like this if you want to use the image name instead of giving the container a name:
docker run debian
docker exec -it `docker ps -q --filter ancestor=debian` bash
This will only work if you're only running one instance of the debian image.
It does help if you're constantly amending the image when working on a new Dockerfile, and wanting to repeatedly run the same command in each new container to check your changes worked as expected.
I was able to fix this by setting a container name in the docker-compose file, and rundocker exec -it with the name form the file.
#Héctor (tnx)
These steps worked for me:
This will start the container named mytapir and spawn a shell into the docker container:
docker run -d --name mytapir -it wsmoses/tapir-built:latest bash
Upon docker ps to ensure the docker container is running:
docker exec -it mytapir /bin/bash
Will spawned a shell into an existing container named mytapir.
And you can stop the container as usual docker stop mytapir.
And starting it via docker start mytapir, if it is not running.
(check via docker ps -a)

Write to /etc/hosts in docker container when running docker container

I am trying to write to the file in /etc/hosts within a docker container when I perform the run command, but when I shh into the container and check the hosts file, nothing has been written.
What is the correct command to do this?
I am running my image with the following command:
docker run -it -p 3000:3000 <imageName> bash echo 192.168.56.101 mypath.dev >> /etc/hosts
Use the "add-host" parameter when running the container:
docker run -it --add-host db-static:86.75.30.9 ubuntu cat /etc/hosts

Resources