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

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

Related

Unable to run docker build inside docker run

I am trying to build an image and then run it in one command. Using stackoverflow question I have menaged to scrape this command:
docker run --rm -it -p 8000:8000 -v "%cd%":/docs $(docker build -q .)
However it produces an error:
docker: invalid reference format.
See 'docker run --help'.
First part of the command ( docker run --rm -it -p 8000:8000 -v "%cd%":/docs .) works properly on already built image the problem lies inside $(docker build -q .). Dockerfile is inside the folder I have opened in cmd.
You need to run it in PowerShell, not cmd.
Try docker run --rm -it -p 8000:8000 -v ${PWD}:/docs $(docker build -q .).

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 run command without -it option

Why when i run the command
docker run ubuntu
without option '-it' is not possible to interact with the created container even when running command start with the -a -i options
docker start -a -i CONTAINER_ID
or when i run
docker start CONTAINER_ID
simply the container has the status "Exit (0) 4 seconds ago"
But when i run
docker run -it ubuntu
i can use bash shell of ubuntu using 'docker start -a -i'
When you run docker run without -it it's still running the container but you've not given it a command, so it finishes and exits.
If you try:
docker run ubuntu /bin/bash -c "echo 'hello'";
It'll run ubunu, then the command, and then finish because there is no reason for it to be kept alive afterwards.
-i is saying keep it alive and work within in the terminal (allow it to be interactive), but if you type exit, you're done and the container stops.
-t is showing the terminal of within the docker container (see: What are pseudo terminals (pty/tty)?)
-it allows you to see the terminal in the docker instance and interact with it.
Additionally you can use -d to run it in the background and then get to it afterwards.
Ex:
docker run -it -d --name mydocker ubuntu;
docker exec -it mydocker /bin/bash;
TLDR is -it allows you connect a terminal to interactively connect to the container.
If you run docker run --help, you can find the details about docker run options.
$ docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
...
-i, --interactive Keep STDIN open even if not attached
...
-t, --tty Allocate a pseudo-TTY

docker : docker pull incendonet/centos7-mono-apache

Docker is not started even if the subsequent command is executed.
docker pull incendonet/centos7-mono-apache
Even if you check with docker ps, it does not exist.
Please tell me the cause.
Docker will be started after you run below command :
docker run -it -d image-name
docker run -it -d incendonet/centos7-mono-apache
docker pull command just fetches image from docker hub to your server/local machine. But to run it you need to use docker run.
Once it is running then it will be shown in your docker ps command and you can use below command to get into container's shell :
docker exec -it <container-id> /bin/bash

Skip need to specify ECS URI for docker images

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.

Resources