5.1 KiB
Alias | Tag | Date | DocType | Hierarchy | TimeStamp | location | CollapseMetaTable | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
2021-09-19 | Personal | NonRoot |
|
true |
Parent:: Selfhosting, Server Alias, Server Tools
^Top
name Save
type command
action Save current file
id Save
^button-dockerSave
Configuring docker
title: Summary
collapse: open
This note runs through [docker](https://docker.com), a free tool to create containers to run independent applications.
style: number
Installation
Program installation
- Prerequisites
~~~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.
- Pull the software signature key & image
~~~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"
~~~
- Install docker
~~~bash
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
~~~
- Test install
~~~bash
sudo systemctl status docker
~~~
Configuring user permissions
#^Top Users with sudo rights need to be added to the 'docker' group for being able to instruct docker:
~~~bash
sudo gpasswd -a (username) docker
sudo newgrp docker
~~~
title: [[Configuring Docker|docker]] for non root users
[[Configuring Docker|docker]] predominantly works for the root user. In order to let non-root users instruct Docker, users need to be added to the Docker group:
`sudo usermod -aG docker (username)`
Potentially, the Docker group needs to be defined:
`sudo groupadd docker`
Installing docker-compose
Docker-compose is a script generatir enabling leaner execution and update of each container. The following commands install the wizard.
~~~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:
~~~bash
sudo chmod +x /usr/local/bin/docker-compose
~~~
Installation can be checked as follows:
~~~bash
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:
~~~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:
- A directory for the app must be created
- A yaml file for docker-compose must be created in the folder
- The docker-compose command for initialisation must be run from the folder:
~~~bash
sudo docker-compose up -d
~~~
Maintaining containers
#^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:
~~~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:
~/.bashrc
The command needs to be run periodically.
Otherwise, use the below sequence in the relevant folder:
~~~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:
~~~bash
docker-compose up -d
~~~
From within the container folder.
Update environment variables
#^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:
- stop docker
~~~bash
sudo systemctl stop docker
~~~
- Edit the json config file
/var/lib/docker/containers/(container id)/config.v2.json
Within the 'Env' declaration, add the requested data field:
~~~bash
"DATA_FIELD=data_value"
~~~
- Restart docker
~~~bash
sudo systemctl start docker
~~~
Basic commands
List containers
~~~bash
docker container ls
~~~
Execute commands in container
~~~bash
docker exec -it -u (username) (command)
~~~