🐳Docker

Docker from ground up.

🐳 What is Docker?

  • Standardized packaging for software and dependencies

  • Isolate apps from each other

  • share the same OS kernel

  • Works for all major Linux distributions.

Note: Docker Containers are NOT VMs

Docker Containers are NOT VMs

Dockerfile:

Series of instructions to build Docker Images.

We define our app's environment with a Dockerfile so it can be reproduced anywhere.

Example of Dockerfile

Dockerfile
# Our Base Image
FROM alpine:latest

# Install Python and pip
RUN apk --update py-pip

# Upgrade pip
RUN pip install --upgrade pip

# Install Python modules needed by the Python app
COPY requirements.txt /user/src/app/
RUN pip install --no-cache-dir -r /usr/src/app/requirements.txt

# Copy files required for the app to run
COPY app.py /user/src/app/
COPY templates/index.html /usr/src/app/templates/

# Tell the port number the container should expose
EXPOSE 8000

# Run the application
CMD ["python", "/usr/src/app/appy.py"]

Docker Compose:

Compose is a tool for defining and running multi-container Docker applications. Use a YAML file to configure application services. Compose works in all environments: production, staging, development, testing, as well as CI workflows. With a single command, we can create and start all services from our configuration.

We define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.

Example of Docker compose.

docker-compose.yml
version: "3.8"
services:

  redis:
    image: redis:alpine
    ports:
      - "6379"
    networks:
      - frontend
    deploy:
      replicas: 2
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

  db:
    image: postgres:9.4
    volumes:
      - db-data:/var/lib/postgresql/data
    networks:
      - backend
    deploy:
      placement:
        max_replicas_per_node: 1
        constraints:
          - "node.role==manager"
  vote:
    image: dockersamples/examplevotingapp_vote:before
    ports:
      - "5000:80"
    networks:
      - frontend
    depends_on:
      - redis
    deploy:
      replicas: 2
      update_config:
        parallelism: 2
      restart_policy:
        condition: on-failure

  result:
    image: dockersamples/examplevotingapp_result:before
    ports:
      - "5001:80"
    networks:
      - backend
    depends_on:
      - db
    deploy:
      replicas: 1
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure

  worker:
    image: dockersamples/examplevotingapp_worker
    networks:
      - frontend
      - backend
    deploy:
      mode: replicated
      replicas: 1
      labels: [APP=VOTING]
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 3
        window: 120s
      placement:
        constraints:
          - "node.role==manager"

  
networks:
  frontend:
  backend:

volumes:
  db-data:

Difference between Docker Compose Vs Dockerfile

A Dockerfile is a simple text file that contains the commands a user could call to assemble an image whereas Docker Compose is a tool for defining and running multi-container Docker applications.

Docker Compose defines the services that make up your app in docker-compose.yml so they can be run together in an isolated environment. It gets an app running in one command by just running docker-compose up. Docker-compose uses the Dockerfile if one adds the build command to your project’s docker-compose.yml. Your Docker workflow should be to build a suitable Dockerfile for each image you wish to create, then use compose to assemble the images using the build command.

Resource:

http://dockerlabs.collabnix.com/

Last updated

Was this helpful?