A nice container docker

What is the docker (code, configuration environment, all packaged in an image file, the image is the operating system of compressed files)

Docker was originally a project dotCloud internal company founder Solomon Hykes during the French initiated 
  in order to Apache 2.0 2013 Nian 3 Yue licensing agreement revenue, major maintenance project code on GitHub. 
Docker use Google launched the Go language development achieved. 
A package is linux docker container, the container provides an easy to use interface. It is the most popular Linux container solutions. 
docker fairly simple interface, users can easily create, destroy containers. 
docker will depend on the program and application procedures, packaged in a file inside. Run this file will generate a virtual container. 
Program run in a virtual container, like a real run on the same physical machine, with docker, do not worry about environmental problems.

Why docker

# Because the time before server deployment applications beginning with a physical machine, then use the virtual machine, 
1 . Would be a waste of time
 2 . Cost is very high
 3 . Waste of resources
 4 . Difficult to migration and expansion, change the machine need to put all the dependencies are installed again
 5 . might not limited hardware vendors, such as Lenovo's written on, can not be used in Dell computers 
appear !!! docker to solve these problems, and also to solve the most vexing environmental configuration problems, he can all the needs of the environment, dependencies into a container, then pack to testers

The difference between physical machines, virtual machines, docker's

Physical machines, virtual machines, docker's relationship is like a physical machine is a residential building, the virtual machine is the building's residents each house, each room is equivalent docker inside the house.

docker VS traditional virtual machine

Three of the concept docker

# Three basic concept docker 
container container equivalent to the class instantiated objects Computer City CD 
mirror image equivalent to that class of machine engraved CD warehouse management repository mirroring Computer City
docker entire life cycle is these three concepts.

Mirroring (operating system of a compressed file)

Docker image is a read-only template. 
    For example: a mirror may comprise a complete CentOS operating system environment, which only Apache or other application required by the user is installed. 
Mirroring can be used to create Docker containers. 
Docker provides a very simple mechanism to create or update an existing mirrored image, the user can even download a ready made image directly from others to be used directly.

container

Relationship between the image and the container, as object-oriented programming classes and instances, the mirror is statically defined ( class ), the container is a runtime mirror entity (object). 
Container can be created, start, stop, delete, pause 
Docker use container to run applications. 
Container is run instances created from the mirror. It can be started, start, stop, delete. Each container is isolated from each other to ensure the safety of the platform. 
The container can be seen as a simple version of the Linux environment (including root privileges, process space, user space and cyberspace, etc.) and running within the application. 
!!! Note: image is read-only, write-container creates a layer of the top layer as at startup.

warehouse

Warehouse is a centralized place to store the image file. Sometimes the warehouse and warehouse registration server (Registry) confused, do not strictly separate. 
In fact, the registered warehouse server often contains several warehouses, each warehouse also contains a number of mirrors, each mirror has a different label (tag). 
Warehouse is divided into two forms of public warehouses (Public) and private warehouse (Private). 
The biggest public warehouse Docker Hub, store a huge number of images available for users to download. Domestic public warehouse includes Docker Pool etc., can provide mainland users a more stable fast read access. 
When a user creates his own image you can use the push command to upload it to a public or private warehouse, so use this image download time, on another machine 
  just need to pull from the warehouse down. 
!!! Note: Docker warehouse concept is similar with Git, GitHub registration server can be understood as such hosting services.

centos installation docker

The official installation
 1 uninstall the old version. 
Sudo yum the Remove Docker \ 
                  Docker - Client \ 
                  Docker -client- Latest \ 
                  Docker - the Common \ 
                  Docker - Latest \ 
                  Docker -latest- logrotate \ 
                  Docker - logrotate \ 
                  Docker - SELinux \ 
                  Docker -engine- SELinux \ 
                  Docker - Engine

 2 . repository provided 
the sudo yum the install -Y yum-utils \ 
  Device -mapper-persistent- the Data \ 
  lvm2 

sudo yum -config- Manager \
     --add- repo \ 
    HTTPS: //download.docker.com/linux/centos/docker- ce.repo

 3 . Installation docker Community Edition 
yum Docker install - ce
 4 . start close Docker 
systemctl start / Status Docker

Configuring docker container accelerator, accelerated image file downloads

-ssl https://get.daocloud.io/daotools/set_mirror.sh curl | SH -s HTTP: // 95,822,026 .m.daocloud.io 
!!! Note: Be sure to modify the configuration files to take effect docker, vim   / etc / Docker / daemon.json 
{ " Registry-Mirrors " : [ " http://95822026.m.daocloud.io " ]}   # remove trailing comma

 docker command

