-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdemoContainerStates
executable file
·149 lines (102 loc) · 3.62 KB
/
demoContainerStates
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env bash
# This material is free software for demonstration purposes only.
# Copyright (c) 2018 Alec Clews
#
# Use of this source code is governed by an MIT license.
# See the project's LICENSE file for more information.
# Show the various states a container can be in.
# NOTE: The comands used below to start containers are slightly
# different to the examples given in the slides as a scripted
# process works a bit differently (instead of having two terminals
# open)
# Thanks to @vissree (https://github.com/vissree/) for some great ideas to improve this script
echo
echo This script will demostrate some of the lifecycle states a container can have
echo and some of the commands you can use to manage containers. Please make sure to
echo read the Docker manuals and the many tutorials for detailed and complete information.
docker container rm -f myAlpine > /dev/null 2>&1
echo
read -p 'Start a container ("docker container run -d --name myAlpine alpine")'
echo
echo '(there is some shell magic used so that the container prints digits every 5 seconds -- look at the script for details)'
docker container run -d --name myAlpine alpine /bin/sh -c 'while echo $(( m += 1 )); do sleep 5; done'
echo
read -p "Use the command \"docker container ls\" to see some basic information about all containers"
echo
docker container ls
echo
echo 'Look at the detailed state of our container called "myAlpine" with the command'
echo
read -p ' docker container inspect myAlpine'
echo
docker container inspect myAlpine
echo
read -p 'and now more usefully run "docker container inspect myAlpine|less"'
echo
docker container inspect myAlpine |less
echo
echo to make life a bit easier we can just extract the container status with the command
echo
echo " docker container inspect --format {{.State.Status}} myAlpine"
echo
echo "which displays"
echo
docker container inspect --format {{.State.Status}} myAlpine
echo
echo "now let's stop the container with the stop command (will take some time to kill the blocked process)"
echo
read -p " docker container stop myAlpine"
echo
docker container stop myAlpine
echo
echo the command \"docker container ls\" shows
echo
docker container ls
echo
echo the status from the inspect command is
echo
docker container inspect --format {{.State.Status}} myAlpine
echo
echo so why didn\'t \"docker container ls\" tell us that?
echo
read -p "try \"docker container ls -a\" instead"
echo
docker container ls -a
echo
echo We can start a \"exited\" container
echo
read -p "with the command \"docker container start myAlpine\""
echo
docker container start myAlpine
echo
read -p "and now the status is"
echo
docker container inspect --format {{.State.Status}} myAlpine
echo
echo Now the container is running again we can attach our terminal to it
echo
read -p " docker container attach myAlpine"
echo
echo NOTE: Hit Ctrl-c when you get bored
docker container attach myAlpine
echo container status is
echo
docker container inspect --format {{.State.Status}} myAlpine
echo
echo So now the the myAlpine container is exited, but
echo how to really get rid of a container? It has to be removed
echo
read -p " docker container rm myAlpine"
echo
docker container rm myAlpine
echo
echo and now \"docker container ls -a\" and \"docker container inspect --format {{.State.Status}} myAlpine\" say the correct thing
echo
docker container ls -a
echo
docker container inspect --format {{.State.Status}} myAlpine
echo
echo and we are done!
echo
echo sometimes you want to remove a container that us still running, without bothering to stop it first
echo use the \"-f\" option, as in \"docker container rm -f myAlpine\"