Docker的常用命令


Docker的常用命令

docker version	#顯示docker的版本信息
docker info	#顯示docker的系統信息,包括鏡像和容器的數量
docker --help	#docker幫助命令

鏡像命令

docker images	查看所有本地主機上的鏡像
docker search mysql	搜索鏡像
docker pull mysql	下載鏡像
docker pull mysql:5.7	指定版本下載
docker rmi -f ID/name	刪除鏡像
docker rmi -f 容器id	刪除指定的鏡像
docker rmi -f 鏡像id 鏡像id 鏡像id 鏡像id	刪除多個鏡像
docker rmi -f $(docker images -aq)	#刪除全部鏡像

容器命令

​ 說明:有了鏡像才可以創建容器,liunx,下載一個centos來測試學習

docker pull centos

新建容器並啟動

docker run [可選參數] image

#參數說明
--name="Name"	容器名字	tomcat01 tomcat02 tomcat03,用來區分容器
-d			    后台方式運行
-it				使用交互方式運行,進入容器查看內容
-p				制定容器的端口 -p	8080:8080
	-p	ip:主機端口:容器端口
	-p	主機端口:容器端口
	-p 容器端口
	容器端口
	
-p	隨機指定端口

#測試,啟動並進入容器
docker run -it centos /bin/bash

列出所有運行的容器

docker ps 命令
	-a	#列出當前正在運行的容器+帶出歷史運行中的容器
	-n=?	#顯示最近創建的容器
	-q	#只顯示容器的編號

刪除容器

docker rm 容器id	#刪除指定的容器,不能刪除在運行的容器,如果要強制刪除 rm -f
docker rm -f $(docker ps -aq)	#刪除所有的容器
docker ps -a -q|xargs docker rm 	#刪除所有的容器

退出容器

exit	#直接容器停止並退出
ctrl + P + Q	#容器不停止退出

啟動和停止容器的操作

docker start 容器id	#啟動容器
docker restart 容器id	#重啟容器
docker stop 容器id	#停止當前正在運行的容器
docker kill 容器id	#停止當前容器

常用其他命令

后台啟動容器

命令 docker run -d 鏡像名
docker run -d centos
#問題docker ps,發現centos停止了

#常見的坑,docker容器使用后台運行,就必須要有一個前台進程,docker發現沒有應用,就會自動停止
#nginx,容器啟動后,發現自己沒有提供服務,就會立即停止,就沒有程序了。

查看日志

docker logs -f -t --tail 容器

#顯示日志
-tf	#顯示日志
-tail number	#顯示日志條數

docker logs -tf --tail 10 容器id

查看容器中的進程信息ps

docker top 容器id

查看鏡像的元數據

#命令
docker inspect 容器id

進入當前正在運行的容器

#我們通常都是使用后台的方式運行的,需要進入容器,修改一些配置
#命令
#方法一:
	docker exec -it 容器id bashshell
	eg:docker exec -it 容器id /bin/bash
	ls
	ps -ef
#方法二:
	docker attach 容器id
	

docker exec	#進入容器后開啟一個新的終端,可以在里面操作(常用)
docker attach	#進入容器正在執行的終端,不會啟動新的進程

從容器內拷貝文件到主機上

docker cp 容器id:容器內路徑	目的主機路徑
	docker cp 容器id:/home/test.java /home

#拷貝是一個手動過程,未來我們使用 -v卷的技術,可以實現

作業練習

docker安裝nginx

	#1.搜索鏡像search 建議大家去docker搜索,可以看到幫助文檔
	#2.下載鏡像 pull
	#3.運行測試
docker images

	-d 后台運行
	--name 給容器命名
	-p 宿主機端口:容器內部端口
docker run -d --name  nginx01 -p 3344:80 nginx

docker ps

curl localhost:3344
#進入容器
docker exec -it nginx01 /bin/bash
whereis nginx
cd /etc/nginx	ls

