Docker Commands Reference

- 2 mins read
CommandDescription
startStart Container
stopStops Container
restartRestart Container
killKill Container
attachAttach terminal to running container
execExcutes command within a running container
inspectDetail output about running container in JSON
historyShows history of container image

Dockerfile Commands

CommandReference
FROMBase image, usually first
WORKDIRSet’s working directory
ADDAdds file from source to destination. Can be URL, Will Unzip
COPYCopy from host machine. Best practice to use unless you need ADD’s functionality.
EXPOSEExpose a port from container at runtime
VOLUMECreates a mount point for container
RUNExecutes command in a new layer and commits to image. Can have many.
CMDProvides default behavior for launching an instance. Only one per image and not hole image is being built.
ENVSets and environment variable that is available through build and during container run.
ARGVariables that can be passes into the build command. Don’t use for sensitive info.
LABELKey-Value parr’s exposed as image metadata

Dockerfile Best Practices

  • Keep image small
  • As few layers as possible
  • Avoid breaking cache
  • Avoid known vulnerabilities

Keeping Image Small

  • Use small or slim base image
  • Combine layers
  • Use .dockerignore
  • Use multistage build
    • Best for compiled code or have lot of intermediate steps
    • Keep final stage as small as possible
    • All other stages are discarded
  • Change user to non-root user
    • Better security
    • Adds additional layer

HEALTHCHECK command

  • Tells Docker how to check status of container
  • HEALTHCHECK –interval=30s CMD curl -f http://localhost/status || exit 1

Compose File Syntax

  • Image
  • Build
  • Image and Build