Skip to content

Instantly share code, notes, and snippets.

@Pushplaybang
Last active October 7, 2018 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pushplaybang/974ff65a17014b37aaafd5f5bcc9e0e2 to your computer and use it in GitHub Desktop.
Save Pushplaybang/974ff65a17014b37aaafd5f5bcc9e0e2 to your computer and use it in GitHub Desktop.
Docker cheat sheet

MY Docker Cheat Sheet

a breif selection of essential commands, tips and notes about working with docker, as a personal reference. use at your own risk.

Working with containers

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

Dockerfile

  • FROM : specifty a base image to start from
  • LABEL : add arbitrary labels, such as author with author=<you-name>
  • WORKDIR : specify a working directory in the container (all commands after, use this as root)
  • COPY : copy somePath into the container working directory
  • RUN : run arbitrary commands
  • CMD : 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.

Building containers

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment