OSDN Git Service

Regular updates
[twpd/master.git] / docker.md
1 ---
2 title: Docker CLI
3 category: Devops
4 layout: 2017/sheet
5 ---
6
7 Manage images
8 -------------
9
10 ### `docker build`
11
12 ```yml
13 docker build [options] .
14   -t "app/container_name"    # name
15   --build-arg APP_HOME=$APP_HOME    # Set build-time variables
16 ```
17
18 Create an `image` from a Dockerfile.
19
20
21 ### `docker run`
22
23 ```yml
24 docker run [options] IMAGE
25   # see `docker create` for options
26 ```
27
28 #### Example
29
30 ```
31 $ docker run -it debian:buster /bin/bash
32 ```
33 Run a command in an `image`.
34
35 Manage containers
36 -----------------
37
38 ### `docker create`
39
40 ```yml
41 docker create [options] IMAGE
42   -a, --attach               # attach stdout/err
43   -i, --interactive          # attach stdin (interactive)
44   -t, --tty                  # pseudo-tty
45       --name NAME            # name your image
46   -p, --publish 5000:5000    # port map (host:container)
47       --expose 5432          # expose a port to linked containers
48   -P, --publish-all          # publish all ports
49       --link container:alias # linking
50   -v, --volume `pwd`:/app    # mount (absolute paths needed)
51   -e, --env NAME=hello       # env vars
52 ```
53
54 #### Example
55
56 ```
57 $ docker create --name app_redis_1 \
58   --expose 6379 \
59   redis:3.0.2
60 ```
61
62 Create a `container` from an `image`.
63
64 ### `docker exec`
65
66 ```yml
67 docker exec [options] CONTAINER COMMAND
68   -d, --detach        # run in background
69   -i, --interactive   # stdin
70   -t, --tty           # interactive
71 ```
72
73 #### Example
74
75 ```
76 $ docker exec app_web_1 tail logs/development.log
77 $ docker exec -t -i app_web_1 rails c
78 ```
79
80 Run commands in a `container`.
81
82
83 ### `docker start`
84
85 ```yml
86 docker start [options] CONTAINER
87   -a, --attach        # attach stdout/err
88   -i, --interactive   # attach stdin
89
90 docker stop [options] CONTAINER
91 ```
92
93 Start/stop a `container`.
94
95
96 ### `docker ps`
97
98 ```
99 $ docker ps
100 $ docker ps -a
101 $ docker kill $ID
102 ```
103
104 Manage `container`s using ps/kill.
105
106
107 ### `docker logs`
108
109 ```
110 $ docker logs $ID
111 $ docker logs $ID 2>&1 | less
112 $ docker logs -f $ID # Follow log output
113 ```
114
115 See what's being logged in an `container`.
116
117
118 Images
119 ------
120
121 ### `docker images`
122
123 ```sh
124 $ docker images
125   REPOSITORY   TAG        ID
126   ubuntu       12.10      b750fe78269d
127   me/myapp     latest     7b2431a8d968
128 ```
129
130 ```sh
131 $ docker images -a   # also show intermediate
132 ```
133
134 Manages `image`s.
135
136 ### `docker rmi`
137
138 ```yml
139 docker rmi b750fe78269d
140 ```
141
142 Deletes `image`s.
143
144 ## Clean up
145
146 ### Clean all
147
148 ```sh
149 docker system prune
150 ```
151
152 Cleans up dangling images, containers, volumes, and networks (ie, not associated with a container)
153
154 ```sh
155 docker system prune -a
156 ```
157
158 Additionally remove any stopped containers and all unused images (not just dangling images)
159
160 ### Containers
161
162 ```sh
163 # Stop all running containers
164 docker stop $(docker ps -a -q)
165
166 # Delete stopped containers
167 docker container prune
168 ```
169
170 ### Images
171
172 ```sh
173 docker image prune [-a]
174 ```
175
176 Delete all the images
177
178 ### Volumes
179
180 ```sh
181 docker volume prune
182 ```
183
184 Delete all the volumes
185
186 Also see
187 --------
188
189  * [Getting Started](http://www.docker.io/gettingstarted/) _(docker.io)_