Development

Docker

What is Docker?

Docker is a platform that uses containerization to simplify the process of building, deploying, and running applications.


Installation

This section provides instructions for installing Docker on Windows, macOS, and Linux.

Windows

  1. System Requirements:

    • Windows 10 or 11 64-bit: Pro, Enterprise, or Education (Build 18362 or later)
  2. Download Docker Desktop:

  3. Run the Installer:

    • Once the download is complete, double-click the Docker Desktop Installer.exe file to run it.
    • Follow the on-screen instructions. You will likely need to allow the installer to make changes to your system.
    • Ensure that you select the option "Use WSL 2 instead of Hyper-V" during the installation process (Recommended).
  4. Restart Your Computer:

    • After the installation is complete, you will be prompted to restart your computer.
  5. Verify Installation:

    • Open PowerShell or Command Prompt and run:
    > docker --version
    
    • You should see the installed Docker version.
    • Also, you can run this command to check docker compose:
    > docker compose version
    

macOS

  1. System Requirements:

    • One of the following versions of macOS:
      • macOS Monterey 12
      • macOS Ventura 13
      • macOS Sonoma 14 (recommended)
    • Apple Silicon or Intel processor.
    • At least 4 GB of RAM.
  2. Download Docker Desktop:

    • Go to the official Docker website: https://www.docker.com/products/docker-desktop/
    • Click on the "Download for Mac" button.
      • Choose "Apple chip" if you have M1, M2, or M3 Apple silicon chips
      • Choose "Intel chip" if you have an intel based mac.
  3. Run the Installer:

    • Once the download is complete, double-click the Docker.dmg file to open it.
    • Drag the Docker icon to the Applications folder.
  4. Start Docker:

    • Go to your Applications folder and double-click the Docker icon to start it.
    • You might be prompted to allow docker to make changes.
  5. Verify Installation:

    • Open the Terminal and run:
    $ docker --version
    
    • You should see the installed Docker version.
    • Also, you can run this command to check docker compose:
    $ docker compose version
    

Linux

These instructions cover installation using the official Docker repositories. Adjust package manager commands as needed for your distribution.

Ubuntu 24.04

  1. Update Package Index:

    $ sudo apt update
    
  2. Install Prerequisites:

    $ sudo apt install ca-certificates curl
    
  3. Add Docker's Official GPG Key:

    $ sudo install -m 0755 -d /etc/apt/keyrings
    $ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
    $ sudo chmod a+r /etc/apt/keyrings/docker.asc
    
  4. Add Docker Repository:

    $ echo \
        "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
        $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \
        sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    
  5. Update Package Index Again:

    $ sudo apt update
    
  6. Install Docker Engine, containerd, and Docker Compose:

    $  sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
    
  7. Setup Permission:

    Check if the docker group exists:

    $ getent group docker
    

    If the group exists, you’ll see output like:

    docker:x:999:
    

    Add your user to the docker group:

    $ sudo usermod -aG docker $USER
    

    Apply the group changes (log out and log back in):

    $ newgrp docker
    

    Verify that Docker works without sudo:

    $ docker ps
    
  8. Verify Installation:

    $ docker run hello-world
    
    • You should see the installed Docker version.
    • Also, you can run this command to check docker compose:
    $ docker --version
    

Docker Image

List all docker images

$ docker images
$ docker restart <image_name>
# or
$ docker restart <image_name1> <image_name2> ...
$ docker start <image_name>
$ docker stop <image_name>

Remove Docker Image

  1. List running containers:

First, you need to identify the containers that are running based on the image(s) you want to delete.

$ docker ps -a

This command lists all the running containers. Take note of the container IDs.

CONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES
6d358d4197cfpostgres:16"docker-entrypoint.s…"2 hours agoUp 2 hours0.0.0.0:5432->5432/tcp, [::]:5432->5432/tcpcanvas-postgres
  1. Stop the running containers:

To delete an image, you need to stop the container(s) using that image. Run the following command for each container:

$ docker stop <container_id>
  1. Remove the containers:

Once the containers are stopped, you can remove them:

$ docker rm <container_id>
#or
$ docker rm -f <container_id> | <container_name>
  1. Delete the Docker images:

Now that the containers are removed, you can safely delete the image(s) using the following command:

$ docker rmi <image_id>

If there are multiple containers or images, you can loop through the IDs to stop, remove, and delete them. To delete an image that’s not being used by any containers (even stopped ones), use this:

$ docker rmi $(docker images -q)

If you also want to remove all Docker images, volumes, and networks, use:

$ docker system prune -a --volumes

Docker Container

List Containers

$ docker ps

Stop a Container

$ docker stop <container_id>

Restart a Container

$ docker restart <container_id>

Access a Container

$ docker exec -it <container_name> bash

Dockerfile


docker-compose.yml

Rebuild Image

$ cd docker_folder
$ docker-compose up --build -d
$ docker-compose restart <container_name>

To restart all containers:

$ docker-compose restart

Build and Run

Build the Docker image using the docker build command. You can tag the image with a name (e.g., my-awesome-app) and specify the . (current directory) to use the Dockerfile there.

$ cd /path/to/your/project
$ docker build -t my-awesome-app .
  • -t my-awesome-app specifies the name/tag of the image.
  • . the dot indicates the current directory as the build context (where Docker will look for the Dockerfile)

Run Ubuntu Server as a Container

$ docker run -dit --name my-ubuntu -p 2200:22 -p 8080:80 -v /data/docker:/home/ubuntu ubuntu:22.04
$ docker start my-ubuntu
$ docker exec -it my-ubuntu bash
$ apt update && apt install -y openssh-server
$ service ssh start
$ adduser myuser
$ usermod -aG sudo myuser
$ nano /etc/ssh/sshd_config

Change configurations:

PermitRootLogin yes
PasswordAuthentication yes

Restart SSH service:

$ service ssh restart

If you want to save this setup into a new image:

$ docker commit my-ubuntu my-ubuntu-image

Now, you can create new containers from this modified image using:

$ docker run -dit --name my-second-ubuntu -p 2222:22 my-ubuntu-image
Previous
Redis
Next
Git