how to use systemctl cmd in docker container? - docker

in my case,i want use systemctl command in docker, how do I do it?
I have modified /var/lib/docker/containers/60f58068d1514a5ba36bf4fa6ce1c14807102c2fa1a961d56aa1a47f38157a96/config.v2.json cmd and path option to /sbin/init but it didn't work

Related

Podman (docker) container

Does someone know how to start and stop services inside the container?
E.g. wish to stop sshd service INSIDE the Ubuntu container, but not in the whole host (RHEL).
you need to get a root shell in the container with docker exec -it <container_name> /bin/bash then you can run the command that you want
Log in to the container using the below command
docker exec -it <container_id> /bin/bash
Using any of the below commands can stop the ssh service
$ sudo /etc/init.d/ssh stop or $ sudo service ssh stop or $ sudo systemctl stop ssh

Docker System has not been booted with systemd as init system

I have an Ubuntu 18.04 image runing on my docker container. I login into it and installed Openresty. also installed systemd. When I use command systemctl I get this error:
System has not been booted with systemd as init system (PID 1). Can't operate.
How can I fix it?
If I understand the OP, he is trying to run systemctl within the container. This does not work because systemd is not running within the container to begin with. It cannot be done in unprivileged containers. There is another question here in SO about why he should not run systemd within a container.
I quickly googled and found this 2014 page about using systemd within a container in docker, where there is a short explanation. The fix is to use a privileged container (running docker run --privileged ...), which is arguably a bad idea but might suit the OP. There is a 2019 update of that last article, and the bottomline is they developed their own container engine (so no docker).
The obvious solution would be to have a single service, so no need for systemd, although that might not be possible in the OP's case.
In summary, possible solutions:
not to use systemd
use a privileged container
not to use docker
In your terminal, you can type:
$ sudo dockerd
and the magic is happen
So, Open other terminal and try it
$ docker ps -a
If you still have a problem with permission, run:
$ sudo usermod -aG docker your-user
Did you try to use: sudo /etc/init.d/docker start instead of systemd ?
I have a similar problem and it solves it.
You need to start your container by this command to enable systemd.
docker run -itd --privileged docker pull ubuntu:18.04 /usr/sbin/init
After toying with Systemd myself and bumping into this I found a good solution to work around this in Docker.
You can setup a cronjob to run on container reboot.
Dockerfile.yml:
COPY startup.sh /home/$USERNAME
WORKDIR /home/$USERNAME
RUN chmod +x startup.sh
RUN runuser -u $USERNAME -- echo "#reboot /home/$USERNAME/startup.sh" >> cronjobs
RUN runuser -u $USERNAME -- crontab cronjobs
RUN runuser -u $USERNAME -- rm cronjobs
https://askubuntu.com/questions/814/how-to-run-scripts-on-start-up#816
To complement #javier-gonzalez answer, if you're following running systemd within container AND getting the error bash: /usr/sbin/init: No such file or directory when trying to run the container, you can use /lib/systemd/systemd as ENTRYPOINT in your Dockerfile instead since /usr/sbin/init since it is just a symlink to the same thing.
FROM ubuntu:<anyversion>
ENTRYPOINT ["/lib/systemd/systemd"]
You may have forgotten to start docker before using it
sudo service docker start

Is there a way to kill a docker container that hangs for "stop" and "kill"?

I have a docker container that when I call:
docker container stop wcfservicesample
or
docker container kill wcfservicesample
It just "hangs". (Meaning the powershell command never returns.)
Is there some way to kill a container that is more forceful than "kill"?
Maybe itis too late, but What helped me under ubuntu linux was:
sudo systemctl restart docker && docker rm -f <<container_id>> ...
I dont know why it do work only on docker restart/

How to keep ubuntu image running?

I try to start container with following command
sudo docker run ubuntu
after that I checked with
sudo docker ps -a
found the container exited already
why does it exit?
How could I keep it running in backgroud without specifying -it and attach to it on demanding?
Solved by myself, a elegant way to keep the container running and waiting for further "attach" or "exec" is the following (to keep the STDIN open by -i option)
docker run -i -d ubuntu
You need to start an application with the docker run command that won't exit.
Example:
docker run -d --entrypoint '/bin/bash cat' ubuntu
You can add the tail command while running the container.
docker run -d ubuntu tail -f /dev/null
the correct syntax is
(from docker run --help)
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
you have forgotten to specify a command.
You should have a look at the docker hub
https://registry.hub.docker.com/
For example for nginx, if you look at
https://hub.docker.com/_/nginx/
you will find
docker run --name some-nginx -v /some/nginx.conf:/etc/nginx/nginx.conf:ro -d nginx
If you look at
https://github.com/nginxinc/docker-nginx/blob/7f3ef0927ec619d20181e677c97f991df0d7d446/Dockerfile
you will notice that the last line of the Dockerfile is
CMD ["nginx", "-g", "daemon off;"]
This means that when you launch the docker image nginx, the implicit action is to start nginx.
If you want the container to not exist, you have to use the -d argument
So it look like this:
docker run -d ubuntu

What is the difference b/w "service docker start" and "docker -d"?

I'm new to docker, and want to restart docker daemon. I want to add the OPTS to start docker like:
docker --registry-mirror=http://<my-docker-mirror-host> -d
I want to know what is they difference? Does they start the same thing?
By the way, I just use above command in my boot2docker, it did't work at all.
if you use service docker start then it will start docker as service with docker's upstart configuration file, e.g. /etc/default/docker for ubuntu and /etc/sysconfig/docker for centos.
if you use docker -d it will run docker in daemon mode.
if you want define your own registry-mirror for docker, you can do this:
ubuntu
$ echo "DOCKER_OPTS=\"\$DOCKER_OPTS --registry-mirror=http://<my-docker-mirror-host>\"" | sudo tee -a /etc/default/docker
$ sudo service docker restart
centos
sudo sed -i 's|other_args=|other_args=--registry-mirror=http://<my-docker-mirror-host> |g' /etc/sysconfig/docker
sudo sed -i "s|OPTIONS='|OPTIONS='--registry-mirror=http://<my-docker-mirror-host> |g" /etc/sysconfig/docker
sudo service docker restart
mac
boot2docker up
boot2docker ssh "echo $'EXTRA_ARGS=\"--registry-mirror=http://<my-docker-mirror-host>\"' | sudo tee -a /var/lib/boot2docker/profile && sudo /etc/init.d/docker restart”
then your docker service with run with your own registry mirror.
To answer your questions (which are valid for debian/ubuntu, I don't have tinylinux handy to test which is used by boot2docker):
service docker start will run a startup script in /etc/init.d/docker
docker -d is the manual version of the previous script, useful when you want to run docker in debug mode. I suspect the example you gave will not do the same thing, because there are more options specified in the service script.
if you want to add more docker command options, edit the /etc/default/docker file
Update after OP's comments:
To add your new switch, you need to specifically edit the variable (which maybe exported) DOCKER_OPTS and add your option to the end of the existing options.
My /etc/default/docker options are:
export DOCKER_OPTS="--tlsverify --tlscacert=/etc/docker/ca.pem
--tlskey=/etc/docker/server-key.pem --tlscert=/etc/docker/server.pem --label=provider=XXXX
--host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2376"
To add the registry-mirror I would edit the DOCKER_OPTS to look like this
export DOCKER_OPTS="--tlsverify --tlscacert=/etc/docker/ca.pem
--tlskey=/etc/docker/server-key.pem --tlscert=/etc/docker/server.pem --label=provider=XXXX
--host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2376
--registry- mirror=192.168.59.103:5555"

Resources