Docker Tutorial: Difference between revisions
No edit summary |
|||
Line 8: | Line 8: | ||
=== 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 49: | Line 49: | ||
* [https://hub.docker.com/editions/community/docker-ce-desktop-windows Windows] | * [https://hub.docker.com/editions/community/docker-ce-desktop-windows Windows] | ||
==== Running as Non-Root ==== | ==== Running as Non-Root (no sudo) ==== | ||
=== Create Containers === | === Create Containers === | ||
==== Basic Creation ==== | |||
These are my go-to recipes for daily container creation: | These are my go-to recipes for daily container creation: | ||
Line 76: | Line 78: | ||
docker create -ti -m 1G -c 1.5 --restart unless-stopped --rm -e MYENV=myenv ubuntu:18.04 | docker create -ti -m 1G -c 1.5 --restart unless-stopped --rm -e MYENV=myenv ubuntu:18.04 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==== Container Information ==== | |||
Here's 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. | |||
'''In the container''' | |||
<syntaxhighlight lang=bash> | |||
# 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. | |||
'''In the container''' | |||
<syntaxhighlight lang=bash> | |||
# 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: | |||
'''In the container''' | |||
<syntaxhighlight lang=bash> | |||
# Option 2: detach from the shell | |||
./long_running_script.sh | |||
# Now press ctrl+p ctrl+q to detach | |||
</syntaxhighlight> | |||
[[Category:docker]][[Category:tutorial]] | [[Category:docker]][[Category:tutorial]] |
Revision as of 18:26, 7 April 2020
Presentation
<pdf page="0" height="455">File:Rr training sesssions docker.pdf</pdf>
Commands Quick Reference
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
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 supported OS:
Running as Non-Root (no sudo)
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's 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.
In the container
# 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. In the container
# 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:
In the container
# Option 2: detach from the shell ./long_running_script.sh # Now press ctrl+p ctrl+q to detach