Changes between Initial Version and Version 1 of Docker


Ignore:
Timestamp:
08/29/2022 08:47:11 PM (20 months ago)
Author:
Tim Harvey
Comment:

initial page

Legend:

Unmodified
Added
Removed
Modified
  • Docker

    v1 v1  
     1Using Docker for development
     2
     3Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. The software that hosts the containers is called Docker Engine. Containers are isolated from one another and bundle their own software, libraries and configuration files.
     4
     5Docker can be hosted on a variety of Operating Systems such as Windows, Linux, and MacOS and is extremely useful to configure OS specific container environments for building embedded Linux firmware.
     6
     7The various board support packages (BSP) that Gateworks provides for building firmware for the various Gateworks Single Board Computers are typically built on Ubuntu Linux development systems. Due to your personal preference as well as Ubuntu's release schedule you may end up with a development host that does not run Ubuntu or the specific version of Ubuntu that is recommended to build a particular BSP. You can use Docker to create a container with a specific version of Ubuntu to overcome this.
     8
     9This wiki page does not cover much of Docker, it is to be used as an example.
     10
     11see also:
     12 * [https://petri.com/docker-for-windows-create-a-linux-container-on-windows-10/ Set up Linux Containers on Windows 10]
     13 * ​https://hub.docker.com/editions/community/docker-ce-server-ubuntu
     14
     15
     16[=#install]
     17== Installing Docker on Ubuntu
     18
     19Install (from official Docker repo)
     20{{{#!bash
     21# download deps
     22sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
     23# install Docker's GPG key
     24curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
     25# install Docker repo
     26sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu  $(lsb_release -cs)  stable"
     27#install
     28sudo apt-get update
     29sudo apt-get install docker-ce docker-ce-cli containerd.io
     30}}}
     31 - 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/)
     32
     33It 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}}}.
     34
     35Testing:
     36 * test by downloading hello-world image and running it
     37{{{#!bash
     38docker run hello-world
     39}}}
     40
     41
     42References:
     43 - https://hub.docker.com/editions/community/docker-ce-server-ubuntu
     44
     45
     46[=#images]
     47== Images
     48Docker Images represent root filesystems that you can run as a container. These images are built based on a {{{Dockerfile}}} containing a few simple instructions.
     49
     50Create Image:
     51 1. Create a Dockerfile for the image (each Dockerfile should be in a directory representing the image)
     52{{{#!bash
     53# create a directory for your images
     54mkdir docker-images && cd docker-images
     55# create a directory for your image
     56mkdir ubuntu-16.04 && cd ubuntu-16.04
     57cat << EOF > Dockerfile
     58# Docwnload base image ubuntu 16.04
     59FROM ubuntu:16.04
     60# Update software repo
     61RUN apt-get update
     62# Install apps we want
     63RUN  apt-get install -y build-essential git python \
     64libssl-dev ncurses-dev device-tree-compiler bc gawk zlib1g-dev file \
     65wget unzip sudo \
     66EOF
     67}}}
     68 1. Build the image:
     69{{{#!bash
     70# build docker image
     71docker build --tag ubuntu-16.04 .
     72}}}
     73 1. List images
     74{{{#!bash
     75# list images
     76docker images
     77}}}
     78
     79Other image commands:
     80{{{#!bash
     81docker commit <user/image> # save container as an image
     82docker save <user/image> #save an image to a tar archive
     83docker buid --tag sampleuser/ubuntu . # build docer image from Dockerfile in current dir
     84docker load # loads a image from file
     85}}}
     86
     87Note that images are stored system-wide in a 'registry'. These images can be pushed to shared registries to share them.
     88
     89References:
     90 - see https://www.howtoforge.com/tutorial/how-to-create-docker-images-with-dockerfile/
     91
     92
     93[=#tags]
     94== Tags
     95An image name is made up of slash-separated name components, optionally prefixed by a registry hostname:port. The hostname must comply with standard DNS rules but may not contain underscores. If registry hostname:port is not present the command uses Docker's public registry located at registry-1.docker.io by default. Name components can contain lowercase letters, digits and separators (period, one or two underscores, or one or more dashes).
     96
     97A tag name must be valid ASCII and may contain lower/upper letters,digits, underscores, periods and dashes.
     98
     99
     100[=#registry]
     101A registry is a storage and content deliver system holding named images available in different tagged versions. Users interact with a registry by using docker push and pull commands
     102
     103The registry is a stateless, open-source, highly scalable server side application that stores and lets you distribute Docker images.
     104
     105The [https://hub.docker.com/ Docker Hub] provides a free-to-use hosted registry with additional features
     106
     107commands:
     108 * start local registry
     109{{{#!bash
     110docker run -d -p 5000:5000 --name registry registry:2
     111}}}
     112 * pull an image from the hub
     113{{{#!bash
     114docker pull ubuntu
     115}}}
     116 * tag that image so it points to your local registry
     117{{{#!bash
     118docker image tag ubuntu localhost:5000/myimage
     119}}}
     120 * push it
     121
     122[=#containers]
     123== Containers
     124A container is a running instance of a system sharing the host machines kernel but using an image root filesystem. When the container is stopped all changes to the filesystem are lost unless you mount volumes within that image to access filesystems on the host.
     125
     126Running container
     127 - Run container with an interactive shell
     128{{{#!bash
     129# run container interactively
     130docker run -it ubuntu-16.04
     131}}}
     132 - run container with a bind-mount from host {{{/usr/src/}}} to containers {{{/usr/src}}}
     133{{{#!bash
     134docker run -it --mount type=bind,source=/usr/src,target=/usr/src ubuntu-16.04
     135}}}
     136 - run container in privileged mode (necessary to perform some root-specific functions like 'mount' within the container)
     137{{{#!bash
     138docker run --privileged -it --mount type=bind,source=/usr/src,target=/usr/src ubuntu-16.04
     139}}}
     140 - Notes:
     141  - use '--rm' to remove the container after exiting
     142  - use '-it' to allocate pseudo-TTY
     143  - specify /bin/bash to launch specific shell
     144  - use '-w' to set working directory
     145  - use '--env foo=bar' or '--env-file ./env.list' to set env vars
     146
     147Other container commands:
     148{{{#!bash
     149docker ps # list all running containers
     150docker images # list all images on local machine
     151docker history <user/image> # list history of an image
     152docker port <container> # display exposed port of a running container
     153docker run -it <user/image> # runs an image, creating a container and changing the terminal to it
     154docker run -p $HOSTPORT:$CONTAINERPORT -d <user/image> # run image in deatched mode wiht port forwarding
     155ctrl+p, ctrl+q detach from container
     156docker attach <container>
     157docker start <container>
     158docker stop <container>
     159docker inspect <container> # show details
     160docker rm -f <container> # delete container
     161docker rmi # delete image
     162docker tag <user/image:tag> <usr/image:newtag> # apply a new tag to an image
     163docker exec <container> shell command # execute a command within a running container
     164}}}
     165
     166
     167[=#volumes]
     168== Volumes
     169Volumes provide a way to share filesystems or more importantly bring non-volatile storage to docker containers.
     170
     171You can specify volume mounts within image files or on the command-line when running a container. The modern command-line option for this is the {{{--mount}}} parameter which requires a type, source, and target. The type can be {{{type=bind}}} to use an OS bind mount or {{{type=volume}}} to utilize a docker IO driver.
     172
     173Examples:
     174 - run container with a bind-mount from host {{{/usr/src/}}} to containers {{{/usr/src}}}
     175{{{#!bash
     176docker run -it --mount type=bind,source=/usr/src,target=/usr/src ubuntu-16.04
     177}}}
     178 - create a volume and mount it in a container:
     179{{{#!bash
     180docker volume create ubuntu-16.04
     181}}}
     182 - run container with a volume mounted from host to {{{/usr/src}}}
     183{{{#!bash
     184docker run -it --mount type=volume,source=ubuntu-16.04,target=/root/workspace ubuntu-16.04
     185}}}
     186
     187
     188Notes:
     189 - use '--tmpfs' (ie --tmpfs /run:rw,noexec,nosuid,size=65536k) to mount a tmpfs
     190
     191
     192== Examples
     193
     194=== Ubuntu 20.04 Focal container
     195
     196This 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.
     197
     1981. Create a docker 'image' based on Ubuntu 20.04:
     199 - On a Linux host, docker images are created from a DockerFile that provides details and commands used to build the image
     200{{{#!bash
     201mkdir ubuntu-focal
     202cat << EOF ubuntu-focal/DockerFile
     203# from base image ubuntu 20.04
     204FROM ubuntu:20.04
     205
     206# Disable Prompt During Packages Installation
     207ARG DEBIAN_FRONTEND=noninteractive
     208
     209# update list of available packages
     210RUN apt-get update
     211
     212# add a non-root user
     213ARG USER=build
     214ARG UID=1000
     215ARG GID=1000
     216RUN groupadd -g $GID $USER && useradd -g $GID -m -s /bin/bash -u $UID $USER
     217
     218# and give that user permission to use sudo
     219RUN apt-get install sudo
     220RUN echo "$USER ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
     221
     222# and set the prompt as a reminder
     223RUN echo 'export PS1="\u@focal:\w\$ "' >> /home/$USER/.bashrc
     224# switch to this user
     225USER $USER
     226WORKDIR /home/$USER
     227}}}
     228 - use the 'docker build' command to build the OS image:
     229{{{#!bash
     230docker build --tag ubuntu-22.04 --build-arg USER=$USER --build-arg UID=$(id -u) --build-arg GID=$(id -g) ubuntu-jammy/
     231}}}
     232  * 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
     233  * building the 'image' is a one-time operation. You only need to repeat it if you change the DockerFile specifying the image
     234  * 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
     2351. Create and start docker container
     236{{{#!bash
     237docker run -it --privileged \
     238  --mount type=bind,source=/usr/src,target=/usr/src \
     239  --mount type=bind,source=/tftpboot,target=/tftpboot \
     240  --name jammy ubuntu-jammy
     241}}}
     242 * the '--mount' options are optional but show how you can 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 /usr/src from the host OS to allow our host OS to get to source files we use
     243 * 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
     244 * 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.
     245
     246If 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.
     247
     248