前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Docker Curriculum

Docker Curriculum

作者头像
Cellinlab
发布2023-05-17 16:49:12
1540
发布2023-05-17 16:49:12
举报
文章被收录于专栏:Cellinlab's BlogCellinlab's Blog

Learn to build and deploy your distributed applications easily to the cloud with Docker.

# Hello World

# Playing with Busybox

代码语言:javascript
复制
# fetch docker image from Docker Hub
docker pull busybox

# list all images
docker images

# run a container based on the image
docker run busybox

# run a container with a command
docker run busybox echo "hello world"

# list all containers
docker ps -a

# run with -it flag to attach us to an interactive tty in the container
docker run -it busybox sh

# clean up containers
docker rm <container_id>

# clean up a bunch of containers
# -a flag to list all containers
# -q flag to only output the container id
# -f flag to filter the output
docker rm $(docker ps -a -q -f status=exited)

# clean up images
docker rmi <image_id>

# Terminology

  • Images - The blueprints of our application which form the basis of containers.
  • Containers - Created from Docker images and run the actual application.
  • Docker Daemon - The background service running on the host that manages building, running and distributing Docker containers.
  • Docker Client - The command line tool that allows the user to interact with the daemon.
  • Docker Hub - A registry of Docker images.

# WebApps with Docker

# Static Sites

代码语言:javascript
复制
# pull the image from Docker Hub
docker pull prakhar1989/static-site

# Create a static container
# --rm automatically removes the container when it exits
# -it attaches us to an interactive tty session in the container
docker run --rm -it prakhar1989/static-site

# Hit Ctrl+C to exit the container

# Create a static container with exposing port
# -d flag to run the container in detached mode
# -P flag to publish all exposed ports to random ports
# --name flag to name the container
docker run -d -P --name static-site prakhar1989/static-site

# list ports that are mapped to the container
docker port static-site

# specify a custom port mapping
docker run -d -p 8888:80 --name static-site prakhar1989/static-site

# stop the container
docker stop static-site

# Docker Images

  • To list all images
代码语言:javascript
复制
docker images

  • An important distinction between base images and child images
    • Base images - that have no parent image, usually images with an OS like Ubuntu, Debian, Alpine, etc.
    • Child images - that are built on top of base images and add additional functionality.

# Our First Image

Create a simple Flask application:

代码语言:javascript
复制
git clone https://github.com/prakhar1989/docker-curriculum.git

cd docker-curriculum/flask-app

# Dockerfile

A Dockerfile is a simple text file that contains a list of commands that Docker clients calls while creating an image.It is a simple wait to automate the image creation process.

代码语言:javascript
复制
# Use an official Python runtime as a parent image
FROM python:3.8

# Set the working directory to /app
WORKDIR /usr/src/app

# Copy the current directory contents into the container at /app
COPY . .

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Specify the port number the container should expose
EXPOSE 5000

# Run app.py when the container launches
CMD [ "python", "./app.py" ]

Build the image:

代码语言:javascript
复制
# build the image
# -t flag to tag the image
# . flag to specify the current directory which contains the Dockerfile
docker build -t cellinlab/catnip .

# Docker on AWS

Publish the image to Docker Hub:

代码语言:javascript
复制
# login to Docker Hub
docker login

# tag the image
docker tag cellinlab/catnip cellinlab/catnip:latest

# push the image
docker push cellinlab/catnip

# AWS Elastic Beanstalk (EB)

  • Dockerrun.aws.json
代码语言:javascript
复制
{
  "AWSEBDockerrunVersion": "1",
  "Image": {
    "Name": "cellinlab/catnip",
    "Update": "true"
  },
  "Ports": [
    {
      "ContainerPort": "5000",
      "HostPort": "9000"
    }
  ],
  "Logging": "/var/log/nginx"
}

# Multi Container Environments

# SF Food Trucks

Clone the repo:

代码语言:javascript
复制
git clone https://github.com/prakhar1989/FoodTrucks

cd FoodTrucks

The flask-app folder contains the Python application, while the utils folder has some utilities to load the data into Elasticsearch.

代码语言:javascript
复制
tree -L 2

# output:
# .
# ├── aws-ecs
# │   └── docker-compose.yml
# ├── docker-compose.yml
# ├── Dockerfile
# ├── flask-app
# │   ├── app.py
# │   ├── package.json
# │   ├── package-lock.json
# │   ├── requirements.txt
# │   ├── static
# │   ├── templates
# │   └── webpack.config.js
# ├── README.md
# ├── setup-aws-ecs.sh
# ├── setup-docker.sh
# ├── shot.png
# └── utils
#     ├── generate_geojson.py
#     └── trucks.geojson

We need two containers. One for the Python application and one for Elasticsearch.

代码语言:javascript
复制
# search for elasticsearch image
docker search elasticsearch

# pull the image
docker pull docker.elastic.co/elasticsearch/elasticsearch:6.3.2

# run the container
# -d flag to run the container in detached mode
# --name flag to name the container
# -p flag to publish the container port to the host
# -e flag to set environment variables
docker run -d --name es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.3.2

# check if the container is running
docker container ls

# check the logs
docker container logs es

# check if Elasticsearch is running
curl localhost:9200

Dockerfile for the flask app:

