Docker commands

Images

Image name format

<docker-id>/<project-repo>:<tag> (default tag: 'lastest') 

Create image:

docker build -t <image> .

Pull/push images from repository:

docker pull <image>
docker push <image>

Import/export images:

docker load -i <path to image>
docker save -o <path to tarfile> <image>

List and inspect images

docker inspect <image>
docker image list

Containers

Docker run creates a new container from a image and executes it.

docker run <options> <image> --name <container name>
  
options:
 --rm removes container after finish
 -d detach/daemon
 -it interactive + terminal 

mappings:
 -p <host port>:<container port>
 -v <host folder>:<container folder>

Create, start and stop:

docker create <image> --name <container name>

docker start/stop <options> <container id/name>
options:
 -a appends output/stderr

Check logs, send SIGTERM and SIGKILL

docker logs <container>
docker stop <container>
docker kill <container>

Execute commands on running container

docker exec <-it> <container> <command>

Commit container to image

docker commit <options> <container> <image>
options:
 -c execute docker file command on image

Other

Check volumes

$ docker inspect -f '' containerid
$ docker run -it -v /tmp:/tmp ubuntu:14.04 /bin/bash

Show host ip

echo "$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+')"

References

Exploring Docker container’s file systemn

Mysql

$docker run -it --network some-network --rm mysql mysql -hsome-mysql -uexample-user -p
$docker run  --name=test-mysql --env="MYSQL_ROOT_PASSWORD=mypassword" mysql

Get password from logs / set password in docker variables:

$ docker logs mysql5.7  2>&1 | grep GENERATED

Create image

$ docker volume create --name mysqldata5.7 
$ docker run --name=mysql5.7  -v mysqldata5.7:/var/lib/mysql --env="MYSQL_ROOT_PASSWORD=mypassword" -d mysql/mysql-server:5.7.28-1.1.13
$ docker exec -it mysql5.7  mysql --host=localhost --user=root --password=mypassword 

Backup and restore

$ docker exec mysql5.7 /usr/bin/mysqldump -u root --password=mypassword computers > backup.sql
$ cat backup.sql | docker exec -i mysql5.7 /usr/bin/mysql -u root --password=mypassword computers

Run SQL

$ cat data.sql  | docker exec  -i  mysql5.7 /usr/bin/mysql -u user --password=password  computers

Debug

$ docker exec -it mysql5.7  /bin/bash
$ docker run -it -v mysqldata5.7:/tmp ubuntu:14.04 /bin/bash

Links:

https://dev.mysql.com/doc/refman/5.7/en/docker-mysql-getting-started.html

https://medium.com/better-programming/setting-up-mysql-database-in-a-docker-d6c69a3e9afe

https://hub.docker.com/r/mysql/mysql-server/tags/

Install Kubectl/Minikube

Test Kubernetes

kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.4
kubectl expose deployment hello-minikube --type=NodePort --port=8080

kubectl get services hello-minikube
minikube service hello-minikube

kubectl port-forward service/hello-minikube 7080:8080
Posted in Uncategorized

Leave a Reply

Your email address will not be published.