Docker container stops after starting with systemd "docker run" - docker

I've looked at : Docker and systemd - service stopping after 10 seconds
and Docker containers shut down after systemd start , but still can't figure out how to get my docker container to start using systemd and not close out.
My psim.service file:
[Unit]
Description=My process
After=docker.service
Requires=docker.service
[Service]
ExecStart=docker run --net=host --name psim -it psim
ExecStop=/bin/docker stop psim
ExecStopPost=/bin/docker rm psim
[Install]
WantedBy=local.target
When I use
"docker run --net=host --name psim -dit psim"
with the detached flag, the container will start but immediately stop and remove itself after a few seconds. If I use either of the following without the detached '-d' flag:
docker run --net=host --name psim -it psim
docker run --net=host --name psim -a STDOUT -it psim
the container won't start at all using systemd, but will start normally if I run that command without systemd. Am I missing something?

Just realized it works if I remove the "-i" flag.

Related

Any commands hang inside docker container

Any commands hang terminal inside docker container.
I login in container with docker exec -t php-zts /bin/bash
And then print any elementary command (date, ls, cd /, etc.)
Command hang
When I press ctrl+c I going back to host machine.
But, if I run any command without container - it's work normally
docker exec -t php-zts date
Wed Jan 26 00:04:38 UTC 2022
tty is enabled in docker-compose.yml
docker system prune and all cleanups can not help me.
I can't identify the problem and smashed my brain. Please help :(
The solution is to use the flag -i/--interactive with docker run. Here is a relevant section of the documentation:
--interactive , -i Keep STDIN open even if not attached
You can try to run your container using -i for interactive and -t for tty which will allow you to navigate and execute commands inside the container
docker run -it --rm alpine
In the other hand you can run the container with docker run then execute commands inside that container like so:
tail -f /dev/null will keep your container running.
-d will run the command in the background.
docker run --rm -d --name container1 alpine tail -f /dev/null
or
docker run --rm -itd --name container1 alpine sh # You can use -id or -td or -itd
This will allow you to run commands from inside the container.
you can choose sh, bash, or any other shell you prefer.
docker exec -it container1 alpine sh

Run interactively with existing docker container

I have a container started as the following:
docker run --interactive --tty --gpus all --name my_container
--workdir "/home/ubuntu" --user ubuntu
--volume /hdd/all_cv/paiv/metis:/home/ubuntu/my --publish 8888:8888 my
how do I run interactively with my_container once I reboot my machine?
Based on the docker documentation, you can attach back to the detached container using docker attach command:
Use docker attach to attach your terminal’s standard input, output, and error (or any combination of the three) to a running container using the container’s ID or name. This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.
So you should try this to have an interactive session with your already running container:
docker attach my_container
If your container is stopped, you just need to start it again
docker ps -aq -f name=my_container | xargs docker start $1

Docker run command without -it option

Why when i run the command
docker run ubuntu
without option '-it' is not possible to interact with the created container even when running command start with the -a -i options
docker start -a -i CONTAINER_ID
or when i run
docker start CONTAINER_ID
simply the container has the status "Exit (0) 4 seconds ago"
But when i run
docker run -it ubuntu
i can use bash shell of ubuntu using 'docker start -a -i'
When you run docker run without -it it's still running the container but you've not given it a command, so it finishes and exits.
If you try:
docker run ubuntu /bin/bash -c "echo 'hello'";
It'll run ubunu, then the command, and then finish because there is no reason for it to be kept alive afterwards.
-i is saying keep it alive and work within in the terminal (allow it to be interactive), but if you type exit, you're done and the container stops.
-t is showing the terminal of within the docker container (see: What are pseudo terminals (pty/tty)?)
-it allows you to see the terminal in the docker instance and interact with it.
Additionally you can use -d to run it in the background and then get to it afterwards.
Ex:
docker run -it -d --name mydocker ubuntu;
docker exec -it mydocker /bin/bash;
TLDR is -it allows you connect a terminal to interactively connect to the container.
If you run docker run --help, you can find the details about docker run options.
$ docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
...
-i, --interactive Keep STDIN open even if not attached
...
-t, --tty Allocate a pseudo-TTY

How to start docker container process after restart machine?

If start a docker container like:
docker run -d -p 5000:5000 --name registry registry:2
It can be seen when run
docker ps
But after restart machine and run docker ps again, it can't been found.
When check docker ps -a can see it exist.
How to week it up at this case if don't want to kill this process to run a new one?
Docker containers don't start automatically on system boot because the Docker default restart policy is set to no.
To achieve that you shoud do:
docker update --restart=always <container ID or name>

Issues Launching Image with Data Volume using CoreOS and Fleet

I have been trying to use FleetCtl to launch docker images one is a Data Volume Image and one is a Nginx Image launched with the --volumes-from option. The Nginx Image will not continue to run on the CoreOs server, but if I go to the server and type the command docker start the image starts and runs. Is there an image with launching Docker images that use a data volume with Fleet?
Docker File for Volume:
FROM busybox
MAINTAINER Zombie Possum
VOLUME ["/usr/share/nginx/html", "/usr/share/nginx/conf"]
COPY dist /usr/share/nginx/html
COPY dist_nginx.conf /usr/share/nginx/conf/dist_nginx.conf
CMD ["/usr/bin/true"]
Fleet File For Volume nginxData.service:
[Unit]
Description=Data Container
Requires=docker.service
After=docker.service
[Service]
TimeoutStartSec=0
KillMode=none
User=core
WorkingDirectory=/home/core
EnvironmentFile=/etc/environment
ExecStartPre=-/usr/bin/docker kill DATA_NGINX
ExecStartPre=-/usr/bin/docker rm DATA_NGINX
ExecStartPre=-/usr/bin/docker pull private_repo/data_nginx:latest
ExecStart=/usr/bin/docker run --name DATA_NGINX private_repo/data_nginx:latest
ExecStop=/usr/bin/docker stop DATA_NGINX
Fleet File for nginx.service:
[Unit]
Description=Nginx Container
Requires=docker.service
After=docker.service
[Service]
TimeoutStartSec=0
KillMode=none
User=core
WorkingDirectory=/home/core
EnvironmentFile=/etc/environment
ExecStartPre=-/usr/bin/docker kill NGINX
ExecStartPre=-/usr/bin/docker rm NGINX
ExecStartPre=-/usr/bin/docker pull private_repo/nginx:latest
ExecStart=/usr/bin/docker run -rm -p 80:80 --name NGINX --volumes-from DATA_NGINX private_repo/nginx:latest
ExecStop=/usr/bin/docker stop NGINX
[X-Fleet]
MachineOf=nginxData.service
Fleet Commands:
fleetctl submit nginxData.service
fleetctl submit nginx.service
fleetctl start nginxData.service
fleetctl start nginx.service
The Dockerfile you provided runs with an error on my Docker host (without using fleet); maybe when fleet does detect that error it removes the container for you, while on a Docker host the container still exists in stopped state despite the error.
Here's the error:
$ docker build --force-rm -t so-26469566 .
$ docker run --name DATA_NGINX so-26469566
exec: "/usr/bin/true": stat /usr/bin/true: no such file or directory2014/10/20 16:59:54 Error response from daemon: Cannot start container 767562758b9f30097a5ed16b98fe818d9c9574bb82b1cfd502bc3403e97d5b0
e: exec: "/usr/bin/true": stat /usr/bin/true: no such file or directory
make: *** [run] Error 1
Try the following CMD statement in your Dockerfile and see if it does changes the behavior of fleet.
CMD ["/bin/true"]
If your nginx.service start and run runing the docker run command directly in CoreOS server, mayby your problem is not in docker image but nginx.service.
Notice you configure your service with:
ExecStartPre=-/usr/bin/docker kill NGINX
ExecStartPre=-/usr/bin/docker rm NGINX
trying to kill and remove NGINX container but you run the container with --rm which remove it automatically when it fail or exit.
Maybe your service not start because its ExecStartPre is failing
Try include
Requires=nginxData.service
After=nginxData.service
too.

Resources