Docker Basics

Install Docker

Before installing, it is mandatory that user needs to be sure there is no other version of docker was installed

$ sudo apt-get remove docker docker-engine docker.io containerd runc

Start installation

$ sudo apt-get update
$ sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Add keyring

$ sudo mkdir -p /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

Check installation is ok

$ sudo docker run hello-world

logout and login if necessary

Use yml file to start a stack

$ docker compose -f <composer-file.yml> up

List containers

  • a: all
  • q: running ones with only ID numbers.

$ docker ps -a

Have a docker image

$ docker pull IMAGE[:TAG]

Browse images at https://hub.docker.com

$ docker pull ubuntu:bionic

Start a container

$ docker run [OPTIONS] IMAGE[:TAG] [COMMAND] [ARG...]

  • -i Start the container in interactive mode
  • -t allocates a pseudo-tty and attaches it to the standard input
  • -name gives desired a name

to prevent the container stops after exiting use -d option, runs the container at the background and prints the container ID.

$ docker run -itd --name=my_ubuntu ubuntu:bionic /bin/bash

Run a command or gets into the container with a shell

$ docker exec [OPTIONS] CONTAINER[_NAME/_ID] [ARG...]

List /var/www/ folder:

$ docker exec -it wordpress ls -hal wp-content/themes

Execute multiple commands

$ docker exec mint:latest -C "add-apt-repository ppa:git-core/ppa; apt update; apt install git -y"

List users of a WP website with WP-CLI command in a WordPress container with a WP-CLI container:

$ docker run -it --rm \
  --volumes-from wordpress:latest \
  --network container:wordpress:latest \
  wordpress:cli user list

Start a shell in the container:

$ docker exec -it d3a45f45ad41 sh
  / # echo This is inside the container.
  This is inside the container.
  / # exit

Get into a container shell (bash):

$ docker exec -it neat-beaver bash

Stop a container

–time / -t gives time to server before stopping.

$ docker stop [-t|--time[=0]] CONTAINERNAME/ID [CONTAINER...]

$ docker stop -t=15 ubuntu:bionic

Stop all containers

$ docker stop 'docker ps -q'

Delete a container

$ docker rm [OPTIONS] CONTAINER [CONTAINER]

$ docker rm f5377666ea29

Stop before deleting, but you can force to delete with -f option

$ docker rm-f f5377666ea29

Delete all containers at once

$ docker stop $(docker ps -q)

$ docker rm (docker ps -aq)

Remove Images

List images to remove, show all:

$ docker images -a

$ docker rmi [IMAGE_NAME/IMAGE_ID]

$ docker image rm [REPO_NAME:TAG]

Remove dangling images, list first :

$ docker images -f dangling=true

$ docker image remove

Concepts

to be continued…

Container

System

Image

Volume

Network

Tag