a breif selection of essential commands, tips and notes about working with docker, as a personal reference. use at your own risk.
create and start a container
docker run [-a] <container-name> <override>
essentially runs the following two commands
docker create <container-name> <override>
docker start [-a] <container-id>
list all running containers
docker ps
list all containers
docker ps --all
delete everything
docker system prune
output logs for a running container
docker logs <container-id>
gracefully stop a running container
docker stop <container-id>
forcefully, and immediately, stop a container
docker kill <container-id>
run a command in a running contianer (optionally interactive)
docker exec [-it] <container-id> <command>
Exiting an interactive session can usually be accomplished cmd+c
will exit, if not try ctrl+d
, or simply type exit
FROM
: specifty a base image to start fromLABEL
: add arbitrary labels, such asauthor
withauthor=<you-name>
WORKDIR
: specify a working directory in the container (all commands after, use this as root)COPY
: copy somePath into the container working directoryRUN
: run arbitrary commandsCMD
: set an initial start command for when your container builds
Here is an example Dockerfile
# use a base image
FROM alpine
# dl and install deps
RUN apk add --update redis
# tell it what to do when it starts
CMD ["redis-server"]
this is a very basic container, here's another, thats slightly more complete:
FROM node:alpine
LABEL author="my-docker-id"
WORKDIR /usr/app
COPY ./package.json ./
RUN npm install
COPY ./ ./
CMD ['npm', 'start']
as each instruction is cached, the order is important, to avoid unneccessary rebuild steps and cache busting.
after creating your Dockerfile
run the following in your project dir:
docker build -t <username/name> .
not that this will name your build with the <username/name> .
. then run the following to start that container, notice we're parsing in the -p
flag to specify the port, and are able to run the container by name, rather than id.
docker run -p <external-port>:<container-port> <container-id or name>