Docker (SYS 265) - Chromosom3/TechNotes GitHub Wiki
Ubuntu 20.04 Setup
Networking and Hostname
To assign a static IP you will need to navigate to the /etc/netplan/
directory. In there you will find your network configuration files. For this lab the configuration file was call 00-installer-config.yaml
. Open the file with your preferred text editor and adjust the section for your network interface. If you don't know your network interface use ip a
to show the interfaces. The configuration file should look similar to the one below. Note when editing the yaml file you can not include tabs, only spaces are allowed.
# This is the network config written by 'subiquity'
network:
ethernets:
ens160:
dhcp4: false
addresses: [10.0.5.12/24]
gateway4: 10.0.5.2
nameservers:
search: [dylan.local]
addresses: [10.0.5.5,1.1.1.1]
version: 2
Once you finish adjusting the configuration file save and run the following command, netplan apply
. This will apply the changes. Now that the network is setup it is time to adjust the hostname. To do this run hostnamectl set-hostname docker01-dylan
. Then adjust the /etc/hosts
file, replacing ubuntu with your new hostname. The lab document mentioned updating the cloud.cfg
file. To do this navigate to /etc/cloud
and edit cloud.cfg
changing preserver_hostname
to true
.
Users and SSH
Like the other systems in the environment there will be a named sudo user. To create a new named user run useradd -md /home/dylan dylan
, passwd dylan
and usermod -aG sudo dylan
. Note if you want bin bash to be the default shell you will either need to add -s /bin/bash
to the useradd command or adjust the /etc/passwd
file. Now that there is a named sudo user on the system root SSH will be disabled. Adjust /etc/ssh/sshd_config
and include PermitRootLogin No
. Then run systemctl restart sshd
to restart SSH and apply changes.
Docker
Docker Instillation
Before installing Docker the system needs to be up to date, sudo apt update && sudo apt upgrade -y
. Once the system is up to date use sudo apt install apt-transport-https ca-certificates curl software-properties-common
to install the prerequisites. Then you will need to add the GPG key for the Docker Repo on your system, curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
. Then finally add the Docker repo to the APT sources sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
. Now install Docker with apt install docker-ce -y
. Once Docker is installed it will automatically run, use systemctl status docker
to check its status.
Docker Commands
Docker only allows commands to be run as the root user or users in the docker group. To allow non-root users to run docker commands run usermod -aG docker dylan
. This will add the user to the docker group thus giving them permission to run docker commands. Running docker
will present a list of commands, running docker comand --help
will allow you to get the help page for that docker command. To test docker you can run docker run hellow-world
. One docker command that is useful is docker ps
. This command will list docker containers. By default it will only show you running Docker containers. If you use the -a flag it will show all Docker containers. Using docker run
with the -d
flag will run the container and instantly detach from it and have it run in the background. This is the same as --detach
. The -P
flag is the same as --publish-all
. This flag publishes all exposed ports to random ports on the host operating system.
Docker Compose
Docker Compose allows the quick and simple management of multi-container applications. To install docker compose you will first need to download the executable, sudo curl -L "https://github.com/docker/compose/releases/download/1.28.5/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
. Then make it executable with chmod +x /usr/local/bin/docker-compose
. Use docker-compose --version
to ensure Docker Compose was setup correctly. To use Docker Compose you will need to create a docker-compose.yml
file. An example file can be found here. This should be placed in its own project directory. From the project directory run docker-compose up -d
.