docker service create image command like `docker run` - docker

Does anyone use docker service create with command like docker run -it ubuntu bash ?
e.g: docker service create --name test redis bash.
I want to run a temp container for debugging on production environment in swarm mode with the same network.
This is my result:
user#ubuntu ~/$ docker service ps test
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR
bmig9qd9tihw7q1kff2bn42ab test.1 redis ubuntu Ready Ready 3 seconds ago
9t4za9r4gb03az3af13akpklv \_ test.1 redis ubuntu Shutdown Complete 4 seconds ago
1php2be7ilp7psulwp31b3ib4 \_ test.1 redis ubuntu Shutdown Complete 10 seconds ago
drwyjdggd13n1emb66oqchmuv \_ test.1 redis ubuntu Shutdown Complete 15 seconds ago
b1zb5ja058ni0b4c0etcnsltk \_ test.1 redis ubuntu Shutdown Complete 21 seconds ago

When you create a service that startup Bash it will imediately stop because it is in detached mode.
You can have the same behavior if you run docker run -d ubuntu bash

Related

I don't know how to do the same thing pull, create and start instead of docker run -dti

Using an image from https://hub.docker.com/_/php.
Create and launch a hogehoge-php container with docker run -dti
% docker run -dti --name hogehoge-php php
Then I can see that the hogehoge-php container is up and running.
confirmed:
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c9e7ff1a952 php "docker-php-entrypoi..." 6 seconds ago Up 5 seconds ago hogehoge-php
try with pull, create and start
I read that the docker run is a command that performs docker pull, docker create, and docker start all at once. So I decided to try it out as follows (I didn't do the docker pull because I already have a local php image).
% docker create --name foofoo-php php
c7687c088dda9b71d9380e9ca472afa436ac63785c1d2c195b8a08147f8adec9
% docker start foofoo-php
foofoo-php
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7687c088dda php "docker-php-entrypoi..." 16 seconds ago Exited (0) 3 seconds ago foofoo-php
When I tried as above, I got Exited (0) 3 seconds ago, and the foofoo-php container created by docker create doesn't stand up all the time.
The foofoo-php container made by docker create doesn't stand up(options not available).
% docker start -dti foofoo-php
unknown shorthand flag: 'd' in -dti
See 'docker start --help'.
The i option is available, and I was able to run it, but it doesn't leave the docker running.
% docker start -i foofoo-php
Interactive shell
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c7687c088dda php "docker-php-entrypoi..." 3 minutes ago Exited (0) 5 seconds ago foofoo-php
How can I create and start instead of run -dti?
By the way, I tried docker run without -dti
By the way, I tried docker run without -dti.
% docker run --name barbarbar-php php
Interactive shell
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6215c71a13a4 php "docker-php-entrypoi..." 15 seconds ago Exited (0) 15 seconds ago barbar-php
The container was created, but it's not standing up. I thought I should try docker start:
% docker start barbar-php
barbar-php
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6215c71a13a4 php "docker-php-entrypoi…" 33 seconds ago Exited (0) 3 seconds ago barbar-php
but the container doesn't start up.
On the other hand, stop and start a container created by docker run -dti
On the other hand, try to stop and start a container created by docker run -dti, it running up after start.
% docker stop hogehoge-php
hogehoge-php
% docker start hogehoge-php
hogehoge-php
% docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6215c71a13a4 php "docker-php-entrypoi…" 4 minutes ago Exited (0) About a minute ago barbar-php
c7687c088dda php "docker-php-entrypoi…" 11 minutes ago Exited (0) 8 minutes ago foofoo-php
2c9e7ff1a952 php "docker-php-entrypoi…" 14 minutes ago Up 2 seconds hogehoge-php <----- Here
Behavior Summary
docker run
with dit option
up after a run
up after start
without dit option
not up after a run
not up after a start
docker create
not up after a run
What you are missing is a docker create -t to allocate a pseudo-TTY for you container.
Since the entrypoint for the image you are running is a php shell running at the foreground, the container, when started, figures out that it doesn't have a tty to attach to an exit with 0.
docker pull php
docker create -t --name hogehoge-php php
docker start hogehoge-php

container fails to start in swarm mode

I tried to start the docker service using swarm mode, but I am not able to connect to port 8080
~ $ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
3tdzofpn6qo5 vigilant_wescoff replicated 0/1 shantanu/abc:latest *:8080->8080/tcp
~ $ docker service ps 3tdzofpn6qo5
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
iki0t3x1oqmz vigilant_wescoff.1 shantanuo/abc:latest ip-172-31-4-142.ap-south-1.compute.internal Ready Ready 1 second ago
z88nyixy7u10 \_ vigilant_wescoff.1 shantanu/abc:latest ip-172-31-4-142.ap-south-1.compute.internal Shutdown Complete 5 minutes ago
zf4fac2a4dlh \_ vigilant_wescoff.1 shantanu/abc:latest ip-172-31-4-142.ap-south-1.compute.internal Shutdown Complete 11 minutes ago
zzqj4lldmxox \_ vigilant_wescoff.1 shantanu/abc:latest ip-172-31-6-134.ap-south-1.compute.internal Shutdown Complete 14 minutes ago
z8eknet7oirq \_ vigilant_wescoff.1 shantanu/abc:latest ip-172-31-20-50.ap-south-1.compute.internal Shutdown Complete 17 minutes ago
I used docker for aws (community version)
https://docs.docker.com/docker-for-aws/#docker-community-edition-ce-for-aws
But I guess that should not make any difference and the container should work. I have tested it using docker run command it works as expected.
In case of swarm mode, how do I know what exactly is going wrong?
You can use docker events on managers to see what the orchestrator is doing (but you can't see the history).
You can use docker events on workers to see what containers/networks/volumes etc. are doing (but you can't see the history).
You can look at the docker service logs to see current and past container logs
You can use docker container inspect to see the exit (error) code of the stopped containers in that service task list.

Ensuring Docker container will start automatically when host starts

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.

How do I remove old service images after an update?

I'm toying around with Docker Swarm. I've deployed a service an performed a couple of updates to see how it works. I'm observing that docker is keeping the old images around for the service.
How do I clean those up?
root#picday-manager:~# docker service ps picday
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
bk6pw0t8vw4r picday.1 ischyrus/picday:latest picday-manager Running Running 6 minutes ago
ocbcjpnc71e6 \_ picday.1 ischyrus/picday:latest picday-manager Shutdown Shutdown 6 minutes ago
lcqqhbp8d99q \_ picday.1 ischyrus/picday:latest picday-manager Shutdown Shutdown 11 minutes ago
db7mco0d4uk0 picday.2 ischyrus/picday:latest picday-manager Running Running 6 minutes ago
z43p0lcdicx4 \_ picday.2 ischyrus/picday:latest picday-manager Shutdown Shutdown 6 minutes ago
These are containers, not images. In docker, there's a rather significant difference between the two (images are the definition used to create a container). Inside of a swarm service, they are referred to as tasks. To adjust how many docker keeps by default, you can change the global threshold with:
docker swarm update --task-history-limit 1
The default value for this is 5.
To remove individual containers, you can remove the container from the host where it's running with:
docker container ls -a | grep picday
docker container rm <container id>

How to restart multiple containers in a docker swarm

I currently have 8 containers across 4 different host machines in my docker setup.
ubuntu#ip-192-168-1-8:~$ docker service ls
ID NAME MODE REPLICAS IMAGE
yyyytxxxxx8 mycontainers global 8/8 myapplications:latest
Running a ps -a on the service yields the following.
ubuntu#ip-192-168-1-8:~$ docker service ps -a yy
NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
mycontainers1 myapplications:latest: ip-192-168-1-5 Running Running 3 hours ago
\_ mycontainers2 myapplications:latest: ip-192-168-1-6 Running Running 3 hours ago
\_ mycontainers3 myapplications:latest: ip-192-168-1-7 Running Running 3 hours ago
\_ mycontainers4 myapplications:latest: ip-192-168-1-8 Running Running 3 hours ago
\_ mycontainers1 myapplications:latest: ip-192-168-1-5 Running Running 3 hours ago
\_ mycontainers2 myapplications:latest: ip-192-168-1-6 Running Running 3 hours ago
\_ mycontainers3 myapplications:latest: ip-192-168-1-7 Running Running 3 hours ago
\_ mycontainers4 myapplications:latest: ip-192-168-1-8 Running Running 3 hours ago
My question is how can i execute a restart of all the containers using the service ID? I dont want to manually log into every node and execute a restart.
In the latest stable version of docker 1.12.x, it is possible to restart the container by updating the service configuration, but in the docker 1.13.0 which is released soon, even if the service setting is not changed, by specifying the --force flag, the container will be restarted. If you do not mind to use the 1.13.0 RC4 you can do it now.
$ docker service update --force mycontainers
Update: Docker 1.13.0 has been released.
https://github.com/docker/docker/releases/tag/v1.13.0
Pre-Docker 1.13, I found that scaling all services down to 0, waiting for shutdown, then scaling everything back up to the previous level works.
docker service scale mycontainers=0
# wait
docker service scale mycontainers=8
Updating the existing service, swarm will recreate all containers. For example, you can simply update a property of the service to archive restarting.

Resources