My Docker Handbook

My Docker Handbook

Docker notes

·

5 min read

Table of contents

No heading

No headings in the article.

Problem

The problem before using docker. Imagine we are developing several applications using PHP, each application has a PHP version that may affect other versions of the stack such as Nginx, redish, and others. Containers can solve all these problems.

Then what is Docker?

Docker is a container technology for hosting applications. Inside the container, there are several images. Each container is isolated, meaning we can create multiple infrastructures.

Install Images:

docker pull redish:latest

View the list of containers:

docker container ls -a

Running containers

docker container start redisexample

Stop container

docker container stop redisexample

Remove containers:

docker container rm redisexample

Running/executing bash in a container

- docker container exec -i -t exampleredis /bin/bash

- cds /

- ls

- exit (exit bash)

PORT Containers:

  • is each port image that is in the container, this port can only be accessed in the container. This port is not the same as our local port, for example, a laptop.

  • then how can our laptop read this port? the solution we have to do port forwarding.

  • port forwarding command:

    docker container create --name containername --publish posthost:portcontainer image:tag
    

    (make sure the ports are correct and not swapped for access)

Running PORTS:

  • our example will run port nginx

  • install image first run:

docker image pull nginx:latest
  • then create image and port:
    docker container create --name nginxexample --publish 8080:80 nginx:latest
    
  • then run container:
docker container start nginxexample
  • see port :
docker container ls -a

run in browser: localhost:8080

Container Environment Variable

  • Environment variables are a technique so that application configurations can be changed dynamically

  • By using environment variables, we can change the configuration of the application, without having to change the application code again

  • The Docker container has parameters that we can use to pass environment variables to the application contained in the container

Adding Environment Variables

  • run the command --env or -e, for example:
docker container create --name containername --env KEY="value" --env KEY2 = "value" image:tag
  • first, install mongo image first:

    docker image pull mongo:latest
    
  • example: run the command

    docker container create --name mongoexample --publish 27017:27017 --env MONGO_INIT_DB_ROOT_USERNAME=kang --env MONGO_INIT_DB_ROOT_PASSWORD=kang mongo:latest
    
  • we can see the environment variables of each image on their respective web pages, for example for mongo: hub.docker.com/_/mongo in the overview section there is an environment script.

Container Stats

How to see resource usage

run command:

docker container stats

Container Resource Limit

If an error occurs, for example, the container takes up too much CPU and Memory, it can impact the performance of other containers, or even the host system. Therefore we have to limit a container

Memory

  • When we create a container we can determine the amount of memory with the --memory command followed by the number that is allowed to be used

  • We can add size in the form of b (bytes), k (kilobytes), m (megabytes), or g (gigabytes), for example, 100m means 100 megabytes

CPUs

  • CPU In addition to managing memory, we can also determine how much CPU the container can use with the --CPUs parameter.

  • For example, if we set the value to 1.5, it means that the container can use one and a half CPU cores

Example Command to add memory and CPUs:

docker container create --name smallnginx --publish 8081:80 --memory 100m --cpus 0.5 nginx:latest

Bind Mounts

  • Bind Mounts is the ability to mount (share) files or folders on the host's system to a container in Docker

  • This file is very useful when for example we want to send a configuration from outside the container

  • To mount, we can use the --mount parameter when creating the container, the contents of the --mount parameter have their own rules (type, source, destination, read-only)

Doing mounting

for example moving data from mongo in the container to local or our computer, the command:

docker container create --name mongodata --publish 27018:27017 --mount "type=bind,source=C:\Users\hello\mongodata,destination =/data/db" --env MONGO_INIT_DB_ROOT_USERNAME=kang --env MONGO_INIT_DB_ROOT_PASSWORD=eko mongo:latest

Running on a project for example laravel:

docker-compose.yaml

version: "2.2"

services:
    main:
        build:
            dockerfile: Dockerfile
            context: .
        container_name: kangmus-app
        restart: always
        volumes:
            - .:/var/www/html
            # - ./docker/supervisord.conf:/etc/supervisord.conf
            # - ./docker/nginx.conf:/etc/nginx/nginx.conf  
            # - ./docker/php.ini /usr/local/etc/php
        ports:
            - "3100:80"
        environment:
            DB_HOST: mysql
            DB_PORT: 3306
            DB_DATABASE: kangmus
            DB_USERNAME: root
            DB_PASSWORD: root

    mysql:
        image: mariadb:10.3
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: root
            MYSQL_DATABASE: kangmus-app
        volumes:
           - ./tmp/mysql_data:/var/lib/mysql

    phpmyadmin:
        image: phpmyadmin
        restart: always
        ports:
            - 8084:80
        environment:
            - PMA_ARBITRARY=1
            - PMA_HOST=mysql
            - PMA_USER=root
            - PMA_PASSWORD=root

Dockerfile

FROM php:8.0.0-apache

#Composer#
COPY composer.json /var/www/html/

WORKDIR /var/www/html

RUN COMPOSER_MEMORY_LIMIT=-1 composer install --no-scripts --no-autoloader
#End of Composer#
RUN docker-php-ext-install mysqli pdo pdo_mysql
#Code Setting#
COPY ./docker/nginx.conf /etc/nginx/nginx.conf
COPY ./docker/supervisord.conf /etc/supervisord.conf
# COPY ./docker/www.conf /usr/local/etc/php-fpm.d/www.conf
COPY ./docker/php.ini /usr/local/etc/php
COPY . /var/www/html/
RUN composer dump-autoload
RUN php artisan config:clear
#End Of Code Setting#

# COPY .env.alpha .env

RUN chown -R www-data /var/www/html/storage && \
    chown -R www-data /var/www/html/public && \
    chgrp -R www-data storage /var/www/html/storage && \
    chmod -R ug=r+w+x storage /var/www/html/storage && \
    chown -R www-data:www-data /tmp

EXPOSE 80 443
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisord.conf"]

And run command:

docker-compose up -d --build

Stop

docker-compose down -v

view logs:

docker-compose logs main

go to /html

docker-compose exec main sh

composer install