docker start and stop container

  # Start and stop container docker 
  docker start container the above mentioned id 
  Docket STOP container the above mentioned id 

  # batch stop docker container 
  docker stop `docker ps -aq`

Verify that the current system is that: cat / etc / os-release

docker STOP mirror id Close mirror 
sudo systemctl stop docker close the entire docker program

for docker container mirror CRUD add
# obtain a hello-world image, default to docker the Hub search docker mirror
docker Search hello-world # search image
docker pull hello-world # download the image, as I buy a CD

#-based mirroring run a container instance
docker-world run the Hello # docker top three can also run image file ID, the same can be performed

# interactive run a ubuntu system is that we can input command
docker run -it ubuntu / bin / bash
of parameters:
  -i run interactively
  -t open a terminal
  / bin / bash linux shell interpreter of

the commit record to generate a new image file
docker commit mirror image name id

# running a living containers, there is a background process container

 # Have run a background process containers, living containers
  docker run -d centos / bin / sh -c "while true; do echo bought a Buddha cold; SLEEP 1; DONE"
  # of parameters
    docker run run image
    -d daemonize background running
    centos mirror name
    / bin / sh sh interpreter of the specified shell
    -c specified period of shell code
    "while true; do echo bought a Buddha cold; sleep 1; done" # dead cycle per second print buy a cold Buddha

  # Run a custom container name record
  docker run --name s18foleng -d centos / bin / sh -c "while true; do echo bought a Buddha cold; sleep 1; done"

  # Export docker mirror
  docker save 9ed836d5dd03> /opt/mydocker.tar.gz

  # Import mirror docker
  docker load </opt/mydocker.tar.gz

  #docker run if the run does not exist in mirror image, the default will go first docker pull

  Mapping the specified port # container, exposing the host 7000, specifies the mapping to the container 5000 , the container 5000 is, in the code control
  docker run -d -p 7000: 5000 training / webapp python app.py

  Parameters explanation:

    -d background container

    -p large P, the random port mapping, small p, designated port mapping

     training / webapp mirrorname

     Command python app.py container to be executed

 Port range 0 to 65535

Delete
# Delete container about
docker rm "container id" # delete records container, the container is followed by an id of the top three
docker re ~ docker ps -ap ~ # delete all records container # Backticks achieved are the result of command
docker rm ~ docker rmi -ap ~ # delete all mirrored, -f force the removal, try to use less
# remove the mirror about
docker rmi "mirror id" # delete images,

change

  # Into the container space is running
  docker exec -it container id / bin / bash

 
 

  # Change the name docker mirror

 
 

  docker tag name of the new mirror image id
  docker tag 9ed836d5dd03 yuchao163 / s18-aliyun -centos-vim

Charles
docker image ls # list docker image file
docker images # ditto,

after running the mirror # will produce a container record,
docker ps # to view the current running of the container logged, the container must have a meaning of existence, otherwise it will hang
docker ps -a # View all run-off container record
docker ps -ap # check out all the containers had run record of the above mentioned id
docker ImagesRF Royalty Free -ap # check out all the mirrors the above mentioned id

# View the container logs
docker logs container id #-time print log
docker container logs -f id # uninterrupted print log

Example: Build your own docker Mirror, mirror submitted to a local warehouse

1.运行一个centos基础镜像,发现没有vim,在容器内部安装vim,且配置阿里源

docker run -it centos /bin/bash

2.安装wget,下载阿里云的yum源

yum install -y wget
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

3.基于阿里云的yum源下载vim 
yum install -y yum

4.退出centos容器环境
exit

5.提交这个容器记录,生成一个新的镜像文件
docker commit 镜像id 镜像名

导出这个镜像,成为一个压缩文件,就可以给大家发送下去

 
 

#登录dockerhub个人主站,托管docker镜像的网址

 
 

https://hub.docker.com/


1.下载dockerhub上的镜像
docker pull superme/s18-hello-docker-world

 
 


2.在linux登录docker hub,然后推送镜像到公网

 
 

docker login

 
 

3.修改本地镜像的名字,为docker hub的仓库id
docker tag 镜像id dockerhubID/镜像名

 
 

4.推送镜像到docker hub

 
 

docker push 镜像id

dockerfile的学习

什么是dockerfile?

