Skip to content

Instantly share code, notes, and snippets.

@somahargitai
Last active November 25, 2020 11:41
Show Gist options
  • Save somahargitai/be26306a7fdc5cc0a37b0efe05c8db66 to your computer and use it in GitHub Desktop.
Save somahargitai/be26306a7fdc5cc0a37b0efe05c8db66 to your computer and use it in GitHub Desktop.
Docker Cheat Sheet

cheatsheet list

Docker cheat sheet

Content

  • Introduction
  • CLI Commands
    • Listing and info
    • Deleting
    • Creating and Running basics
    • Parameter in CMD / Entrypoint
    • Environmental variables and build args
  • Dockerization examples

Introduction

Installation

  • Available installer for every OS on Docker website, from 2020 Win 10 Home is also supported.
  • Dockerhub registration expected to be able to try hub services for images

Notes on Docker command standardization

For better understanding and better syntax, docker image ls, docker container ls like syntax is newly created instead of traditional docker images and docker ps. Still, both will work, so I will provide them below.

Best practice links

Best practices in Docker docs

CLI Commands

Listing and Info

  • images docker images or docker image ls.
  • containers docker ps or docker container ls
    • -a show containers not running too
    • -l show latest only
    • q show ID only

Inspect

You can get information about an image, a container. For example: environmental variables, entrypoint, state

docker inspect 5fe2fe

Deleting & Stopping

  • image docker image rm alpine:3.4
  • container docker container rm 6fe5ce
    • --force, -f force removal with SIGKILL
  • stop all containers docker stop $(docker ps -a -q)
  • remove all containers docker rm $(docker ps -a -q)
  • remove all images docker image rm $(docker images)

Creating and Running basics

  • docker build -t myimage . create image with Dockerfile
  • docker pull hello-world pull image called hello-world from the remote (docker hub)
  • docker create -name my-container hello-world create your container from hello-world image
  • docker start my-container start the container created
  • docker stop 4ce5ek stop the container
  • docker restart 4ce5ek

docker run

docker run starts the container and if not found, automatically runs docker pull and docker create before.

docker run -d -p 8080:8080 --name container_name image_name

  • -d detached mode
    • docker attach get back to container CLI
  • -n name container
  • -p, --publish publish port to the host, 3000:5000 means 5000 is the inside port, 3000 is the public
  • -i interactive mode
  • -t, -it start a pseudo-terminal to interact with container
    • docker exec 5ec4lf cat /etc/*release*
    • docker exec 5ec4lf ls
    • docker exec 5ec4lf curl http://localhost:8080

Parameter in CMD / Entrypoint

As example, run sleep command in ubuntu image for 5 millisec

call command from terminal

docker run ubuntu sleep 5

bake it into image

docker build - t ubuntu-sleeper
docker run ubuntu-sleeper
FROM ubuntu
CMD sleep 5

it is the same:

FROM ubuntu
CMD ["sleep", "5"]

in CMD [...] executable first, parameter next

sleep command in image, time parameter is a run parameter

docker run ubuntu-sleeper 5
FROM ubuntu
ENTRYPOINT["sleep"]

default parameter if not provided

FROM ubuntu
ENTRYPOINT["sleep"]
CMD[5]

docker run -v /opt/datadir:/var/lib/mysql mzsql volume mapping keep data even if container does not run any more

Environmental variables and build args

availability

  • ARG - only available during image build
  • ENV - available for container

So PRINTME will be printed, but APPLE won't:

...
ARG APPLE
ENV PRINTME=$APPLE
...
...
console.log(process.env.PRINTME)
console.log(process.env.APPLE)
...

feed parameter in docker build

docker build --build-arg APPLE="fruit fruit" -t imagename .

parameters to be printed in build output:

...
ARG APPLE 
ENV APPLE=$APPLE # it is not necessary, with this line you can use APPLE in runtime
RUN echo "hello world"
RUN echo ${APPLE}
...

parameters to be printed in runtime in a javascript app:

...
console.log('here ${process.env.APPLE}');
...

feed parameter in docker run

docker run -p 8080:8080 --env PEAR="vegetable" myimage`
...
console.log(process.env.PEAR);
...

Publish on Docker Hub

docker login

docker push my-image

Dockerization examples

Flask app

|Python|Flask|Ubuntu|Pip|

based on mmumshad's test app

do it in the CLI first

  • docker run -it ubuntu bash start a container and a bash CLI inside
  • apt-get update
  • apt-get install -y python answer 'yes' to all the installation checks
  • apt-get install python3-pip
  • pip3 install flask
  • cat > /opt/app.py create code file to write it
  • paste code:
import os
from flask import flask
app=FLask(__name__)
@app.route("/")
def main():
  return "Welcome"
@app.route(`/how are you`)
def hello():
  return 'I am good'
if __name__ == "__main__":
  app.run(host="0.0.0.0", port=8080)
  • run: FLASK_APP=/opt/app.py flask run --host-0.0.0.0

if works, do it in Dockerfile

  • dockerfile
FROM ubuntu
RUN apt-get update
RUN apt-get install -y python python3-pip
RUN pip3 install flask
COPY app.py /opt/app.py
ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0
  • build docker build .
  • build again with name: docker build . -t my-simple-webapp this build process with tagging will be faster, reuses the existing image
  • run docker run -p 8080:5000 my-simple-webapp`

Now you can open it in browser on http://localhost:8080 or http://localhost:8080/how are you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment