Self-Paced Training (3) - Docker Operations
Agenda
Troubleshooting Containers
Overview of Security Practices
Private Registry
Intro to Docker Machine
Intro to Docker Swarm
Intro to Docker Compose
Building micro service applications with Docker
Container logging
View the output of the containers PID 1 process: docker logs <container name>
View and follow the output: docker logs -f <container name>
Limit the output: docker logs -f —tail 5 <container name>
Container application logs
Typically, apps have a well defined log location
Map a host folder to the application’s application log folder in the container
In this way, you can view the log generated in the container from your host folder
Run a container using nginx image and mount a volume to map the /nginxlogs folder in the host to the /var/log/nginx folder in the container: docker run -d -P -v /nginxlogs:/var/log/nginx nginx
Check container logs
Run a new container using the tomcat image: docker run -d -P tomcat
View the container log: docker logs <container id>
On your host machine, create a folder /container/logs/nginx
Run a new container using the NGINX image and mount the /container/logs/nginx folder into /var/log/nginx: docker run -d -P -v /container/logs/nginx:/var/log/nginx nginx
Look inside your /container/logs/nginx folder and notice the new log files from the container
Inspecting a container
docker inspect command displays all the details about a container
Outputs details in JSON array
Use grep to find a specific property
Display all details of the specified container: docker inspect <container name>
Display the IP address of the specified container: docker inspect <container name> | grep IPAddress
Format: docker inspect —format [{.NetworkSettings.IPAddress}] <container name>
Starting and Stoping Decker daemon
If you started Docker as a service, use service command to stop, start and restart the Docker daemon
sudo service docker stop
sudo service docker start
sudo service docker restart
If not running as a service, run Docker executable in daemon mode to start the daemon: sudo docker -d &
If not running as a service, send a SIGTERM to the Docker process to stop it
Run ‘pidof docker’ to find the Docker process PID
sudo kill $(pid of docker)
Docker daemon upstart configuration file
Located in /etc/default/docker
Use DOCKER_OPTS to control the startup options for the daemon when running as a service
Restart the service for changes to take effect: sudo service docker restart
Start daemon with log level of debug and allow connections to an insecure registry at the domain of my_server.org : DOCKER_OPTS=“—log-level debug —insecure-registry my_server.org:5000”
Docker daemon logging
Start the docker daemon with —log-level parameter and specify the logging level
Levels are (in order from most verbose to least):
Debug
Info
Warn
Error
Fatal
Run docker daemon with debug log level (log written on terminal): sudo docker -d —log-level=debug
Configuring in DOCKER_OPS (log output will be written to /var/log/upstart/docker.log): DOCKER_OPTS=“—log-level debug”
Linux containers and security
Docker helps make applications safer as it provides a reduced set of default privileges and capabilities
Namespaces provide an isolated view of the system. Each container has its own
IPC, network stack, root file system etc…
Processes running in one container cannot see and effect processes in another container
Control groups (Cgroups) isolate resource usage per container
Ensures that a compromised container won’t bring down the entire host by exhausting resources
Quick security considerations
Docker daemon needs to run as root
Only ensure that trusted users can control the Docker daemon
Watch who you add to docker group
If binding the daemon to a TCP socket, secure it with TLS
Use Linux hardening solution
Apparmor
SELinux
GRSEC
Private Registry
Allows you to run your own registry instead of using Docker Hub
Multiple options
Run registry server using container
Docker Hub Enterprise
Two versions:
Registry v1.0 for Docker 1.5 and below
Registry v2.0 for Docker 1.6
Setting up a private registry
Run the registry server inside a container
Use the registry image at https://registry.hub.docker.com/u/library/registry
Image contains a preconfigured version of registry v2.0
Run a new container using the registry image: docker run -d -p 5000:5000 registry:2.0
Push and pull from private registry
First tag the image with host IP or domain of the registry server, then run docker push
Tag image and specify the registry host: docker tag <image id> my_server.net:5000/my-app:1.0
Push image to registry: docker push my_server.net:5000/my-app:1.0
Pull image from registry: docker pull my_server.net:5000/my-app:1.0
List tags: curl -v -X GET http://localhost:5000/v2/mynginx/tags/list
Docker machine overview
Docker machine is a tool that automatically provisions Docker hosts and installs the Docker Engine on them
Create additional hosts on your own computer
Create hosts on cloud providers(e.g. Amazon AWS, DigitalOcean etc…)
Machine creates the server, installs Docker and configures the Docker client
Installing Machine
Download the binary for the operating system at https://github.com/docker/machine/releases/tag/v0.2.0
Place the binary into a folder of your choice
Add the folder to your system environment PATH
Creating a host
Use 'docker-machine create’ command and specify the driver to use
Use virtual box driver if creating hosts on a Windows or Mac
Need to have Virtual Box installed (https://www.virtualbox.org/)
Create a host named “testiest” on the current machine, using Virtual Box: docker-machine create —driver virtual box testhost
Provisioning hosts in the cloud
Each cloud provider has different options on the docker-machine create command
See https://docs.docker.com/machine/#drivers as reference
Example with DigitalOcean
docker-machine create —driver digitalocean —digitalocean-access-token <your access token> —digitalocean-size 2gb testhost
List machines: docker-machine ls
Docker machine SSH
Allows us to connect to a provisioned host using SSH
Logs in using the SSH key that is created when creating the machine
Connect to host3 using SSH: docker-machine ssh host3
What is Docker Swarm
Docker Swarm is a tool that clusters Docker hosts and schedules containers
Turns a pool of host machines into a single virtual host
Ships with simple scheduling backend
Supports many discovery backends
Hosted discovery
etcd
Consul
ZooKeeper
Static files
https://docs.docker.com/swarm/discovery
Setup process (using hosted discovery)
On the machine that you will use as the Swarm master, run a command to create the cluster
Start Swarm master
For each node with Docker installed, run a command to start the Swarm agent
Note: Agents can be started before or after the master
Installing and running Swarm
Most convenient option is to use the Swarm image on Docker Hub https://registry.hub.docker.com/u/library/swarm/
Swarm container is a convenient packaging mechanism for the Swarm binary
Swarm containers can be run from the image to do the following
Create a cluster
Start the Swarm manager
Join nodes to the cluster
List nodes on a cluster
Create the Swarm cluster
'swarm create’ command will output the cluster token
Token is an alphanumeric sequence of characters that identifies the cluster when using the hosted discovery protocol
Copy this number somewhere
Run a container using the swarm image. We run the create command of the Swarm application inside and get the output on our terminal. —rm means to remove the container once it has finished running.
docker run —rm swarm create
Start the Swarm manager
Run a container that run the ‘swarm manager’
Make sure to map the swarm port in the container to a port on the host: docker run -d -P swarm manage token://<cluster token>
Connect a node to the cluster
Run a container that funs the ‘swarm join’ command
Specify the IP address of the node and the port the Docker daemon is listening on
Note: Your Docker daemon on the machine must be configured to listen on a TCP port instead of just on the unix socket.
docker run -d swarm join —addr=<node ip>:<daemon port> token://<cluster token>
sudo service docker stop
sudo vim /etc/default/docker
sudo service docker start
DOCKER_HOST=localhost:2375
export DOCKER_HOST
Connect the Docker client to Swarm
Point your Docker client to the Swarm manager container
Two methods:
Configuring the DOCKER_HOST variable with the Swarm IP and port
Run docker with -H and specify the Swarm IP and port
Look at the container port mapping to find the Swarm port
Configure the DOCKER_HOST variable
export DOCKER_HOST=127.0.0.1:<swarm port>
Run docker client and specify the daemon to connect to
docker -H tcp://127.0.0.1:<swarm port>
Checking your connected nodes
Run ‘docker info’
Since client is connected to Swarm, it will show the nodes
Run a container in the cluster
Standard ‘docker run’ command
Swarm master decides which node to run the container on based on your scheduling strategy
https://docs.docker.com/swarm/scheduler/strategy
Running ‘docker ps’ will sow which node a container is on
What is Compose
Docker Compose is a tool for creating and managing multi container applications
Containers are all defined in a single file called ‘docker-compose.ml'
Each container runs a particular component / service of your application.
For example:
Web front end
User authentication
Payments
Database
Container links are defined
Compose will spin up all your containers in a single command
Configuring the Compose yml file
Defines the services that make up your application
Each service contains instructions for building and running a container
Example
javaclient:
build: . (building using Dockerfile in current directory)
command: java HelloWorld
links:
-redis
redis:
image: redis (Use the latest redis Image from Docker Hub)
Build and image instruction
‘build' defines the path to Dockerfile that will be used to build the image
Container will be run using the image build
‘image’ defines the image that will be used to run the container
All services must have either a build or image instruction
Running your application
Use ‘docker-compose up’
Up command will
Build the image for each service
Create and start the containers
Install docker-compose
https://docs.docker.com/compose/install/
curl -L https://github.com/docker/compose/releases/download/1.2.0/docker-compose- uname -s - uname -m > /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Self-Paced Training (3) - Docker Operations的更多相关文章
- Self-Paced Training (2) - Docker Fundamentals
Agenda- Building Images Dockerfile Managing Images and Containers Distributing Images on Docker Hub ...
- 在Docker中运行web应用
启动一个简单的web 应用 使用社区提供的模板,启动一个简单的web应用,熟悉下各种Docker命令的使用: # docker run -d -P training/webapp python app ...
- 在生产环境使用Docker部署应用
导读 Docker现在越来越流行,但是真正在生产环境部署Docker还是个比较新的概念,还没有一个标准的流程.作者是ROR的程序员,作者结合平时的部署经验,联系Docker的特点,向大家分享了其在生产 ...
- Docker快速入门
Docker已经火了很长一段时间,最近打算在阿里云上好好熟悉一下Docker的相关应用,为今后的工作做准备. 基本概念 Docker是基于Go语言实现的云开源项目,诞生于2013年初,最初发起者是do ...
- Docker系统七:Docker数据管理
Docker的数据管理 I. 基本概念 Docker容器一旦删除,其相关的rootf文件系统就会被删除,其容器内的数据将一并删除,为了保存相关数据,Docker提出了数据卷的概念. II. 数据卷 D ...
- Podman and Buildah for Docker users
转自:https://developers.redhat.com/blog/2019/02/21/podman-and-buildah-for-docker-users/ I was asked re ...
- Docker:Deploy your app
Prerequisites Install Docker. Get Docker Compose as described in Part 3 prerequisites. Get Docker Ma ...
- Docker技术入门与实战 第二版-学习笔记-7-数据管理(volume)
Docker 数据管理 为什么要进行数据管理呢?因为当我们在使用container时,可能会在里面创建一些数据或文件,但是当我们停掉或删除这个容器时,这些数据或文件也会同样被删除,这是我们并不想看见的 ...
- learning docker steps(8) ----- docker network 初次体验
参考: docker network 主要是介绍了docker 容器之间的组网模式, 一般来说实像组网主要是依赖于bridge iptalbes vlan来实现,但是附带的如端口转发会降低效率. 新型 ...
随机推荐
- 解决在ubuntu下requests 无法找到模块packages
我明明用pip install requests安装成功了,但是依然报下面的错 错误1 requests.packages.urllib3.disable_warnings()AttributeErr ...
- 1070: [SCOI2007]修车 - BZOJ
Description 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序,使 ...
- FACL的使用
ACL的使用 ACL即Access Control List 主要的目的是提供传统的owner,group,others的read,write,execute权限之外的具体权限设置,ACL可以针对单一 ...
- UVA 11149 Power of Matrix 快速幂
题目链接: http://acm.hust.edu.cn/vjudge/contest/122094#problem/G Power of Matrix Time Limit:3000MSMemory ...
- Codeforces Beta Round #10 D. LCIS
题目链接: http://www.codeforces.com/contest/10/problem/D D. LCIS time limit per test:1 secondmemory limi ...
- 使用highcharts 绘制Web图表
问题描述: 使用highcharts 绘制Web图表 Highcharts说明: 问题解决: (1)安装Highcharts 在这些图表中,数据源是一个典型的JavaScrip ...
- C# mongodb [上]
概述 MongoDB是一个高性能,开源,无模式的文档型数据库,使用C++开发.是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是 ...
- 【BZOJ】【2194】快速傅里叶之二
FFT c[k]=sigma a[i]*b[i-k] 这个形式不好搞…… 而我们熟悉的卷积的形式是这样的 c[k]=sigma a[i]*b[k-i]也就是[下标之和是定值] 所以我们将a数组反转一下 ...
- C#中两个日期类型相减得到天数
protected int GetDuration(DateTime start, DateTime finish) { return (finish - start).Days; } 直接相减得到的 ...
- NYOJ-44 子串和 AC 分类: NYOJ 2014-01-04 22:53 154人阅读 评论(0) 收藏
作为菜鸟一枚,对子串和的代码完全就是硬算 的..结果是TLE #include<stdio.h> int jh(int x,int y,int num[],int sum[]); int ...