Skip need to specify ECS URI for docker images - docker

Right now I do this:
docker run -it --rm 201811111111.dkr.ecr.us-west-2.amazonaws.com/foo/bar:latest /bin/sh
Is there a way to configure Docker so I can just do this?
docker run -it --rm foo/bar:latest /bin/sh

There's no global configuration you can set to make this happen. However, for each tag, you can do this:
docker tag 201811111111.dkr.ecr.us-west-2.amazonaws.com/foo/bar:latest foo/bar:latest
which lets you do this:
docker run -it --rm foo/bar:latest /bin/sh
docker images will show both the images.

Related

Any commands hang inside docker container

Any commands hang terminal inside docker container.
I login in container with docker exec -t php-zts /bin/bash
And then print any elementary command (date, ls, cd /, etc.)
Command hang
When I press ctrl+c I going back to host machine.
But, if I run any command without container - it's work normally
docker exec -t php-zts date
Wed Jan 26 00:04:38 UTC 2022
tty is enabled in docker-compose.yml
docker system prune and all cleanups can not help me.
I can't identify the problem and smashed my brain. Please help :(
The solution is to use the flag -i/--interactive with docker run. Here is a relevant section of the documentation:
--interactive , -i Keep STDIN open even if not attached
You can try to run your container using -i for interactive and -t for tty which will allow you to navigate and execute commands inside the container
docker run -it --rm alpine
In the other hand you can run the container with docker run then execute commands inside that container like so:
tail -f /dev/null will keep your container running.
-d will run the command in the background.
docker run --rm -d --name container1 alpine tail -f /dev/null
or
docker run --rm -itd --name container1 alpine sh # You can use -id or -td or -itd
This will allow you to run commands from inside the container.
you can choose sh, bash, or any other shell you prefer.
docker exec -it container1 alpine sh

Docker - Mount Volume while executing the container

I use a docker container, where i dynamically want to mount a volume. So i want every time i invoke "exec" to mount a different host-path. this is currently not possible.
My current method (Static):
# First Time
docker run -dit -v <from1>:/<to> --name <NAME> <IMAGE>
docker exec <NAME> bash -c "<my-bash-command>"
# Any following time:
docker stop <NAME>
docker rm <NAME>
docker run -dit -v <from2>:/<to> --name <NAME> <IMAGE>
docker exec <NAME> bash -c "<my-bash-command>"
So currently i have to stop, remove and recreate the entire container just because i have a different "from" path.
I hope there is a way that i could create and already start the container in the background, and just during a command execution mount the volume.
Example (pseudo code, this wont work)
# First Time
docker run -dit --name <NAME> <IMAGE>
docker exec -v <from1>:/<to> <NAME> bash -c "<my-bash-command>"
# Any following time:
docker exec -v <from2>:/<to> <NAME> bash -c "<my-bash-command>"
docker exec -v <from3>:/<to> <NAME> bash -c "<my-bash-command>"
...
Is there a solution for this? Because i need to keep the same container and i dont want to create a new container every time a run a command (as i will use persistent data inside the container, which get tossed away if i remove the container)
The whole idea behind containers is to encapsulate small tasks that are reusable. The containers should be transient, meaning, I should be able to delete the container and create new one without loosing data (all data should be outside the container)
If your containers follow this approach, you can run in the following way.
docker run -v <from2>:/<to> <NAME> bash -c "<my-bash-command>"
docker run -v <from3>:/<to> <NAME> bash -c "<my-bash-command>"
From the nature of the question and what you are trying to do I can understand that the container has internal state on which you depend on the subsequent commands, and this is the root-cause of the problem.
From the commands that are shared, I don't see anything that is depending between the containers, (ex. volumes, ports, etc.), so nothing preventing you to run the containers as follows:
# First Time
docker run -dit -v <from1>:/<to> --name <NAME> <IMAGE>
docker exec <NAME> bash -c "<my-bash-command>"
# Any following time:
docker run -dit -v <from2>:/<to> --name <NAME2> <IMAGE>
docker exec <NAME2> bash -c "<my-bash-command>"
If you have dependancies, maybe the dependancies should be in another container and then both the running container and the new container can link to the dependency container and consume the information that is required. You can use file system, network services, etc. to link the containers.

Check contents of folder in Docker container

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

How to pass in stdin input to a go app from dockerimage

I am still not well versed with docker so apologies in advance.
I need to run this package: https://github.com/sclevine/yj
What I have done so far is
pulled this image in my GOPATH (i don't think it was needed to be on GOPATH though).
Then i did docker build yj .
Now, I don't know how to execute the command to convert github yaml to hcl. I have tried following commands but all gave "help" message from the program
docker run -it <image_id> /bin/bash
docker run -it <image_id> /bin/yj
docker run -it <image_id>
docker run -it <image_id> -yc
docker run -it <image_id> /bin/yh -yc
docker run -it <image_id> /bin/yh
docker run -it --entrypoint /bin/bash ad5d67b05c22
docker run -it <image_id> -xyc
docker run -it <image_id> yj -yc
Few commands like docker run -it <image_id> yj -yc, didn't show any error. the cursoer stayed in console (without any prompt). I tried pasting my yaml file but nothing happened.
There are a few things to clarify here:
Your build command has syntax error and it should be something like docker build -t yj . which will build a new image with name yj and tag latest
Whenever you run docker run -it <image_id> /bin/bash it will create a new container and you will have to explicitly remove it. You can see all those containers using docker ps -a. For one-off usage, please add a flag --rm so that docker will remove the container whenever the container exits.
Once the image yj has been built, here are some of the commands that you can run to see how it works
docker run --rm -i yj <<EOF
key: value
EOF
or
echo key: value | docker run --rm -i yj

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)

Resources