docker安裝tomcat
docker run -it -rm tomcat:9.0
#我們之前的啟動是后台,停止了容器之后,容器還是可以查到
docker run -it --rm,一般用來測試,用完之后就刪除

#下載在啟動
docker pull tomcat:9.0

#啟動運行
docker run -d -p 3355:8080 --name tomcat01 tomcat

#測試訪問沒有問題

#進入容器

docker exec -it tomcat01 /bin/bash

#發現問題
	1、linux命令少了
	2、沒有webapps	阿里雲鏡像的原因,默認是最小鏡像,所有不必要的都剔除掉
#保證最小的可運行環境

思考:如果我們每次都要部署項目,每次進入容器是不是十分麻煩?我要是可以在容器外部提供一個映射路徑,webapps,我們在外部部署項目,自動同步就好了!

部署es+kibana

#es暴露的端口很多
#es十分的消耗內存
#es的數據一般需要放置到安全目錄,需要掛載
$ docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2

#增加內存的限制,修改配置文件 -e 環境配置修改
$ docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="Xms64m -Xmx512m" elasticsearch:7.6.2

可視化

portainer

Rancher(CI/CD再用)

什么是portainer?

Docker圖形化界面管理工具,提供一個后台面板供我們操作

docker run -d -p 8088:9000 \
--restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer

訪問測試:http://ip:8088/,通過他來訪問

commit鏡像

docker commit 提交容器成為一個新的副本

#命令和git類似
docker commit -m "提交的描述信息" -a="作者" 容器id 目標鏡像名:[tag]

實戰測試

#啟動一個默認的tomcat
#發現這個默認的webapps是沒有webapps應用的	鏡像的原因,官方的鏡像默認webapps是沒有文件的
#自己拷貝進去了基本的文件
#將我們操作后的容器提交為一個鏡像!我們以后就使用我們修改過后的鏡像即可,這就是我們自己修改的一個鏡像

學習方式說明:理解概念,但是一定要實踐,最后理論和實踐結合一次性搞定這個知識。

如果你想要保存當前容器的狀態,就可以通過commit提交,獲得一個鏡像
就好比我們學習VM時候的快照
到這里才算是入門Docker!

容器數據卷

什么是容器數據卷?

​ docker理念回顧

​ 將應用和環境打包成一個鏡像

​ 數據?如果數據都在容器中,那么我們刪除容器,數據就會丟失! 需求:數據可以持久化

​ MySQL,容器刪了,刪庫跑路 需求:MySQL數據可以存儲在本地!

​ 容器之間可以有一個數據共享的技術!Docker容器中產生的數據,同步到本地!

​ 這就是卷技術!目錄的掛載,將我們容器里面的目錄,掛載到Linux上面

總結一句話:容器的持久化和同步操作!容器間也是可以數據共享的

使用數據卷

方式一:直接用命令來掛載 -v
docker run -it -v 主機目錄:容器內端目錄
#測試
docker run -it -v /home/ceshi:/home centos /bin/bash
#啟動起來之后我們可以通過docker inspect 容器id

​ 1、停止容器

​ 2、宿主機上修改文件

​ 3、啟動容器

​ 4、容器內的數據依舊是同步的

好處:我們以后修改只需要在本地修改,容器內會自動同步!

實戰:安裝MySQL

​ 思考:MySQL的數據持久化問題

#獲取鏡像
docker pull mysql

#運行容器,需要做數據掛載:	#安裝啟動mysql,需要配置密碼,這是要注意的!
官方測試:docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

#啟動我們的
-d	后台運行
-p	端口映射
-v	卷掛載
-e	環境配置
--name	容器名字
docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 -d mysql:5.7

假設我們將容器刪除
docker rm -f mysql01

發現我們掛載到本地的數據依舊沒有丟失,這就實現了容器持久化的功能

具名和匿名掛載

