How to execute command inside of container from hosts directly? - linux

If we've to do some operations(execute commands) inside of docker container, we can go inside it and then execute commands -
docker exec -it <ContainerId> bash # go inside of container
cd /usr/local/tomcat/bin # hit command inside of container
./catalina.sh start # hit command inside of container
After this we need to hit Ctrl + C to come out of container.
But without going inside of container, can we execute commands inside of it from host directly like -
// command to attach to container
// command 1 to execute
// command 2 to execute
// no command required to come out of container as above commands directly hit from hosts

yes you can run your command at once:
docker exec -it <ContainerId> /usr/local/tomcat/bin/catalina.sh start
if you need to run multiple commands, you can pass them straight to bash:
docker exec -it <ContainerId> bash -c 'command1 && command2'

Related

How to execute linux commands in the shell opened through /bin/bash

I am new to Linux stuff and would like to know how to run command while opening the shell through /bin/bash?
Eg the steps that I want to perform:
Step1: Run the docker exec command to start the quickstart virtual machine.
$ docker exec -it 7f8c1a16e5b2 /bin/bash
Step2: The above command gives the handle of the quickstart vm on the console. Now I want to run the below command by default when ever some one starts the docker quickstart console (step 1)
cd
. ./.bash_profile
I need some guidance on how to do this. Obviously, putting all these statements in one shell script isn't helping as the commands of Step2 are to be executed in the newly opened shell (of quickstart vm). The idea is to put all these statements in a single shell script and execute it when we want to get hold of the session within the VM console.
You can pass the commands you want to be executed inside the container to bash with the -c option.
That would look something like this:
docker exec -it 7f8c1a16e5b2 /bin/bash -c "cd && . ./.bash_profile && /bin/bash"

How to re-enter to docker containter prompt after exiting

I'm running docker following this procedure:
$ sudo service docker start
$ docker pull epgg/eg
$ docker run -p 8081:80 --name eg -it epgg/eg bash
root#35f54d7d290f:~#
Notice at the last step it creates a root prompt root#35f54d7d290f:~#.
When I do
root#35f54d7d290f:~# exit
exit
The docker process end and the Apache inside the container is dead.
How can I exit the container safely, and how can I re-enter the docker
container prompt back.
When you run following command it performs 2 operations.
$ docker run -p 8081:80 --name eg -it epgg/eg bash
It creates a container named eg
It has only one purpose/process bash that you have overridden using cmd parameter.
That means when bash shell is over/complete/exit container has no objective to run & hence your docker container will also entered into stopped stage.
Ideally you should create a container to run apache-server as the main process (either by default entry-point or cmd).
$ docker run -p 8081:80 --name eg -d epgg/eg
And then using following command you can enter inside the running container.
$ docker exec -it eg bash
here name of your container is eg
(Note since you already have a container named "eg" you may want to remove it first)
$ docker rm -f eg
Since the purpose of any container is to launch/run any process/processes, the container stops/exits when that process is finished. So to keep running the container in the background, it must have an active process inside it.
You can use -d shorthand while running the container to detach
the container from the terminal like this:
docker run -d -it --name {container_name} {image}:{tag}
But this doesn't guarantee to run any process actively in the
background, so even in this case container will stop when the
process comes to an end.
To run the apache server actively in the background you need to use
-DFOREGROUND flag while starting the container:
/usr/sbin/httpd -DFOREGROUND (for centOS/RHEL)
This will run your apache service in background.
In other cases, to keep your services running in the detached mode
simply pass on the /bin/bash command, this will keep the bash
shell active in the background.
docker run -d -it --name {container_name} {image}:{tag} /bin/bash
Anyway, to come outside the running container without exiting the container and the process, simply press: Ctrl+P+Q.
To attach container again to the terminal use this:
docker attach {container_name}

Docker container does not give me a shell

I am trying to get a shell inside the Docker container moul/phoronix-test-suite on Docker Hub using this command
docker run -t -i moul/phoronix-test-suite /bin/bash
but just after executing the command (binary file), the container stops and I get no shell into it.
[slazer#localhost ~]$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0993189463e6 moul/phoronix-test-suite "phoronix-test-suite " 7 seconds ago Exited (0) 3 seconds ago kickass_shockley
It is a ubuntu:trusty container. How can I get a shell into it, so that I can send arguments to the command phoronix-test-suite?
docker run -t -i moul/phoronix-test-suite /bin/bash will not give you a bash (contrary to docker run -it fedora bash)
According to its Dockerfile, what it will do is execute
phoronix-test-suite /bin/bash
Meaning, it will pass /bin/bash as parameter to phoronix-test-suite, which will exit immediately. That leaves you no time to execute a docker exec -it <container> bash in order to open a bash in an active container session.
Have you tried restarting your docker? It might need to restart or even reboot the host.

Docker: Unable to run shell script stored in a mounted volume

I am running Docker (1.10.2) on Windows. I created a script to echo 'Hello World' on my machine and stored it in C:/Users/username/MountTest. I created a new container and mounted this directory (MountTest) as a data volume. The command I ran to do so is shown below:
docker run -t -i --name mounttest -v /c/Users/sarin/MountTest:/home ubuntu /bin/bash
Next, I run the command to execute the script within the container mounttest.
docker exec -it mounttest sh /home/helloworld.sh
The result is as follows:
: not foundworld.sh: 2: /home/helloworld.sh:
Hello World
I get the desired output (echo Hello World) but I want to understand the reason behind the not found errors.
Note: This question might look similar to Run shell script on docker from shared volume, but it addresses permission related issues.
References:
The helloworld.sh file:
#!/bin/sh
echo 'Hello World'
The mounted volumes information is captured below.
Considering the default ENTRYPOINT for the 'ubuntu' image is sh -c, the final command executed on docker exec is:
sh -c 'sh /home/helloworld.sh'
It looks a bit strange and might be the cause of the error message.
Try simply:
docker exec -it mounttest /home/helloworld.sh
# or
docker exec -it mounttest sh -c '/home/helloworld.sh'
Of course, the docker exec should be done in a boot2docker ssh session, simalar to the shell session in which you did a docker run.
Since the docker run opens a bash, you should make a new boot2docker session (docker-machine ssh), and in that new boot2docker shell session, try the docker exec.
Trying docker exec from within the bash made by docker run means trying to do DiD (Docker in Docker). It is not relevant for your test.

Access mongo shell running in docker container in linux script

I want to create a bash script that removes a user from a mongodb database that is running inside a docker container.
Normally through the terminal I would execute docker exec -it mycontainername bash
and then once I'm in the container I execute mongo mydbname --eval "db.users.remove({"firstname":"Bob"})"
I just have no idea how to do it in a linux bash script.
After I execute script that contains the docker exec command, it leaves the script and opens the docker container's terminal and prompts me to enter a command.
Would using the detached -d flag work?
Found out you can replace bash with the name of the shell you want to open, so in this case mongo.

Resources