Skip to content

kuldeepmehra27/Docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Table of Contents

Docker(docker) written in GO language : Docker compose(docker compose) written in PYTHON

Docker

Docker is an open source platform that enables developers to build, deploy, run, update and manage containers—standardized, executable components that combine application source code with the operating system (OS) libraries and dependencies required to run that code in any environment. For more details visit here.

Docker technology

Docker technology consists of the runtime, engine and orchestrator

  • Runtime: Responsible for starting and stopping containers.

  • Daemon/Engine: Expose the Docker remote API, manage images, volumes, networks etc.

  • Orchestrator: Manging clusters (Swarns) of nodes running docker.

Docker technology

Docker architecture

Docker uses a client-server architecture. The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. The Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon. The Docker client and daemon communicate using a REST API, over UNIX sockets or a network interface. Another Docker client is Docker Compose, that lets you work with applications consisting of a set of containers.

Docker architecture

docker pull: Docker pull with verify if the image is already downloaded locally, if not it will download it from DockerHub. Ex: docker pull drupal

docker build: Docker build will build the current Dockerfile and will create locally an image for our application. Ex: docker build .

docker run: Docker run will take the image and run the container. Ex: docker run nginx

docker push: Docker push will upload the image/extension/plugin to DockerHub. Ex: docker push

Orchestration

Container orchestration is the automation of much of the operational effort required to run containerized workloads and services. This includes a wide range of things software teams need to manage a container’s lifecycle, including provisioning, deployment, scaling (up and down), networking, load balancing and more.

The things that container orchestration will do:

  1. Deployment
  2. Scaling: schedule containers to the right worker node for the best resources utilization
  3. Networking: create load balancers for external and internal services communication
  4. Operations and Insight: automatically bring up instances of a services in failure; provide integration points for service mesh and logging

References

Images

A Docker image is a read-only template that contains a set of instructions for creating a container that can run on the Docker platform. It provides a convenient way to package up applications and preconfigured server environments, which you can use for your own private use or share publicly with other Docker users. Docker images are also the starting point for anyone using Docker for the first time.

Docker Image

Containers

Containers are executable units of software in which application code is packaged, along with its libraries and dependencies, in common ways so that it can be run anywhere, whether it be on desktop, traditional IT, or the cloud.

Your basic isolated Docker process. Containers are to Virtual Machines as threads are to processes.

A container is a runable instance of an image. Can create, start, stop, move or delete a container using the Docker API or CLI.

Container

Volumes

Two main categories of data: Persistent and non-persistent

  • Non-persistent: Data gets written to the top layer and gets deleted with the container.

  • Persistent: Need to keep even after container deletion.

Volumes are used to persist data

Docker volumes are a widely used and useful tool for ensuring data persistence while working in containers. Docker volumes are file systems mounted on Docker containers to preserve data generated by the running container.

References

Dockerfile

A Dockerfile is the starting point for creating a container image. It describes an application and tells Docker how to build it into an image. For more details visit here

WHY: To customize our container

Dockerfile ==> docker build ==> Docker image ==> docker run ==> Docker Container

Dockerfile

Instructions / Commands

  • .dockerignore
  • FROM Sets the Base Image for subsequent instructions.
  • MAINTAINER (deprecated - use LABEL instead) Set the Author field of the generated images.
  • RUN execute any commands in a new layer on top of the current image and commit the results.
  • CMD provide defaults for an executing container.
  • EXPOSE informs Docker that the container listens on the specified network ports at runtime. NOTE: does not actually make ports accessible.
  • ENV sets environment variable.
  • ADD copies new files, directories or remote file to container. Invalidates caches. Avoid ADD and use COPY instead.
  • COPY copies new files or directories to container. By default this copies as root regardless of the USER/WORKDIR settings. Use --chown=<user>:<group> to give ownership to another user/group. (Same for ADD.)
  • ENTRYPOINT configures a container that will run as an executable.
  • VOLUME creates a mount point for externally mounted volumes or other containers.
  • USER sets the user name for following RUN / CMD / ENTRYPOINT commands.
  • WORKDIR sets the working directory.
  • ARG defines a build-time variable.
  • ONBUILD adds a trigger instruction when the image is used as the base for another build.
  • STOPSIGNAL sets the system call signal that will be sent to the container to exit.
  • LABEL apply key/value metadata to your images, containers, or daemons.
  • SHELL override default shell is used by docker to run commands.
  • HEALTHCHECK tells docker how to test a container to check that it is still working.

