Docker Cheatsheet

Docker run params

-d: Run as a daemonized process
--rm: When a container stops, it's deleted
-i: Use STDIN
-t: Allocates sudo terminal to interact with the terminal

Restart stopped docker container

docker start -ai

Push an image to the Docker repo

docker push <your name>/<image name>

Pull an image from the Docker repo

docker pull <name of image>

Debugging

If you want to start debugging a Dockerfile you're working on, do the following:

docker run -d --name=<container name> <your name>/<image name> watch "echo 'test' >> /var/log/test.log"

Next, attach to it:

docker exec -it <name of running container> bash #/bin/sh for alpine

Makefile template

docker-build:
	docker build . -t <your name>/<image name>

docker-debug:
	docker run -d --name=<container name> <your name>/<image name> watch "echo 'test' >> /var/log/test.log"
	docker exec -it <container name> bash #/bin/sh for alpine

docker-destroy:
	 docker stop <container name> && docker rm <container name>

Resources

https://medium.com/@betz.mark/ten-tips-for-debugging-docker-containers-cde4da841a1d

Useful commands

Export container to tar:

docker save <container name or id> > <name of tar>.tar

Import docker tar:

docker load < <name of tar>.tar

Stop & Remove docker containers:

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

Remove docker image:

docker rmi <image name or id>

Get shell on running docker container:

docker exec -it <container name or id> /bin/bash

Start container and get shell (will stop container if you stop or exit terminal):

docker start -ai <container name or id>

Start container:

docker start <container name or id>

List docker volumes:

docker volume ls

Get mount point of a docker volume

docker volume inspect --format '{{ .Mountpoint }}' <volume name>

Copy file from running container to host

docker cp <container name>:<path to file> <location on host machine to copy file to>

Copy file form host to running container

docker cp <file> <container name>:<where you want file to go on container>

Pass proxy settings as params to docker run

-e http_proxy='<proxy settings>' \
-e https_proxy="<your proxy settings>" \
-e no_proxy="<your proxy settings>" \
-e HTTP_PROXY="<your proxy settings>" \
-e HTTPS_PROXY="<your proxy settings>" \
-e NO_PROXY="<your proxy settings>"

Resource: https://stackoverflow.com/questions/30494050/how-do-i-pass-environment-variables-to-docker-containers

Use proxy settings with a Docker Image

If you need proxy action in your Docker containers, add the following to your Dockerfile:

ENV http_proxy="<your proxy settings>" \
    https_proxy="<your proxy settings>" \
    no_proxy="<your proxy settings>" \
    HTTP_PROXY="<your proxy settings>" \
    HTTPS_PROXY="<your proxy settings>" \
    NO_PROXY="<your proxy settings>"

Mount directory using relative path

For this example, let's imagine we have a container which takes an argument, and the output from running the container should go into a shared folder:

docker run -v $(PWD)/<folder to share w/ container>:/<location of folder to share in container>/<folder to share> <your name>/<image name> <argument container takes>

Get bash shell to container as a specific user

docker exec -it -u <user> <container name> /bin/bash

For example:

docker exec -it -u root jovial_mclean /bin/bash

Miscellaneous concepts

Docker shortcuts in zsh

Under your ~/.zshrc, look for this field:

plugins=(<whatever>)

Once you've located it, change it to look something like this:

plugins=(git docker docker-compose nmap)

Welcome to the world of tab completion for your docker commands. You've leveled up. Bonus points for nmap and git shortcuts, which (of course) are completely unrelated to docker.

Install Docker on Kali

https://www.ptrace-security.com/2017/06/14/how-to-install-docker-on-kali-linux-2017-1/

Container Registry in Gitlab

Login to the repository:

docker login repository.gitlab.com:4567

Create container image:

docker build -t repository.gitlab.com:4567/project-name/container-name:version .
# To also have a latest tag:
docker build -t repository.gitlab.com:4567/project-name/container-name:latest .

Upload container image:

docker push repository.gitlab.com:4567/project-name/container-name:version
# To also have a latest tag:
docker push repository.gitlab.com:4567/project-name/container-name:latest

Logout of the repository:

docker logout repository.gitlab.com:4567

Docker Compose

Installation on Ubuntu

As root:

curl -L https://github.com/docker/compose/releases/download/<version>/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose 

# To test:
docker-compose --version

Resource: https://www.digitalocean.com/community/tutorials/how-to-install-docker-compose-on-ubuntu-16-04

Run bash script in container

Add this line at end the of the bash script: exec bash

Resource: https://github.com/docker-library/wordpress/issues/205