Docker Cheatsheet - Collabnix
Docker Cheatsheet - Collabnix
Docker Cheatsheet
A cheat sheet is a concise summary of important information that is meant to be used as a quick
reference. Cheatsheets are often used in the form of a list or a table, and they typically cover a
specific topic or subject area. In the context of Docker, a Docker cheatsheet is a summary of
commonly used Docker commands and their options and other useful information related to Docker.
Cheatsheets can be particularly helpful when learning a new tool or technology, as they provide a
convenient way to quickly look up and remind oneself of key concepts and commands. They can also
be useful for experienced users who need to recall a specific command or option but may not
remember all the details.
https://collabnix.com/docker-cheatsheet/ 1/8
31.03.2023 18:16 Docker Cheatsheet – Collabnix
3. Manage containers
docker create
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
--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
Example
Example
5. Start a Container
docker start
https://collabnix.com/docker-cheatsheet/ 2/8
31.03.2023 18:16 Docker Cheatsheet – Collabnix
docker start [options] CONTAINER
-a, --attach # attach stdout/err
-i, --interactive # attach stdin
Start/stop a container .
6. Managing Container
docker ps
$ docker ps
$ docker ps -a
$ docker kill $ID
7. Managing Images
docker images
$ docker images
REPOSITORY TAG ID
ubuntu 12.10 b750fe78269d
me/myapp latest 7b2431a8d968
Manages image s.
8. Delete Image
docker rmi
docker rmi b750fe78269d
Deletes image s.
Also see
Getting Started (docker.io)
9. Dockerfile
Inheritance
FROM ruby:2.2.2
Variables
ENV APP_HOME /myapp
RUN mkdir $APP_HOME
Initialization
https://collabnix.com/docker-cheatsheet/ 3/8
31.03.2023 18:16 Docker Cheatsheet – Collabnix
WORKDIR /myapp
VOLUME ["/data"]
# Specification for mount point
Onbuild
ONBUILD RUN bundle install
# when used with another file
Commands
EXPOSE 5900
CMD ["bundle", "exec", "rails", "server"]
Entrypoint
ENTRYPOINT ["executable", "param1", "param2"]
ENTRYPOINT command param1 param2
This will use shell processing to substitute shell variables, and will ignore any CMD or docker
run command line arguments.
Metadata
LABEL version="1.0"
See also
https://docs.docker.com/engine/reference/builder/
docker-compose
Basic example
https://collabnix.com/docker-cheatsheet/ 4/8
31.03.2023 18:16 Docker Cheatsheet – Collabnix
# docker-compose.yml
version: '2'
services:
web:
build: .
# build from Dockerfile
context: ./Path
dockerfile: Dockerfile
ports:
- "5000:5000"
volumes:
- .:/code
redis:
image: redis
Commands
docker-compose start
docker-compose stop
docker-compose pause
docker-compose unpause
docker-compose ps
docker-compose up
docker-compose down
Reference
{: .-three-column}
Building
web:
# build from Dockerfile
build: .
Ports
ports:
- "3000"
- "8000:80" # guest:host
https://collabnix.com/docker-cheatsheet/ 5/8
31.03.2023 18:16 Docker Cheatsheet – Collabnix
Commands
# command to execute
command: bundle exec thin -p 3000
command: [bundle, exec, thin, -p, 3000]
Environment variables
# environment vars
environment:
RACK_ENV: development
environment:
- RACK_ENV=development
Dependencies
# makes the `db` service available as the hostname `database`
# (implies depends_on)
links:
- db:database
- redis
Other options
# make this service extend another
extends:
file: common.yml # optional
service: webapp
volumes:
- /var/lib/mysql
- ./_data:/var/lib/mysql
Advanced features
Labels
https://collabnix.com/docker-cheatsheet/ 6/8
31.03.2023 18:16 Docker Cheatsheet – Collabnix
services:
web:
labels:
com.example.description: "Accounting web app"
DNS servers
services:
web:
dns: 8.8.8.8
dns:
- 8.8.8.8
- 8.8.4.4
Devices
services:
web:
devices:
- "/dev/ttyUSB0:/dev/ttyUSB0"
External links
services:
web:
external_links:
- redis_1
- project_db_1:mysql
Hosts
services:
web:
extra_hosts:
- "somehost:192.168.1.100"
sevices
To view list of all the services runnning in swarm
docker service ls
https://collabnix.com/docker-cheatsheet/ 7/8
31.03.2023 18:16 Docker Cheatsheet – Collabnix
Cleaning up
To clean or prune unused (dangling) images
To leave swarm
https://collabnix.com/docker-cheatsheet/ 8/8