dockerfile其实就是之前我们都是手动的执行命令,而dockerfile是在这个文件中制定了相对应的代码,自动的执行命令。
其实就是一个存储代码的地方,告诉系统我们要怎么样执行这段代码?

dockerfile中的指令

#项目依赖于什么操作系统
FROM scratch #制作base image 基础镜像,尽量使用官方的image作为base image
FROM centos #使用base image
FROM ubuntu:14.04 #带有tag的base image

#label标签,谁写的,以及帮助信息
LABEL version=“1.0” #容器元信息,帮助信息,Metadata,类似于代码注释
LABEL maintainer=“[email protected]"

#run指令,是一个万能指令
#对于复杂的RUN命令,避免无用的分层,多条命令用反斜线换行,合成一条命令!
RUN yum update && yum install -y vim \
    Python-dev #反斜线换行
RUN /bin/bash -c "source $HOME/.bashrc;echo $HOME”

#workdir  相当于linux中的 cd,切换目录
WORKDIR  /etc
WORKDIR root

#add  把本地的代码添加到容器中,还可以用来解压缩文件

ADD shuai.txt  /opt   #把本地的shuai.txt添加到镜像中的/opt下
ADD shuai.tar.gz  /opt  #解压缩之后添加到镜像中的/opt下

#copy 将宿主机的文件,拷贝到容器中,不会解压缩文件
COPY hello test/  #等同于上述ADD效果

ADD与COPY
   - 优先使用COPY命令
    -ADD除了COPY功能还有解压功能
#ENV 相当于定义了一个变量,定义了之后,提高代码的可维护性
#比如,我写了很多个 d=1,有一天我要把这个d改为2的时候,我是不是要把每一个都给改了呀,使用了ENV就可以避免这个问题

ENV MYSQL_VERSION 1.6
RUN yum install -y mysql-server = "${MYSQL_VERSION}"

dockerfile实战,构建自己的flask镜像

准备工作:
  创建's18'目录,在目录中创建s18-flask.py 和Dockerfile文件(后者名称是固定不能改变的)

1.准备一个flask代码文件 cat s18-flask.py 2.编写dockerfile FROM centos                  #指定centos基础镜像 COPY CentOS-Base.repo /etc/yum.repos.d/ #拷贝宿主机的文件,到容器空间下 COPY epel.repo /etc/yum.repos.d/ #拷贝宿主机的文件,到容器空间下 RUN yum clean all #执行清空yum缓存的命令 RUN yum install python-setuptools -y #想安装python依赖工具 RUN easy_install flask #是想让docker自动的帮咱们安装python2的flask模块 COPY flask.py /opt/ #把本地的代码文件,拷贝到容器的/opt目录下 WORKDIR /opt #进入到/opt目录下 EXPOSE 8080 #暴露容器的8080端口,供给外部宿主机去访问 CMD ["python","flask.py"] #cmd代表你要执行的命令 3.构建build这个dockerfile 确保文件都准备好了,如下所示 [root@wangdachui s18dockerfile]# ls CentOS-Base.repo Dockerfile epel.repo s18-flask.py 4.构建dockerfile docker build . 5.通过自己构建的镜像,运行一个flask程序 docker run -d --name flask -p 5555:8080 s18-flask.py   参数:
    --name 表示这个程序执行的是什么
6.通过浏览器去访问linux宿主机的 5555端口,即可访问到flask程序

搭建docker私有仓库

1.下载docker私有镜像
docker pull registry

2.修改docker的配置文件 
#修改如下配置文件
vim /etc/docker/daemon.json


修改内容如下
{"registry-mirrors": ["http://95822026.m.daocloud.io"],
"insecure-registries":["192.168.226.128:5000"]
}

3.修改docker的service配置文件,让它加载/etc/docker/daemon.json
修改如下文件
vim /lib/systemd/system/docker.service

添加如下配置到 [service]代码块中
EnvironmentFile=-/etc/docker/daemon.json

4.重启docker的服务
systemctl daemon-reload
systemctl restart docker

5.重新启动一个私有镜像仓库的容器实例

docker run --privileged=true -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry     registry

--privileged=true  docker容器的安全机制:设置特权级运行的容器


6.推送本地镜像,到私有仓库中
docker push 192.168.226.128:5000/s18-hello-world

7.检查docker私有仓库的api地址,检查json数据
http://192.168.226.128:5000/v2/_catalog

8.尝试下载私有仓库的镜像
docker pull 192.168.226.128:5000/s18-hello-world

 

  

 

Guess you like

Origin www.cnblogs.com/zty1304368100/p/11219098.html