|
|
---
|
|
|
|
|
|
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]]
|
|
|
|
|
|
---
|
|
|
|
|
|
 
|
|
|
|
|
|
```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
|
|
|
|
|
|
 
|
|
|
|
|
|
#### Program installation
|
|
|
|
|
|
1. **Prerequisites**
|
|
|
|
|
|
`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**
|
|
|
|
|
|
`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**
|
|
|
|
|
|
`sudo apt update`
|
|
|
`sudo apt install docker-ce docker-ce-cli containerd.io`
|
|
|
|
|
|
4. **Test install**
|
|
|
|
|
|
`sudo systemctl status docker`
|
|
|
|
|
|
 
|
|
|
|
|
|
#### Configuring user permissions
|
|
|
|
|
|
Users with sudo rights need to be added to the 'docker' group for being able to instruct docker:
|
|
|
|
|
|
`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.
|
|
|
|
|
|
```
|
|
|
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:
|
|
|
|
|
|
`sudo chmod +x /usr/local/bin/docker-compose`
|
|
|
|
|
|
Installation can be checked as follows:
|
|
|
|
|
|
`sudo docker–compose --version`
|
|
|
|
|
|
 
|
|
|
|
|
|
---
|
|
|
|
|
|
 
|
|
|
|
|
|
### docker elements
|
|
|
|
|
|
 
|
|
|
|
|
|
#### 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:
|
|
|
|
|
|
```
|
|
|
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:
|
|
|
|
|
|
`sudo docker-compose up -d`
|
|
|
|
|
|
 
|
|
|
|
|
|
#### Maintaining containers
|
|
|
|
|
|
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:
|
|
|
|
|
|
>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:
|
|
|
|
|
|
`~/.bashrc`
|
|
|
|
|
|
The command needs to be run periodically.
|
|
|
|
|
|
 
|
|
|
|
|
|
#### Updating containers
|
|
|
|
|
|
For updating containers, just amend the docker-compose file and run the following command to preserve the mounted data:
|
|
|
|
|
|
`docker-compose up -d`
|
|
|
|
|
|
From within the container folder.
|
|
|
|
|
|
 
|
|
|
|
|
|
#### Update environment variables
|
|
|
|
|
|
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**
|
|
|
|
|
|
`sudo systemctl stop docker`
|
|
|
|
|
|
2. **Edit the json config file**
|
|
|
|
|
|
Location: `/var/lib/docker/containers/(container id)/config.v2.json`
|
|
|
|
|
|
Within the 'Env' declaration, add the requested data field:
|
|
|
|
|
|
> "DATA_FIELD=data_value"
|
|
|
|
|
|
3. **Restart docker**
|
|
|
|
|
|
`sudo systemctl start docker`
|
|
|
|
|
|
 
|
|
|
|
|
|
---
|
|
|
|
|
|
 
|
|
|
|
|
|
### Basic commands
|
|
|
|
|
|
 
|
|
|
|
|
|
#### List containers
|
|
|
|
|
|
`docker container ls`
|
|
|
|
|
|
 
|
|
|
|
|
|
#### Execute commands in container
|
|
|
|
|
|
`docker exec -it -u (username) (command)`
|
|
|
|
|
|
 
|
|
|
  |