Changes between Version 4 and Version 5 of Docker


Ignore:
Timestamp:
07/18/2025 10:06:08 PM (3 days ago)
Author:
Tim Harvey
Comment:

added examples for ubuntu 22.04 and 24.04 containers and updated installation instructions

Legend:

Unmodified
Added
Removed
Modified
  • Docker

    v4 v5  
    1919[=#install]
    2020== Installing Docker on Ubuntu
    21 
    2221Install (from official Docker repo)
    2322{{{#!bash
     
    2827# install Docker repo
    2928sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu  $(lsb_release -cs)  stable"
    30 #install
     29# install
    3130sudo apt-get update
    3231sudo apt-get install docker-ce docker-ce-cli containerd.io
    33 }}}
    34  - Note the docker group is created but no users are added to it so you need to use sudo to run docker commands or add your user to the docker group (see https://docs.docker.com/install/linux/linux-postinstall/)
    35 
    36 It is recommended that you add your user to the docker group so that you do not have to run commands as root. The commands referenced on this page assume you have done that and if you have not, you need to run any docker commands via {{{sudo}}}.
     32# add your local user to the docker group
     33sudo usermod -aG docker $USER
     34}}}
    3735
    3836Testing:
     
    5755mkdir docker-images && cd docker-images
    5856# create a directory for your image
    59 mkdir ubuntu-16.04 && cd ubuntu-16.04
    60 cat << EOF > Dockerfile
    61 # Docwnload base image ubuntu 16.04
     57mkdir ubuntu-16.04
     58cat << EOF > ubuntu-16.04/Dockerfile
     59# Download base image ubuntu 16.04
    6260FROM ubuntu:16.04
    6361# Update software repo
     
    6664RUN  apt-get install -y build-essential git python \
    6765libssl-dev ncurses-dev device-tree-compiler bc gawk zlib1g-dev file \
    68 wget unzip sudo \
     66wget unzip sudo
    6967EOF
    7068}}}
     
    7270{{{#!bash
    7371# build docker image
    74 docker build --tag ubuntu-16.04 .
     72docker build --tag ubuntu-16.04 ubuntu-16.04
    7573}}}
    7674 1. List images
     
    195193== Examples
    196194
     195The following examples show how to create various Ubuntu containers from an Ubuntu host OS as a non root user. The steps will vary a bit on other host Operating Systems.
     196
     197The examples create a local user with sudo access using variables (USER, UID, GID) that can be specified when you build the image to make it easier to share files on your host when using a bind mount.
     198
     199We pass arguments via the 'docker build' command passing in your USER, UID, GID and the Dockerfile uses these to create an image with a user matching these args such that if you run a container with this image you can bind-mount directories from your host and have the same file ownership and permissions as the user on the host.
     200
     201We show an example of launching a container binding your home directory and a /tftpboot directory to take advantage of this.
     202
     203The docker-run '--it' option is a shortcut for '--interactive' and '--tty' which allocates a pseudo-TTY terminal and keeps STDIN open even if not attached. These options allow the bash process to start in the container, attaches the host OS console to the processes standard input/output/error, and allocates a text-only console.
     204
     205The docker-run '--rm' option automatically removes the container when it exits so that you don't accidentally leave a bunch of unused containers that allocate resources. If instead you wish to create a container that you can detach from and come back to later you can remove the '--rm' option and attach to it again later with the 'docker attach <containerid>' command after using 'docker container ls' to show available containers.
     206
     207The docker-run '--privileged' option will allow accessing host devices from the container in case you want to access a removable storage device for example.
     208
     209The docker-run '--hostname' option provides a custom hostname that helps you remember you are in a docker container. By default /etc/hostname will contain the container ID. Often the hostname is included in your prompt as is the case with Ubuntu so providing a custom hostname helps you keep track of what you are doing.
     210
    197211=== Ubuntu 20.04 Focal container
    198 
    199 This example shows how to create an Ubuntu 20.04 Focal container from an Ubuntu host OS. The steps will vary a bit on other host Operating Systems.
    200 
    201 The example here creates a local user with sudo access using variables (USER, UID, GID) that can be specified when you build the image to make it easier to share files on your host when using a bind mount.
    202 
    2032121. Create a docker 'image' based on Ubuntu 20.04:
    204  - On a Linux host, docker images are created from a DockerFile that provides details and commands used to build the image
     213 - On a Linux host, docker images are created from a Dockerfile that provides details and commands used to build the image
    205214{{{#!bash
    206215mkdir ubuntu-focal
    207 cat << EOF > ubuntu-focal/DockerFile
     216cat << \EOF > ubuntu-focal/Dockerfile
    208217# from base image ubuntu 20.04
    209218FROM ubuntu:20.04
     
    222231
    223232# and give that user permission to use sudo
    224 RUN apt-get install sudo
     233RUN apt-get install -y sudo
    225234RUN echo "$USER ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
    226235
    227 # and set the prompt as a reminder
    228 RUN echo 'export PS1="\u@docker-focal:\w\$ "' >> /etc/bash.bashrc
    229236# switch to this user
    230237USER $USER
     
    234241 - use the 'docker build' command to build the OS image passing in args for your USER, UID, GID
    235242{{{#!bash
    236 docker build --tag ubuntu-22.04 --build-arg USER=$USER --build-arg UID=$(id -u) --build-arg GID=$(id -g) ubuntu-focal/
    237 }}}
    238   * the '--build-arg' options above are optional but show how you can use your host Linux OS user/group to replace the default ones specified in the DockerFile. This can be very useful if you mount a filesystem from your host OS into your container and want to share user permissions
    239   * building the 'image' is a one-time operation. You only need to repeat it if you change the DockerFile specifying the image
    240   * if wanted you can add additional 'RUN apt-get install -y <packages>' steps before the 'USER $USER' command (which switches the user from root to a non-root user, otherwise you would need to use sudo) to install additional packages so that you don't have to do that every time you create a new container from the image
     243docker build --tag ubuntu-focal --build-arg USER=$USER --build-arg UID=$(id -u) --build-arg GID=$(id -g) ubuntu-focal/
     244}}}
     245
    2412461. Create and start docker container
    242247{{{#!bash
    243 docker run -it --privileged \
     248docker run --rm -it --privileged \
    244249  --volume /home/$USER:/home/$USER \
    245250  --volume /tftpboot/tftpboot \
     251  --hostname docker-focal \
     252  --name focal ubuntu-focal
     253}}}
     254
     255
     256=== Ubuntu 22.04 Jammy container
     2571. Create a docker 'image' based on Ubuntu 22.04:
     258 - On a Linux host, docker images are created from a Dockerfile that provides details and commands used to build the image
     259{{{#!bash
     260mkdir ubuntu-jammy
     261cat << \EOF > ubuntu-jammy/Dockerfile
     262# from base image ubuntu 22.04
     263FROM ubuntu:22.04
     264
     265# Disable Prompt During Packages Installation
     266ARG DEBIAN_FRONTEND=noninteractive
     267
     268# update list of available packages
     269RUN apt-get update
     270
     271# add a non-root user
     272ARG USER=build
     273ARG UID=1000
     274ARG GID=1000
     275RUN groupadd -g $GID $USER && useradd -g $GID -m -s /bin/bash -u $UID $USER
     276
     277# and give that user permission to use sudo
     278RUN apt-get install -y sudo
     279RUN echo "$USER ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
     280
     281# switch to this user
     282USER $USER
     283WORKDIR /home/$USER
     284EOF
     285}}}
     286 - use the 'docker build' command to build the OS image passing in args for your USER, UID, GID
     287{{{#!bash
     288docker build --tag ubuntu-jammy --build-arg USER=$USER --build-arg UID=$(id -u) --build-arg GID=$(id -g) ubuntu-jammy/
     289}}}
     290
     2911. Create and start docker container
     292{{{#!bash
     293docker run --rm -it --privileged \
     294  --volume /home/$USER:/home/$USER \
     295  --volume /tftpboot/tftpboot \
     296  --hostname docker-jammy \
    246297  --name jammy ubuntu-jammy
    247298}}}
    248  * the '--volume' options are optional but show how you can bind mount directories from your host OS to the container's OS. In this case we have mounted /tftpboot so we can copy files from the container to the hosts's tftpboot directory where a TFTP server may be running. We also mount your home director from the host OS to allow our host OS to get to source files we use
    249  * Note that once you 'run' a container it remains present even if you have detached from it meaning you can later re-attach to it
    250  * The '--it' option is a shortcut for '--interactive' and '--tty' which allocates a pseudo-TTY terminal and keeps STDIN open even if not attached. These options allow the bash process to start in the container, attaches the host OS console to the processes standard input/output/error, and allocates a text-only console.
    251 
    252 If you at some point exit your docker container you can attach to it again later with the 'docker attach <containerid>' command after using 'docker container ls' to show available containers.
    253 
    254 
     299
     300
     301=== Ubuntu 24.04 Noble container
     3021. Create a docker 'image' based on Ubuntu 24.04:
     303 - On a Linux host, docker images are created from a Dockerfile that provides details and commands used to build the image
     304{{{#!bash
     305mkdir ubuntu-noble
     306cat << \EOF > ubuntu-noble/Dockerfile
     307# from base image ubuntu 24.04
     308FROM ubuntu:24.04
     309# Ubuntu 24.04 official docker image creates an 'ubuntu' user with uid/gid 1000 that we want to use here - remove it
     310RUN userdel -r ubuntu
     311
     312# Disable Prompt During Packages Installation
     313ARG DEBIAN_FRONTEND=noninteractive
     314
     315# update list of available packages
     316RUN apt-get update
     317
     318# add a non-root user
     319ARG USER=build
     320ARG UID=1000
     321ARG GID=1000
     322RUN groupadd -g $GID $USER && useradd -g $GID -m -s /bin/bash -u $UID $USER
     323
     324# and give that user permission to use sudo
     325RUN apt-get install -y sudo
     326RUN echo "$USER ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
     327
     328# switch to this user
     329USER $USER
     330WORKDIR /home/$USER
     331EOF
     332}}}
     333 - use the 'docker build' command to build the OS image passing in args for your USER, UID, GID
     334{{{#!bash
     335docker build --tag ubuntu-noble --build-arg USER=$USER --build-arg UID=$(id -u) --build-arg GID=$(id -g) ubuntu-noble/
     336}}}
     337
     3381. Create and start docker container
     339{{{#!bash
     340docker run --rm -it --privileged \
     341  --volume /home/$USER:/home/$USER \
     342  --volume /tftpboot/tftpboot \
     343  --hostname docker-noble \
     344  --name noble ubuntu-noble
     345}}}
     346