Docker use from the outside to the entry

Interview

This first Docker book. Author: James Turnbull

 

installation

Here only the instructions to install the Windows environment (Windows7 above)

Docker Toolbox tool you can use: http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/?spm=5176.8351553.0.0.58a71991lwK6LZ

Then click Next installed,

There are three icons, ToolBox Docker is a collection of components, including a tiny virtual machine, a tool to support command-line installation under Windows.

And one of the Docker Quickstart Terminal, provides a platform command-line operations, Kitematic (Alpha) provides a GUI client platform.

 

 Note that: the use of docker run -v command does not work under Windows, because you can not mount the local directory.

 

 

Docker Component Description

1.Docker client and server

Docker clients are located outside, each client by Docker daemon as an intermediary, connected to the Docker containers. Docker Docker container and daemons, which belongs to the contents of Docker host.

 

 

2.Docker Mirror

Open Docker Quickstart Terminal enter the command: docker images, you can view the local mirror. A mirror position is stored locally (at / var / lib / docker in)

docker images

User-based mirroring, create the container.

In Docker Lane, root file system is always read-only and will be more value through the root file system read-only file system. Such a file system called a mirror.

The container and cover are stacked on the image, the new image is formed, following the mirror image is called the parent, the bottom of the base image. Uppermost layer is a read-write file system for program execution in the read-write Docker layer.

This stack is called copy-on-write, so that when the top after modification, covering part of the original, but also constitute a new image, and then release (quickly build).

 

3.Registry

Registry to save the user's image, download, upload image. Docker is stored a lot of official and private mirrored on Docker Hub. Such as mirroring MySQL database. Users can also set up their own Registry, Registry there are a total and private points.

4.Docker container

Use a mirror to create a container container. I think it is an example of the image. Use the command: docker run -i -t ubuntu / bin / bash such a container according to the command to create a mirror, the mirror image does not exist automatic download

Position (/ var / lib / docker / containers). View need to use the Toolbox: docker-machine ssh into a virtual machine can view. As shown, sometimes you need to sudo, or switch to an administrator to enter the directory:

The second picture shows the container ID number

 

 

 

 

Change mirror source

Sometimes, a good download speed is necessary. And everyone else, here is a change of the source image to download the program.

Use Ali cloud acceleration, perhaps you can see the open: https://cr.console.aliyun.com/cn-shenzhen/instances/mirrors

Registered account, locate the container mirror service, open, to find their own unique address, the corresponding system and enter into the dashboard. You can view the image source is changed with the docker info.

 

 

 

View docker information

In the terminal input docker info, can view the current number of containers, the number of mirrors, as well as other changes being mirrored

 

 

 

Running the first container

Run the following code, -i ensure STDIN open container, -t create a pseudo tty terminal, which can interact. The code image of the Ubuntu use, when the mirror is not present then it will automatically pull downloading, saved locally. This image is a base image.

Container created is a cut Ubuntu system, has its own network and the network interface IP address, and host communications places.

docker run -i -t ubuntu /bin/bash

The line will run into the container, and automatically create a name and ID for a container.

Names can be specified at the time of the run, in the manner specified as follows:

docker run --name my_container -i -t ubuntu /bin/bash

 

输入后直接进入该容器中,输入hostname可以查看到ID。

通过

apt update
apt install net-tools

 

命令安装完ifconfig的程序时候,可以通过ifconfig -a查看到该容器的IP地址。

 

 

 可以通过以下代码在容器中,查看进程,安装vim等

ps aux
apt install vim

 

 

输入exit退出容器,或者ctrl+d退出。退出容器,/bin/bash命令结束,容器停止。

通过以下代码查看创建的容器详细信息,包括ID,名字。在引用的时候,可以使用ID,也可以使用名字引用。

docker ps -a

 

启动,创建,和附着容器

如下命令即可启动:(对应到上图中ID的第一个,也可以使用NAMES的值代替ID,这里ID没有写全了,不过能用)

docker start 6d47

使用如下代码查看STATUS,可以看到启动了:

docker ps -a

 

 容器创建使用docker create命令,创建却不运行它。

虽然容器启动了,可是没有进到容器中,不能进行交互式会话。

使用如下命令,附着到正在运行的容器,进行会话。

docker attach 6d47

这样就又回到交互式环境中了:

 

 

创建和使用守护式容器

守护式容器没有交互式会话,长期运行在后台,提供服务即可。使用的命令还是run命令。

运行如下命令,即可创建一个守护式容器。

docker run --name my_container -d ubuntu /bin/sh -c "while true;do echo hello world;sleep 1;done"

运行完以后,通过docker ps -a可以查看状态,它在后台运行。会在每秒中输出一个hello world。

因为使用了-d参数,所以才能够放到后台运行。

查看该容器中的输出效果:

docker logs my_container

 

使用如下跟踪守护式进程日志(ctrl +c退出跟踪(容器不会停止)):

另外,-t可以增加日志的时间戳信息,如--tail 4,可以只显示最后四条信息

docker logs -f my_container

 

 

Docker日志驱动,使用

docker run --log-driver="syslog" --name container -d ubuntu /bin/sh -c "while true;do echo hello world;sleep 1;done"

