Mosquitto broker won't restart with Docker on Raspberry reboot - docker

I installed Mosquitto broker with Docker on Raspbian this way:
docker pull eclipse-mosquitto
docker run -d -p 1883:1883 -p 9001:9001 --name=mosquitto eclipse-mosquitto --restart=always
When I reboot the Raspberry, the container seems that is not running and I cannot connect to it. If I try to run it again I get:
docker: Error response from daemon: Conflict. The container name
"/mosquitto" is already in use by container
"3187ab53a3a2067b9d6ce0sa647a8d90cb52485f5540ca4eacad1c4e662ffa9d". You have
to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
So I need to remove it
docker rm -f mosquitto
and restart it again.
What I miss?

Docker Engine prevents two containers from having the same name.
So if you run twice the command like this:
docker run -d -p 1883:1883 -p 9001:9001 --name=mosquitto eclipse-mosquitto
docker stop mosquitto # simulates your reboot
docker run -d -p 1883:1883 -p 9001:9001 --name=mosquitto eclipse-mosquitto
Then the second attempt will fail, as you noticed.
Actually, I guess that you had put the option --restart=always in the wrong place. (More precisely, the arguments given after the image name are not seen as Docker CLI options, they are provided to the entrypoint: docker run [OPTIONS] image-name [ARGUMENTS])
Could you try this (and reboot)?
docker run -d -p 1883:1883 -p 9001:9001 --name=mosquitto --restart=always eclipse-mosquitto
Otherwise, you could just as well do:
docker start eclipse-mosquitto
after a docker stop or a reboot that wouldn't succeed in restarting the container.

Related

How to get access to the docker cmd from docker container?

I've tried to get access to docker command from a container I got an issue says that
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
the container created using this CMD line :
sudo docker run -p 8080:8080 -p 50000:50000 -v /var/run/docker.sock:/usr/lib/systemd/system/docker.socket --name jenkins-master -d jenkins
Hey Guys the issue was in the socket volume I mean in this section
-v /var/run/docker.sock:/usr/lib/systemd/system/docker.socket
should be change to
-v /var/run/docker.sock:/var/run/docker.sock

Docker Local Registry run from different port

I installed Docker Local Registry as below
docker pull registry
after
docker run -d -p 5001:5001 -v C:/localhub/registry:/var/lib/registry --restart=always --name hub.local registry
because of 5000 port using another application.
but i can't reach to
http://localhost:5001/v2/_catalog
The first part of the -p value is the host port and the second part is the port within the container.
This code runs the registry on port 5001
docker run -d -p 5001:5000 --name hub.local registry
If you want to change the port the registry listens on within the container, you must use this code
docker run -d -e REGISTRY_HTTP_ADDR=0.0.0.0:5001 -p 5001:5001 --name hub.local registry
'''
docker run -d -p 5001:5000 -v C:/localhub/registry:/var/lib/registry --restart=always --name hub.local registry
'''
Keep the internal port the same and change only your local port

Can we run docker inside a docker container which is running in a virtual-box of Ubuntu 18.04?

I want to run docker inside another docker container. My main container is running in a virtualbox of OS Ubuntu 18.04 which is there on my Windows 10. On trying to run it, it is showing me as:
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
How can I resolve this issue?
Yes, you can do this. Check for dind (docker in docker) on docker webpage how to achieve it: https://hub.docker.com/_/docker
Your error indicates that either dockerd in the top level container is not running or you didn't mount docker.sock on the dependent container to communicate with dockerd running on your top-level container.
I am running electric-flow in a docker container in my Ubuntu virtual-box using this docker command: docker run --name efserver --hostname=efserver -d -p 8080:8080 -p 9990:9990 -p 7800:7800 -p 7070:80 -p 443:443 -p 8443:8443 -p 8200:8200 -i -t ecdocker/eflow-ce. Inside this docker container, I want to install and run docker so that my CI/CD pipeline in electric-flow can access and use docker commands.
From your above description, ecdocker/eflow-ce is your CI/CD solution container, and you just want to use docker command in this container, then you did not need dind solution. You can just access to a container's host docker server.
Something like follows:
docker run --privileged --name efserver --hostname=efserver -d -p 8080:8080 -p 9990:9990 -p 7800:7800 -p 7070:80 -p 443:443 -p 8443:8443 -p 8200:8200 -v $(which docker):/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock -i -t ecdocker/eflow-ce
Compared to your old command:
Add --privileged
Add -v $(which docker):/usr/bin/docker, then you can use docker client in container.
Add -v /var/run/docker.sock:/var/run/docker.sock, then you can access host's docker daemon using client in container.

Docker: how to run apache service when container starts?

I don't use any docker file.
I run my container with:
docker run -it --restart always -p 80:80 v1 /bin/bash
Then I need to type in
service apache2 start
How to run the apache service automatically when docker container starts ?
I have no idea what v1 is, sorry. But if you check the official docs, you will see, that docker run has the following format:
$ docker run [OPTIONS] IMAGE[:TAG|#DIGEST] [COMMAND] [ARG...]
COMMAND is the command executed on start. The docs even suggest how to run nginx on start automatically:
docker run -d -p 80:80 my_image service nginx start
From all this you may derive, that you need to run something like
docker run -it --restart always -p 80:80 v1 service apache2 start

Docker expose port

Could I expose different docker container points to the same HTTP port on the host?
Example
docker container run --publish 80:80 -d -it --name wp wordpress
docker container run --publish 90:80 -d -it --name ci jenkins
docker container run --publish 100:80 -d -it --name gitlab gitlab/gitlab-ce
With that commands you are not using the same port at host. The nomenclature for -pis "hostPort:containerPort" so in that way you are mapping container's port 80 from all of them to your host at ports 80, 90 and 100. So no conflict at all.
Anyway, to answer to your question about possible conflicting. In first instance, your commands should be:
docker container run --publish 80:80 -d -it --name wp wordpress
docker container run --publish 80:80 -d -it --name ci jenkins
docker container run --publish 80:80 -d -it --name gitlab gitlab/gitlab-ce
In this way, you can do that commands but you'll probably get an error saying Bind for 0.0.0.0:80 failed: port is already allocated..
Anyway, in the hypothetical case of docker allowing that without an error...
The first one you map is which is going to work because on "docker run" command there are iptables commands for openning ports from container to host, and iptables rules work in "first matching is which works" style. So you'll have 3 iptables rules in this case but the one is going to work is the first.

Resources