#匿名掛載
-v	容器內路徑
docker run -d -P --name nginx01 -v /etc/nginx nginx

#查看所有的volume情況
docker volume ls

#這里發現,這種就是匿名掛載,我們在-v只寫了容器內的路徑,沒有寫容器外的路徑

#具名掛載
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx

docker volume ls

#通過	-v	卷名:容器內路徑
#查看一下這個卷
docker volume inspect juming-nginx

所有的docker容器內的卷,沒有指定目錄的情況下都在/var/lib/docker/volumes/xxxx/_data

我們通過具名掛載可以方便的找到我們的一個卷,大多數情況使用具名掛載

如何確定是具名掛載路徑,還是指定路徑掛載!
-v	容器內路徑	#匿名掛載
-v	卷名:容器內路徑	#具名掛載
-v	/宿主機路徑::容器內路徑	#指定路徑掛載

拓展:

#通過	-v	容器內路徑:ro	rw改變讀寫權限
ro	readonly	#只讀
rw	readwrite	#讀寫

#一旦設置了容器權限,容器對我們掛在出來的內容就有限定了!
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:ro nginx
docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx:rw nginx

ro	只要看到ro就說明這個路徑只能通過宿主機來操作,容器內不無法操作

初識Dockerfile

​ 就是用來構建docker鏡像的構建文件!命令腳本

​ 通過這個腳本可以生成鏡像,鏡像是一層一層的,腳本一個個的命令,每個命令都是一層!

#創建一個dockerfile文件,名字可以隨機	建議Dockerfile
#文件中的內容	指令大寫	參數
FROM centos
VOLUME ["volume01","volume02"]
CMD echo "-----end-----"
CMD /bin/bash

#這里的每個命令,就是鏡像的一層

這個卷和外部一定有一個同步的目錄!

查看一下卷掛載的路徑

測試一下剛才的文件是否同步出去了!

這種方式我們以后用的會十分的多,因為通常我們自己構建鏡像

假設構建鏡像的時候沒有掛載卷,要手動鏡像掛載 -v 卷名 容器內路徑!

數據卷容器

多個mysql同步數據!

docker run -it --name docker01 kuangshen/centos:1.0
ls
ls -l
docker run -it --name docker02 --volumes-from docker01 kuangshen/centos:1.0
ls -l

結論:

​ 容器之間配置信息的傳遞,數據卷容器的生命周期一直持續到沒有容器使用為止。

​ 但是一旦你持久化到了本地,這個時候,本地的數據是不會刪除的!

DockerFile

構建步驟:

​ 1、編寫一個dockerfile文件

​ 2、docker build 構建成為一個鏡像

​ 3、docker run 運行鏡像

​ 4、docker push 發布鏡像(DockerHub、阿里雲鏡像倉庫)

DockerFile構建過程

基礎知識:

1、每個保留關鍵字(指令)都必須是大寫字母

2、執行從上到下順序執行

3、#表示注釋

4、每一個指令都會創建提交一個新的鏡像層,並提交!

dockerfile是面向開發的,我們以后要發布項目,做鏡像,就要編寫dockerfile文件,這個文件十分簡單!

Docker鏡像逐漸成為企業交付的標准,必須要掌握!

步驟:開發、部署、運維。。。

DockerFile:構建文件,定義了一切的步驟,源代碼

Dockerimages:通過DockerFile構建生成的鏡像,最終發布和運行的產品,原來是jar war

Docker容器:容器就是鏡像運行起來提供服務的

DockerFile的命令

