Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Learning Linux in a Docker container

What is Docker

  • A light-weight Virtual Server.

  • Makes it easy to have the exact same setup for development, testing, productions.

  • Makes it easy for people to set up their development environment.

  • De-facto standard for containerization.

What is Linux

  • The kernel
  • An operating system

Linux distributions

Install Docker

Docker on Windows

  • Run the cmd

Docker on Linux and macOS

  • Open a terminal

Docker version

docker -v

Docker Run Ubuntu

docker run -it -w /opt --name ubu ubuntu:23.04

Create a footprint on the machine

  • ls

  • exit

  • list content of the current directory

  • create a file

  • list the content again

  • exit

ls -l
echo "Hello World" > GABOR_WAS_HERE
ls -l
exit

Docker restart container

  • start
  • restart
docker container start -i ubu
ls -l
  • As we can see the file is still there.

Docker list running containers

  • ps

  • In another terminal/command window:

docker ps

Docker list all containers

  • ps

  • Exit from the container

docker ps -a

Docker list images

  • images
docker images
docker image list
docker image ls

Docker remove images

  • rmi
docker rmi ubuntu:23.04
docker image rm ubuntu:23.04
  • This will fail as the container is still referencing it

Docker remove container

  • rm
docker container rm ubu

Exercise 1

  • Repeate the above (except that don't remove the image)
  • Make sure you have a running system

root

  • Several meanings:

  • The name of the superuser or administrator: root

  • The name we use for the common ancestor of the filesystem: /

  • The name of the home-directory of user 'root': /root

  • /opt is usually where people put all kinds of optional installations

Linux Users

If we had a full linux system we would have users, and home directories for the users. We would use the system as one of these users and use either su to switch to the root user or better yet sudo to run one-off commands as the root user.

  • users (root and other users)
  • /home directory
  • su
  • sudo
ls /root
ls /home/

Linux File system - directories

On Linux (actually in the shell we user) there are many commands to deal with the filesystem. Some of them are listed here.

ls
ls -l
ll

alias

pwd

cd home
cd ..
cd root

mkdir docs
mkdir docs/text
mkdir documents
mkdir xyz

rmdir xyz

cd TAB

  • ls list the content of a folder.
  • alias create aliases for commands so we won't have to type them.
  • pwd print (current) working directory.
  • cd change directory
  • mkdir make directory
  • rmdir remove directory
  • Use TAB completition where possible!

Linux File system - files

echo Hello World
echo text > filename
cat filename
echo more text >> filename

Ubuntu Installing packages

nano
htop
apt-get update
apt-get install nano htop

Bashrc

  • The configuration file of our shell
/root/.bashrc
~/.bashrc
  • Configure a new alias

Exercise 2

  • Repeate the above
  • In the /root directory create a few subdirectories
  • Create a few files with echo, append to the files
  • Install nano
  • Edit the files
  • Create a new alias for the current shell (eg. lx to do something)
  • Add it to the .bashrc and check if it is persistant

which type

  • which
which ls
which python
which ll
type ll

grep

grep expression filename
ll /etc/apt/sources.list
cat /etc/apt/sources.list
grep security /etc/apt/sources.list
grep -v security /etc/apt/sources.list
grep '#' /etc/apt/sources.list
grep '# ' /etc/apt/sources.list
grep '^# ' /etc/apt/sources.list


grep -v '#' /etc/apt/sources.list
grep -v '#' /etc/apt/sources.list | grep security

find files

find .
find /etc

Pipelines

find . | grep h

Create file with bash for loop

for i in {0..100}; do echo "Hello $i"; done > /opt/hello

du df

du
du -h
du -hs

df
df -h

History

history

wc - word count

wc

clear screen

clear
Ctrl-L

printenv

  • printenv
  • sort
printenv
printenv | sort

man (manual)

man grep
This system has been minimized by removing packages and content that are
not required on a system that users do not log into.

To restore this content, including manpages, you can run the 'unminimize'
command. You will still need to ensure the 'man-db' package is installed.
unminimize
yes | unminimize

Docker with CentOS

docker run -it --name cent -w /opt centos:7
  • To install nano type in the following:
yum install nano
  • In order to install htop first we need to add EPEL (Extra Packages for Enterprise Linux)
yum install epel-release
yum install htop
  • Then we can use it
htop
  • exit and start again:
docker container start -i cent