创建,但是在Docker Toolbox中,需要使用:

docker-machine ssh
syslogd

 

命令进入Docker Toolbox虚拟机,然后运行syslogd,来启动syslog守护进程

 

容器进程查看、统计信息

在终端查看守护式容器中的进程:

docker top my_container

 

 

在容器中运行进程

后台命令示例使用代码:

docker exec -d my_container touch /etc/new_file

 

则在该容器中(-d表示后台执行),创建一个新文件

交互式命令示例使用代码:

docker exec -t -i my_container /bin/bash

该代码可以进去与之前创建的守护式容器进行交互(当然我之前使用了 docker exec -d my_container kill -9 xxx结束了容器中的循环echo的代码了)。

 

 

停止、自重启守护式容器

使用如下(发送SIGTERM信号),停止正在运行的该容器:(也可以使用docker kill 发送SIGKILL信号更快停止)

docker stop my_container

创建守护式容器中使用了:docker run --name my_container -d ubuntu /bin/sh -c "while true;do echo hello world;sleep 1;done",这里添加--restart部分即可

docker run --restart=always --name my_container -d ubuntu /bin/sh -c "while true;do echo hello world;sleep 1;done"

可以使用always表示是否正常退出都重启,使用参数为on-failure:5表示失败重启,次数为5次。

 

查看容器详细信息与删除容器

查看详细信息如下,其中第二条用于过滤信息。

docker inspect my_container
docker inspect --format='{{.State.Running}}' my_container

删除容器使用rm命令

docker rm my_container

使用-f选项,可以删除正在运行中的容器。

删除所有容器的方法是:

 

docker rm `docker ps -a -q`

其中的子句列出了所有容器的id。

  

拉取、查找镜像

拉取:

之前通过run获取的是自动获取,这里手动下载镜像示例:

docker pull ubuntu:12.04

该命令会从Docker Hub上拉取镜像

之后可以通过docker images查看本地镜像。在拉取过程中,不指定则使用标签为latest,这里指定了标签为12.04.

这种标签机制可以存活多个相同名字不同标签的镜像。

在run的过程中,指定基础镜像时候,需要加上该标签更合适(默认latest):

docker run --name my_container -i -t ubuntu:12.04 /bin/bash

从用户那里拉取镜像时候,:需要包含:用户名,仓库名。

之前拉取只有仓库名,是因为其为顶级仓库,由优质厂商提供的镜像。

查找镜像:

使用代码示例:

docker search puppet

 

 

会提示一些信息,排名等

然后通过以下拉取镜像即可

docker pull xxx/xxxx

前面是用户名,后面是仓库名。

 

构建镜像--commit

这里指代的是在原有镜像的基础上,进行修改,然后重新生成新的镜像。

创建容器,进行修改后退出容器,提交即可:

docker run -i -t ubuntu /bin/bash

apt update
apt -y install apache2

docker commit $ID $用户名/$仓库名

其中,提交参数有 -a 后面添加作者,-m后面添加提交说明,仓库名后添加    :$标签   ,表示提交使用的标签。

提交后成为镜像,又可以拿来产生容器。

 

构建镜像--build与Dockerfile

使用build和Dockerfile创建镜像。其中Dockerfile中填写了一些说明信息。下面构建了文件夹作为环境上下文,然后在该环境中创建文件。

mkdir static_web
cd static_web
touch Dockerfile
nano Dockerfile

文件中写入的内容例如:

FROM ubuntu:14.04
MAINTAINER Bai "[email protected]"
RUN apt-get update && apt-get install -y nginx
RUN echo 'Hi I am in your container ' > /usr/share/nginx/html/index.html
EXPOSE 80

 

这里的每一个作为一个控制说明。分别说明了基础镜像,作者,邮箱,运行命令和向外公布的端口。

当然其还有如CMD,ENTRYPOINT,WORKDIR等等的标记,用于构建Dockerfile。#表示注释

其中run的命令在有时候需要写成如下格式(exec格式的RUN指令):

RUN ["apt-get", "install","-y","nginx"]

进行保存后,在该路径下执行命令:

docker build -t="jamtur01/static_web:v1" .

进行在该路径下构建镜像,每一步会形成一个新的镜像,中途失败,下次会冲上次失败的地方开始(镜像堆叠效果)。

使用docker history + 镜像可以查看堆叠过程。

以上构建代码指明了用户名,仓库名,标签。

另外,也可以在git仓库中构建镜像。

 

Docker运行nginx

在构建一个nginx镜像成功以后,通过以下命令,进行端口映射,然后创建容器:以下代码使用了地址加随机端口的方式,进行了映射。当然还有其它各种映射方案。

sudo docker run -d -p 127.0.0.1::80 --name static jm/static_web_1 nginx -g "daemon off;"

通过以下查看端口映射情况(都可):

docker ps -l 
docker port + ID + 被映射端口

  

上传镜像

使用:docker push username/image

可能需要登录:

docker login命令即可

 

删除镜像

使用docker rmi +镜像即可

 

Guess you like

Origin www.cnblogs.com/bai2018/p/11964778.html