How to stop and start Docker container from bash script Synology? - bash

Hey guys so I'm on a synology DS218+ and I'm running docker and hosting my own instance of gitlab in it.
So now I'm trying to write an update Script with bash.
docker stop gitlab_td3v
rm -rf /volume1/docker/"gitlab"
rm -rf /volume1/gitlab/"gitlab_backup.rb"
cp -r /volume1/gitlab /volume1/docker/"gitlab"
cp /volume1/gitlab/config/gitlab.rb /volume1/gitlab/"gitlab_backup.rb"
docker rm -f gitlab_td3v
docker pull gitlab/gitlab-ee:latest
docker run --detach --hostname Myhost.name --name gitlab_td3v --publish ..:.. --publish ..:.. --publish ..:.. --restart always --volume /volume1/gitlab/config:/etc/gitlab --volume /volume1/gitlab/data:/var/opt/gitlab --volume /volume1/gitlab/logs:/var/log/gitlab gitlab/gitlab-ee:latest
The problem I have now as you can see in the image I'm trying to stop and remove the container but It doesn't recognise it's name, but if I echo docker ps in the file it gives me the container with the name I used in the file.
CLI
Also when I just run the commands in the bash shell it works only as soon as I try to run the file it doesn't work and now I'm really confuse cause I made sure I'm on the same user on the same shell and everything and that I am but in the file it won't let me select the container over the name or the id I get from docker ps.
So now the question I guess is did anyone have the same problem or does anyone know a fix for it?
Would really appreciate it thanks.

The script must be executed as root.
docker stop CONTAINERNAME
CONTAINERNAME is definied by value "NAME"
See by
docker stats

Related

How to use bash commands alongside Docker restart policies?

In a ROS project, I have the following bash script that I use to run a docker container:
#!/bin/bash
source ~/catkin_ws/devel/setup.bash
rosnode kill some_ros_node
roslaunch supporting_ros_package launch_file.launch &
docker run -it \
--restart=always \
--privileged \
--net=host \
my_image:latest \
/bin/bash -c\
"
roslaunch my_package my_launch_file.launch
"
export containerId=$(docker ps -l -q)
However, what I'd like to happen is, for every time the container restarts (especially as the machine is booted up), the bash commands preceding the docker run command to also re-run on the host machine (not within the container).
How might I achieve this?
There are a few ways I can think of doing this:
Add this script to a system service. See this answer regarding adding a system service: See this
Add this script into another container that is also set to restart always ... but mount the docker socket into this other container like this: See this

Jenkins console does not show the output of command runs on docker container

Running below command to execute my tests on docker container
sudo docker exec -i 6d49272f772c bash -c "mvn clean install test"
Above command running on Jenkins execute bash. But Jenkins console does not show the logs for test execution.
I had a similar problem with docker start (which is similar to docker exec). I used the -i option and it would work fine outside Jenkins, but the console in Jenkins didn't show any output from this command. I replaced -i with -a similar to the following:
sudo docker container create -it --name container-name some-docker-image some-command
sudo docker container start -a container-name
sudo docker container rm -f container-name
The docker exec method doesn't have a -a option so possibly removing the -i option would work too (since you are not interacting with the container in Jenkins), so if that doesn't work than you can convert to the following commands and achieve similar results with standard out being captured.

Run bash command before running container

I want to run a pre-existing Docker image like so:
docker run -d --name cdt-selenium selenium/standalone-firefox:3.4.0-chromium
So there is no Dockerfile that I control for this image. However, I would like to copy some files into this container.
If I did control the Dockerfile, I would like to run these commands:
RUN mkdir -p /root/cdt-tests/csv-data
COPY ./csv-data/* /root/cdt-tests/csv-data
Is there a way to run those commands in the same line as the Docker run command above?
I tried this:
docker run -d --name cdt-selenium selenium/standalone-firefox:3.4.0-chromium
docker exec cdt-selenium mkdir -p /root/cdt-tests/csv-data
docker cp cdt-selenium:/root/cdt-tests/csv-data ./csv-data
but I get a permissions error on the docker exec line
All images have a FROM line, and that can be any other image. So you can make a Dockerfile with:
FROM selenium/standalone-firefox:3.4.0-chromium
USER root
RUN mkdir -p /root/cdt-tests/csv-data
COPY ./csv-data/* /root/cdt-tests/csv-data
USER seluser
that will build your own image with your commands run.
You'd build it and create your own tag:
docker build -t alexander/selenium:3.4.0-chromium .
And then run it:
docker run -d --name cdt-selenium alexander/selenium:3.4.0-chromium
Edit: the exec command you ran failed because docker runs this container as a different user. You can see that in their Dockerfile. To solve that, run the exec with the root user option (-u root):
docker exec -u root cdt-selenium mkdir -p /root/cdt-tests/csv-data

Shell script: Remove hello world docker container without knowing ID

I'm running hello world docker container in a shell script:
#!/bin/bash
sudo docker run hello-world
I'm doing this to verify if the installation of docker was correct. After this I would like to remove this container again. But as I don't know the ID of this new container I can't remove it:
sudo docker rm hello-world
... is failing.
In the docs of docker: Explore the Application it's described how you can do this.
When you docker run hello-world, the container gets created with a random name that you can check with the following command:
docker container ls --all
Under the NAMES column of the output of the command you can check the generated name (you can see below on my example image. peaceful_morse in my case).
Then you can use this name as a parameter when you call the docker remove command:
docker rm peaceful_morse
Image with all the steps:
Give the container a name:
sudo docker run --name hello-world-container hello-world
Then you can remove it by name:
sudo docker rm hello-world-container

Docker. How to get bash\ssh inside runned container (run -d)?

I want to ssh or bash into runned docker container. Please, see example:
$ sudo docker run -d webserver
webserver is clean image from ubuntu:14.04
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
665b4a1e17b6 webserver:latest /bin/bash ... ... 22/tcp, 80/tcp loving_heisenberg
now I want to get something like this (go into runned container):
$ sudo docker run -t -i webserver (or maybe 665b4a1e17b6 instead)
$ root#665b4a1e17b6:/#
Previously I used Vagrant so I want to get behavior similar to vagrant ssh. Please, could anyone help me?
After the release of Docker version 1.3, the correct way to get a shell or other process on a running container is using the docker exec command. For example, you would run the following to get a shell on a running container:
docker exec -it myContainer /bin/bash
You can find more information in the documentation.
The answer is docker attach command.
For information see: https://askubuntu.com/a/507009/159189

Resources