Docker employs a layered approach to documentation, where every change you make to the container creates a new layer
Steps to install: https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository
systemctl status docker # verify Docker is running
systemctl enable --now docker # enable Docker immediately (if not running and not enabled)
usermod -aG docker <username> # add yourself to docker group so you don't need sudo each time
docker ps
- the COMMAND
is the command that was run when the container was createdapt update
before trying to install anythingsystemctl
# -i: interactive mode
# -t: psuedo TTY
# -d: detached - run in the bg
docker info # details about current docker installation, images, containers, etc
docker search <keyword> # search for pre-existing image
docker pull <image-name> # pull docker image to local machine
docker images # list docker images on your server
docker rmi <image-id> # remove image from your server
docker rm <container-id> # remove persistent container
docker run <image-name> # create running container from image
docker run -it ubuntu /bin/bash # create container from ubuntu image and get a bash shell in it
docker ps # get info about running containers
docker ps -a # get info about all containers
docker start <container-id> # start a stopped container
docker stop <container-id> # stop a running container (SIGTERM -> SIGKILL)
docker attach <container-id> # get shell in running container
docker run -dit <image-name> <command> # run in detached mode - do not stop until explicitly told
CTRL + P, CTRL + Q # exit container without stopping
docker commit <container-id> <repo>/<image-name>:<tag> # create image from running container
# --- Port redirection --- #
docker run -dit -p <host-port>:<container-port> <image> <command>
docker run -dit -p 8080:80 ubuntu /bin/bash # forward container traffic on 80 to host port 8080
File with a set of instructions to create a Docker image - a recipe for a Docker image
Steps:
Dockerfile
FROM ubuntu # define base image. Downloads from Dockerhub if not local
MAINTAINER Ryan <ryan@ryan.com> # optional: declaring image author
# Avoid confirmation messages
ARG DEBIAN_FRONTEND=noninteractive # set env var so package installations do not ask questions - use default answers
# Update the container's packages
RUN apt update; apt dist-upgrade -y # run these commands when creating the image
# Install apache2 and vim
RUN apt install -y apache2 vim-nox # install these packages
# Start Apache
ENTRYPOINT apache2ctl -D FOREGROUND # command to run the app in the container
# --- Create image from Dockerfile --- #
# run in same dir as Dockerfile
docker build -t <repo>/<image-name>:<tag> . # build image from dockerfile - run in dir with dockerfile
docker build -t myrepo/apache-server:1.0 . # example
Descendant of LXC (Lex-C, “Linux Containers”) which uses cgroups, but LXD does not replace LCD - only builds on top of it:
lxc
command, but commands for LXD layer use lxd
ubuntu
user# --- Setup --- #
sudo snap install lxd # install lxd snap
usermod -aG lxd <username> # add to lxd group
lxd init # initialize new LXD installation
...
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: none
Would you like the LXD server to be available over the network? (yes/no) [default=no]: yes
...
# --- Common commands --- #
lxc launch ubuntu:22.04 <container-name> # downloads and starts container
lxc list # list all containers
lxc start <container-name> # start container
lxc stop <container-name> # stop container
lxc delete <container-name> # delete container
lxc image list # list downloaded images
lxc image delete <image-name> # delete image
# --- Managing containers --- #
lxc exec <container-name> bash # execute bash command in container
lxc exect <container-name> -- su --login <username> # login as specific user
lxc config set my container boot.autostart 1 # config container to start on boot
curl <container-ip-addr> # verify container is online