Getting started with docker

What is Docker?

Docker is a virtual environment container, you can package your development environment, code, configuration files, etc. into this container, and publish and apply it to any platform. For example, you use Python to develop the background of the website locally. After the development and testing are completed, you can package Python3 and its dependent packages, Flask and its various plug-ins, Mysql, Nginx, etc. into a container, and then deploy it to any location you want. environment of.

Docker official documents are relatively complete, it is recommended to read the official documents if you have the ability .



Three concepts of Docker

  1. Image: Similar to an image in a virtual machine, it is a read-only template for the Docker engine that contains a file system. Any application needs an environment to run, and images are used to provide this environment. For example, an Ubuntu image is a template containing the Ubuntu operating system environment. Similarly, if Apache software is installed on the image, it can be called an Apache image.
  2. Container: Similar to a lightweight sandbox, it can be regarded as a minimalist Linux system environment (including root privileges, process space, user space and network space, etc.), and the applications running in it . The Docker engine uses containers to run and isolate applications. Containers are application instances created by images. You can create, start, stop, and delete containers. Each container is isolated from each other and does not affect each other. Note: The image itself is read-only. When the container starts from the image, Docker creates a writable layer on top of the image, and the image itself remains unchanged.
  3. Repository: Similar to the code repository, here is the image repository, which is where Docker uses to centrally store image files. Note the difference with the registry server (Registry): the registry server is the place where the warehouse is stored, and there are usually multiple warehouses; and the warehouse is the place where the mirror is stored. Generally, each warehouse stores a type of mirror, and each mirror is distinguished by a tag. For example, the Ubuntu repository stores Ubuntu images of multiple versions (12.04, 14.04, etc.).


Docker installation and uninstallation

Docker can be installed on various platforms such as Windows, Linux, Mac, etc. For details, see the document Install Docker . After the installation is complete, you can view the version information of Docker:

[root@xxx ~]# docker version
Client:
 Version:      1.12.3
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   6b644ec
 Built:
 OS/Arch:      linux/amd64

Server:
 Version:      1.12.3
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   6b644ec
 Built:
 OS/Arch:      linux/amd64

See Docker's help information: # docker --help. The usage of various commands will not be repeated, and some explanations will be given later on which commands are used.



Basic operations on mirroring in Docker

After installing the Docker engine, you can perform basic operations on the image.

We start from the official registration server ( ) in the pull repository of the CentOS image. As mentioned earlier, each repository will have multiple images, which are marked with tags. If no tag is added, the latest image is used by default:

[root@xxx ~]# docker search centos    # 查看centos镜像是否存在
[root@xxx ~]# docker pull centos    # 利用pull命令获取镜像
Using default tag: latest
latest: Pulling from library/centos
08d48e6f1cff: Pull complete
Digest: sha256:b2f9d1c0ff5f87a4743104d099a3d561002ac500db1b9bfa02a783a46e0d366c
Status: Downloaded newer image for centos:latest

[root@xxx ~]# docker images    # 查看当前系统中的images信息
REPOSITORY      TAG            IMAGE ID       CREATED        SIZE
centos          latest         0584b3d2cf6d   9 days ago     196.5 MB

The above is to download an existing mirror, and there are two ways to help you create your own mirror.

(1) Use the image to start a container and modify it ==> use commit to submit the updated copy


[root@xxx ~]# docker run -it centos:latest /bin/bash    # 启动一个容器
[root@72f1a8a0e394 /]#    # 这里命令行形式变了,表示已经进入了一个新环境
[root@72f1a8a0e394 /]# git --version    # 此时的容器中没有git
bash: git: command not found
[root@72f1a8a0e394 /]# yum install git    # 利用yum安装git
......
[root@72f1a8a0e394 /]# git --version   # 此时的容器中已经装有git了
git version 1.8.3.1

At this point, use exit to exit the container, and then view the program (container) running in docker:

[root@xxx ~]# docker ps -a
CONTAINER ID  IMAGE    COMMAND      CREATED   STATUS   PORTS    NAMES
72f1a8a0e394  centos:latest "/bin/bash"  9 minutes ago   Exited (0) 3 minutes ago      angry_hodgkin

Here, the container is converted into an image, that is, the commit operation is performed. After completion, you can use docker images to view:

[root@xxx ~]# docker commit -m "centos with git" -a "qixianhu" 72f1a8a0e394 xianhu/centos:git

[root@xxx ~]# docker images
REPOSITORY       TAG    IMAGE ID         CREATED             SIZE
xianhu/centos    git    52166e4475ed     5 seconds ago       358.1 MB
centos           latest 0584b3d2cf6d     9 days ago          196.5 MB

Among them, -m specifies the description information; -a specifies the user information; 72f1a8a0e394 represents the id of the container; xianhu/centos:git specifies the user name, warehouse name and tag information of the target image. Note the username xianhu here, which will be used later.

At this point, our new image xianhu/centos:git is available in the Docker engine. The difference between this image and the original CentOS image is that there is an additional Git tool. At this point, the container we created using the new image has its own git.

[root@xxx ~]# docker run -it xianhu/centos:git /bin/bash
[root@520afc596c51 /]# git --version
git version 1.8.3.1

Use exit to exit the container. Note that there are two containers in the Docker engine at this time, which can be viewed with docker ps -a.

(2) Use Dockerfile to create an image

Dockerfile可以理解为一种配置文件,用来告诉docker build命令应该执行哪些操作。一个简易的Dockerfile文件如下所示,官方说明:Dockerfile reference

# 说明该镜像以哪个镜像为基础
FROM centos:latest

