Is there a way to start a Docker container automatically when the host starts? Before, I use the ‘—restart always’ parameter with docker run but it only works if Docker Engine is not killed.
As your comment, I think you have misunderstood about --restart always.
Once docker run --restart always container is run, the container is restarted every time the host is restarted even though you stop the container explicitly .
For example.
$ docker run --restart always --detach --name auto-start-redis redis
d04dfbd73eb9d2ba5beac41363aa5c45c0e034e08173daa6146c3c704e0cd1da
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d04dfbd73eb9 redis "docker-entrypoint..." 4 seconds ago Up 4 seconds 6379/tcp auto-start-redis
$ reboot
# After reboot-------------------------------
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d04dfbd73eb9 redis "docker-entrypoint..." About a minute ago Up 21 seconds 6379/tcp auto-start-redis
$ docker stop auto-start-redis
auto-start-redis
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d04dfbd73eb9 redis "docker-entrypoint..." 2 minutes ago Exited (0) 30 seconds ago auto-start-redis
$ reboot
# After reboot-------------------------------
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d04dfbd73eb9 redis "docker-entrypoint..." 3 minutes ago Up 12 seconds 6379/tcp auto-start-redis
However, of course, it is based upon a premise that docker-host is auto-started. docker-host in here means docker daemon process itself. Usually docker-host will auto-start by default but if it is not, you need to set it yourself.
I am not sure which OS you are using but when it comes to Ubuntu16, you can make it with systemctl command.
$ sudo systemctl enable docker
# To tell systemd to start services automatically at boot, you must enable.
If you use docker swarm, you can make global service with --mode global flag that ensures run on every node in docker swarm.
docker service create --mode global ...
If you don't use docker swarm, the best solution I think is to use init system of your system like systemd as #I.R.R said. You can make your own service file for systemd and specify the condition when the service starts like below.
[Unit]
Description=Your App
After=docker
Refer to this article by digital ocean.
Related
I am trying to run a container based on the debian:stretch image, but this does not work:
docker container run --detach debian:stretch
outputs:
7976eb7074289a741a2b183634345fc8519359cba4d543c03b0a6d4e5d7e0d53
And
docker ps -a
outputs:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7976eb707428 debian:stretch "bash" 3 seconds ago Exited (0) 2 seconds ago vigorous_lumiere
Whereas it works well with the latest nginx image:
docker run --detach nginx:latest
53ed18b5d1a7c72aa92bab0ca679269514db79f31a1d3759c2e25c7fdb1e82ff
docker ps -a
outputs:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
53ed18b5d1a7 nginx:latest "nginx -g 'daemon of…" 2 seconds ago Up 2 seconds 80/tcp admiring_hawking
7976eb707428 debian:stretch "bash" About a minute ago Exited (0) About a minute ago vigorous_lumiere
Why does the container based on the debian:stretch image that I am instantiating does not work?
Does this come from the debian image?
I am running Docker version 18.09.1, build 4c52b90 on Ubuntu 16.04 LTS
Your container literally doesn’t do anything: it starts a shell, but since it’s running as a background process and doesn’t have anything on its stdin, it immediately exits.
You should read the official Docker tutorial on building and running custom images. Generally you should work by building your application into a custom image, setting up that image’s default CMD to run your application, and using docker build and docker run (or a tool like Docker Compose) to run the assembly. There’s not much point in running a plain Linux distribution container.
(Also remember that it’s extremely routine to docker rm containers, and so anything you do in an interactive shell in a container is very likely to get lost.)
I use docker-compose to create a bunch of containers and link them together. For some of the container definitions, I might have restart: always as the restart policy.
Now I have a postgres container that respawns back to life if stopped.
$docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a8bb2b781630 postgres:latest "docker-entrypoint.s…" About an hour ago Up About an hour 5432/tcp dcat_postgres.1.z3pyl24kiq2n4clt0ua77nfx5
docker stop a8bb2b781630
a8bb2b781630
$ docker rm -f a8bb2b781630
a8bb2b781630
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
93fa7b72c2ca postgres:latest "docker-entrypoint.s…" 12 seconds ago Up 4 seconds 5432/tcp dcat_postgres.1.oucuo5zg3y9ws3p7jvlfztflb
Using docker-compose down in the dir that started the service doesn't work either.
$ docker-compose down
Removing dcat_postgres_1 ... done
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7ee7fb0e98cd postgres:latest "docker-entrypoint.s…" 13 seconds ago Up 5 seconds 5432/tcp dcat_postgres.1.jhv1q6vns1avakqjxdusmbb78
How can I kill a container and keep it from coming back to life?
EDIT: The container respawns even after restarting the Docker service.
Docker - 18.06.1-ce-mac73 (26764)
macOS High-Sierra, (10.13.6)
I figured it out. Turns out it was related to docker swarm. I had experimented with it at some point without fully understanding what it is and what it does and apparently it just stayed there.
All I had to do was:
docker swarm leave --force
and it worked like a head-shot to an actual zombie.
Can you try an option like moby/moby issue 10032:
docker stop $(docker ps -a -q) &
docker update --restart=no $(docker ps -a -q) &
systemctl restart docker
(this assume here you have only one running container: the one you cannot prevent to start)
A docker rm -f should be enough though, unless you are using docker with a provision tool like Puppet.
As it turned out, another process (other than docker itself) was responsible for the container to restart (here docker swarm)
Update 2020/2021: For multiple containers, possibly without having to restart the docker daemon
docker ps -a --format="{{.ID}}" | \
xargs docker update --restart=no | \
xargs docker stop
Check if you need, as in the issue, remove the images as well ( | xargs docker rmi $(docker images -qa) --force)
(sorry using the term "kill" with quotes is not about docker-compose kill, is about "UNIX ps kill" after what the process really go out of the "UNIX ps list")
Usual docker run can be "killed" by usual docker stop, because after stop I not see the container at docker ps -a... If it is correct, there are a semantic bug with docker-compose because I can't "kill" the containers, they stay at docker ps.
After my simple docker-compose up (without &) I do ^C and the containers stay there at docker ps -a... Impossible to kill by docker compose stop.
NOTE: when I use ordinary docker run and after it docker stop there are nothing at docker ps -a, so I can say "I killed it".
Usual docker run can be "killed" by usual docker stop, because after stop I not see the container at docker ps.
No. docker stop just stops a running container, it doesn' t remove the container. This happens only in case you've used docker run --rm .... This --rm option means that when the container is stopped, it will be removed/deleted.
Docker
docker run ... creates and runs a container
docker stop ... stops a running container
docker start ... starts a stopped container
docker rm ... removes a stopped container
Docker Compose
docker-compose up creates and runs a collection of containers
docker-compose stop stops the containers
docker-compose start starts the containers
docker-compose down stops and removes the containers
Be careful...
As it discussed in the comments section, by using docker-compose down other things can also take place regarding volumes, networks. Keep in mind that you might lose data (if your container is a database for example) and make sure you have saved them or you are somehow able to create them again.
Check out running containers:
docker ps
Example output:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e86521d81a96 app_php "docker-php-entrypoi…" 2 hours ago Up About an hour 0.0.0.0:8080->80/tcp app_php_1
7a30681b6255 mysql:5.6 "docker-entrypoint.s…" 3 hours ago Up About an hour 0.0.0.0:3306->3306/tcp app_db_1
21aa3eef5f42 phpmyadmin/phpmyadmin "/run.sh supervisord…" 4 hours ago Up About an hour 9000/tcp, 0.0.0.0:8081->80/tcp app_phpmyadmin_1
9afc52b3f82f mailhog/mailhog "MailHog" 4 hours ago Up About an hour 1025/tcp, 0.0.0.0:8082->8025/tcp app_mailhog_1
then stop one by the container id:
docker kill part_of_the_id/name
For instance:
docker kill e86 or docker kill app_php_1
Docker-compose is just a script to help you manage one or multiple containers running in a group and is absolutely not required to manage your containers.
To remove the container completely you have to remove the container docker rm container_id_or_name
To stop all running containers:
docker stop $(docker ps -q)
You can use docker rm <container-name> to do that. This command will stop and remove service container. Anonymous volumes attached to the container will not be removed.
Whenever I start Docker on my Mac, there are four containers that come along for the ride and start up automatically.
$ docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
//starts docker, makes tea
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d78483fa7f27 magento/magento2devbox-web:latest "/usr/local/bin/en..." 4 weeks ago Up 5 minutes 5000/tcp, 9000/tcp, 44100/tcp, 0.0.0.0:32774->22/tcp, 0.0.0.0:32773->80/tcp magento2devbox_web_03b003abaeb68eadd315c2e4763d0326
01f62a720e40 mysql:5.6 "docker-entrypoint..." 4 weeks ago Up 5 minutes 0.0.0.0:32772->3306/tcp magento2devbox_db_03b003abaeb68eadd315c2e4763d0326
005e0708d8f7 magento/magento2devbox-web:latest "/usr/local/bin/en..." 6 months ago Up 5 minutes 5000/tcp, 9000/tcp, 44100/tcp, 0.0.0.0:32770->22/tcp, 0.0.0.0:32769->80/tcp magento2devbox_web_258e08743d8e54a4b3e6acfd3b2d7159
00b38cf0fdb9 mysql:5.6 "docker-entrypoint..." 6 months ago Up 5 minutes 0.0.0.0:32768->3306/tcp magento2devbox_db_258e08743d8e54a4b3e6acfd3b2d715
How do I tell (Docker? The containers?) that I don't need these four containers to start up automatically anymore?
Check the restart policy of those containers using docker inspect NAME|ID.
If it is always or on-failure, then you have the explanation.
To change the restart policy, use docker update --restart <new policy>.
These two commands set the restart policy to no for all running containers and then kill them all (make sure you understand this before doing it):
docker update `docker ps -q` --restart no
docker kill `docker ps -q`
After that, restart your docker daemon and you should see nothing from docker ps.
Use docker stop [container_id] to stop each running container. They should not start back up the next time you restart the docker daemon.
In the future, when you start the containers, make sure that when you call docker run you're not passing the --restart flag. If you're using docker compose make sure to omit the 'restart' option from your docker-compose.yml file.
From the documentation:
To configure the restart policy for a container, use the --restart flag when using the docker run command. The value of the --restart flag can be any of the following:
no - Do not automatically restart the container. (the default)
on-failure - Restart the container if it exits due to an error, which manifests as a non-zero exit code.
unless-stopped - Restart the container unless it is explicitly stopped or Docker itself is stopped or restarted.
always - Always restart the container if it stops.
The following example starts a Redis container and configures it to always restart unless it is explicitly stopped or Docker is restarted.
$ docker run -dit --restart unless-stopped redis
https://docs.docker.com/engine/admin/start-containers-automatically/#use-a-restart-policy
I created the container with the following command:
docker run -d -p 52022:22 basickarl/docker-git-test
Here are the commands:
root#basickarl:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
root#basickarl:~# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4ac54468455 basickarl/docker-git-test:latest "/bin/bash" 7 minutes ago Exited (0) 26 seconds ago adoring_lumiere
22d7c5d83871 basickarl/docker-git-test:latest "/bin/bash" 2 hours ago Exited (127) About an hour ago thirsty_wright
root#basickarl:~# docker attach --sig-proxy=false e4
FATA[0000] You cannot attach to a stopped container, start it first
root#basickarl:~# docker start e4
e4
root#basickarl:~# docker attach --sig-proxy=false e4
FATA[0000] You cannot attach to a stopped container, start it first
root#basickarl:~#
Not much to say really, I'm expecting the container to start and stay upp. Here are logs:
root#basickarl:~# docker logs e4
root#basickarl:~#
You are trying to run bash, an interactive shell that requires a tty in order to operate. It doesn't really make sense to run this in "detached" mode with -d, but you can do this by adding -it to the command line, which ensures that the container has a valid tty associated with it and that stdin remains connected:
docker run -it -d -p 52022:22 basickarl/docker-git-test
You would more commonly run some sort of long-lived non-interactive process (like sshd, or a web server, or a database server, or a process manager like systemd or supervisor) when starting detached containers.
If you are trying to run a service like sshd, you cannot simply run service ssh start. This will -- depending on the distribution you're running inside your container -- do one of two things:
It will try to contact a process manager like systemd or upstart to start the service. Because there is no service manager running, this will fail.
It will actually start sshd, but it will be started in the background. This means that (a) the service sshd start command exits, which means that (b) Docker considers your container to have failed, so it cleans everything up.
If you want to run just ssh in a container, consider an example like this.
If you want to run sshd and other processes inside the container, you will need to investigate some sort of process supervisor.
What I need is to use Docker with MariaDb on different port /3301/ on my Ubuntu machine because I already had MySql installed and running on 3306.
To do this after half day searching did it using:
docker run -it -d -p 3301:3306 -v ~/mdbdata/mariaDb:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root --name mariaDb mariadb
This pulls the image with latest MariaDb, creates container called mariaDb, and run mysql on port 3301. All data of which is located in home directory in /mdbdata/mariaDb.
To login in mysql after that can use:
mysql -u root -proot -h 127.0.0.1 -P3301
Used sources are:
The answer of Iarks in this article /using -it -d was the key :) /
how-to-install-and-use-docker-on-ubuntu-16-04
installing-and-using-mariadb-via-docker
mariadb-and-docker-use-cases-part-1
Good luck all!