cheatsheets/docker.md

211 lines
3.1 KiB
Markdown
Raw Permalink Normal View History

2013-10-14 02:36:58 +00:00
---
2015-11-24 05:02:17 +00:00
title: Docker CLI
category: Devops
2013-10-14 02:36:58 +00:00
---
2015-06-20 17:17:06 +00:00
Manage images
-------------
2017-10-20 10:23:19 +00:00
### `docker build`
2015-06-20 17:17:06 +00:00
```yml
docker build [options] .
-t "app/container_name" # name
--build-arg APP_HOME=$APP_HOME # Set build-time variables
2015-06-20 17:17:06 +00:00
```
2017-10-20 10:23:19 +00:00
Create an `image` from a Dockerfile.
### `docker run`
2015-06-20 17:17:06 +00:00
```yml
docker run [options] IMAGE
# see `docker create` for options
```
2019-10-24 05:51:49 +00:00
#### Example
2019-11-17 07:09:31 +00:00
2019-10-24 05:51:49 +00:00
```
$ docker run -it debian:buster /bin/bash
```
2019-11-17 07:09:31 +00:00
Run a command in an `image`.
2019-10-24 05:51:49 +00:00
2015-06-20 17:17:06 +00:00
Manage containers
-----------------
2017-10-20 10:14:34 +00:00
### `docker create`
2015-06-20 17:17:06 +00:00
```yml
docker create [options] IMAGE
-a, --attach # attach stdout/err
-i, --interactive # attach stdin (interactive)
-t, --tty # pseudo-tty
--name NAME # name your image
-p, --publish 5000:5000 # port map (host:container)
2015-06-20 17:17:06 +00:00
--expose 5432 # expose a port to linked containers
-P, --publish-all # publish all ports
--link container:alias # linking
-v, --volume `pwd`:/app # mount (absolute paths needed)
-e, --env NAME=hello # env vars
```
2017-10-20 10:14:34 +00:00
#### Example
2015-06-20 17:17:06 +00:00
```
$ docker create --name app_redis_1 \
--expose 6379 \
redis:3.0.2
```
2017-10-20 10:14:34 +00:00
Create a `container` from an `image`.
### `docker exec`
2015-06-20 17:17:06 +00:00
```yml
docker exec [options] CONTAINER COMMAND
-d, --detach # run in background
-i, --interactive # stdin
-t, --tty # interactive
```
2017-10-20 10:14:34 +00:00
#### Example
2015-06-20 17:17:06 +00:00
```
$ docker exec app_web_1 tail logs/development.log
$ docker exec -t -i app_web_1 rails c
```
2017-10-20 10:14:34 +00:00
Run commands in a `container`.
### `docker start`
2015-06-20 17:17:06 +00:00
```yml
docker start [options] CONTAINER
-a, --attach # attach stdout/err
-i, --interactive # attach stdin
docker stop [options] CONTAINER
```
2017-10-20 10:14:34 +00:00
Start/stop a `container`.
### `docker ps`
2015-06-20 17:17:06 +00:00
```
$ docker ps
$ docker ps -a
$ docker kill $ID
```
2013-08-24 07:43:10 +00:00
2017-10-20 10:14:34 +00:00
Manage `container`s using ps/kill.
2019-04-10 12:06:35 +00:00
### `docker logs`
```
$ docker logs $ID
$ docker logs $ID 2>&1 | less
$ docker logs -f $ID # Follow log output
```
See what's being logged in an `container`.
2017-10-20 10:14:34 +00:00
Images
------
2013-08-24 07:43:10 +00:00
2017-10-20 10:14:34 +00:00
### `docker images`
2013-08-24 07:43:10 +00:00
2015-06-20 17:17:06 +00:00
```sh
$ docker images
REPOSITORY TAG ID
ubuntu 12.10 b750fe78269d
me/myapp latest 7b2431a8d968
2017-10-20 10:14:34 +00:00
```
2013-08-24 07:43:10 +00:00
2017-10-20 10:14:34 +00:00
```sh
2015-06-20 17:17:06 +00:00
$ docker images -a # also show intermediate
```
2013-08-24 07:43:10 +00:00
2017-10-20 10:14:34 +00:00
Manages `image`s.
### `docker rmi`
2013-08-24 07:43:10 +00:00
2015-06-20 17:17:06 +00:00
```yml
docker rmi b750fe78269d
```
2013-08-24 07:43:10 +00:00
2017-10-20 10:14:34 +00:00
Deletes `image`s.
2019-10-15 09:27:06 +00:00
## Clean up
### Clean all
2019-10-08 08:18:25 +00:00
```sh
docker system prune
```
2019-10-15 09:27:06 +00:00
Cleans up dangling images, containers, volumes, and networks (ie, not associated with a container)
2019-10-08 08:18:25 +00:00
```sh
docker system prune -a
```
2019-10-15 09:27:06 +00:00
Additionally remove any stopped containers and all unused images (not just dangling images)
### Containers
2019-10-08 08:18:25 +00:00
```sh
2019-10-15 09:27:06 +00:00
# Stop all running containers
2019-10-08 08:18:25 +00:00
docker stop $(docker ps -a -q)
2019-10-15 09:27:06 +00:00
# Delete stopped containers
docker container prune
2019-10-08 08:18:25 +00:00
```
2019-10-15 09:27:06 +00:00
### Images
2019-10-08 08:18:25 +00:00
```sh
2019-10-15 09:27:06 +00:00
docker image prune [-a]
2019-10-08 08:18:25 +00:00
```
2019-10-15 09:27:06 +00:00
Delete all the images
2019-10-08 08:18:25 +00:00
2019-10-15 09:27:06 +00:00
### Volumes
2019-10-08 08:18:25 +00:00
```sh
docker volume prune
```
2019-10-15 09:27:06 +00:00
Delete all the volumes
2019-10-08 08:18:25 +00:00
### Systems
```sh
docker system df
```
Show docker disk usage
```sh
docker system events
```
Get real time events from the server
```sh
docker system info
```
Display system-wide information
2017-10-20 10:14:34 +00:00
Also see
--------
2013-08-24 07:43:10 +00:00
* [Getting Started](https://www.docker.io/gettingstarted/) _(docker.io)_