FROM	#基礎鏡像,一切從這里開始構建
MAINTAINER	#鏡像是誰寫的,姓名+郵箱
RUN	#鏡像構建時候需要運行的命令
ADD	#步驟,tomcat鏡像,這個tomcat壓縮包!添加內容
WORKDIR		#鏡像的工作目錄
VOLUME		#掛載的目錄
EXPOSE		#保留端口配置
CMD		    #指定這個容器啟動時候運行的命令,只有最后一個會生效,可被代替
ENTRYPOINT	#指定這個容器啟動時候運行的命令,可以追加命令
ONBUILD		#當構建一個被繼承的Dockerfile 這個時候就會運行ONBUILD的指令,觸發指令
COPY	    #將文件拷貝到鏡像中
ENV		    #構建的時候設置環境變量
#通過這個文件構建鏡像
docker build -f dockerfile文件路徑 -t 鏡像名:[tag]

可以列出本地進行的變更歷史

docker history 容器id

我們平時拿到一個鏡像,可以研究一下他是怎么做的了

發布自己的鏡像

DockerHub

1、地址https://hub.docker.com/注冊自己的賬號

2、確定這個賬號可以登錄

3、在我們自己的服務器上提交鏡像

docker login --help
docker login -u xiaowang

4、登陸成功之后就可以提交鏡像了

docker images
docker push xiaowang/divtomcat:1.0

阿里雲鏡像服務

1、登錄阿里雲

2、找到容器鏡像服務

3、創建命名空間

4、創建容器鏡像

5、瀏覽阿里雲

1. 登錄阿里雲Docker Registry
$ sudo docker login --username=郭靖1767164 registry.cn-hangzhou.aliyuncs.com

用於登錄的用戶名為阿里雲賬號全名,密碼為開通服務時設置的密碼。

您可以在訪問憑證頁面修改憑證密碼。

2. 從Registry中拉取鏡像
$ sudo docker pull registry.cn-hangzhou.aliyuncs.com/guojingtest/guojingtest:[鏡像版本號]
3. 將鏡像推送到Registry
$ sudo docker login --username=郭靖1767164 registry.cn-hangzhou.aliyuncs.com$ sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/guojingtest/guojingtest:[鏡像版本號]$ sudo docker push registry.cn-hangzhou.aliyuncs.com/guojingtest/guojingtest:[鏡像版本號]

請根據實際鏡像信息替換示例中的[ImageId]和[鏡像版本號]參數。

4. 選擇合適的鏡像倉庫地址

從ECS推送鏡像時,可以選擇使用鏡像倉庫內網地址。推送速度將得到提升並且將不會損耗您的公網流量。

如果您使用的機器位於VPC網絡,請使用 registry-vpc.cn-hangzhou.aliyuncs.com 作為Registry的域名登錄,並作為鏡像命名空間前綴。

5. 示例

使用"docker tag"命令重命名鏡像,並將它通過專有網絡地址推送至Registry。

$ sudo docker imagesREPOSITORY                                                         TAG                 IMAGE ID            CREATED             VIRTUAL SIZEregistry.aliyuncs.com/acs/agent                                    0.7-dfb6816         37bb9c63c8b2        7 days ago          37.89 MB$ sudo docker tag 37bb9c63c8b2 registry-vpc.cn-hangzhou.aliyuncs.com/acs/agent:0.7-dfb6816

使用 "docker push" 命令將該鏡像推送至遠程。

$ sudo docker push registry-vpc.cn-hangzhou.aliyuncs.com/acs/agent:0.7-dfb6816

Docker網絡

理解Docker

自定義網絡

網絡連通

實戰:部署redis集群

SpringBoot微服務打包Docker鏡像

1、構建springboot項目

2、打包應用

3、編寫dockerfile

4、構建鏡像

5、發布運行

​ 以后我們使用了docker之后,給別人交付一個鏡像即可!

Docker Compose

簡介

docker
dockerfile build run 手動操作,單個容器!
微服務,100個服務器!依賴關系

docker compose管理容器!定義多個容器!

作用:批量容器編排

自己理解

Compose是Docker官方的開源項目,需要安裝!

DockerFile可以讓程序在任何地方應用