代码语言:javascript
复制
# start from base
FROM ubuntu:18.04

LABEL maintainer="Cell <cellinlab@gmail.com>"

# install system-wide deps for python and node
RUN apt-get -yqq update
RUN apt-get -yqq install python3-pip python3-dev curl gnupg
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash
RUN apt-get install -yq nodejs

# copy our application code
ADD flask-app /opt/flask-app
WORKDIR /opt/flask-app

# fetch app specific deps
RUN npm install
RUN npm run build
RUN pip3 install -r requirements.txt

# expose port
EXPOSE 5000

# start app
CMD [ "python3", "./app.py" ]

Build the image:

代码语言:javascript
复制
# build the image
docker build -t cellinlab/foodtrucks-web .

# run the container
docker run -P --rm --name foodtrucks-web cellinlab/foodtrucks-web

Flask app was unable to connect to Elasticsearch.

# Docker Network

When docker is installed, it creates there three networks automatically:

代码语言:javascript
复制
docker network ls

# output:
# NETWORK ID     NAME      DRIVER    SCOPE
# 3ba686ae5939   bridge    bridge    local
# 872df209dfe2   host      host      local
# 295b09245ca6   none      null      local

# inspect the bridge network
docker network inspect bridge

The bridge network is the network in which containers are run by default.The bridge network is shared by every conatiner by default.

Create a new network:

代码语言:javascript
复制
# create a new network
docker network create foodtrucks-net

# check if the network is created
docker network ls

# output:
# NETWORK ID     NAME             DRIVER    SCOPE
# 3ba686ae5939   bridge           bridge    local
# f7ba51f89c4a   foodtrucks-net   bridge    local
# 872df209dfe2   host             host      local
# 295b09245ca6   none             null      local

A bridge network uses a software bridge which allows containers connected to the same bridge network to communicate, while providing isolation from containers which are not connected to that bridge network.

代码语言:javascript
复制
# run the elasticsearch container in the new network
docker conatiner stop es

docker conatiner rm es

docker run -d --name es --net foodtrucks-net -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.3.2

# check if the container is joined to the network
docker network inspect foodtrucks-net

Try to run the flask app container in the new network:

代码语言:javascript
复制
docker run -it --rm --name foodtrucks-web --net foodtrucks-net cellinlab/foodtrucks-web bash

# check if the app can connect to elasticsearch
curl es:9200

# output:
# {
#   "name" : "zFgYJzl",
#   "cluster_name" : "docker-cluster",
#   "cluster_uuid" : "C9llxDniSImHAqupRj5bjg",
#   "version" : {
#     "number" : "6.3.2",
#     "build_flavor" : "default",
#     "build_type" : "tar",
#     "build_hash" : "053779d",
#     "build_date" : "2018-07-20T05:20:23.451332Z",
#     "build_snapshot" : false,
#     "lucene_version" : "7.3.1",
#     "minimum_wire_compatibility_version" : "5.6.0",
#     "minimum_index_compatibility_version" : "5.0.0"
#   },
#   "tagline" : "You Know, for Search"
# }

Launch the flask app container in the new network:

代码语言:javascript
复制
docker run -d --net foodtrucks-net -p 5000:5000 --name foodtrucks-web cellinlab/foodtrucks-web

# check if the container is running
docker container ls

# check the app
curl -I 0.0.0.0:5000

Do these steps in a bash script:

setup-docker.sh:

代码语言:javascript
复制
#!/bin/bash

# build the image
docker build -t cellinlab/foodtrucks-web .

# create a new network
docker network create foodtrucks-net

# run the elasticsearch container in the new network
docker run -d --name es --net foodtrucks-net -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.3.2

# run the flask app container in the new network
docker run -d --net foodtrucks-net -p 5000:5000 --name foodtrucks-web cellinlab/foodtrucks-web

# Docker Compose

Compose is a tool for defining and running multi-container Docker applications with YAML files.

Compose works in all environments: production, staging, development, testing, as well as CI workflows.

Install docker-compose:

代码语言:javascript
复制
# install docker-compose
pip install docker-compose

# check if docker-compose is installed
docker-compose --version

Docker Compose file docker-compose.yml:

代码语言:javascript
复制
version: "3"
services:
  es:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.3.2
    container_name: es
    environment:
      - discovery.type=single-node
    ports:
      - 9200:9200
    volumes:
      - esdata1:/usr/share/elasticsearch/data
  web:
    # image: cellinlab/foodtrucks-web
    build: . # build the image from the Dockerfile
    command: python3 app.py
    depends_on:
      - es
    ports:
      - 5000:5000
    volumes:
      - ./flask-app:/opt/flask-app
volumes:
  esdata1:
    driver: local

Run the docker-compose file:

代码语言:javascript
复制
# run the docker-compose file
docker-compose up -d

# check
docker-compose ps
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021/6/3,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • # Hello World
    • # Playing with Busybox
      • # Terminology
      • # WebApps with Docker
        • # Static Sites
          • # Docker Images
            • # Our First Image
              • # Dockerfile
                • # Docker on AWS
                  • # AWS Elastic Beanstalk (EB)
                  • # Multi Container Environments
                    • # SF Food Trucks
                      • # Docker Network
                        • # Docker Compose
                        相关产品与服务
                        容器镜像服务
                        容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档