docker实际操作指令记录------For Mac
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker实际操作指令记录------For Mac相关的知识,希望对你有一定的参考价值。
参考技术A a lightweight, stand-alone, executable package that includes everything needed to run a piece of software , including the code, a runtime, libraries, environnement variables, and config files.- a runtime instance of image
- what the image becomes in memory when actually executed
- runs completely isolated from the host environnement by default
- only access host files and ports
at the top level, define the interactions of all the services
defines how containers behave in production
at the bottom of the hierarchy of such an app executed images
Define a container with a Dockerfile :
Dockerfile app.py requirements.txt
* the build command
* this creates a Docker image called "friendlyhello" from the current directory
* tag this created image by "-t"
Where's your built images? in your machine's local Docker image registry
- run the app, map your machine's port 4000 to the container's exposed port 80 using "-p" :
- http://0.0.0.0:80 : coming from inside the container
- http:// localhost:4000 : the correct URL
- run in detached mode :
- see the abbreviated container ID with
- to end the process, using the CONTAINER ID :
- Object :
* upload our build
* run it somewhere else
* learn how to push to registries to make deployment of containers actually happen
- Registry :
* a collection of repositories
- Repository :
* a collection of images
* like a Github repository
* except the code is already built
- An account on a repository : cloud.docker.com
- STEPS :
- learn how to scale your application by running this container in a service & enable load-balancing
- Prerequisites :
* docker run -p 80:80 username/repo:tag
* ensure your image is working by running this and visiting http://localhost
In a distributed application, different pieces of app are called "Services"
For example, a video sharing site:
* a service for storing application data in db
* a service for video transcoding in the background
* a service for the front-end
* only runs an image
* codifies the way that images run :
-> what ports it should use
-> how many replicas of the container should run so the service has the capability it needs
* Scaling a service changes the number of containers instances running that piece of software
a YAML file that defines how docker containers should behave in production
* replicas: 5
Run 5 instances of the images we uploaded as a service called web
* condition: on-failure
immediately restart containers if one fails
* "80:80"
the 1st 80 means the port on the host
* the 1st "networks: - webnet "
instruct web's containers to share port 80 via a load-balanced network called webnet
* the 2nd "networks: -webnet"
define the webnet network with the default settings (which is a load-balancing overlay network )
STEP 01
STEP 02
(give your app a name getstarted)
STEP 03
(see a list of five containers you just launched)
STEP 04
scale the app by changing the replicas values in docker-compose.yml, saving the change, and re-running the "docker stack deploy" command :
* In part Services :
- take an app you wrote
* In part Containers
- define how it should run in production by turning it to a service
- scaling it up 5 x in the process
* In part Swarm :
- deploy this application onto a cluster
- running it on multiple machines
* Multi-container, multi-machine applications are made possible by joining multiple machines into a "dockerized" cluster called a
a group of machines that are running Docker and have been joined into a cluster
* run the Docker commands, these commands run on a cluster by a
use strategy to run containers:
- "emptiest mode" : which fills the least utilised machines with containers
- "global" : which ensures that each machine gets exactly one instance of the specified container
instruct swarm manager to use these strategies in the compose file
* the only machine in a swarm to:
- execute your commands
- authorise other machines to join the swarm as workers
- just provide capacity
- do not have the authority to tell any other machine what it can or cannot do
- Docker work mode:
* a single-host mode on your local machine
* swarm mode
- swarm mode:
* Enabling swarm mode instantly makes the current machine a swarm manager
* Docker executes commands on the swarm, rather than on the current machine.
- A swarm is made up of multiple nodes , which can be either physical or virtual machines.
- basic concepts:
to enable swarm node and make your current machine a swarm manager
(on other machines)
to have them join the swarm as a worker
- create a couple of Vos using the Virtualbox driver :
(myvm1 - manager; myvm2 - worker)
- send commands to your VMs using
("docker swarm init" - instruct myvm1 to become a swarm manager)
- to have myvm2 join your new swarm as a worker:
- deploy your app on your new swarm
- only swarm manager like myvm1 can execute commands
- workers are just for capacity
- copy the docker-compose file to the swarm manager myvm1 home directory (alias: ~)
- Now that myvm1 use its powers as a swarm manager to deploy your app
the app is deployed on a cluster
- you'll see the containers have been distributed between both myvm1 and myvm2
- To get your VMs' IP addresses
- visit either of them on a browser
Swarm Chapitre:
- learn how to set up a swarm
* a swarm: a cluster of machines running Docker
- and deploy an app on a swarm, with containers running in concert on multiple machines
Stack Chapitre:
- A stack:
* the top of the hierarchy of distributed app
* a group of integrated services that share dependencies, and can be orchestrated and scaled together
- A single stack is capable of defining and coordination the functionality of an entire application
- In part Swarm : a single stack of a single service runs on a single host
- In part Stack : a stack of multiple services related to each other and run them on multiple machines
1. /images/php7_fpm_base: php7_fpm_base
2. /images/app: app
3. /images/nginx: webnginx
4. /images/mysql: mysql
1. Docker Compose
* Docker compose runs on localhost or virtual machine
* Prerequistes:
- already install Docker Engine or Docker Compose
- Dockerfile: to build an image
- create a Dockerfile in the directory of the image
- Dockerfile: to tell all the dependencies that this image needs
requirements.txt
docker-compose.yml
- build: .
use an image that's build from the Dockerfile in the current directory
- ports: - "5000:5000"
the 1st port 5000 on the host machine
the 2nd port exposed port 5000 on the container
- . : /code
Mounts the project directory on the host to /code inside the container, allowing you to modify the code without having to rebuild the image
- Linux host machine: http://loaclhost:5000
Mac docker machine: http://MACHINE_VM_IP:5000
- list local images:
- inspect local images:
- Because the application code is 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.
- STEPs:
run your services in the background in the "detached" mode
to see what is currently running
allows to run one-off commands for your service
to stop your services once you've finished with them
to bring everything down, removing the containers entirely, with the down command
--volumes
remove the data volumes used by the Redis container
2. Stack file for Docker Cloud
* A stack: a collection of services that make up an application
* A stack file:
- a file in YAML format that defines one or more services
- similar to docker-compose.yml file for Docker Compose
- but a few extensions
- default name:
docker-cloud.yml
* Manage service stacks:
- Stacks:
a convient way to automatically deploy multiple services that are linked to each other, without needing to define each one separately
- Stack files:
define :
* environnement variables
* deployment tags
* the number of containers
* related environnement-specific config
lb/web/redis :
Each key define in docker-cloud.yml creates a service with that name in Docker Cloud.
* Create a Stack:
- from the web interface
- using CLI:
* Update an existing stack:
- specify an existing stack when you create a service
- later want to add a service to an existing stack
- from the Docker Cloud web interface
- using CLI:
.../test/ykw_BonjourAUSY
# execute an interactive bash shell on the container
to see the running local services
to see the local containers
Docker之基础指令
简单介绍
之前已经开启了一个容器,并且也进行了守护进程的验证,接下来介绍以下几个基础的指令运行。
实际操作
1、sudo docker run -i -t 具有 /bin/bash
(1)docker run:创建一个新的容器并运行一个命令
(2)-i:标志保证容器中STDIN是开启的
(3)-t:标志为要创建的容器分配一个伪tty终端
如同:
在此界面下可以输入一些指令,如同:
当然也可以在这个容器内进行软件的安装:apt-get update && apt-get install vim,用来安装一个vim。
2、开启一个容器时,如果没有给容器命名,则会自动随机给容器命名,但是可以进行自定义命名:--name
sudo docker run --name *** -i -t *** ***
3、如果之前开启过一个容器,而现在又想重新开启就可以使用start或者restart指令:
sudo docker start ***
sudo docker restart ***
这里星号表示你容器的名称或者ID号,可以使用sudo docker ps -a查看。
4、从这里看到容器启动后只返回容器的名称而以,并没有预料中运行结果,这里需要使用到另外一个指令:attach
sudo docker attach ***
5、查看日志
(1)静态日志
sudo docker logs ***
这个用来查看最新的日志,如同:
(2)动态日志
sudo docker logs -f ***
这个用来查看动态日志,形式如同静态日志,只是会实时监控。
(3)加上时间的日志
sudo docker logs -ft ***
在动态日志的基础上进行加入时间显示,如同:
6、查看守护进程的进程
(1)top
sudo docker top ***
如同:
(2)stats
sudo docker stats ***
此指令可以用来查看多个或者一个容器的进程,这里只启动一个容器:
7、自动重启
sudo docker run --restart=always ***
在容器出现错误而导致停止时,就会自动重启了。
8、(1)查看容器信息
sudo docker ps -a
如同:
(2)获取容器信息
sudo docker inspect ***
如同:
相比ps来看,内容详细很多。
(3)指定获取信息
sudo docker inspect --format ‘{{ .NetworkSettings.IPAddress }}‘ ***
如同:
以上就是基础指令的使用。
参考:
《第一本Docker书》
以上是关于docker实际操作指令记录------For Mac的主要内容,如果未能解决你的问题,请参考以下文章
记录一次Docker For Windows10镜像加速器配置
docker 容器commit指令和export指令有什么区别?(commit保存镜像提交历史记录,export不保存)