version: '2.0'
services:
  web:
    build: .
    ports:
    - "5000:5000"
    volumes:
    - .:/code
    - logvolume01:/var/log
    links:
    - redis
  redis:
    image: redis
volumes:
  logvolume01: {}
For

Compose:重要概念

​ 服務services, 容器,應用(web, redis, mysql...)

​ 項目project,一組關聯的容器,博客,web,mysql,wp

安裝

1、下載:

sudo curl -L "https://github.com/docker/compose/releases/download/1.27.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

2、授權

sudo chmod +x /usr/local/bin/docker-compose

3、體驗

python應用、計算器、redis!

1、應用app.py
2、DockerFile應用打包為鏡像
3、Docker-compose yaml文件,完整的上線服務!
4、啟動項目	docker-compose up

流程:

1、創建網絡
2、執行docker-compose yaml
3、啟動服務

默認的服務名 文件名_服務名__num

開源項目

博客

​ 下載程序、安裝數據庫、配置...

​ compose應用 =>一鍵啟動

​ 1、下載項目(docker-compose.yaml)

​ 2、如果需要文件 Dockerfile

​ 3、直接一鍵啟動項目

前台啟動

	docker -d
​	docker-compose up -d

直接一鍵啟動

掌握:docker基礎、原理、網絡、服務、集群、錯誤排查、日志
Step 1: Setup

Define the application dependencies.

  1. Create a directory for the project:

    $ mkdir composetest
    $ cd composetest
    
  2. Create a file called app.py in your project directory and paste this in:

    import time
    
    import redis
    from flask import Flask
    
    app = Flask(__name__)
    cache = redis.Redis(host='redis', port=6379)
    
    
    def get_hit_count():
        retries = 5
        while True:
            try:
                return cache.incr('hits')
            except redis.exceptions.ConnectionError as exc:
                if retries == 0:
                    raise exc
                retries -= 1
                time.sleep(0.5)
    
    
    @app.route('/')
    def hello():
        count = get_hit_count()
        return 'Hello World! I have been seen {} times.\n'.format(count)
    

    In this example, redis is the hostname of the redis container on the application’s network. We use the default port for Redis, 6379.

    Handling transient errors

    Note the way the get_hit_count function is written. This basic retry loop lets us attempt our request multiple times if the redis service is not available. This is useful at startup while the application comes online, but also makes our application more resilient if the Redis service needs to be restarted anytime during the app’s lifetime. In a cluster, this also helps handling momentary connection drops between nodes.

  3. Create another file called requirements.txt in your project directory and paste this in:

    flask
    redis
    
Step 2: Create a Dockerfile

In this step, you write a Dockerfile that builds a Docker image. The image contains all the dependencies the Python application requires, including Python itself.

In your project directory, create a file named Dockerfile and paste the following:

FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP app.py
ENV FLASK_RUN_HOST 0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]

This tells Docker to:

  • Build an image starting with the Python 3.7 image.
  • Set the working directory to /code.
  • Set environment variables used by the flask command.
  • Install gcc and other dependencies
  • Copy requirements.txt and install the Python dependencies.
  • Add metadata to the image to describe that the container is listening on port 5000
  • Copy the current directory . in the project to the workdir . in the image.
  • Set the default command for the container to flask run.

For more information on how to write Dockerfiles, see the Docker user guide and the Dockerfile reference.

Step 3: Define services in a Compose file

Create a file called docker-compose.yml in your project directory and paste the following:

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
  redis:
    image: "redis:alpine"

This Compose file defines two services: web and redis.

Web service

The web service uses an image that’s built from the Dockerfile in the current directory. It then binds the container and the host machine to the exposed port, 5000. This example service uses the default port for the Flask web server, 5000.

Redis service

The redis service uses a public Redis image pulled from the Docker Hub registry.

