Docker (for Windows) and Cheatsheet - adonisv79/bytecommander.com GitHub Wiki

Installation

Docker is now supported for windows and also allows for creating windows based images. To work on windows locally, you must enable Hyper-V in the Windows Features Windows Features

Next, let us install the official installer for windows. Go to Docker for windows on DockerHub and click the download button. Run the executable and follow the instructions step by step.

Note: There is an option that states to use 'Windows Containers'. don't use that. It is ok to switch later but this feature for now is useless if you will be using 3rd party made images like Redis whoc does not have an image built on Windows.

Commonly used images

The dockerhub offers a lot of publicly made images for anything. just visit https://hub.docker.com/search?q=&type=image

Commands

Get current version (also usefull for checking if docker is installed and running)

docker --version

Run a docker container. This will spawn a run-time instance from an existing image in your machine (thru the Docker Daemon). if the image does not exist, it will try to locate it in dockerhub and download the image first.

// run 'docker run --help' for more options
docker run hello-world
docker run --name "my-redis" redis:latest
docker run --name "my-service" -p 3000:3000 -e REDIS_PORT=6379 --link my-redis adonisv79/test-express
// most common used attributes are -i (enable STDIN) and -t (use TTY or terminal)
// -- name obviously gives it a name
// -p assigns an internal (container) port to the machine's actual port.

Get list of running containers (add '-a' to list also inactive ones)

docker ps

Building a docker image

docker build -t projects/versiontag-1.0.0 .

Get IP Address of a running container

docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-sessions

Renaming docker containers

// docker rename {container-id or container-name} {new-name}
docker my-redis redis-sessions

Start, Stop, Restart

// docker {start/stop/restart} {container-id or container-name}
docker restart redis-sessions

Get list of images

docker images

Pull an image from dockerhub

docker pull redis

Remove all unused images, containers, volumes and network (add -a to remove even used ones)

docker system prune

remove a specific image

// format: docker rmi {image-id or image-tag}
docker rmi redis:latest

remove a specific container

// format: docker rm {container-id or container-name}
docker rm redis-sessions

Logging

// format: docker container logs {container-id or container-name}
docker container logs redis-sessions

Exec (used to execute a command inside a container. but commonly used to go inside a docker image)

// '-it' is similar to '-i and -t' which stands for interactive mode and allow TTY
// docker exec -it {container-id or container-name} bash
docker exec -it my-ubuntu bash

ZIP/Archive by Export. This is useful for saving containers into a local tar file to be distributed or archived and loadeded later.

// format: docker export --output={filename} {container-id or container-name}
docker export --output="MyFileName" my-ubuntu
// format: docker import {path}
docker import /home/adonisv79/MyFileName