click here to get more details about environment replacement.

Note: Check this .env file for more details.

Example

# Base image
FROM ubuntu:22.04
RUN echo "It is working fine!!"

ENV TZ=Asia/Kolkata DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install tzdata

# Work directory
WORKDIR /var/www/html

# Install Apache
RUN apt-get -y install apache2 
RUN apt-get -y install apache2-utils 
RUN apt-get clean 
EXPOSE 80

# Enable rewrite module
RUN a2enmod rewrite

# Install PHP
RUN apt-get -y install --no-install-recommends php8.1
RUN apt-get -y install php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl

# Install composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
	php composer-setup.php && \
	mv composer.phar /usr/local/bin/composer && \
	php -r "unlink('composer-setup.php');"
ENV PATH="~/.composer/vendor/bin:${PATH}"

# Remove apache default index html file
RUN rm -rf index.html

# Define path variable
ARG SITE_PATH=/var/www/html
ENV PATH=$SITE_PATH

# This is resolve 404 isseue
RUN echo "\n<Directory /var/www/>" >> /etc/apache2/apache2.conf
RUN echo "\t Options Indexes FollowSymLinks" >> /etc/apache2/apache2.conf
RUN echo "\t AllowOverride All" >> /etc/apache2/apache2.conf
RUN echo "\t Require all granted" >> /etc/apache2/apache2.conf
RUN echo "</Directory>" >> /etc/apache2/apache2.conf

RUN echo "Done, please browse to http://127.0.0.1:8000/web to check!"
RUN service apache2 restart


#Install drush
RUN composer global require drush/drush

CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]

Note: Check this Dockerfile for more details.

Lifecycle

Docker Compose

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose visit here.

Example

version: '3.1'

services:

# Pull image of mariadb
  drupal10_db: # this is host of database
    image: mariadb:10.5.9
    container_name: mariadb
    ports:
      - 3308:3307
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=drupal10
      - MYSQL_USER=drupal10
      - MYSQL_PASSWORD=drupal10

# Pull image of drupal from dockerfile
  drupal:
    container_name: "${COMPOSE_PROJECT_NAME}"
    depends_on:
      - drupal10_db
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:80"
    volumes:
      - d_modules:/var/www/html/web/modules
      - d_themes:/var/www/html/web/themes
      - d_profiles:/var/www/html/web/profiles
      - d_sites:/var/www/html/web/sites
    links:
      - drupal10_db:mariadb
    restart: always

# Pull image of phpmyadmin
  phpmyadmin:
    image: phpmyadmin:5.2.0
    container_name: phpmyadmin
    ports:
      - 9000:80
    environment:
      - PMA_ARBITRARY=1
    restart: always

volumes:
  d_modules:
  d_themes:
  d_profiles:
  d_sites:

Note: Check this docker-compose.yml file for more details.

Installing Docker

1. Installing Docker on Ubuntu

  • Remove old version of Docker
$ sudo apt-get purge docker-ce docker-ce-cli containerd.io docker-compose-plugin

$ sudo rm -rf /var/lib/docker
 
$ sudo rm -rf /var/lib/containerd

  • Installing Docker on Ubuntu
$ sudo apt-get update
 
$ sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

$ sudo mkdir -p /etc/apt/keyrings
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

$ echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

$ sudo groupadd docker
$ sudo usermod -aG docker $USER

Check if docker is successfully installed in your system

$ systemctl start docker

$ docker run hello-world

Note: For ubuntu desktop version Follow this guide

2. Installing Docker on Mac Follow official guide

3. Installing Docker on Windows Follow official guide

Play with Docker

Play with Docker (PWD) provides a free-to-use fully functional Docker playground that lasts for 4 hours. For playing with docker click here

About

Docker study guide with sample project

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published