# 构建者的基本信息
MAINTAINER xianhu

# 在build这个镜像时执行的操作
RUN yum update
RUN yum install -y git

# 拷贝本地文件到镜像中
COPY ./* /usr/share/gitdir/

有了Dockerfile之后,就可以利用build命令构建镜像了:

[root@xxx ~]# docker build -t="xianhu/centos:gitdir" .

其中-t用来指定新镜像的用户信息、tag等。最后的点表示在当前目录寻找Dockerfile。

构建完成之后,同样可以使用docker images命令查看:

[root@xxx ~]# docker images
REPOSITORY        TAG       IMAGE ID      CREATED            SIZE
xianhu/centos     gitdir    0749ecbca587  34 minutes ago     359.7 MB
xianhu/centos     git       52166e4475ed  About an hour ago  358.1 MB
centos            latest    0584b3d2cf6d  9 days ago         196.5 MB

以上就是构建自己镜像的两种方法。其中也涉及到了容器的一些操作。如果想删除容器或者镜像,可以使用rm命令,注意:删除镜像前必须先删除以此镜像为基础的容器。

[root@xxx ~]# docker rm container_name/container_id
[root@xxx ~]# docker rmi image_name/image_id

镜像其他操作指令:

[root@xxx ~]# docker save -o centos.tar xianhu/centos:git    # 保存镜像, -o也可以是--output
[root@xxx ~]# docker load -i centos.tar    # 加载镜像, -i也可以是--input

Docker中关于容器的基本操作

在前边镜像的章节中,我们已经看到了如何基于镜像启动一个容器,即docker run操作。
[root@xxx ~]# docker run -it centos:latest /bin/bash

这里-it是两个参数:-i和-t。前者表示打开并保持stdout,后者表示分配一个终端(pseudo-tty)。此时如果使用exit退出,则容器的状态处于Exit,而不是后台运行。如果想让容器一直运行,而不是停止,可以使用快捷键 ctrl+p ctrl+q 退出,此时容器的状态为Up。

除了这两个参数之外,run命令还有很多其他参数。其中比较有用的是-d后台运行:


[root@xxx ~]# docker run centos:latest /bin/bash -c "while true; do echo hello; sleep 1; done"
[root@xxx ~]# docker run -d centos:latest /bin/bash -c "while true; do echo hello; sleep 1; done"

这里第二条命令使用了-d参数,使这个容器处于后台运行的状态,不会对当前终端产生任何输出,所有的stdout都输出到log,可以使用docker logs container_name/container_id查看。

启动、停止、重启容器命令:

[root@xxx ~]# docker start container_name/container_id
[root@xxx ~]# docker stop container_name/container_id
[root@xxx ~]# docker restart container_name/container_id

后台启动一个容器后,如果想进入到这个容器,可以使用attach命令:

[root@xxx ~]# docker attach container_name/container_id

删除容器的命令前边已经提到过了:

[root@xxx ~]# docker rm container_name/container_id

Docker中关于仓库的基本操作

Docker官方维护了一个DockerHub的公共仓库,里边包含有很多平时用的较多的镜像。除了从上边下载镜像之外,我们也可以将自己自定义的镜像发布(push)到DockerHub上。

在镜像操作章节中,我们新建了一个xianhu/centos:git镜像。

(1)访问hub.docker.com/,如果没有账号,需要先注册一个。

(2)利用命令docker login登录DockerHub,输入用户名、密码即可登录成功:

[root@xxx ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: xianhu
Password:
Login Succeeded

(3)将本地的镜像推送到DockerHub上,这里的xianhu要和登录时的username一致:

[root@xxx ~]# docker push xianhu/centos:git    # 成功推送
[root@xxx ~]# docker push xxx/centos:git    # 失败
The push refers to a repository [docker.io/xxx/centos]
unauthorized: authentication required

(4)以后别人就可以从你的仓库中下载合适的镜像了。

[root@xxx ~]# docker pull xianhu/centos:git

对应于镜像的两种创建方法,镜像的更新也有两种:

  • 创建容器之后做更改,之后commit生成镜像,然后push到仓库中。
  • 更新Dockerfile。在工作时一般建议这种方式,更简洁明了。

这里再一次回顾一下三个重要的概念:镜像、容器、仓库:
从仓库(一般为DockerHub)下载(pull)一个镜像,Docker执行run方法得到一个容器,用户在容器里执行各种操作。Docker执行commit方法将一个容器转化为镜像。Docker利用login、push等命令将本地镜像推送(push)到仓库。其他机器或服务器上就可以使用该镜像去生成容器,进而运行相应的应用程序了。

利用Docker创建一个用于Flask开发的Python环境

上边已经解释和练习了Docker的基本操作命令,下边以实例的形式完整走一遍流程。

我们创建一个用于Flask开发的Python环境,包含Git、Python3、Flask以及其他依赖包等。

完整命令如下:

[root@xxx ~]# docker pull centos
[root@xxx ~]# docker run -it centos:latest /bin/bash
# 此时进入容器,安装Python3、Git、Flask及其依赖包等,安装完成后exit退出
[root@xxx ~]# docker commit -m "Flask" -a "xianhu" container_id xianhu/flask:v1
[root@xxx ~]# docker push xianhu/flask:v1

Docker的功能和特性还有很多,各种运行命令、参数等也都有待学习和练习,比如如何管理数据、如何管理网络、如何互相配合工作、如何编写更专业的Dockerfile等。本文先入门为主,以后有时间再慢慢更新关于Docker的知识。


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324869834&siteId=291194637