Containers
Docker containers

Commands

Run container

1
container run - p 80:80 -d --name webserver nginx
Copied!
pass "--rm" to remove container when it stops.

List all containers (pass -a to see all containers)

1
docker container ls [-a]
Copied!

Get logs from container

1
docker container logs <container-name>
Copied!

List running processes in the container

1
docker container top <container-name>
Copied!

Get system information about running containers

1
docker container stats
Copied!

Get metadata about running container

1
docker container inspect <container-name>
Copied!
Example:
1
[
2
{
3
"Id": "2471fee7e410a4ba48e69e80ecdc7033f46f7012ca493d1dba92dc9a6fda46f7",
4
"Created": "2020-01-13T01:27:41.8547458Z",
5
"Path": "nginx",
6
"Args": [
7
"-g",
8
"daemon off;"
9
],
10
"State": {
11
"Status": "running",
12
"Running": true,
13
"Paused": false,
14
"Restarting": false,
15
"OOMKilled": false,
16
"Dead": false,
17
"Pid": 2513,
18
"ExitCode": 0,
19
"Error": "",
20
"StartedAt": "2020-01-13T01:27:42.7242686Z",
21
"FinishedAt": "0001-01-01T00:00:00Z"
22
},
23
"Image": "sha256:c7460dfcab502275e9c842588df406444069c00a48d9a995619c243079a4c2f7",
24
"ResolvConfPath": "/var/lib/docker/containers/2471fee7e410a4ba48e69e80ecdc7033f46f7012ca493d1dba92dc9a6fda46f7/resolv.conf",
25
"HostnamePath": "/var/lib/docker/containers/2471fee7e410a4ba48e69e80ecdc7033f46f7012ca493d1dba92dc9a6fda46f7/hostname",
26
"HostsPath": "/var/lib/docker/containers/2471fee7e410a4ba48e69e80ecdc7033f46f7012ca493d1dba92dc9a6fda46f7/hosts",
27
"LogPath": "/var/lib/docker/containers/2471fee7e410a4ba48e69e80ecdc7033f46f7012ca493d1dba92dc9a6fda46f7/2471fee7e410a4ba48e69e80ecdc7033f46f7012ca493d1dba92dc9a6fda46f7-json.log",
28
"Name": "/webserver",
29
"RestartCount": 0,
30
"Driver": "overlay2",
31
"Platform": "linux",
32
"MountLabel": "",
33
"ProcessLabel": "",
34
"AppArmorProfile": "",
35
"ExecIDs": null,
36
"HostConfig": {
37
"Binds": null,
38
"ContainerIDFile": "",
39
"LogConfig": {
40
"Type": "json-file",
41
"Config": {}
42
},
43
"NetworkMode": "default",
44
"PortBindings": {
45
"80/tcp": [
46
{
47
"HostIp": "",
48
"HostPort": "80"
49
}
50
]
51
},
52
"RestartPolicy": {
53
"Name": "no",
54
"MaximumRetryCount": 0
55
},
56
"AutoRemove": false,
57
"VolumeDriver": "",
58
"VolumesFrom": null,
59
"CapAdd": null,
60
"CapDrop": null,
61
"Capabilities": null,
62
"Dns": [],
63
"DnsOptions": [],
64
"DnsSearch": [],
65
"ExtraHosts": null,
66
"GroupAdd": null,
67
"IpcMode": "private",
68
"Cgroup": "",
69
"Links": null,
70
"OomScoreAdj": 0,
71
"PidMode": "",
72
"Privileged": false,
73
"PublishAllPorts": false,
74
"ReadonlyRootfs": false,
75
"SecurityOpt": null,
76
"UTSMode": "",
77
"UsernsMode": "",
78
"ShmSize": 67108864,
79
"Runtime": "runc",
80
"ConsoleSize": [
81
0,
82
0
83
],
84
"Isolation": "",
85
"CpuShares": 0,
86
"Memory": 0,
87
"NanoCpus": 0,
88
"CgroupParent": "",
89
"BlkioWeight": 0,
90
"BlkioWeightDevice": [],
91
"BlkioDeviceReadBps": null,
92
"BlkioDeviceWriteBps": null,
93
"BlkioDeviceReadIOps": null,
94
"BlkioDeviceWriteIOps": null,
95
"CpuPeriod": 0,
96
"CpuQuota": 0,
97
"CpuRealtimePeriod": 0,
98
"CpuRealtimeRuntime": 0,
99
"CpusetCpus": "",
100
"CpusetMems": "",
101
"Devices": [],
102
"DeviceCgroupRules": null,
103
"DeviceRequests": null,
104
"KernelMemory": 0,
105
"KernelMemoryTCP": 0,
106
"MemoryReservation": 0,
107
"MemorySwap": 0,
108
"MemorySwappiness": null,
109
"OomKillDisable": false,
110
"PidsLimit": null,
111
"Ulimits": null,
112
"CpuCount": 0,
113
"CpuPercent": 0,
114
"IOMaximumIOps": 0,
115
"IOMaximumBandwidth": 0,
116
"MaskedPaths": [
117
"/proc/asound",
118
"/proc/acpi",
119
"/proc/kcore",
120
"/proc/keys",
121
"/proc/latency_stats",
122
"/proc/timer_list",
123
"/proc/timer_stats",
124
"/proc/sched_debug",
125
"/proc/scsi",
126
"/sys/firmware"
127
],
128
"ReadonlyPaths": [
129
"/proc/bus",
130
"/proc/fs",
131
"/proc/irq",
132
"/proc/sys",
133
"/proc/sysrq-trigger"
134
]
135
},
136
"GraphDriver": {
137
"Data": {
138
"LowerDir": "/var/lib/docker/overlay2/a4d587b17605cd7d55e5e4e53ca94f85eb3873717774c7ccaf2ffdbfda7ede6f-init/diff:/var/lib/docker/overlay2/b0b1f6dce35e8f037a3f0bd7310c9d262bfbe50bf2a99d8d1d5bb9a8c2d674e7/diff:/var/lib/docker/overlay2/d2a2f0e8c23a861795bcab3d13f17ca643c17a9b97c74c85696393828186cb1a/diff:/var/lib/docker/overlay2/a20f4c7748e1f828f0d533783d04930c7f16d6435dbebe4b093dd7c05488b666/diff",
139
"MergedDir": "/var/lib/docker/overlay2/a4d587b17605cd7d55e5e4e53ca94f85eb3873717774c7ccaf2ffdbfda7ede6f/merged",
140
"UpperDir": "/var/lib/docker/overlay2/a4d587b17605cd7d55e5e4e53ca94f85eb3873717774c7ccaf2ffdbfda7ede6f/diff",
141
"WorkDir": "/var/lib/docker/overlay2/a4d587b17605cd7d55e5e4e53ca94f85eb3873717774c7ccaf2ffdbfda7ede6f/work"
142
},
143
"Name": "overlay2"
144
},
145
"Mounts": [],
146
"Config": {
147
"Hostname": "2471fee7e410",
148
"Domainname": "",
149
"User": "",
150
"AttachStdin": false,
151
"AttachStdout": false,
152
"AttachStderr": false,
153
"ExposedPorts": {
154
"80/tcp": {}
155
},
156
"Tty": false,
157
"OpenStdin": false,
158
"StdinOnce": false,
159
"Env": [
160
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
161
"NGINX_VERSION=1.17.7",
162
"NJS_VERSION=0.3.7",
163
"PKG_RELEASE=1~buster"
164
],
165
"Cmd": [
166
"nginx",
167
"-g",
168
"daemon off;"
169
],
170
"Image": "nginx",
171
"Volumes": null,
172
"WorkingDir": "",
173
"Entrypoint": null,
174
"OnBuild": null,
175
"Labels": {
176
"maintainer": "NGINX Docker Maintainers <[email protected]>"
177
},
178
"StopSignal": "SIGTERM"
179
},
180
"NetworkSettings": {
181
"Bridge": "",
182
"SandboxID": "dac7e1d502934740d4bdcedf1756d75514e4a5387bae842a6770026886d9eeaf",
183
"HairpinMode": false,
184
"LinkLocalIPv6Address": "",
185
"LinkLocalIPv6PrefixLen": 0,
186
"Ports": {
187
"80/tcp": [
188
{
189
"HostIp": "0.0.0.0",
190
"HostPort": "80"
191
}
192
]
193
},
194
"SandboxKey": "/var/run/docker/netns/dac7e1d50293",
195
"SecondaryIPAddresses": null,
196
"SecondaryIPv6Addresses": null,
197
"EndpointID": "d3731f0e83b1ee4bc67f9221c0056e2eef91b8e3ea2e86636765bbad13b35b9f",
198
"Gateway": "172.17.0.1",
199
"GlobalIPv6Address": "",
200
"GlobalIPv6PrefixLen": 0,
201
"IPAddress": "172.17.0.2",
202
"IPPrefixLen": 16,
203
"IPv6Gateway": "",
204
"MacAddress": "02:42:ac:11:00:02",
205
"Networks": {
206
"bridge": {
207
"IPAMConfig": null,
208
"Links": null,
209
"Aliases": null,
210
"NetworkID": "45ecc9dd0aa8816755250010c1b01d899e2de4700db943e6ed8d6224c56bf9e7",
211
"EndpointID": "d3731f0e83b1ee4bc67f9221c0056e2eef91b8e3ea2e86636765bbad13b35b9f",
212
"Gateway": "172.17.0.1",
213
"IPAddress": "172.17.0.2",
214
"IPPrefixLen": 16,
215
"IPv6Gateway": "",
216
"GlobalIPv6Address": "",
217
"GlobalIPv6PrefixLen": 0,
218
"MacAddress": "02:42:ac:11:00:02",
219
"DriverOpts": null
220
}
221
}
222
}
223
}
224
]
Copied!

Stop running container

1
docker container stop <container-name|container-id>
Copied!

Remove container

(pass -f to force remove container)
1
docker container [-f] rm <container-name|container-id>
Copied!

Start container with different process then defined in CMD or ENTRYPOINT

1
docker container run -it --name webserver nginx bash
Copied!
will start "bash" in interactive mode in "nginx" container named "webserver". Once connected, a "ps" can be installed to make sure the only process running is "bash"
1
apt-get update
2
apt-get install -y procps
3
ps aux
Copied!

Connect to the running docker container and run shell

Connect to running container with shell (depends whether specific shell is installed)
1
docker container exec -it <container-name|id> bash
Copied!

List container port mappings:

1
docker container port <container-name|container-id>
Copied!
if container has a host to container port mappings, command should produce something like: 80/tcp -> 0.0.0.0:80
Last modified 1yr ago
Copy link