Docker#

Installation#

Dockerfiles#

dev.exact.engineering(docker/dev.exact.engineering/Dockerfile)#

# This line 
FROM debian:12

# Set environment variables to non-interactive (prevents prompts during installation)
ENV DEBIAN_FRONTEND=noninteractive

# Development utilities
RUN apt-get update && \
    apt-get install -y \
    bash \
    vim \
    wget \
    make \
    npm \
    && rm -rf /var/lib/apt/lists/*

# Install Redocly CLI globally
RUN npm install -g @redocly/cli

# Install PostgreSQL and required packages
RUN apt-get update && \
    apt-get install -y \
    postgresql \
    postgresql-contrib \
    && apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Create a volume for PostgreSQL data
VOLUME ["/var/lib/postgresql/data"]

# Install RClone
RUN apt-get update && \
    apt-get install -y \
    rclone

# Create configuration directory for RClone
RUN mkdir -p /etc/rclone

# Python generic dependencies
RUN apt-get update && \
    apt-get install -y \
    python3-pip \
    python3-yaml \
    && rm -rf /var/lib/apt/lists/*

# Sphinx
RUN python3 -m pip install sphinx sphinx-book-theme --break-system-packages


# Sphinx Latex dependencies
# Based on dependencies determined by Sphinx Latex Docker image
# https://github.com/sphinx-doc/sphinx-docker-images/blob/master/latexpdf/Dockerfile
RUN apt-get update \
 && apt-get install --no-install-recommends --yes \
      graphviz \
      imagemagick \
      make \
      latexmk \
      lmodern \
      fonts-freefont-otf \
      texlive-latex-recommended \
      texlive-latex-extra \
      texlive-fonts-recommended \
      texlive-fonts-extra \
      texlive-lang-cjk \
      texlive-lang-chinese \
      texlive-lang-japanese \
      texlive-luatex \
      texlive-xetex \
      xindy \
      tex-gyre
RUN python3 -m pip install Pillow --break-system-packages

# Socketify.py
RUN apt-get update && \
    apt-get install -y \
    libuv1 \
    zlib1g \
    && rm -rf /var/lib/apt/lists/*

RUN python3 -m pip install socketify --break-system-packages

# Nginx
RUN apt-get update && \
    apt-get install -y \
    nginx \
    && rm -rf /var/lib/apt/lists/*

# Install Cloudflared dependencies
RUN apt-get update && \
    apt-get install -y \
    curl \
    gnupg \
    lsb-release \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Add Cloudflare's GPG key
RUN mkdir -p --mode=0755 /usr/share/keyrings
RUN curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null
RUN echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared buster main' | tee /etc/apt/sources.list.d/cloudflared.list

# Install Cloudflared
RUN apt-get update && apt-get install cloudflared


# MetaCRUD2 dependencies
RUN python3 -m pip install psycopg2-binary numpy --break-system-packages

# Initialize PostgreSQL
# Copy configuration files
COPY ./etc/postgresql/postgresql.conf /etc/postgresql/postgresql.conf
COPY ./etc/postgresql/pg_hba.conf /etc/postgresql/pg_hba.conf
COPY ./etc/postgresql/15/main/postgresql.conf /etc/postgresql/15/main/postgresql.conf
COPY ./etc/postgresql/15/main/pg_hba.conf /etc/postgresql/15/main/pg_hba.conf

# Initialize PostgreSQL script steps
RUN su postgres -c "service postgresql start" && \
    su postgres -c "psql -c 'CREATE DATABASE metacrud;'" && \
    su postgres -c "psql -c \"CREATE USER metacrud WITH ENCRYPTED PASSWORD 'metacrud';\"" && \
    su postgres -c "psql -c \"GRANT ALL PRIVILEGES ON DATABASE metacrud TO metacrud;\"" && \
    su postgres -c "psql -c 'ALTER ROLE metacrud WITH SUPERUSER;'" && \
    su postgres -c "psql -c 'ALTER ROLE metacrud WITH CREATEDB;'" && \
    su postgres -c "psql -c 'ALTER ROLE metacrud WITH CREATEROLE;'" && \
    su postgres -c "psql -c 'ALTER ROLE metacrud WITH REPLICATION;'" && \
    su postgres -c "psql -c 'ALTER ROLE metacrud WITH BYPASSRLS;'"


# Nginx
# Copy Nginx configuration
RUN rm -rf /etc/nginx/*
COPY ./etc/nginx /etc/nginx
# Make empty directory to serve as root for Nginx
RUN rm -rf /var/www/*
RUN mkdir -p /var/www/engineering-exact
RUN mkdir -p /var/www/engineering-exact-systems-docs
RUN mkdir -p /var/www/com-exactcontrols

# Expose the PostgreSQL port
EXPOSE 5432

# Expose ports for Nginx
# EXPOSE 81
# EXPOSE 82
# EXPOSE 443


####### Cloudflared
#COPY ../etc/cloudflared/* /etc/cloudflared/
#RUN cloudflared service install

####### MetaCRUD2 Server
#RUN mkdir -p /usr/local/lib/python3.11/dist-packages/metacrud2

# Expose ports for MetaCRUD2 Server
EXPOSE 3000

##########################################################################
####### Development
##########################################################################
#RUN mkdir -p /src/metacrud2
#COPY ./* /src/metacrud2/
#RUN mkdir -p /usr/local/lib/python3.11/dist-packages/metacrud2
RUN mkdir -p /src/metacrud2

##########################################################################
####### Startup
##########################################################################
# Set user and working directory
# USER $USER
WORKDIR /src/metacrud2
#CMD service postgresql start && \
#    service nginx start && \
#    service cloudflared start && \
#    cd /src/metacrud2 && make metacrud2 && \
#    cd /src/metacrud2 && make ui && \
#    cd /src/metacrud2 && make docs && \
#    cp /src/metacrud2/bin/metacrud2 /bin/metacrud2 && \
#    chmod +x /bin/metacrud2 && \
#    bash
CMD service nginx start && \
    service postgresql start && \
    tail -f /dev/null

Commands#

List All Docker Containers#

To list all Docker containers (both running and stopped):

docker ps -a

To list only running containers:

docker ps
Explanation:
  • ps: stands for “Process Status”. The terminology is borrowed from Linux.

  • -a: shows all containers, including stopped ones.

  • -l: shows detailed information about each container.

Stop All Docker Containers#

docker stop $(docker ps -a -q)
Explanation:
  • $(...): This is a command substitution. It runs the command inside the parentheses and substitutes the output into the command.

  • $(docker ps -a -q): This command generates a list of container IDs.

  • ps: This command lists the containers.

  • -a: This option is used to show all containers, including stopped ones.

  • -q: This option is used to output only the container IDs.

  • docker stop: This command stops the specified containers.

Stop a Specific Docker Container#

docker stop <container_id>
Explanation:
  • <container_id>: The ID of the container to stop.

  • docker stop: This command stops the specified container.

  • example: docker stop 1234567890

Delete All Docker Containers#

docker rm $(docker ps -a -q)
Explanation:
  • $(...): This is a command substitution. It runs the command inside the parentheses and substitutes the output into the command.

  • $(docker ps -a -q): This command generates a list of container IDs.

  • ps: This command lists the containers.

  • -a: This option is used to show all containers, including stopped ones.

  • -q: This option is used to output only the container IDs.

  • docker rm: This command deletes the specified containers.

Delete a Specific Docker Container#

docker rm <container_id>
Explanation:
  • <container_id>: The ID of the container to delete.

  • docker rm: This command deletes the specified container.

  • example: docker rm 1234567890

Delete All Docker Images#

docker rmi $(docker images -q)
Explanation:
  • $(...): This is a command substitution. It runs the command inside the parentheses and substitutes the output into the command.

  • $(docker images -q): This command generates a list of image IDs.

  • images: This command lists the images.

  • -q: This option is used to output only the image IDs.

  • docker rmi: This command deletes the specified images.

Delete a Specific Docker Image#

docker rmi <image_id>
Explanation:
  • <image_id>: The ID of the image to delete.

  • docker rmi: This command deletes the specified image.

  • example: docker rmi 1234567890

List All Docker Images#

Basic command to list images:

docker images
Common options:
  • docker images -a or --all: Shows all images (including intermediate layers)

  • docker images -q or --quiet: Only shows image IDs

  • docker images --digests: Shows digest information

  • docker images --no-trunc: Displays full image IDs without truncation

  • docker images --format "{{.ID}}: {{.Repository}}" : Custom output format

Example combining options:

# List only image IDs for all images
docker images -a -q

# List images with specific format
docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"

Start a Docker Container#

docker run [-it] [-d] <image_name>
Explanation:
  • -it: This option is used to run the container in interactive mode and allocate a pseudo-TTY.

  • -d: This option is used to run the container in detached mode.

  • <image_name>: The name of the image to run.

  • docker run: This command runs the container.

  • example: docker run -d my_image

Build a Specific Docker Image#

docker build -t <image_name> <path_to_dockerfile>
Explanation:
  • -t: This option is used to tag the image with a name.

  • <image_name>: The name of the image to build.

  • <path_to_dockerfile>: The path to the Dockerfile.

  • docker build: This command builds the image.

  • example: docker build -t my_image ./path/to/dockerfile

Map an External Port to a Docker Container Port#

docker run -p <host_port>:<container_port> <image_name>
Explanation:
  • -p: This option is used to map the specified port on the host to the specified port on the container.

  • <host_port>: The port on the host machine.

  • <container_port>: The port on the container.

  • <image_name>: The name of the image to run.

  • docker run: This command runs the container.

  • example: docker run -p 8080:80 my_image

Map an External Directory to a Docker Container Directory#

docker run -v <host_directory>:<container_directory> <image_name>
Explanation:
  • -v or --volume: This option creates a volume mount between host and container

  • <host_directory>: The path to the directory on your host machine

  • <container_directory>: The path where the directory will appear in the container

  • <image_name>: The name of the image to run

  • example: docker run -v /home/user/data:/data my_image

Common options:
  • -v can be used multiple times to mount multiple directories

  • Add :ro at the end for read-only access: -v /host/dir:/container/dir:ro

  • Use --mount for more explicit volume mounting syntax

Example with multiple volumes:

docker run \
    -v /home/user/configs:/etc/configs \
    -v /home/user/data:/var/data:ro \
    my_image

Connect to a Running Docker Container#

docker exec -it <container_id> /bin/bash
Explanation:
  • -it: This option is used to run the container in interactive mode and allocate a pseudo-TTY.

  • <container_id>: The ID of the container to connect to.

  • /bin/bash: The command to run inside the container.

  • docker exec: This command runs the specified command inside the container.

  • example: docker exec -it 1234567890 /bin/bash