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
System Requirements:
- Windows 10 or 11 64-bit: Pro, Enterprise, or Education (Build 18362 or later)
Download Docker Desktop:
- Go to the official Docker website: https://www.docker.com/products/docker-desktop/
- Click on the "Download for Windows" button.
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).
- Once the download is complete, double-click the
Restart Your Computer:
- After the installation is complete, you will be prompted to restart your computer.
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
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.
- One of the following versions of macOS:
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.
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.
- Once the download is complete, double-click the
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.
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
Update Package Index:
$ sudo apt update
Install Prerequisites:
$ sudo apt install ca-certificates curl
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
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
Update Package Index Again:
$ sudo apt update
Install Docker Engine, containerd, and Docker Compose:
$ sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
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
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
- 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 ID | IMAGE | COMMAND | CREATED | STATUS | PORTS | NAMES |
---|---|---|---|---|---|---|
6d358d4197cf | postgres:16 | "docker-entrypoint.s…" | 2 hours ago | Up 2 hours | 0.0.0.0:5432->5432/tcp, [::]:5432->5432/tcp | canvas-postgres |
- 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>
- Remove the containers:
Once the containers are stopped, you can remove them:
$ docker rm <container_id>
#or
$ docker rm -f <container_id> | <container_name>
- 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