Step 4: Build and run your app with Compose
  1. From your project directory, start up your application by running docker-compose up.

    $ docker-compose up
    Creating network "composetest_default" with the default driver
    Creating composetest_web_1 ...
    Creating composetest_redis_1 ...
    Creating composetest_web_1
    Creating composetest_redis_1 ... done
    Attaching to composetest_web_1, composetest_redis_1
    web_1    |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
    redis_1  | 1:C 17 Aug 22:11:10.480 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    redis_1  | 1:C 17 Aug 22:11:10.480 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
    redis_1  | 1:C 17 Aug 22:11:10.480 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
    web_1    |  * Restarting with stat
    redis_1  | 1:M 17 Aug 22:11:10.483 * Running mode=standalone, port=6379.
    redis_1  | 1:M 17 Aug 22:11:10.483 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    web_1    |  * Debugger is active!
    redis_1  | 1:M 17 Aug 22:11:10.483 # Server initialized
    redis_1  | 1:M 17 Aug 22:11:10.483 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    web_1    |  * Debugger PIN: 330-787-903
    redis_1  | 1:M 17 Aug 22:11:10.483 * Ready to accept connections
    

    Compose pulls a Redis image, builds an image for your code, and starts the services you defined. In this case, the code is statically copied into the image at build time.

  2. Enter http://localhost:5000/ in a browser to see the application running.

    If you’re using Docker natively on Linux, Docker Desktop for Mac, or Docker Desktop for Windows, then the web app should now be listening on port 5000 on your Docker daemon host. Point your web browser to http://localhost:5000 to find the Hello World message. If this doesn’t resolve, you can also try http://127.0.0.1:5000.

    If you’re using Docker Machine on a Mac or Windows, use docker-machine ip MACHINE_VM to get the IP address of your Docker host. Then, open http://MACHINE_VM_IP:5000 in a browser.

    You should see a message in your browser saying:

    Hello World! I have been seen 1 times.
    

    hello world in browser

  3. Refresh the page.

    The number should increment.

    Hello World! I have been seen 2 times.
    

    hello world in browser

  4. Switch to another terminal window, and type docker image ls to list local images.

    Listing images at this point should return redis and web.

    $ docker image ls
    REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
    composetest_web         latest              e2c21aa48cc1        4 minutes ago       93.8MB
    python                  3.4-alpine          84e6077c7ab6        7 days ago          82.5MB
    redis                   alpine              9d8fa9aa0e5b        3 weeks ago         27.5MB
    

    You can inspect images with docker inspect <tag or id>.

  5. Stop the application, either by running docker-compose down from within your project directory in the second terminal, or by hitting CTRL+C in the original terminal where you started the app.

Step 5: Edit the Compose file to add a bind mount

Edit docker-compose.yml in your project directory to add a bind mount for the web service:

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/code
    environment:
      FLASK_ENV: development
  redis:
    image: "redis:alpine"

The new volumes key mounts the project directory (current directory) on the host to /code inside the container, allowing you to modify the code on the fly, without having to rebuild the image. The environment key sets the FLASK_ENV environment variable, which tells flask run to run in development mode and reload the code on change. This mode should only be used in development.

Step 6: Re-build and run the app with Compose

From your project directory, type docker-compose up to build the app with the updated Compose file, and run it.

$ docker-compose up
Creating network "composetest_default" with the default driver
Creating composetest_web_1 ...
Creating composetest_redis_1 ...
Creating composetest_web_1
Creating composetest_redis_1 ... done
Attaching to composetest_web_1, composetest_redis_1
web_1    |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
...

Check the Hello World message in a web browser again, and refresh to see the count increment.

Shared folders, volumes, and bind mounts

  • If your project is outside of the Users directory (cd ~), then you need to share the drive or location of the Dockerfile and volume you are using. If you get runtime errors indicating an application file is not found, a volume mount is denied, or a service cannot start, try enabling file or drive sharing. Volume mounting requires shared drives for projects that live outside of C:\Users (Windows) or /Users (Mac), and is required for any project on Docker Desktop for Windows that uses Linux containers. For more information, see File sharing on Docker for Mac, and the general examples on how to Manage data in containers.
  • If you are using Oracle VirtualBox on an older Windows OS, you might encounter an issue with shared folders as described in this VB trouble ticket. Newer Windows systems meet the requirements for Docker Desktop for Windows and do not need VirtualBox.
