You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

290 lines
4.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
Alias: ["docker"]
Tag: ["Computer", "Server", "Container"]
Date: 2021-09-19
DocType: "Personal"
Hierarchy: "NonRoot"
TimeStamp:
location: [51.514678599999996, -0.18378583926867909]
CollapseMetaTable: Yes
---
Parent:: [[Selfhosting]], [[Server Alias]], [[Server Tools]]
---
^Top
 
```button
name Save
type command
action Save current file
id Save
```
^button-dockerSave
 
# Configuring docker
 
```ad-abstract
title: Summary
collapse: open
This note runs through [docker](https://docker.com), a free tool to create containers to run independent applications.
```
 
```toc
style: number
```
 
---
 
### Installation
[[#^Top|TOP]]
 
#### Program installation
1. **Prerequisites**
```ad-command
~~~bash
sudo apt update
sudo apt install curl apt-transport-https ca-certificates software-properties-common
~~~
```
If the packages are already installed and to the latest version, the OS will skip.
2. **Pull the software signature key & image**
```ad-command
~~~bash
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key ad
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
~~~
```
3. **Install docker**
```ad-command
~~~bash
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
~~~
```
4. **Test install**
```ad-command
~~~bash
sudo systemctl status docker
~~~
```
 
#### Configuring user permissions
[[#^Top|TOP]]
Users with sudo rights need to be added to the 'docker' group for being able to instruct docker:
```ad-command
~~~bash
sudo gpasswd -a (username) docker
sudo newgrp docker
~~~
```
 
#### Installing docker-compose
Docker-compose is a script generatir enabling leaner execution and update of each container. The following commands install the wizard.
```ad-command
~~~bash
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
~~~
```
File permissions need to be changed for docker-compose to be executable:
```ad-command
~~~bash
sudo chmod +x /usr/local/bin/docker-compose
~~~
```
Installation can be checked as follows:
```ad-command
~~~bash
sudo dockercompose --version
~~~
```
 
---
 
### docker elements
[[#^Top|TOP]]
 
#### docker network
In order for docker to work, each container needs an internal IP address. This is defined by an internal docker network as follows:
```ad-command
~~~bash
sudo docker network create --driver=bridge --subnet=xxx.yyy.zzz.0/24 --gateway=xxx.yyy.zzz.1 dockernet
~~~
```
x, y, z to be defined by the user.
 
#### Creating containers
Containers are created through docker-compose. Tutorial exist for each application to run in docker. In short:
1. A directory for the app must be created
2. A yaml file for docker-compose must be created in the folder
3. The docker-compose command for initialisation must be run from the folder:
```ad-command
~~~bash
sudo docker-compose up -d
~~~
```
 
#### Maintaining containers
[[#^Top|TOP]]
Maintaining containers with docker is arduous and easier to do with docker-compose.
Easiest is to create aliases in the .bashrc of home directory by adding:
```ad-code
~~~bash
alias dc-up='sudo docker-compose --compatibility up -d'
alias dc-update='sudo docker-compose pull && sudo docker-compose --compatibility up -d'
alias dc-update-all='for d in ./*/ ; do (cd "$d" && dc-update); done'
~~~
```
To the following file:
```ad-path
~/.bashrc
```
The command needs to be run periodically.
Otherwise, use the below sequence in the relevant folder:
```ad-command
~~~bash
docker-compose down
docker-compose pull
docker-compose up -d
~~~
```
 
#### Updating containers
For updating containers, just amend the docker-compose file and run the following command to preserve the mounted data:
```ad-command
~~~bash
docker-compose up -d
~~~
```
From within the container folder.
 
#### Update environment variables
[[#^Top|TOP]]
Docker does not have a standard way to update environment variables, and requires to take down and then re-initialise a container with the appropriate variable fed in the run script. To avoid that, the followong steps can be taken:
1. **stop docker**
```ad-command
~~~bash
sudo systemctl stop docker
~~~
```
2. **Edit the json config file**
```ad-path
/var/lib/docker/containers/(container id)/config.v2.json
```
Within the 'Env' declaration, add the requested data field:
```ad-code
~~~bash
"DATA_FIELD=data_value"
~~~
```
3. **Restart docker**
```ad-command
~~~bash
sudo systemctl start docker
~~~
```
 
---
 
### Basic commands
[[#^Top|TOP]]
 
#### List containers
```ad-command
~~~bash
docker container ls
~~~
```
 
#### Execute commands in container
```ad-command
~~~bash
docker exec -it -u (username) (command)
~~~
```
[[#^Top|TOP]]