Skip to content

Instantly share code, notes, and snippets.

@ljaraque
Last active June 26, 2023 15:00
Show Gist options
  • Save ljaraque/33b3a247c910222f9c6e54ea38f209d0 to your computer and use it in GitHub Desktop.
Save ljaraque/33b3a247c910222f9c6e54ea38f209d0 to your computer and use it in GitHub Desktop.
Docker installation in Debian11

Docker on Debian 11 - Installation and Examples

1. Installation:

ref_1: https://www.redswitches.com/blog/install-docker-on-debian/

sudo apt update
sudo apt upgrade
sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
docker --version
docker: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/containers/create": dial unix /var/run/docker.sock: connect: permission denied.
  • Add docker group if it does not exist: sudo groupadd docker
  • Add user to docker group: sudo usermod -aG docker ${USER}
  • log out and log in: su ${USER}

Then docker run hello-world should work and show:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete 
Digest: sha256:a13ec89cdf897b3e551bd9f89d499db6ff3a7f44c5b9eb8bca40da20eb4ea1fa
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

2. Manage Docker:

  • Check status: sudo systemctl status docker
  • Stop: sudo systemctl stop docker
  • Start: sudo systemctl start docker
  • Restart: sudo systemctl restart docker
  • Disable: sudo systemctl disable docker
  • Enable: sudo systemctl enable docker
  • Example: Pull a CentOS image and make it a container: docker run -it --name redswitches centos /bin/bash
  • Verify the correct image and container: cat /etc/os-release and we should get:
NAME="CentOS Linux"
VERSION="8"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
[root@9d9f283b7233 /]# 
  • Check available docker images: docker images
  • Check containers running: docker container ps
  • Check all containers including exited ones: docker container ps -a
  • Run container: docker run <name-of_image>
  • Run container with foreground interface: docker run -it <name-of_image>
  • Run container with foreground interface and assign a name to it: docker run -it --name <name-of-container> <name-of_image>
  • Start a container: docker start <container-id-or-name>
  • Restart a container: docker restart <container-id-or-name>
  • Stop running container: docker stop <container-id-or-name>
  • Remove existing container: docker rm <container-id-or-name>
  • Remove existing imag: docker rmi <container-id-or-name>
  • Get into a running container with bash: docker exec -it <container-id-or-name> /bin/bash

3. Examples of Docker with Django:

  • Dockerfile:
# base image
FROM python:3.10

# set env variable
ENV DockerHOME=/home/app/webapp

# set work directory
RUN mkdir -p $DockerHOME

# where is the code
WORKDIR $DockerHOME

# set environment variables  
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1 

# install dependencies  
RUN pip install --upgrade pip

# copy whole project to your docker home directory. 
COPY . $DockerHOME

# run this command to install all dependencies  
RUN pip install -r requirements.txt 

# port where the Django app runs  
EXPOSE 8000 

CMD python manage.py runserver 0.0.0.0:8000

IMPORTANT: in CMD python manage.py runserver 0.0.0.0:8000 if 0.0.0.0 is not specified then the connection with host will not work as needed.

  • Build docker image with name <image_name> (Must be from Dockerfile location path):
docker build . -t <image-name>
  • Run image as a container named in a container port connecting to host port :
docker run -p <host-port>:<container-exposed-port> <container-name>
  • Know IP of running container:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container-name>
  • Useful command to check open ports in linux: nc -zv <ip-address> <port>

to be continued .... with [1]

. Other References:
[0] Docker-Curriculum: https://docker-curriculum.com/
[1] https://testdriven.io/blog/dockerizing-django-with-postgres-gunicorn-and-nginx/
[2] https://hasansajedi.medium.com/microservice-app-with-drf-part1-71d54fe506a
[3] https://hasansajedi.medium.com/microservice-app-with-drf-part2-1c94cb0c5c05
[4] https://www.nginx.com/blog/introduction-to-microservices/
[5] http://martinfowler.com/bliki/MonolithFirst.html
[6] Traefik instead of NGINX: https://testdriven.io/blog/django-docker-traefik/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment