许 志强

许 志强

1647903720

【Docker教程】什么是Docker Compose?

上一章中,我们学习了常用的Docker命令,以及如何运行由多个容器组成的项目。当一个容器变得复杂之后,很可能需要类似以下的命令来运行容器:

docker run \
  --detach \
  --name registry \
  --hostname registry \
  --volume $(pwd)/app/registry:/var/lib/registry \
  --publish 5000:5000 \
  --restart unless-stopped \
  registry:latest

如果我们有多个容器,若每次都要输入这些命令就很麻烦了。Docker为了解决更好管理多个容器的问题,就推出了 Docker Compose 这个项目,帮助我们更方便地定义和运行多个容器。

Docker Compose 很好使用,我们只需要将所有容器的设置放到一个单独的 docker-compose.yml 文件中。本章我们就通过两个项目来学习Docker Compose,帮助我们深入掌握Docker Compose。首先大家先下载公开的repo,代码都在里面:

$ git clone https://github.com/turingplanet/docker-tutorial.git
$ cd docker-tutorial/3-docker-compose/app1

前后端项目

在第一个项目中,我们有两个容器,一个是backend负责启动服务器,开启后端端口。第二个是frontend,实现前端的展示。如果想要同时启动两个容器,我们可以将设置写在 docker-compose.yml 中:

version: "3.9"

services:

  backend:  
    build: ./backend
    ports: 
      - "5000:5000"
    volumes:
      - ./backend:/app

  frontend:
    build: ./frontend
    ports:  
      - "3000:3000"

其中参数的含义是:

  • version “3.9”:代表我们使用的是Docker Compose的3.9版本。
  • services:这个区域指定了我们会创建的容器,在这个例子中,我们有两个服务,backend和frontend。
  • backend:这是后端程序的名字。
  • build:指定了 Dockerfile 的路径,.backend 代表在 docker-compose.yml 所在目录下的 backend 文件夹中。
  • ports:指定本机端口和容器端口的映射关系。
  • volumes:制定挂载的参数,相当于 -v 之后的内容。

如果要构建镜像 (image),只需要输入 docker-compose build 命令,紧接着使用 docker-compose up 启动容器:

$ docker-compose build
$ docker-compose up

如果要启动特定的服务只需要在 docker-compose up 后面填上服务的名字即可:

$ docker-compose up <service1> <service2>
$ docker-compose up backend

如果要关闭容器,输入 docker-compose stop 即可。如果要查看当前容器的状态,只需要输入 docker-compose ps:

     Name              Command         State     Ports
------------------------------------------------------
app1_backend_1    python3 server.py   Exit 137        
app1_frontend_1   python3 server.py   Exit 137  

redis项目

在第二个项目中,我们会联动两个服务:

version: "3.9"

services:

  redis-server:
    image: "redis"

  backend:  
    environment:
      - REDIS_HOST=redis-server
    build: ./backend
    ports: 
      - "5000:5000"

第一个服务是 redis 数据库,第二个服务是后端服务器,backend 中有一个环境变量 REDIS_HOST 被设置为 redis-server,这样后端的程序就能连到 redis 容器了:

from flask import Flask, render_template
import redis
import os
REDIS_HOST = os.environ.get('REDIS_HOST', '0.0.0.0')

app = Flask(__name__)
cache = redis.Redis(host = REDIS_HOST, port = 6379, db = 0)
cache.set('hits', 0)

@app.route('/')
def get_data():
    hit_num = cache.incr('hits')
    return f'I have been seen {hit_num} times.\n'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

如果要启动容器,只需要输入 docker-compose up 即可,然后打开游览器,输入 localhost:5000 就能看到后端的信息。

Docker-compose 常用命令整理

$ docker-compose build # Builds the images, does not start the containers.
$ docker-compose up # Builds the images if the images do not exist and starts the containers.
$ docker-compose stop # Stops your containers, but it won't remove them.
$ docker-compose down # Stops your containers, and also removes the stopped containers as well as any networks that were created. 

视频纲要:
00:25 - Docker Compose 概念
02:12 -  前后端项目
07:48 - Redis项目
 

下一章:Docker Registry和DockerHub

#docker 

What is GEEK

Buddha Community

【Docker教程】什么是Docker Compose?
Jerod  Durgan

Jerod Durgan

1625975580

How To Build A Kubernetes Backend In Compose CLI ?

Docker Compose is a great tool from Docker, it is used by millions to deploy and manage multi-containers applications. Docker Compose is basically 2 things:

  • The Compose file is a specification in yaml (named docker-compose.yaml by default) that Docker Compose takes as input. This file defines the components of a containerized application and how they are connected together. It should be written according to the Compose specification whose details are available in this repository
  • Docker Compose is the tool that processes this Compose file: it checks that it is written according to the Compose specification and deploys the application components

Anca Iordache from Docker, explains the move to the Compose Spec into the open and how she has started to develop a kube backend for the Compose CLI as a side project:

“The Compose format is very popular among developers due to its simplicity and there was always a lot of interest in having tools to deploy Compose files on platforms other than a single Docker Engine or Swarm. To make Compose go beyond Docker and Swarm, early in 2020, we opened the Compose specification to enable anybody to build tools around it. We used the new open specification and reference libraries to build support for Amazon ECS and Microsoft ACI into the Docker CLI for deploying Compose apps on these platforms. An obvious next target was Kubernetes as it is highly popular and there is a lot of interest in deploying Compose apps onto it. We wrote an initial proof of concept to test this integration but it hasn’t been added to Docker’s product roadmap yet. I have picked it up as my hack project to continue making progress with this integration. The current code for the Kubernetes backend can be found in the public repository docker/compose-cli and everybody is welcome to contribute to it.”

#kubernetes #docker #docker-compose #compose cli

许 志强

许 志强

1647903720

【Docker教程】什么是Docker Compose?

上一章中,我们学习了常用的Docker命令,以及如何运行由多个容器组成的项目。当一个容器变得复杂之后,很可能需要类似以下的命令来运行容器:

docker run \
  --detach \
  --name registry \
  --hostname registry \
  --volume $(pwd)/app/registry:/var/lib/registry \
  --publish 5000:5000 \
  --restart unless-stopped \
  registry:latest

如果我们有多个容器,若每次都要输入这些命令就很麻烦了。Docker为了解决更好管理多个容器的问题,就推出了 Docker Compose 这个项目,帮助我们更方便地定义和运行多个容器。

Docker Compose 很好使用,我们只需要将所有容器的设置放到一个单独的 docker-compose.yml 文件中。本章我们就通过两个项目来学习Docker Compose,帮助我们深入掌握Docker Compose。首先大家先下载公开的repo,代码都在里面:

$ git clone https://github.com/turingplanet/docker-tutorial.git
$ cd docker-tutorial/3-docker-compose/app1

前后端项目

在第一个项目中,我们有两个容器,一个是backend负责启动服务器,开启后端端口。第二个是frontend,实现前端的展示。如果想要同时启动两个容器,我们可以将设置写在 docker-compose.yml 中:

version: "3.9"

services:

  backend:  
    build: ./backend
    ports: 
      - "5000:5000"
    volumes:
      - ./backend:/app

  frontend:
    build: ./frontend
    ports:  
      - "3000:3000"

其中参数的含义是:

  • version “3.9”:代表我们使用的是Docker Compose的3.9版本。
  • services:这个区域指定了我们会创建的容器,在这个例子中,我们有两个服务,backend和frontend。
  • backend:这是后端程序的名字。
  • build:指定了 Dockerfile 的路径,.backend 代表在 docker-compose.yml 所在目录下的 backend 文件夹中。
  • ports:指定本机端口和容器端口的映射关系。
  • volumes:制定挂载的参数,相当于 -v 之后的内容。

如果要构建镜像 (image),只需要输入 docker-compose build 命令,紧接着使用 docker-compose up 启动容器:

$ docker-compose build
$ docker-compose up

如果要启动特定的服务只需要在 docker-compose up 后面填上服务的名字即可:

$ docker-compose up <service1> <service2>
$ docker-compose up backend

如果要关闭容器,输入 docker-compose stop 即可。如果要查看当前容器的状态,只需要输入 docker-compose ps:

     Name              Command         State     Ports
------------------------------------------------------
app1_backend_1    python3 server.py   Exit 137        
app1_frontend_1   python3 server.py   Exit 137  

redis项目

在第二个项目中,我们会联动两个服务:

version: "3.9"

services:

  redis-server:
    image: "redis"

  backend:  
    environment:
      - REDIS_HOST=redis-server
    build: ./backend
    ports: 
      - "5000:5000"

第一个服务是 redis 数据库,第二个服务是后端服务器,backend 中有一个环境变量 REDIS_HOST 被设置为 redis-server,这样后端的程序就能连到 redis 容器了:

from flask import Flask, render_template
import redis
import os
REDIS_HOST = os.environ.get('REDIS_HOST', '0.0.0.0')

app = Flask(__name__)
cache = redis.Redis(host = REDIS_HOST, port = 6379, db = 0)
cache.set('hits', 0)

@app.route('/')
def get_data():
    hit_num = cache.incr('hits')
    return f'I have been seen {hit_num} times.\n'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

如果要启动容器,只需要输入 docker-compose up 即可,然后打开游览器,输入 localhost:5000 就能看到后端的信息。

Docker-compose 常用命令整理

$ docker-compose build # Builds the images, does not start the containers.
$ docker-compose up # Builds the images if the images do not exist and starts the containers.
$ docker-compose stop # Stops your containers, but it won't remove them.
$ docker-compose down # Stops your containers, and also removes the stopped containers as well as any networks that were created. 

视频纲要:
00:25 - Docker Compose 概念
02:12 -  前后端项目
07:48 - Redis项目
 

下一章:Docker Registry和DockerHub

#docker 

Hollie  Ratke

Hollie Ratke

1596863220

composer.json v/s composer.lock

In this article you will learn what is the difference between composer install & composer update.

  1. composer.json v/s composer.lock
  2. Composer Install & Composer Update
  3. Tip while using composer

Prerequisites

You need to have composer installed in your computer or server.

Even if you don’t have any knowledge on the composer thats totally fine. I have written an details article for it here What Is Composer? How Does It Work? Useful Composer Commands And Usage.


composer.json v/s composer.lock

Composer Update ( Refers composer.json file )

When you do **composer update** it will check for composer.json file and updates all the packages/libraries that are listed in it & once the packages are updated it will rewrite new updates in **composer.json** & **composer.lock** file by deleting old package updates.

Basically the following process

  1. Read composer.json
  2. Remove installed packages that are not required in composer.json
  3. Check latest versions of required packages in composer.json from https://packagist.org
  4. Install the latest versions of your packages
  5. Update composer.lock with installed packages version & even update composer.json file with it
  6. composer install

Eg: Lets take an example of this, in your composer.json file you may have this

"require": {
    "guzzlehttp/guzzle": "^6.3",
}

Think that you have installed guzzle package for CURL requests 2 months back in your project with version was 6.3.0.

When you do composer update composer will go and check in its repositories if any new update available for the guzzle package. If any new update like 6.3.2 then it will go ahead and update the package to 6.3.2.

Along with updating the package it will also update the composer.json and composer.lock file.


Composer Install ( Uses composer.lock file)

When you do **composer install** it will check for **composer.lock** file and install all the packages/libraries that are listed in **composer.lock** file.

This command won’t update anything like composer update.

  1. composer.lock file
  2. If it does not exists then run composer-update and create it
  3. If exists then read composer.lock file for installation fo packages
  4. Install the packages specified in the composer.lock file

Composer Install & Composer Update

Composer Install (Production, Development & Testing)

Use this command in any of the product stages ie Production, Development & Testing. As this wont have any affect on the composer.json & composer.lock file.


Composer Update (Development & Testing)

Use this command in **Development & Testing **stages of your product only. As this will update the composer.json & composer.lock files.

Be very cautious with this command


Tip While Using Composer

Few of the tips I would like to give you guys from my experiences are as follows

1) Add **vendor** folder in .gitignore

Yes! You saw it right. Make sure to put vendor in .gitignore file if using GIT else similar file with other version control. When you use version control make sure to only commit composer.json & composer.lock file.

If any of your colleagues needs to use this vendor folder then let them run composer install or composer update as per needs

  1. Commit composer.lock file to production

Don’t neglect composer.lock while committing your code to production. Because in production you will be doing composer install, if it doesn’t find composer.lock then it will update the composer.json file.


Conclusion

Hope this was helpful for you.

WHATS NEXT?

You might be interest to learn more on composer please find my whole article on it

How To Install Packages Parallel For Faster Development In Composer

What Is Composer? How Does It Work? Useful Composer Commands And Usage

Happy Coding :)

#json #composer #stackcoder

Julie  Donnelly

Julie Donnelly

1602421800

Compose and Pipe in JavaScript

Compose and Pipe are among the most powerful concepts in functional programming in JavaScript. However, they can be difficult to understand. This article will help you to better grasp these functions.

Compose

In algebra, function compositionallows you to apply one function to the output of another function.

An example of function composition

Photo by the author.

In this example, the function g is applied to the result of applying the function f to x. As we can see, function composition works from right to left.

#javascript #compose #pipes #functional-programming

Madelyn  Frami

Madelyn Frami

1602326880

Composing Event Streams: SnakeGame$

Diving deeper into building user interfaces you may encounter a few challenges in how to manage asynchronicity between features and events, the y may occur in a given point in time.

So we will be recreating the snake game using event composition with Observables.

warning: this is not a introduction to Observables neither to event composition and it may have a lot of mistakes which you could/ may point them out and I will be more than happy to take into account as a feedback.

There are plenty async primitives that empower us to model our application from callbacks, promises, to all way around to Observables. I chose Observables because it makes the data flow natural and fluid. In my so adorable opinion, I think, they are the best and funniest async primitive to composing events on Javascript.

TLTR:stops this madness and show me the code!

For starters we will modelling user interaction as a stream. So every time the user press a key down the snake will reacts accordingly, in other words we could map keys over updates in the snake position. We will be listen for ‘ArrowUp’, ‘ArrowDown’, ‘ArrowLeft’, and ‘ArrowRight’ keys but you may use another set of keys combination.

#composing #observables #javascript #async #rxjs