script failed to run docker-compose and ./asadmin start-domain domain1 - docker

i try to do a simple script to run docker-compose and when container is running by docker-compose
it should enter inside container and start glassfish with ./asadmin start-domain domain1
Script of running docker-compose & glassfish container
#!/bin/sh
docker-compose up -d
docker exec -it glassfishapp bash -c 'cd glassfish5/bin && ./asadmin start-domain domain1'
when i run docker-compose and i enter into glassfish container with docker exec -it id bash
i can access to my application deploy into glassfish image
but when i use script i can't access

docker-compose up -d will fork into the background per the -d option, which means your script will immediately try to connect to a container which is almost certainly not started yet.

Related

Life-cycle difference between docker run and docker start

I have a fundamental question about container life cycle.
For example I run the following command
Create new ubuntu container and run the bash command
docker run -it ubuntu bash
In the container's bash
exit
The new container will be in state EXITED
docker ps -a
Then I use docker start to restart the container
docker start xxxx(container name)
docker exec -it xxxx(container name) /bin/bash
In the restarted container's bash
exit
The restarted container is still running
docker ps -a
May I know the reason behind for this behavior? Thank you!
With the docker run command:
docker run -it ubuntu bash
the container is started with the execution of the bash command, so when you exit from the bash, the container also exits as bash is the main process running inside the container.
However with the docker exec command:
docker exec -it xxxx(container name) /bin/bash
the container is already running the command defined by the CMD/ENTRYPOINT and bash is the command executed as a separate process. So, exiting from bash after docker start exits the bash process and the main process is still continued.

Ubuntu container immediately exits after `docker run ubuntu`

I am on Windows 10 and I have WSL2 enabled. When I do docker pull ubuntu followed by docker run ubuntu, a new ubuntu container with a randomly generated name shows up in my dashboard and it starts for half a second, but then immediately stops. If I press the start button the same behaviour is observed. I tried running these commands from Command Prompt, PowerShell, and my downloaded Ubuntu 18.04 distro (which is also my default WSL2 distro) all with the same result.
How do I fix this?
Also, docker logs <container_name> doesn't result in anything and double-clicking the container name in the dashboard doesn't show any logs.
A docker container exits when its main process finishes its execution. Now when you check Dockerfile of Ubuntu image you can see the
CMD ["/bin/bash"] which get execute when we start the container.
so if you need to run the container in the background you can do
docker run -id --name=myubuntu ubuntu
Or you can directly launch the container with an interactive shell using
docker run -it --name=myubuntu ubuntu /bin/bash
$ docker run -d ubuntu bash -c "tail -f /dev/null"
8181cb08da63e8c2b43696155088f1a7da58023d426f11dbc52ec4867a2f5bdf
Using a command that hangs (e.g tail -f /dev/null) ensures that the container won't close early.
You can then enter the container using
$ docker exec -it 8181cb08da63e8c2b43696155088f1a7da58023d426f11dbc52ec4867a2f5bdf /bin/bash
To make it easier you could name the container by using the --name flag
$ docker run -d --name=myubuntu ubuntu bash -c "tail -f /dev/null"
fd534285934a6d62e6955ee330134e342d97a6900f4531f5ee60f729d4c6d43d
Then enter the container using
$ docker exec -it myubuntu /bin/bash

Running shell script from PC in running Docker

I have pulled one docker image and docker container is running successfully as well. But I want to run one shell script in the running docker. The shell script is located in my hard disk. I am unable to find out which command to use and how to give pathname of the shell file so that it can be executed in running docker.
Please guide me.
Regards
TL;DR
There are two ways that could work in your case.
You can run one-liner-script using docker exec sh/bash with -c argument:
docker exec -i <your_container_id> sh -c 'sh-command-1 && sh-command-2 && sh-command-n'
You can copy shell script into container using docker cp and then run it in docker context:
docker cp ~/your-shell-script.sh <your_container_id>:/tmp
docker exec -i <your_container_id> /tmp/your-shell-script.sh
Precaution
Not all containers allow to run shell scripts in their context. You can check it executing any shell command in docker:
docker exec -i <your_container_id> echo "Shell works"
For future reference check section Understand how CMD and ENTRYPOINT interact
Docker Exec One-liner
docker exec -i <your_container_id> sh -c 'sh-command-1 && sh-command-2 && sh-command-n'
If your container has sh or bash or BusyBox shell wrapper (such as alpine, you can send one-line shell script to container's shell.
Limitations:
only short scripts;
hard to pass command-line arguments;
only if your container has shell.
Docker Copy and Execute Script
docker cp ~/your-shell-script.sh <your_container_id>:/tmp
docker exec -i <your_container_id> /tmp/your-shell-script.sh -arg1 -arg2
You can copy script from host to container and then execute it.
You can pass arguments to the script.
You can run script with root credentials with -u root: docker exec -i -u root <your_container_id> /tmp/your-shell-script.sh -arg1 -arg2
You can run script interactively with -t: docker exec -it <your_container_id> /tmp/your-shell-script.sh -arg1 -arg2
Limitations:
one more command to execute;
only if your container has shell.

How to restart container using container-id?

I created a container using the command
docker run ubuntu /bin/bash -c "echo 'cool content' > /tmp/cool-file"
Now I see the container has exited
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9e5017aef3f9 ubuntu "/bin/bash -c 'echo '" 38 seconds ago Exited (0) 36 seconds ago elegant_euler
Question: How can I restart and get in to the interactive mode for this container using its container-id?
I cannot use docker run -it <image_name> since this expects image name and not container id.
I tried using docker attach , but I think this only works for running containers.
I don't want to commit this container just yet so, how can I restart and get in to interactive mode for this container using it's container-id?
EDIT: I'm able to get in to other containers using docker start {container-id} and then running docker attach {container-id}. I wonder if there is something peculiar with the way I created the container which would result in this behavior. I'm just starting out with docker so do direct me in right direction if I'm missing some basic bit.
A container exits when it completes its command. So the container started with
docker run ubuntu /bin/bash -c "echo 'cool content' > /tmp/cool-file"
will exit as soon as the command echo is completed. In this case it doesn't make sense to restart that container.
If you run a new container in detached mode you'll be able to keep it live and to attach it in a second time.
So, in your case you should run a new container from the image in detached mode running a command like /bin/bash , then you can run the echo and attach it
docker run -d -ti ubuntu /bin/bash
docker exec -ti <containerId> /bin/bash -c "echo 'cool content' > /tmp/cool-file"
The container will be kept alive, so you can exec more commands on it, e.g.
docker exec -ti /bin/bash -c "cat /tmp/cool-file"
or run a new /bin/bash to "attach" your container and to work in it as a command prompt
docker exec -ti <containerId> /bin/bash
root#<containerId>:/# cat /tmp/cool-file
cool content
You can succesfully stop / start /restart this container
docker stop <containerId> && docker start <containerId>
or
docker restart <containerId>
Remind that when you restart a container it executes again its original command. So if you would able to restart the container of your use case (but you dont't) it would run again /bin/bash -c "cat /tmp/cool-file"
Restarting a container that run with command /bin/bash , it will run again the same command when restarting.
You normally can't change the command to RUN when restarting an existing container; to do it you can try some tricks as suggested at How to start a stopped docker container with a different command.
i tried myself:
docker restart <container_id>
docker exec -it <container_id> bash
works both perfect to restart and get into interactive terminal.
Check Docker start command
docker stop {containerId} && docker start -i {containerId}

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

Resources