Step 7: Update the application

Because the application code is now mounted into the container using a volume, you can make changes to its code and see the changes instantly, without having to rebuild the image.

  1. Change the greeting in app.py and save it. For example, change the Hello World! message to Hello from Docker!:

    return 'Hello from Docker! I have been seen {} times.\n'.format(count)
    
  2. Refresh the app in your browser. The greeting should be updated, and the counter should still be incrementing.

    hello world in browser

Step 8: Experiment with some other commands

If you want to run your services in the background, you can pass the -d flag (for “detached” mode) to docker-compose up and use docker-compose ps to see what is currently running:

$ docker-compose up -d
Starting composetest_redis_1...
Starting composetest_web_1...

$ docker-compose ps
Name                 Command            State       Ports
-------------------------------------------------------------------
composetest_redis_1   /usr/local/bin/run         Up
composetest_web_1     /bin/sh -c python app.py   Up      5000->5000/tcp

The docker-compose run command allows you to run one-off commands for your services. For example, to see what environment variables are available to the web service:

$ docker-compose run web env

See docker-compose --help to see other available commands. You can also install command completion for the bash and zsh shell, which also shows you available commands.

If you started Compose with docker-compose up -d, stop your services once you’ve finished with them:

$ docker-compose stop

You can bring everything down, removing the containers entirely, with the down command. Pass --volumes to also remove the data volume used by the Redis container:

$ docker-compose down --volumes

At this point, you have seen the basics of how Compose works.

假設項目需要重新打包:

docker-compose up --build	#重新構建

總結

​ 工程、服務、容器

​ 項目compose:三層

		工程
​		服務 服務
​		容器 運行實例!	docker k8s 容器  pods	kubectl	apply-f "xxx.ymal"

Docker Swarm

集群方式的部署,4台阿里雲服務器,2.4G

docker swarm join加入一個節點

#獲取令牌
docker swarm join-token manager
docker swarm join-token worker

把后面的節點都搭建進去

1、生成主節點init

2、加入(管理者、worker)

3、目標:雙主雙從

raft算法:

十分簡單:集群,可用!三個主節點	>1台管理節點存活
raft協議:保證大多數節點存活,才可以使用,高可用!

體會:

彈性、負載、擴縮容、集群!

以后告別docker run!

docker-compose up! 啟動一個項目,單機!

集群:swarm docker service

k8s service pods

容器 服務 副本

redis服務 => 10個副本(同時開啟10個redis服務)

​ 創建服務、動態擴展服務、動態更新服務

灰度發布、金絲雀發布

1、docker run	容器啟動!不具有擴縮容器
2、docker service 服務!	具備擴縮容器,滾動更新!
docker service create -p 8888:80 --name my-nginx nginx

docker service ls

#動態擴縮容
docker service update --replicas 3 my-nginx

2、docker service scale my-nginx=2
#移除
docker swarm其實並不難
只要會搭建集群,啟動服務,動態管理容器就可以!

服務,集群中任意節點都可以訪問,服務可以有多個副本動態擴縮容,實現高可用!

彈性、擴縮容!

服務器的高可用,任何企業,雲!

k8s:雲原生應用!

Docker Stack

Docker Secret

Docker Config

​ 容器單獨沒有什么意義,有意義的是,容器編排!

K8s

概念總結

Node

swarm

Service:核心,用戶訪問!

Task

邏輯是不變的:

	命令	管理	api	調度	工作節點	task任務容器維護創建
​	kubtectl service api

企業實戰

Docker Compose

Docker Swarm

CI/CD jenkins


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM