systemd

core@core-01 ~ $ ls -l /etc/systemd/system
total 24
-rw-r–r–. 1 root root 149 Mar 31 09:49 docker-tcp.socket
drwxr-xr-x. 2 root root 4096 Mar 31 09:49 flanneld.service.d
drwxr-xr-x. 2 root root 4096 Mar 31 09:49 sockets.target.wants

core@core-01 ~ $ cat /etc/systemd/system/docker-tcp.socket
[Unit]
Description=Docker Socket for the API

[Socket]
ListenStream=2375
Service=docker.service
BindIPv6Only=both

[Install]
WantedBy=sockets.target
———————————–

core@core-01 ~ $ sudo vim /etc/systemd/system/hello-world.service
[Unit]
Description=Hello World Service
After=docker.service
Requires=docker.serivce

[Service]
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill hello-world
ExecStartPre=-/usr/bin/docker rm helloworld
ExecStartPre=/usr/bin/docker pull busybox
ExecStart=/usr/bin/docker run –name hello-world busybox /bin/sh -c “while true;do echo Hello world;sleep 1;done”
ExecStop=/usr/bin/docker rm -f hello-world

[Install]
WantedBy=multi-user.target

core@core-01 ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

core@core-01 ~ $ sudo systemctl enable hello-world.service
Created symlink /etc/systemd/system/multi-user.target.wants/hello-world.service → /etc/systemd/system/hello-world.service.

core@core-01 ~ $ sudo systemctl start hello-world.service

core@core-01 ~ $ systemctl status hello-world.service
● hello-world.service – Hello World Service
Loaded: loaded (/etc/systemd/system/hello-world.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2017-03-31 11:23:31 UTC; 19s ago
Process: 2037 ExecStartPre=/usr/bin/docker pull busybox (code=exited, status=0/SUCCESS)
Process: 2031 ExecStartPre=/usr/bin/docker rm helloworld (code=exited, status=1/FAILURE)
Process: 1922 ExecStartPre=/usr/bin/docker kill hello-world (code=exited, status=1/FAILURE)
Main PID: 2050 (docker)
Tasks: 5
Memory: 15.7M
CPU: 111ms
CGroup: /system.slice/hello-world.service
└─2050 /usr/bin/docker run –name hello-world busybox /bin/sh -c while true; do echo Hello world;sleep 1; done

Mar 31 11:23:41 core-01 docker[2050]: Hello world
Mar 31 11:23:42 core-01 docker[2050]: Hello world
Mar 31 11:23:43 core-01 docker[2050]: Hello world
Mar 31 11:23:44 core-01 docker[2050]: Hello world
Mar 31 11:23:45 core-01 docker[2050]: Hello world
Mar 31 11:23:46 core-01 docker[2050]: Hello world
Mar 31 11:23:47 core-01 docker[2050]: Hello world
Mar 31 11:23:48 core-01 docker[2050]: Hello world
Mar 31 11:23:49 core-01 docker[2050]: Hello world
Mar 31 11:23:50 core-01 docker[2050]: Hello world

core@core-01 ~ $ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:2f:c5:52 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic eth0
valid_lft 84009sec preferred_lft 84009sec
inet6 fe80::a00:27ff:fe2f:c552/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:09:ab:12 brd ff:ff:ff:ff:ff:ff
inet 172.17.8.101/24 brd 172.17.8.255 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe09:ab12/64 scope link
valid_lft forever preferred_lft forever
4: flannel0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1472 qdisc fq_codel state UNKNOWN group default qlen 500
link/none
inet 10.1.13.0/16 scope global flannel0
valid_lft forever preferred_lft forever
inet6 fe80::78d5:b41b:5d25:a7a1/64 scope link flags 800
valid_lft forever preferred_lft forever
5: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
link/ether 02:42:85:6a:2c:e2 brd ff:ff:ff:ff:ff:ff
inet 10.1.13.1/24 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:85ff:fe6a:2ce2/64 scope link
valid_lft forever preferred_lft forever
core@core-01 ~ $ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 00f017a8c2a6 3 weeks ago 1.11 MB

core@core-01 ~ $ sudo systemctl start hello-world.service

core@core-01 ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3fc6d59b94fc busybox “/bin/sh -c ‘while tr” 3 seconds ago Up 2 seconds hello-world

core@core-01 ~ $ journalctl -f -u hello-world.service
— Logs begin at Fri 2017-03-31 09:49:37 UTC. —
Mar 31 11:30:27 core-01 docker[2432]: Hello world
Mar 31 11:30:28 core-01 docker[2432]: Hello world
Mar 31 11:30:29 core-01 docker[2432]: Hello world
Mar 31 11:30:30 core-01 docker[2432]: Hello world
Mar 31 11:30:31 core-01 docker[2432]: Hello world
Mar 31 11:30:32 core-01 docker[2432]: Hello world
Mar 31 11:30:33 core-01 docker[2432]: Hello world
Mar 31 11:30:34 core-01 docker[2432]: Hello world
Mar 31 11:30:35 core-01 docker[2432]: Hello world

core@core-01 ~ $ sudo systemctl stop hello-world
core@core-01 ~ $ sudo systemctl status hello-world
● hello-world.service – Hello World Service
Loaded: loaded (/etc/systemd/system/hello-world.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2017-03-31 11:32:01 UTC; 15s ago
Process: 2790 ExecStop=/usr/bin/docker rm -f hello-world (code=exited, status=0/SUCCESS)
Process: 2432 ExecStart=/usr/bin/docker run –name hello-world busybox /bin/sh -c while true; do echo Hello world;sleep 1; done (code=exited,
Process: 2427 ExecStartPre=/usr/bin/docker pull busybox (code=exited, status=0/SUCCESS)
Process: 2423 ExecStartPre=/usr/bin/docker rm helloworld (code=exited, status=1/FAILURE)
Process: 2407 ExecStartPre=/usr/bin/docker kill hello-world (code=exited, status=1/FAILURE)
Main PID: 2432 (code=exited, status=137)

Mar 31 11:31:58 core-01 docker[2432]: Hello world
Mar 31 11:31:59 core-01 docker[2432]: Hello world
Mar 31 11:32:00 core-01 docker[2432]: Hello world
Mar 31 11:32:01 core-01 docker[2432]: Hello world
Mar 31 11:32:01 core-01 systemd[1]: Stopping Hello World Service…
Mar 31 11:32:01 core-01 systemd[1]: hello-world.service: Main process exited, code=exited, status=137/n/a
Mar 31 11:32:01 core-01 docker[2790]: hello-world
Mar 31 11:32:01 core-01 systemd[1]: Stopped Hello World Service.
Mar 31 11:32:01 core-01 systemd[1]: hello-world.service: Unit entered failed state.
Mar 31 11:32:01 core-01 systemd[1]: hello-world.service: Failed with result ‘exit-code’.

####################################################

core@core-01 ~ $ sudo vim /etc/systemd/system/helloworld2@.service

[Unit]
Description=%n Service
After=docker.service
Requires=docker.serivce

[Service]
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill %p-%i
ExecStartPre=-/usr/bin/docker rm %p-%i
ExecStartPre=/usr/bin/docker pull busybox
ExecStart=/usr/bin/docker run –name %p-%i busybox /bin/sh -c “while true; do echo Hello world from %p-%i running on %H;sleep 1; done”
ExecStop=/usr/bin/docker rm -f %p-%i

[Install]
WantedBy=multi-user.target
core@core-01 ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5f61eed0a272 busybox “/bin/sh -c ‘while tr” 10 minutes ago Up 10 minutes hello-world
core@core-01 ~ $ sudo systemctl start helloworld2@.service

core@core-01 ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6fc5c9f97a28 busybox “/bin/sh -c ‘while tr” 3 seconds ago Up 2 seconds helloworld2-1
a565b99f6d32 busybox “/bin/sh -c ‘while tr” About a minute ago Up About a minute hello-world

core@core-01 ~ $ journalctl -f -u helloworld2@1
— Logs begin at Fri 2017-03-31 09:49:37 UTC. —
Mar 31 12:17:18 core-01 docker[4410]: Hello world from helloworld2-1 running on core-01
Mar 31 12:17:19 core-01 docker[4410]: Hello world from helloworld2-1 running on core-01
Mar 31 12:17:20 core-01 docker[4410]: Hello world from helloworld2-1 running on core-01
Mar 31 12:17:21 core-01 docker[4410]: Hello world from helloworld2-1 running on core-01
Mar 31 12:17:22 core-01 docker[4410]: Hello world from helloworld2-1 running on core-01

core@core-01 ~ $ journalctl -f -u helloworld
— Logs begin at Fri 2017-03-31 09:49:37 UTC. —
Mar 31 12:18:02 core-01 docker[4244]: Hello world
Mar 31 12:18:03 core-01 docker[4244]: Hello world
Mar 31 12:18:04 core-01 docker[4244]: Hello world

core@core-01 ~ $ sudo systemctl start helloworld2@{2..4}.service

core@core-01 ~ $ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
737f31ffe3d9 busybox “/bin/sh -c ‘while tr” 10 seconds ago Up 9 seconds helloworld2-4
b99e31cf0e42 busybox “/bin/sh -c ‘while tr” 10 seconds ago Up 9 seconds helloworld2-2
cd246d74dd51 busybox “/bin/sh -c ‘while tr” 10 seconds ago Up 9 seconds helloworld2-3
6fc5c9f97a28 busybox “/bin/sh -c ‘while tr” 3 minutes ago Up 3 minutes helloworld2-1
a565b99f6d32 busybox “/bin/sh -c ‘while tr” 5 minutes ago Up 5 minutes hello-world

core@core-01 ~ $ docker info
Containers: 5
Running: 5
Paused: 0
Stopped: 0
Images: 1
Server Version: 1.12.6
Storage Driver: overlay
Backing Filesystem: extfs
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: null host bridge overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: seccomp selinux
Kernel Version: 4.9.9-coreos-r1
Operating System: Container Linux by CoreOS 1298.6.0 (Ladybug)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 732.9 MiB
Name: core-01
ID: OLW5:SKV2:SNHS:EHMT:JM55:ZHZR:2UXN:7LB7:4L2N:DJPC:HORK:UNRV
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Insecure Registries:
127.0.0.0/8

core@core-01 ~ $ docker –version
Docker version 1.12.6, build d5236f0
core@core-01 ~ $ docker run -it busybox /bin/sh
/ # ping google.com
PING google.com (172.217.5.78): 56 data bytes
64 bytes from 172.217.5.78: seq=0 ttl=61 time=215.573 ms
64 bytes from 172.217.5.78: seq=1 ttl=61 time=209.698 ms
^C
— google.com ping statistics —
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 209.698/212.635/215.573 ms
/ #

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s