Persisting gradle daemon after docker exec -it <container_name> gradle build - docker

I'm using docker exec -it <container_name> gradle build to run gradle (5.6.2/JDK 11) builds in a docker container. This approach works fine, but the daemon is destroyed after the command is completed. How can I keep the daemon running in the container after my build is complete?
I have tried gradle --forground but have learned that this creates incompatible daemons and is an undesirable option.

This problem cannot be solved using Gradle. The daemons are lost because the Docker container stops. You should drop into an interactive shell instead:
docker run --rm -it gradle:5.6.2-jdk11 bash
Now run Gradle commands, and the daemon will be reused on subsequent commands:
root#014faa72d745:/home/gradle# gradle help
Once you're done, exit from the container:
root#014faa72d745:/home/gradle# exit
Note: You can use a bind mount to get your current working directory's files accessible in the container:
docker run --rm -it --mount type=bind,src=$PWD,dst=/app -w /app gradle:5.6.2-jdk11 bash

Related

Batch file not executing a command

I am trying to create a .bat file that will back-up the database inside the container. After the first command (that is used to enter the container) the next one is ignored.
docker exec -it CONTAINER_NAME /bin/bash
cd /var/opt/mssql/data
Any ideas why? If I'm trying to manually write cd /var/opt/mssql/data in the opened cmd it works.
When running docker exec -it CONTAINER_NAME /bin/bash, you are opening a bash-shell INSIDE the docker container.
The next command, i.e. cd /var/opt/mssql/data, is only executed, if the previous command, docker exec -it CONTAINER_NAME /bin/bash has exited successfully, which means that the shell on the docker container has been closed/exited.
This means that cd /var/opt/mssql/data is then executed on the local machine and not inside the docker container.
To run a command inside the docker container, use the following command
docker exec -it CONTAINER_NAME /bin/bash -c "<command>"
Although it may be better to create a script inside the container during the build process or mount a script inside the docker container while starting the container and then simply call this script with the above mentioned command.

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.

re-running a script in a docker container

I have created a docker image that includes some python code and a shell script that can execute it. It is going to process a bunch of images from the host system.
This command should create a new contaier and run it.
sudo docker run -v /host/folder:/container/folder opencv:latest bash /extract-embeddings.sh
At the end, the container exits. If I type the same command, then another container is created and exited at completion. But how is the correct usage of containers? Should I use restart, start or run (and then clean up exited containers after)? It just seems unnessary to create a new container each time.
I basically just want a docker image containing some code and 3-4 different commands I can execute whenever needed.
And the docker start command doesn't seem to accept "bash /extract-embeddings.sh" as parameters, instead things bash and extract-embeddings.sh are containers. So maybe I am misunderstanding the lifecycle of containers or the usage.
edit:
Got it to work with:
docker run -t -d --name opencv -v /host/folder:/container/folder
docker exec -it opencv bash /extract-embeddings.sh
You can write the Dockerfile to create your docker image and keep the scripts into it-
Dockerfile:
FROM opencv:latest
COPY ./your-script /some_folder
Create image:
docker build -t my_image .
Run your container:
docker run -d --name my_container
Run the script inside the container:
docker exec -it <container_id_or_name> bash /some_folder/your-script
Build your own docker image that starts with opencv:latest and give the command you run as the entrypoint. Dockerfile could be like
FROM opencv:latest
CMD ["/bin/bash", "/extract-embeddings.sh"]
Use docker create to create a named container.
sudo docker create --name=processmyimage -v /host/folder:/container/folder myopencv:latest
Then use docker start each time you want to run it.
sudo docker start processmyimage
This works well if there is only one command you want to run. If there is more than one command, I would take the approach of building an image that runs unrelated command forever (like a tail -f < /dev/null). Then you can use
sudo docker exec -d /bin/bash < cmd-to-run >
for each command

Docker container not running after creating with mounted volume

I am trying to use an image that I pulled from the docker database. However I need data from the host to use some programs loaded into the image. I created a container with this
sudo docker run --name="mdrap" -v "/home/ubuntu/profile/reads/SE:/usr/local/src/volume" sigenae/drap
it appears that everything works and then I start the container
sudo docker start mdrap
but when I check the running containers it is not listed there and if I try to load the container into /bin/bash it tells me the container is not running. I am a beginner with docker and am only trying to use an image to run programs with all the required dependencies, what am I doing wrong?
docker start is only to start a stopped container. It's not necessary after a docker run. (but more after a docker **create**, like in the documentation)
A container is started as long as it's main process is running.
As soon as the main process stops, the container stops.
The main process of a container can be either:
the ENTRYPOINT if defined
the CMD if no ENTRYPOINT and no command line argument
the command line argument
In your case, as you don't have any command line argument (after the image name on the docker run command) and the image only defines a CMD (=/bin/bash), your container is trying to start a /bin/bash.
But, as you don't launch the container with the --interactive/-i nor --tty/-t (again like in the documentation), your process as nothing to interact with and stops (idem for each start of this container).
So your solution is simply to follow the documentation:
docker create --name drap --privileged -v /home/ubuntu/profile/reads/SE:/usr/local/src/volume -i -t sigenae/drap /bin/bash
docker start drap
docker exec -i -t drap /bin/bash
Or even simpler:
docker run --name drap --privileged -v /home/ubuntu/profile/reads/SE:/usr/local/src/volume -i -t sigenae/drap /bin/bash

run docker commands from command prompt versus jenkins script

I have a test Ubuntu server with docker-machine installed. I have a number of docker containers running on the servers. Including a Jenkins container. I run jenkins with the following command
docker run -d --name jenkins -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker --restart=always -p 8080:8080 -v ~/jenkinsHome:/var/jenkins_home docker-jenkins
I am working on managing my images through Jenkins. I can start all but one of my containers via Jenkins shell script. The one container that fails appears to start in the script (I do a docker PS after the docker run in script). However, the container stops after the script completes. I am using the same docker run command that works on the command prompt, but it fails in Jenkins script:
sudo docker run -d --net=host -v ~/plex-config:/config -v ~/Media:/media -p 32400:32400 wernight/plex-media-server
I have double checked folder permissions and they are correct. Can anyone direct me to possible reasons the run command is failing in Jenkins, but not at the command prompt?
using docker ps -a I was able to get an ID for the stopped container. Then by using docker logs I was able to see the error was a folder permission issue. Then digging deeper, it was a user permission error mis-match between the user Jenkins runs as inside it's container not being able to pass the folder correctly. I have decided to circumvent the problem by using docker stop and start commands and not using the docker run command.

Resources