Docker Tutorial: Difference between revisions
No edit summary |
Efernandez (talk | contribs) |
||
(36 intermediate revisions by 5 users not shown) | |||
Line 1: | Line 1: | ||
[ | <seo title="Docker Tutorial | Docker | RidgeRun" titlemode="replace" keywords="GStreamer, Linux SDK, Linux BSP, Embedded Linux, Device Drivers, NVIDIA, Xilinx, TI, NXP, Freescale, Embedded Linux driver development, Linux Software development, Embedded Linux SDK, Embedded Linux Application development, GStreamer Multimedia Framework, Jetson Xavier NX, Jetson Xavier, Xavier, Xavier NX, Docker, Docker Tutorial, Container, Docker container, Container GUI, Docker Container GUI" description="This wiki page from RidgeRun is about the Docker Tutorial"></seo> | ||
<table> | |||
<tr> | |||
<td><div class="clear; float:right">__TOC__</div></td> | |||
<td> | |||
<center> | |||
{{ContactUs Button}} | |||
</center> | |||
<td> | |||
</tr> | |||
</table> | |||
== Docker Tutorial Presentation == | |||
See the presentation in the following link | |||
* [https://developer.ridgerun.com/wiki/images/1/19/Rr_training_sesssions_docker.pdf Docker training] | |||
<center> | |||
<pdf>File:Example.pdf</pdf> | |||
</center> | |||
== Commands Quick Reference == | == Commands Quick Reference == | ||
=== Legend === | |||
The following code applies throughout the rest of the wiki. | |||
<syntaxhighlight lang=bash> | |||
# Commands to be run in the host | |||
</syntaxhighlight> | |||
<syntaxhighlight lang=bash style="border:1px dashed black"> | |||
# Commands to be run in the container | |||
</syntaxhighlight> | |||
<syntaxhighlight lang=bash style="border:1px dashed blue"> | |||
# Dockerfile | |||
</syntaxhighlight> | |||
=== Install === | === Install === | ||
==== Stable Version ==== | |||
On Debian-based systems Docker may be installed via Apt (taken from the [https://docs.docker.com/install/linux/docker-ce/ubuntu/ official documentation]): | On Debian-based systems Docker may be installed via Apt (taken from the [https://docs.docker.com/install/linux/docker-ce/ubuntu/ official documentation]): | ||
Line 24: | Line 64: | ||
sudo apt install docker-ce docker-ce-cli containerd.io | sudo apt install docker-ce docker-ce-cli containerd.io | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==== Running as Non-Root (no sudo) ==== | |||
To run as non-root user: | To run as non-root user: | ||
Line 39: | Line 81: | ||
docker ps | docker ps | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==== Other OSs ==== | |||
Other supported OS: | Other supported OS: | ||
Line 45: | Line 89: | ||
=== Create Containers === | === Create Containers === | ||
==== Basic Creation ==== | ==== Basic Creation ==== | ||
These are my go-to recipes for daily container creation: | |||
<syntaxhighlight lang=bash> | <syntaxhighlight lang=bash> | ||
# | # Basic container creation | ||
docker create -ti --name mycontainer ubuntu:18.04 | docker create -ti --name mycontainer ubuntu:18.04 | ||
# Share dir: /home/mgruner (host) in /host (container) | # Share dir: /home/mgruner (host) in /host (container) | ||
docker create -ti -v /home/mgruner/:/host ubuntu:18.04 | docker create -ti -v /home/mgruner/:/host ubuntu:18.04 | ||
Line 65: | Line 103: | ||
# Share file: /home/mgruner/file.txt (host) in /host.txt (container) | # Share file: /home/mgruner/file.txt (host) in /host.txt (container) | ||
docker create -ti -v /home/mgruner/file.txt:/host.txt ubuntu:18.04 | docker create -ti -v /home/mgruner/file.txt:/host.txt ubuntu:18.04 | ||
# Create a persistent volume for future use | |||
docker create -ti -v myvolume:/host ubuntu:18.04 | |||
# Additional useful options | |||
# -m Memory limit | |||
# -c CPU percentage | |||
# --restart Restart policy | |||
# --rm Remove container after usage | |||
# -e Environment variable | |||
docker create -ti -m 1G -c 1.5 --restart unless-stopped --rm -e MYENV=myenv ubuntu:18.04 | |||
</syntaxhighlight> | |||
==== Container Information ==== | |||
Here are some commands on how to query info about containers | |||
<syntaxhighlight lang=bash> | |||
# List existing containers (started) | |||
docker ps | |||
# List existing containers (all) | |||
docker ps -a | |||
# Get container info (IP address, for example) | |||
docker inspect mycontainer | |||
</syntaxhighlight> | |||
=== Use Containers === | |||
==== Basic Usage ==== | |||
Very basic container usage: | |||
<syntaxhighlight lang=bash> | |||
# Start the container | |||
docker start mycontainer | |||
# Open a shell in the container | |||
docker exec -ti mycontainer bash | |||
# Stop the container | |||
docker stop mycontainer | |||
</syntaxhighlight> | |||
==== Common Initial Setups ==== | |||
Here's my goto recipe when creating a container from scratch. | |||
<syntaxhighlight lang=Bash style="border:1px dashed black"> | |||
# Load up apt caches | |||
apt update | |||
# Install common packages | |||
apt install git build-essential autoconf Libtool autotools emacs vi | |||
</syntaxhighlight> | |||
You can also setup SSH for network access. | |||
<syntaxhighlight lang=bash style="border:1px dashed black"> | |||
# Install ssh server and sudo for your new user | |||
apt install sudo openssh-server | |||
# Create a new user (to avoid using root to access) | |||
adduser ridgerun | |||
# Give it root access | |||
adduser ridgerun sudo | |||
# Manually start ssh server (every time container is started) | |||
service ssh start | |||
# Exit the container | |||
exit | |||
</syntaxhighlight> | |||
==== Running Long Processes ==== | |||
Sometimes you need to keep a long running process on the container in a remote server, and disconnect from the server and go to sleep. Here's how to run a command and disconnect from the container without stopping the session. | |||
<syntaxhighlight lang=bash> | |||
# Option 1: detach at creation time | |||
docker exec -d -ti mycontainer /long_running_script.sh | |||
</syntaxhighlight> | |||
Alternatively: | |||
<syntaxhighlight lang=bash style="border:1px dashed black"> | |||
# Option 2: detach from the shell | |||
./long_running_script.sh | |||
# Now press ctrl+p ctrl+q to detach | |||
</syntaxhighlight> | |||
Now attach to your container by running: | |||
<syntaxhighlight lang=bash> | |||
# Reattach to the container | |||
docker attach mycontainer | |||
</syntaxhighlight> | |||
=== Save a Container Snapshot === | |||
==== New Image ==== | |||
You can create a new image from '''the current state of a container''': | |||
<syntaxhighlight lang=bash> | |||
# Save an image based on a container | |||
docker commit -a "Michael Gruner <michael.gruner@ridgerun.com>" -m "Example" mycontainer myimage:mytag | |||
</syntaxhighlight> | |||
You can create a new image from '''another image''': | |||
<syntaxhighlight lang=bash> | |||
# Create an alias | |||
docker tag myimage:mytag newimage:newtag | |||
</syntaxhighlight> | |||
==== Image Utils ==== | |||
Here are some common utils to interact with images. | |||
<syntaxhighlight lang=bash> | |||
# List existing images | |||
docker images | |||
# List existing images (alternative) | |||
docker image ls | |||
# Remove image | |||
docker image rm newimage:newtag | |||
# Remove all unused images | |||
docker image prune | |||
</syntaxhighlight> | |||
==== Sharing Images ==== | |||
You may share via tarball: | |||
<syntaxhighlight lang=bash> | |||
# Save image | |||
docker save myimage:mytag | gzip > myimage_mytag.tar.gz | |||
# Load image from tarball | |||
docker load < myimage_mytag.tar.gz | |||
</syntaxhighlight> | |||
You may share via docker registry: | |||
<syntaxhighlight lang=bash> | |||
# Point to custom registry | |||
docker tag newimage:newtag dockerhub.cr.ridgerun.com:5000/ridgerun/myimage:mytag | |||
# Push image | |||
docker push dockerhub.cr.ridgerun.com:5000/ridgerun/myimage:mytag | |||
# Point do ridgerun repository at Docker Hub | |||
docker tag myimage:mytag ridgerun/myimage:mytag | |||
# Push image | |||
docker push ridgerun/myimage:mytag | |||
</syntaxhighlight> | |||
==== Dockerfiles ==== | |||
Alternatively, you may create an image from a Dockerfile. | |||
<syntaxhighlight lang=Dockerfile style="border:1px dashed blue"> | |||
FROM ubuntu:18.04 | |||
RUN apt-get update && \ | |||
apt-get install -y openssh-server sudo && \ | |||
useradd -p $(openssl passwd -1 mgruner) mgruner && \ adduser mgruner sudo | |||
ENTRYPOINT service ssh restart && bash | |||
</syntaxhighlight> | |||
<syntaxhighlight lang=Bash> | |||
# Build an image from a dockerfile | |||
docker build -t imagename:imagetag . | |||
# List images | |||
docker images | |||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Delete the Container === | |||
< | |||
# Create a persistent volume | <syntaxhighlight lang=Bash> | ||
docker create -ti -v | # Stop the container | ||
docker mycontiainer stop | |||
# Remote the container | |||
docker rm mycontainer | |||
</syntaxhighlight> | |||
== Advanced Tips == | |||
=== Container Manager (Portainer) === | |||
Create and run the Portainer container | |||
<syntaxhighlight lang=Bash> | |||
# Create a persistent memory volume | |||
docker volume create portainer_data | |||
# Run portainer forever! | |||
docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer | |||
</syntaxhighlight> | |||
Access to your URL: http://localhost:9000 | |||
=== Container GUI === | |||
==== Via X11 ==== | |||
Your docker container may display graphical windows by forwarding them through X11 to your host container. In Mac you may want to look at [https://www.xquartz.org XQuartz]. | |||
<syntaxhighlight lang=Bash> | |||
docker create -ti --name guicontainer -e DISPLAY="host.docker.internal:0" -v /tmp/.X11-unix:/tmp/.X11-unix myimage:mytag | |||
</syntaxhighlight> | |||
You will need to enable X11 remote connections on your system: | |||
;Debian based | |||
:xhost + 127.0.0.1 | |||
;Mac OSX based | |||
:XQuartz Preferences -> Security (disable ''Authenticate connections'' and enable ''Allow connections from network clients'') | |||
==== Via X2Go ==== | |||
Another alternative is to use the X2Go server. This is very useful if you want to interact with the full desktop environment. | |||
In the container install the following: | |||
<syntaxhighlight lang=Bash style="border:1px dashed black"> | |||
# Install x2go server, ssh server, desktop environment and sudo | |||
apt install x2goserver openssh-server xfce4 sudo | |||
# Create a user | |||
adduser ridgerun | |||
# Give the new user sudo access | |||
adduser ridgerun sudo | |||
# Start ssh server | |||
service ssh start | |||
</syntaxhighlight> | |||
Now install the client on your host: | |||
<syntaxhighlight lang=Bash> | |||
# Install x2go client | |||
sudo apt install x2goclient | |||
</syntaxhighlight> | |||
Start the client and fill out the following when creating a new session: | |||
;Host | |||
:The IP of the container | |||
;Login | |||
:ridgerun (or the user you chose) | |||
;SSH port | |||
:22 | |||
;Session Type | |||
:XFCE | |||
=== Moving docker location === | |||
Docker by default uses <code>/var/lib/docker</code> to store all images, layers and data. If this location is taking too much space or you want to move, and you want for example to use space from another partition or disk, you can use the following to change docker location: | |||
1. Stop docker service | |||
<syntaxhighlight lang=bash> | |||
sudo systemctl stop docker | |||
</syntaxhighlight> | |||
2. Sync your current docker state to another folder | |||
<syntaxhighlight lang=bash> | |||
rsync -avxP /var/lib/docker/ <another directory>/docker | |||
sudo rm -rf /var/lib/docker | |||
</syntaxhighlight> | |||
3. Modify the docker service <code>/lib/systemd/system/docker.service</code> to use this different directory: | |||
<syntaxhighlight lang=bash> | |||
ExecStart=/usr/bin/dockerd --data-root <another directory>/docker -H fd:// --containerd=/run/containerd/containerd.sock | |||
</syntaxhighlight> | |||
4. Reload services files and start the docker service | |||
<syntaxhighlight lang=bash> | |||
systemctl daemon-reload | |||
systemctl start docker | |||
</syntaxhighlight> | </syntaxhighlight> | ||
{{ContactUs}} | |||
[[Category:GStreamer]][[Category:Jetson]][[Category:JetsonNano]][[Category:JetsonTX2]][[Category:NVIDIA Xavier]][[Category:JetsonXavierNX]][[Category:docker]][[Category:NVIDIA Jetson Orin]] |
Latest revision as of 16:47, 9 August 2024
|
Docker Tutorial Presentation
See the presentation in the following link
<pdf>File:Example.pdf</pdf>
Commands Quick Reference
Legend
The following code applies throughout the rest of the wiki.
# Commands to be run in the host
# Commands to be run in the container
# Dockerfile
Install
Stable Version
On Debian-based systems Docker may be installed via Apt (taken from the official documentation):
# Uninstall old versions sudo apt remove docker docker-engine docker.io containerd runc # Install dependencies sudo apt update sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common # Install GPG Key curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - # Add Docker PPA sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" # Finally install sudo apt update sudo apt install docker-ce docker-ce-cli containerd.io
Running as Non-Root (no sudo)
To run as non-root user:
# Create the docker group sudo groupadd docker # Add yourself to the group sudo usermod -aG docker $USER # To avoid re-logging newgrp docker # Test proper installation docker ps
Other OSs
Other supported OS:
Create Containers
Basic Creation
These are my go-to recipes for daily container creation:
# Basic container creation docker create -ti --name mycontainer ubuntu:18.04 # Share dir: /home/mgruner (host) in /host (container) docker create -ti -v /home/mgruner/:/host ubuntu:18.04 # Share file: /home/mgruner/file.txt (host) in /host.txt (container) docker create -ti -v /home/mgruner/file.txt:/host.txt ubuntu:18.04 # Create a persistent volume for future use docker create -ti -v myvolume:/host ubuntu:18.04 # Additional useful options # -m Memory limit # -c CPU percentage # --restart Restart policy # --rm Remove container after usage # -e Environment variable docker create -ti -m 1G -c 1.5 --restart unless-stopped --rm -e MYENV=myenv ubuntu:18.04
Container Information
Here are some commands on how to query info about containers
# List existing containers (started) docker ps # List existing containers (all) docker ps -a # Get container info (IP address, for example) docker inspect mycontainer
Use Containers
Basic Usage
Very basic container usage:
# Start the container docker start mycontainer # Open a shell in the container docker exec -ti mycontainer bash # Stop the container docker stop mycontainer
Common Initial Setups
Here's my goto recipe when creating a container from scratch.
# Load up apt caches apt update # Install common packages apt install git build-essential autoconf Libtool autotools emacs vi
You can also setup SSH for network access.
# Install ssh server and sudo for your new user apt install sudo openssh-server # Create a new user (to avoid using root to access) adduser ridgerun # Give it root access adduser ridgerun sudo # Manually start ssh server (every time container is started) service ssh start # Exit the container exit
Running Long Processes
Sometimes you need to keep a long running process on the container in a remote server, and disconnect from the server and go to sleep. Here's how to run a command and disconnect from the container without stopping the session.
# Option 1: detach at creation time docker exec -d -ti mycontainer /long_running_script.sh
Alternatively:
# Option 2: detach from the shell ./long_running_script.sh # Now press ctrl+p ctrl+q to detach
Now attach to your container by running:
# Reattach to the container docker attach mycontainer
Save a Container Snapshot
New Image
You can create a new image from the current state of a container:
# Save an image based on a container docker commit -a "Michael Gruner <michael.gruner@ridgerun.com>" -m "Example" mycontainer myimage:mytag
You can create a new image from another image:
# Create an alias docker tag myimage:mytag newimage:newtag
Image Utils
Here are some common utils to interact with images.
# List existing images docker images # List existing images (alternative) docker image ls # Remove image docker image rm newimage:newtag # Remove all unused images docker image prune
Sharing Images
You may share via tarball:
# Save image docker save myimage:mytag | gzip > myimage_mytag.tar.gz # Load image from tarball docker load < myimage_mytag.tar.gz
You may share via docker registry:
# Point to custom registry docker tag newimage:newtag dockerhub.cr.ridgerun.com:5000/ridgerun/myimage:mytag # Push image docker push dockerhub.cr.ridgerun.com:5000/ridgerun/myimage:mytag # Point do ridgerun repository at Docker Hub docker tag myimage:mytag ridgerun/myimage:mytag # Push image docker push ridgerun/myimage:mytag
Dockerfiles
Alternatively, you may create an image from a Dockerfile.
FROM ubuntu:18.04 RUN apt-get update && \ apt-get install -y openssh-server sudo && \ useradd -p $(openssl passwd -1 mgruner) mgruner && \ adduser mgruner sudo ENTRYPOINT service ssh restart && bash
# Build an image from a dockerfile docker build -t imagename:imagetag . # List images docker images
Delete the Container
# Stop the container docker mycontiainer stop # Remote the container docker rm mycontainer
Advanced Tips
Container Manager (Portainer)
Create and run the Portainer container
# Create a persistent memory volume docker volume create portainer_data # Run portainer forever! docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer
Access to your URL: http://localhost:9000
Container GUI
Via X11
Your docker container may display graphical windows by forwarding them through X11 to your host container. In Mac you may want to look at XQuartz.
docker create -ti --name guicontainer -e DISPLAY="host.docker.internal:0" -v /tmp/.X11-unix:/tmp/.X11-unix myimage:mytag
You will need to enable X11 remote connections on your system:
- Debian based
- xhost + 127.0.0.1
- Mac OSX based
- XQuartz Preferences -> Security (disable Authenticate connections and enable Allow connections from network clients)
Via X2Go
Another alternative is to use the X2Go server. This is very useful if you want to interact with the full desktop environment.
In the container install the following:
# Install x2go server, ssh server, desktop environment and sudo apt install x2goserver openssh-server xfce4 sudo # Create a user adduser ridgerun # Give the new user sudo access adduser ridgerun sudo # Start ssh server service ssh start
Now install the client on your host:
# Install x2go client sudo apt install x2goclient
Start the client and fill out the following when creating a new session:
- Host
- The IP of the container
- Login
- ridgerun (or the user you chose)
- SSH port
- 22
- Session Type
- XFCE
Moving docker location
Docker by default uses /var/lib/docker
to store all images, layers and data. If this location is taking too much space or you want to move, and you want for example to use space from another partition or disk, you can use the following to change docker location:
1. Stop docker service
sudo systemctl stop docker
2. Sync your current docker state to another folder
rsync -avxP /var/lib/docker/ <another directory>/docker sudo rm -rf /var/lib/docker
3. Modify the docker service /lib/systemd/system/docker.service
to use this different directory:
ExecStart=/usr/bin/dockerd --data-root <another directory>/docker -H fd:// --containerd=/run/containerd/containerd.sock
4. Reload services files and start the docker service
systemctl daemon-reload systemctl start docker
RidgeRun Resources | |||||
Contact Us
|