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 and gets inside of it, or with more technical way of saying, lands on its bash shell.

$ 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

Dockerfile Basics

An example for a simple ViteJS client app

# CLIENT
FROM node:20-alpine

# Set working directory
WORKDIR /app

# Copy package*.json files to install dependencies
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy application code
COPY . .

# Set environment variable
ENV VITE_BACKEND_PORT=8048

# Expose port (note: this should match the port used in the CMD command)
EXPOSE 4173

# Buil the app to preview
RUN npm build

# Set command to run on container start
CMD ["npm", "run", "preview"]

And building that application as a docker image

docker build -t simple-vite-client .

Then run it continuously

docker run -d -p 4173:4173 simple-vite-client

Concepts

System

It is all about docker system, for a basic example to use the system command in docker may be to get the storage status of all docker environment at the server machine:

docker system df

Containers

Applications, micro-services or they even be called processes that runs inside boxes from the Images. A simple express node server application could be built as an image then be run as a service (app itself) as many as desired inside separated containers. Those containers -if I’m still not ingorant about that detail, simly- has variaous operating systems as like virutal machines but not like qemu based machine, instead basic OS environment that will be needed to run out app as an isolated instance. Most of the images uses Alpine linux OS which is the litest OS that might be had, even could take only 5MB! So most of the microservices are running on that OS in containers.

Images

Images are the seeds of your field of container planting. An image can be simply an OS, a node application that expects HTTP calls to interact to a DB system. Even though this DB system could be another image, which it is anyways in most cases. A postgreSQL DB service is an image to be able to be used from different containers. One container can use the same server to keep data for a website, the other user which is created in that DB service from another container that runs a micro-service, or third application even might be using the same container of postgreSQL service app from its image. By that way docker gives oportunity to build an architecutre of services ro be designed eailsy with its isolated structure.

Volumes

Networks

Tags

Tags simply separates the images that has the same main goal from each other like versions of the images, or names that have so-manner-to-say slight differences.