Skip to content

Instantly share code, notes, and snippets.

@cray0000
Last active October 11, 2018 19:31
Show Gist options
  • Save cray0000/721aad7aefa569f450fcc28c187e03b0 to your computer and use it in GitHub Desktop.
Save cray0000/721aad7aefa569f450fcc28c187e03b0 to your computer and use it in GitHub Desktop.
Docker usage cheat sheet (only the most useful stuff)

Docker Usage Cheat Sheet (the crash course) + Dockerfile example

Here are all the basic commands you need to know to work with Docker in the development environment:

  1. build an image named myAppImage from the current project folder (Dockerfile must be present):

    $ docker build . -t myAppImage
  2. run a container named myAppContainer from the image named myAppImage. In interactive mode (pressing Ctrl+C will stop it):

    $ docker run -it --rm --name myAppContainer myAppImage
  3. run a container the same way as in previous example and also:

    • make the port 8080 on your host machine listen to the port 3000 inside the container
    • make the folder ./data on your host machine available inside the container as /data/db
    $ docker run -it --rm --name myAppContainer -p 8080:3000 -v $(pwd)/data:/data/db myAppImage
  4. when you want to get inside the terminal of the running container named myAppContainer (while docker run -ti is running in another terminal window) to check its filesystem or run any other commands (for example to run top or mongo):

    $ docker exec -it myAppContainer sh
  5. to view all currently running containers:

    $ docker ps
  6. to view all images you did build or download:

    $ docker images
  7. [CLEANUP] to force stop (if running) and remove the container named myAppContainer:

    $ docker rm -f myAppContainer
  8. [CLEANUP] to remove the image named myAppImage:

    $ docker rmi myAppImage

HINTS:

  • docker ps shows only the containers which are currently running (eat CPU and memory)
  • docker ps -a will also show the stopped containers (which don't eat CPU and memory, but still take space on your hard drive)
  • running the command docker run with the --rm flag tells docker to autoremove the container when you press Ctrl+C
  • if you always use --rm flag for docker run you won't have any stopped containers, and can simply use docker ps
  • you should avoid having stopped containers, because they eat your space, and you probably are not going to use them again after they were stopped. So it is recommend to always use --rm in development environment.

Advanced

  1. Cleanup all exited containers

    $ docker ps -aq --no-trunc -f status=exited | xargs docker rm
  2. Cleanup all untagged images

    $ docker images -q --filter dangling=true | xargs docker rmi

List of resources with a similar info:

  1. https://devhints.io/docker
  2. https://zaiste.net/removing_docker_containers/

Simplest Dockerfile example

Below is a fully documented example of a simple Dockerfile for a node.js app which is supposed to listen for the port 3000:

# use alpine version of node 10
FROM node:10-alpine
# put all source code into the /app folder
ADD . /app
# set the working dir of any subsequent RUN and CMD commands to be /app
WORKDIR /app
# prepare everything to be able to run the app.
# This is executed when you build the docker image:
# docker build -t myAppImage .
#
RUN \
# install packages
npm install && \
# build the project
npm run build
# command to run the actual application.
# This is executed when you run the docker image:
# docker run -it --rm --name myAppContainer -p 3000:3000 myAppImage
#
CMD node build/server.js
# port the application listens to.
# You would want to expose this port to your host machine by specifying `-p` flag in `docker run`
EXPOSE 3000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment