Docker

“Do one thing, do it small.”

Get Help

#### Get Help
docker image --help
docker container --help
docker volume --help
docker network --help

## Convert
convert docker run to compose file : https://github.com/magicmark/composerize
or this online tool: https://www.composerize.com/

###### Switch between Docker Desktop and Docker CE (local docker installation)
docker context ls

#In order to use Docker Desktop, you need to switch to the context called “desktop-linux”.

docker context use desktop-linux

Images

##### list docker images
docker image ls

##### pull new image
docker pull ubuntu:noble

##### Remove all images
docker rmi $(docker images -a -q)

#### Build image from current directory
docker image build -t myapp .

To ensure that you're getting the latest versions of dependencies in your build, you can use the --no-cache option to avoid cache hits.

docker build --no-cache -t myapp:my-tag .

##### Build and start the image
### --rm destroy container after startup, --no-cache to avoid problems with multiple RUN connads
### port 80 inside the container, 8080 exposed
docker image build --no-cache -t image_myapp . && docker run --rm -tid --name container_myapp -p 8080:80 image_myapp

Containers

##### list containers
docker ps -a

##### start docker container and log in to bash
docker run -ti ubuntu:noble /bin/bash

##### start docker container and give it a name
docker run -ti --name myubuntucontainer ubuntu:noble /bin/bash

##### start stopped container and log in to bash
docker container start -i myubuntucontainer

##### execute command in running container, -d - run it in the background
docker exec -d myubuntucontainer touch /tmp/execWorks

##### log in to running container
docker exec -it myubuntucontainer bash

##### remove container
docker container rm myubuntucontainer

##### Run app with docker run. Start in project directory
docker run -d -v $(pwd)/www:/usr/local/apache2/htdocs:ro --name p01_httpd -p "8080:80" httpd:2.4

##### Start container with a volume to preserve data. -volume name p03_php_www | Port 8080 is on the host machine.

docker run -d --mount source=p03_php_www,target=/var/www --name p03_php -p "8080:80" localhost/p03_php

##### Remove all stopped containers
docker rm $(docker ps -a -q)

Dockerfile examples

Simple, small and unsecure

FROM ubuntu:20.04
WORKDIR /var/www

####asking for geo location
ENV DEBIAN_FRONTEND=noninteractive 

## install application dependencies
RUN apt-get update
RUN apt-get install php7.4 php7.4-gd php7.4-xml php7.4-zip php7.4-mbstring php7.4-curl php7.4-json php7.4-pgsql php7.4-soap php7.4-fpm libapache2-mod-php7.4 -y
RUN a2enmod rewrite
## copy source code
COPY . /var/www/my-app
COPY 000-default.conf /etc/apache2/sites-available
EXPOSE 80



## setup an app user so the container doesn't run as the root user

USER www-data
##start apache as first service inside the container
CMD ["apache2ctl", "-D", "FOREGROUND"]
Ready to start in Elastic container service, read only file system. High security even with old php version
FROM debian:trixie-slim
WORKDIR /var/www/my-app

## copy source code
COPY . /var/www/my-app
### entry point for cache 
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
####asking for geo location
ENV DEBIAN_FRONTEND=noninteractive

## add onrej ppa and install php, no-install-recomends do not install recommended packages, in this way the image is smaller, but some packages can be missing
RUN apt-get update -y && apt-get install --no-install-recommends -y  \
   apt-transport-https \
   lsb-release \
   ca-certificates \
   wget \
   && wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg \
   && echo "deb https://packages.sury.org/php/ $(lsb\_release -sc) main" | tee /etc/apt/sources.list.d/php.list \
   && apt-get update -y && apt-get install --no-install-recommends -y \
   php7.4 \
   php7.4-gd \
   php7.4-xml \
   php7.4-zip \
   php7.4-mbstring \
   php7.4-curl \
   php7.4-json \
   php7.4-pgsql \
   php7.4-soap \
   php7.4-cli \
   php7.4-common \
   php7.4-opcache \
   php7.4-readline \
   apache2 \
   libapache2-mod-php7.4 \
   && a2enmod rewrite \
   && chown -R www-data:www-data /var/www/my-app \
   && mkdir -p /var/run/apache2 && \
   chown -R www-data:www-data /var/run/apache2 /var/log/apache2 \
   && sed -ri 's/^Listen 80/Listen 8080/' /etc/apache2/ports.conf \
   && sed -i 's/^\s*variables_order\s*=.*$/variables_order = "EGPCS"/' /etc/php/7.4/apache2/php.ini \
   && sed -i 's|^ErrorLog ${APACHE_LOG_DIR}/error.log|# ErrorLog ${APACHE_LOG_DIR}/error.log|' /etc/apache2/apache2.conf \
   && sed -i 's|^PidFile .*|PidFile /tmp/apache2.pid|' /etc/apache2/apache2.conf \
   && echo "ErrorLog /dev/stderr" >> /etc/apache2/apache2.conf \
   && echo "CustomLog /dev/stdout combined" >> /etc/apache2/apache2.conf \
   && echo "session.save_path = /tmp/php-sessions" >> /etc/php/7.4/apache2/php.ini \
   && echo "error_log = php://stderr" >> /etc/php/7.4/apache2/php.ini \
   && rm /etc/apache2/conf-enabled/other-vhosts-access-log.conf \
   && rm -rf /var/lib/apt/lists/*

## copy apache config
COPY 000-default.conf /etc/apache2/sites-available

VOLUME ["/tmp"]

EXPOSE 8080



## setup an app user so the container doesn't run as the root user
USER www-data

ENTRYPOINT ["entrypoint.sh"]

entrypoint.sh
#!/bin/sh
set -e

mkdir -p /tmp/cache \
	/tmp/php-sessions 

chmod 733 /tmp/php-sessions

exec apache2ctl -D FOREGROUND