docker cheatsheet šŸ³

intro

  • docker --version

    docker engine version

  • docker version

    docker version in detail

  • docker events

    get real time events from the server
    when a container stops, an image is pushed, daemon reloads etc

  • docker system df

    shows docker disk usage

quick start from hub.docker.com

git clone https://github.com/docker/doodle.git
cd doodle\cheers2019
docker build -t erencelik/cheers2019 .
docker run -it --rm erencelik/cheers2019
docker login
docker push erencelik/cheers2019

gets the repo, builds an image from the Dockerfile
runs it as a container and pushes the image to your registry in hub.docker.com

images

  • docker images

    lists all images

  • docker tag erencelik/dummy-project:1.0.0.RELEASE erencelik/dummy-project:latest

    tags an image
    docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

  • docker pull ubuntu

    downloads the image
    if no tag specified, downloads 'latest' as default

  • docker image rm <iid>

    removes image locally
    first 2-3 chars of image id is enough as long as it's unique in local
    if you get the error:
    unable to delete - image has dependent child images
    try using repository name and tag instead of image id:
    docker image rm ubuntu:latest

  • docker search ubuntu

    searches images from docker hub and lists

  • docker image history <iid>

    shows the history of an image

  • docker image inspect <iid>

    shows detailed information of an image

  • docker build .

    builds an image from a Dockerfile in current directory
    an url or path can be given

  • docker push dummy-project:latest

    push an image or a repository to a registry

containers

  • docker container ls

    shows the running containers

  • docker container ls -a

    shows all containers

  • docker ps

    shows the running containers

  • docker ps -a

    shows all containers

  • docker container run hello-world or docker run hello-world

    runs a container
    if not available locally, downloads

  • docker run -p 1903:8080 erencelik/dummy-project

    publishes exposed port(8080) of project to host(1903)
    host-port:container-port

  • docker run -p 1903:8080 -d erencelik/dummy-project

    runs in detached mode (background)

  • docker run -it ubuntu

    i for interactive and t for tty
    it runs the container and takes you straight inside the container
    here it creates an interactive bash shell in ubuntu
    'exit' command will exit shell

  • docker run -dit ubuntu

    we do the same but in detached mode
    it creates shell but we don't directly go there
    docker exec -it <cid> sh
    with this command we can go shell after running in detached mode

  • docker run -tid --name my-ubuntu ubuntu

    assigns a name to the container

  • docker run -it --rm erencelik/cheers2019

    --rm automatically removes the container when it exits

  • docker container stop <cid>

    stops a running container

  • docker container pause <cid>

  • docker container unpause <cid>

  • docker container inspect <cid>

  • docker container prune

    removes all stopped containers

  • docker container kill <cid>

    immediate stop

  • docker run -p 8080:8080 --restart=always erencelik/dummy-project

    when you restart docker desktop, the container restarts too.
    restart=always or restart=no. default is no.

  • docker top <cid>

    displays the running processes of container

  • docker stats

    shows all the stats about the running containers

  • docker run -p 8080:8080 -m 512m erencelik/dummy-project

    specific max memory: 512m/1g

  • docker logs <cid>

    shows the logs of a container

  • docker logs -f <cid>

    follow logs

  • docker cp

    copies files/folders between a container and the local filesystem
    docker cp CONTAINER:SRC_PATH DEST_PATH

  • docker commit my-ubuntu erencelik/dummy-project:0.0.2

    creates a new image from a containerā€™s changes
    this command allows us to take a running container and save its current state as an image
    for example you can run some commands in a running container via exec, make some changes
    and immediately create a new image with the latest situation
    takes two parameters: docker commit CONTAINER REPOSITORY[:TAG]

task:

  1. run a container from ubuntu image with a specific name
  2. install nginx in this container
  3. view nginx welcome page and exit
  • docker run -it --name my-ubuntu ubuntu

  • apt install nginx

    if you get this error: Unable to locate package nginx
    _run this command and try again:
    apt update
    in ubuntu, it's always suggested to run an update before installing someting new

  • service nginx start

  • apt install curl

  • curl localhost

  • exit