Skip to content

Instantly share code, notes, and snippets.

@fatihbaltaci
Last active July 2, 2020 11:06
Show Gist options
  • Save fatihbaltaci/d1fa95b9cf166de793d186c1d75f53f6 to your computer and use it in GitHub Desktop.
Save fatihbaltaci/d1fa95b9cf166de793d186c1d75f53f6 to your computer and use it in GitHub Desktop.
Useful Docker Features / Commands

Table of Contents

Notation

$ : Host, non root user

# : Host, root user

(docker)$ : Inside docker container, non root user

(docker)# : Inside docker container, root user

Useful Commands

Remove Dangling Images (none:none)

$ docker rmi -f `docker images -f "dangling=true" -q`

or

$ docker image prune

Remove Stopped Containers

$ docker rm -f `docker container ps -aq --filter status=exited --filter status=created`

or

$ docker container prune

Change Timezone in Dockerfile

RUN ln -sf /usr/share/zoneinfo/Turkey /etc/localtime

Uninstall Docker

$ sudo apt-get purge -y docker-engine docker docker.io docker-ce  
$ sudo apt-get autoremove -y --purge docker-engine docker docker.io docker-ce 

Install Docker (Latest Version)

$ sudo apt-get update && sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common && \
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - && \
    sudo add-apt-repository \
    "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) \
    stable" && \
    sudo apt-get update && sudo apt-get install docker-ce docker-ce-cli containerd.io

Open GUI within Docker Container

On the host system run following commands:

$ xhost local:root
$ xhost +local:docker

Run docker container:

Necessary arguments for GUI applications: -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=unix$DISPLAY

$ docker run -it --rm -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=unix$DISPLAY ubuntu

Inside docker container, install and run xeyes for testing.

(docker)# apt update && apt install -y x11-apps
(docker)# xeyes

Remote Docker Access with TCP for Ubuntu (Not Secure, without TLS)

https://medium.com/@sudarakayasindu/enabling-and-accessing-docker-engine-api-on-a-remote-docker-host-on-ubuntu-16-04-2c15f55f5d39

  • Open:
$ sudo vi /lib/systemd/system/docker.service
  • Change ExecStart Line:
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock -H tcp://0.0.0.0:4243
  • Restart Docker Daemon and Service:
$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

Disable FIREWALL https://linuxize.com/post/how-to-stop-and-disable-firewalld-on-centos-7/

  • Check:
$ sudo docker -H tcp://127.0.0.1:4243 ps

https://docs.docker.com/install/linux/linux-postinstall/#configure-where-the-docker-daemon-listens-for-connections#configure-where-the-docker-daemon-listens-for-connections

Docker Image Save and Load

  • Save:
$ docker save image_name > image.tar
$ gzip image.tar
  • Load:
$ gunzip image.tar.gz
$ docker load < image.tar

Restart Docker Daemon

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

Default nvidia runtime

  • Install nvidia-container-runtime

Ubuntu

$ sudo apt-get install nvidia-container-runtime

Centos

$ sudo yum install nvidia-container-runtime
  • Edit /etc/docker/daemon.json
{
    "default-runtime": "nvidia",
    "runtimes": {
        "nvidia": {
            "path": "/usr/bin/nvidia-container-runtime",
            "runtimeArgs": []
        }
    }
}
sudo pkill -SIGHUP dockerd

Install nvidia-docker2 Offline

  • Docker version: 18.09.2

  • Download .zip file that contains .deb files from Google Drive and extract it:

https://drive.google.com/file/d/1ihoSEZvQDumYX7P_VxUGmWFeRJn4X3lM/view?usp=sharing
  • Run run.sh:
$ bash run.sh
  • All the .deb files are prepared with the following command:
$ apt install apt-transport-https 
$ rm -rf /var/cache/apt/archives/*
$ curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | apt-key add -
$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
$ curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | tee /etc/apt/sources.list.d/nvidia-docker.list
$ apt-get update

$ apt-get install --download-only nvidia-docker2=2.0.3+docker18.09.2-1 nvidia-container-runtime=2.0.0+docker18.09.2-1
  • .deb files are stored in /var/cache/apt/archives/

Create docker context

With SSH

$ docker context create gcp_ssh --docker "host=ssh://myserver"

System Space Usage

$ docker system df -v

Install Docker Compose on Raspbian

$ sudo apt-get install libffi-dev libssl-dev
$ sudo apt-get install -y python3-pip
$ sudo apt-get remove python-configparser
$ sudo pip3 install docker-compose

vscode

Template: https://github.com/fatihbaltaci/vscode_docker_template

Project Settings

.vscode/settings.json

{
    "python.pythonPath": "python",
    "python.linting.pylintEnabled": true,
    "python.linting.pycodestyleEnabled": false,
    "python.linting.enabled": true,
    // "docker.host":"ssh://your-remote-user@your-remote-machine-fqdn-or-ip-here",
}

Remote Containers Settings

.devcontainer/devcontainer.json

{
	"name": "Existing Dockerfile",

	"context": "..",

	"dockerFile": "../Dockerfile",
        // "image": "ubuntu:18.04",

	"runArgs": [
		"-v", "${env:HOME}${env:USERPROFILE}/.ssh:/root/.ssh-localhost:ro",
		"-p", "1414:1414",
	],

	"settings": {
		"terminal.integrated.shell.linux": "/bin/bash",
		"python.pythonPath": "python",
		"python.linting.enabled": true,
		"python.linting.pylintEnabled": true,
		"python.linting.pylintPath": "pylint",
		"python.linting.pylintArgs": ["--extension-pkg-whitelist=cv2"]
	},

	"postCreateCommand": "mkdir -p ~/.ssh && cp -r ~/.ssh-localhost/* ~/.ssh && chmod 700 ~/.ssh && chmod 600 ~/.ssh/*",

	"extensions": ["ms-python.python"]
}

Errors

1

NVIDIA/nvidia-docker#1165

unknown flag: --gpus
sudo apt-get install docker-ce-cli
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment