How do you execute a command inside a process in Docker? - docker

How can/could I run a command inside a process in docker. So for instance if I was running apt-get curl in a container it would want a input of Y/n. How could I accept that? (yes i know about the --assume-yes argument)

From your terminal, execute the following command:
docker exec -it <container or id> /bin/bash <your command>
So you are inside your container

Related

Using docker with running process

I've created this docker file which works for
FROM debian:9
ENV CF_CLI_VERSION "6.21.1"
# Install prerequisites
RUN ln -s /lib/ /lib64
RUN apt-get update && apt-get install curl -y
RUN curl -L "https://cli.run.pivotal.io/stable?release=linux64-binary&version=${CF_CLI_VERSION}" | tar -zx -C /usr/local/bin
And it works as expected, now I run it like following
docker run -i -t cf-cli cf -v
and I see the version
Now every command which I want to run is something like
docker run -i -t cf-cli cf -something
my question is how can I enter into container and do ls etc without every-time doing
docker run -i -t cf-cli ...
I want to enter to the container like you enter to machine.
Step 1:
Run the container in background:
docke run -d --name myapp dockerimage
Step2:
Exec into the containr myapp:
docker exec -it myapp bash
run any commands inside as u wish
Have a look at docker exec. You'll probably want something like docker exec -it containername bash depending on the shell installed in the container.
If I correcly understand you just need
docker exec -it <runningcontainername> bash

How can I execute command inside a Docker container from a Composer image?

I am using docker run --rm -it -v $(pwd):/app composer/composer:1.1-alpine install. But, I need to execute some Bash codes before that, how can I achieve that?
You can enter a running container by running the command:
docker exec -it <container-name> sh

Running dev container exec bash not responding

I have following Dockerfile:
FROM elixir:1.4.5
COPY . /
RUN mix compile
CMD echo "Application started" && elixir --name $MY_POD_NAMESPACE#$MY_POD_IP --no-halt --cookie $ERLANG_COOKIE -S mix run
It starts and runs well, but when I try either attatch or exec XXX bash it does not respond at all.
Both the commands are different as such
docker attach containerid gets your to main process which was running and if it doesn't output anything further then you will not see anything. You should rather use docker logs containerid to see the output of your code
docker exec containerId bash means you want to get to a bash process inside the container. This command would execute and end immediately as you have not specified the interactive and the tty flags. Update it to use it as below
docker exec -it containerId bash
And you should be able to get a bash. If it still doesn't work then use docker stats containerId to see what kind of CPU and memory usage your container has
If docker exec -it container-id bash doesn't work then try docker exec -it container-id sh
Sometimes bash command doesn't work.

Running a script in docker container and not killing the script when leaving terminal

I have got some docker container for instance my_container
I Want to run a long living script in my container, but not killing it while leaving the shell
I would like to do something like that
docker exec -ti my_container /bin/bash
And then
screen -S myScreen
Then
Executing my script in screen and exit the terminal
Unfortunately, I cannot execute screen in docker terminal
this maybe help you.
docker exec -i -t c2ab7ae71ab8 sh -c "exec >/dev/tty 2>/dev/tty </dev/tty && /usr/bin/screen -r nmsrv -s /bin/bash"
and this is the reference link
Only way I can think of is to run your container with your script at the start;
docker run -d --name my_container nginx /etc/init.d/myscript
If you have to run the script directly in an already running container, you can do that with exec:
docker exec my_container /path/to/some_script.sh
or if you wanna run it through Php:
docker exec my_container php /path/to/some_script.php
That said, you typically don't want to run scripts in already running containers, but to just use the same image as some already running container. You can do that with a standard docker run:
docker run -a stdout --rm some_repo/some_image:some_tag php /path/to/some_script.php

Run command to docker container in a script

I need run command to docker container in a script, i trying with:
docker run -it <container> /bin/bash -c "command"
I had no error but the command is not executed
I can not create a new image, i must not stop the service ... I do it manually
It's possible to invoke a command in a running container:
docker exec <container_id> rm -f /tmp/cache/*/*/*

Resources