Docker

other cheats

Mac Install

install docker client:

brew install docker

install daemon:

(cask = GUI app install)
brew cask install docker

Docker Images

find images

docker search mariadb

search image details:

Docker Hub

pull image to localhost

docker pull mariadb:latest

docker pull mariadb/server:latest

build an image

reference

#-t = repository name + tags. Here repository name = teemu/mariadb, tags would be added as teemu/mariadb:v0.1
#-f = location of dockerfile, otherwise Dockerfile in context dir
#./docker in the end = context dir, look here for DockerFile and any files it references

docker build -t teemu/mariadb -f ~/Dropbox/mariadb_dockerfile.txt ./docker

docker build -t mytf .

build without using cached images

docker build --no-cache

list local images

docker image ls

remove local image

docker image rm <imagename>

run image: run

#–name = container name,
#-d = detached, container exits when root process in container exits, container will run in background with -d.
#to see logs in a background container, do docker logs -f CONTAINERID
#-e = set environment variable, can have multiple -e for multiple environment variables

docker run --name mymariadb-container -e MYSQL_ROOT_PASSWORD=secret_pw -d mariadb/server:latest

Networking

docker networking

access host from inside docker image

wget "host.docker.internal:8080/my_url" -> localhost is marked as "host.docker.internal" from inside container

map container port to host

publish vs expose: in summary it seems publish automatically exposes, so if you look at a Dockerfile with EXPOSE statements, read the link <-. it seems expose is more for documentation but check elsewhere for better informed content.

#publish port 3306 from container as 3310 on host (see link for expose vs publish):

docker run --name mymariadb-container -p3310:3306 -e MYSQL_ROOT_PASSWORD=secret_pw -d mariadb/server:latest

#publish all ports, on same port on host

docker run --name mymariadb-container -P -e MYSQL_ROOT_PASSWORD=secret_pw -d mariadb/server:latest

GPU access

#enable all gpu’s in the container, map datadir to containers /mystuff, and host port 8889 to container port 8888:

docker run --gpus all --name jupyt -v /home/bob/mydatadir:/mystuff -p8889:8888 -td tensorflow/tensorflow:latest-gpu

List containers

running containers:

more info

docker ps

include stopped:

docker ps -a

or alternatively:

docker container ls -a

show container size:

docker ps -s

size + include stopped:

docker ps -as

container shell

shell into container

#-i = interactive, pipes STDOUT from container to current shell, STDIN from current shell to container
#-t = pseudo tty, allows the piped commands to be passed to the container shell

docker exec -it mymariadb-container bash

docker exec -it my_container_name sh

more: using interactive without -t

Docker Info

system info:

docker info

show container details:

docker inspect mymariadb-container

docker container IP address

docker inspect <containername> | grep IPAddress

container logs

docker logs <containername>

follow log like tail -f:

docker logs <containername> -f

list running containers (+ports)

docker container ls

list containers matching name filter:

docker container ls -f name=mymariadb

Cleanup

stop a running container

docker stop <id>

docker stop <name>

remove stopped containers

docker container prune

docker container prune -f #<- f=force, = do not ask for confirmation

remove everything locally:

(images, containers, …)

docker system prune

Docker Compose:

environment variables

.env <- in folder where docker-compose is run

start all containers in compose file

docker compose up

stop all containers in compose file

docker compose down

jupyter in docker

jupyter notebook --no-browser --ip=0.0.0.0 --allow-root --NotebookApp.token='' --NotebookApp.password=''

jupyter

following are jupyter notebook related commands, not really docker but i need to put them somewhere so why not.

set jupyter themes

pip install jupyterthemes

list jupyter these

jt -l

change to dark theme:

jt -t chesterish

reset jupyter theme:

jt -r

Advertisement