手把手教你玩转docker(二)

简介: 手把手教你玩转docker(二)

手把手教你玩转docker(一):https://developer.aliyun.com/article/1495415

3、查看容器日志:

首先要run起来一个容器

然后docker logs -f 容器id即可查看该容器的日志

[root@little ~ ]#docker run alpine /bin/echo “你好” 2>&1 >/dev/null

[root@little ~ ]#docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

eb32cf000b27 alpine “/bin/echo 你好” 11 seconds ago Exited (0) 10 seconds ago strange_raman

[root@little ~ ]#docker logs eb32cf000b27

你好

docker设置容器日志大小

全局设置:新建/etc/docker/daemon.json,若有就不用新建了;添加log-dirver和log-opts参数。

vim /etc/docker/daemon.json

{

“log-driver”:“json-file”

“log-opts”:{“max-size”:“500m”,“max-file”:“3”}

}

max-size=500m,意味着一个容器日志大小上限是500M,

max-file=3,意味着一个容器有三个日志,分别是id+.json、id+1.json、id+2.json。

重启docker守护进程,该方法只对新建的容器起效

systemctl daemon-reload

systemctl restart docker

项目部署步骤:

1.先在docker中运行一个镜像资源

2.更新系统容器的软件

3.在系统容器配置项目依赖环境

4.将项目文件上传到容器

5.将系统容器打包成新的镜像 commit

6.将新的镜像资源打包成压缩包 save -o

7.将压缩包上传到线上服务器 scp

8.线上服务器安装docker

9.将压缩包的镜像资源进行导入 load -i

4、使用公共仓库

登陆官方网站:https://hub.docker.com/ 注册账号和密码

在Docker hub中创建一个资源, create respositories, 创建后会提示如何push镜像到公共仓库

当提示: docker push dai163309889254/zabbix:tagname 时, 使用该命令push镜像

登录公共仓库:

[root@little ~ ]#docker login docker.io

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: lixxx1

Password:

WARNING! Your password will be stored unencrypted in /root/.docker/config.json.

Configure a credential helper to remove this warning. See

https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

登录文件放在/root/.docker/config.json

[root@little .docker ]#ll

total 4

-rw------- 1 root root 107 Feb 21 16:33 config.json

[root@little .docker ]#cat config.json

{

“auths”: {

“https://index.docker.io/v1/”: {

“auth”: “bGl0dGxlZnVuOTE6amluZ2hhbzE5OTExMDM=”

}

}

}[root@little .docker ]#echo “bGl0dGxlZnVuOTE6amluZ2hhbzE5OTExMDM=”|bas

base64 basename bash bashbug bashbug-64

[root@little .docker ]#echo “bGl0dGxlZnVuOTE6amluZ2hhbzE5OTExMDM=”|base

base64 basename

[root@little .docker ]#echo “bGl0dGxlZnVuOTE6amluZ2hhbzE5OTExMDM=”|base64 -d

littlefun91:jinghao1991103[root@little .docker ]#

密码是base64加密方式

tag重命名是在原来的基础上新建一个新的,不改变原有存在的

标准的命名是:

registry_name/repository_name/image_name:tag

registry_name是注册服务器名字

repository_name是我们在docker.io上面注册的用户名

将本地镜像推送到docker.io: docker.io可以省略

先按标准命名规范将镜像改名:

[root@little ~ ]#docker tag centos7.8:latest docker.io/littlefun91/centos7.8:v1.0

然后直接推送:

[root@little ~ ]#docker push litxxxxn331/centos7.8

littlefun91/centos7.8 litxxxxn331/centos7.8:v1.0

[root@little ~ ]#docker push litxxxxn331/centos7.8:v1.0

The push refers to repository [docker.io/littlefun91/centos7.8]

fb82b029bea0: Mounted from daduber/centos7.8.2003

v1.0: digest: sha256:50b9a3bc27378889210f88d6d0695938e45a912aa99b3fdacfb9a0fef511f15a size: 529

这样就将本地镜像推送到docker.io自己的账户

将仓库中的镜像拉下来:

docker pull littlefun91/nginx_with_curl:v1.0

登出Docker Hub

docker logout

在docker.io搜索镜像包

搜索镜像包:

[root@220 ~ ]# docker search alpine

NAME DESCRIPTION STARS OFFICIAL AUTOMATED

alpine A minimal Docker image based on Alpine Linux… 8841 [OK]

alpine/git A simple git container running in alpine li… 197 [OK]

alpine/socat Run socat command in alpine container 79 [OK]

puppet/puppet-agent-alpine Puppet Agent as a Docker Image. Based on Alp… 17

docker/desktop-kubernetes Kubernetes binaries for Desktop. Compiled fr… 12

puppet/r10k r10k on a Docker image. Based on Alpine 4

rancher/alpine-git 2

treehouses/alpine 2

zavolab/zavolab_minimal alpine 3.7 with bash, perl, gzip, wget… 1 [OK]

nasqueron/rabbitmqadmin RabbitMQ management plugin CLI tool Lightwei… 1 [OK]

ibmcom/fhe-toolkit-alpine-amd64 The IBM Fully Homomorphic Encryption (FHE) T… 1

grafana/alpine Alpine Linux with ca-certificates package in… 1

rapidfort/alpine-rfdev 1

fsfe/alpine-pipenv 0

machines/alpine 0

rhomicom/alpine_nodejs 0

docker/tutum-test-alpine Alpine 3.12 image 0

armswdev/c-hello-world Simple hello-world C program on Alpine Linux… 0

fluxcd/gitsrv Alpine git server used for Flux end-to-end t… 0

openebs/alpine-fio Retagged from dmonakhov/alpine-fio 0

ovehub/alpine-pypy-vips 0

ibmcom/fhe-toolkit-alpine-s390x The IBM Fully Homomorphic Encryption (FHE) T… 0

treehouses/alpine-tags 0

ibmcom/alpine-helm-s390x 0

drud/alpine

5、搭建私有仓库:

在 Docker 中,当我们执行 docker pull xxx 的时候 ,

它实际上是从 registry.hub.docker.com 这个地址去查找,这就是Docker公司为我们提供的公共仓库。

在工作中,我们不可能把企业项目push到公有仓库进行管理。所以为了更好的管理镜像,

Docker不仅提供了一个中央仓库,同时也允许我们搭建本地私有仓库。

这一篇介绍registry、harbor两种私有仓库搭建。

1.下载registry镜像资源:

[root@little ~ ]#docker image pull registry

2.修改配置文件:

[root@little ~ ]#vim /etc/docker/daemon.json

{“registry-mirrors”: [“http://abcd1234.m.daocloud.io”],“insecure-registries”:[“10.1.1.100:5500”]}

~

重启docker服务:

[root@little ~ ]#systemctl restart docker.service

3.运行私有仓库镜像:

[root@little ~ ]#docker run -d --name myrepo -v /opt/docker_registry:/data/registry --network=host registry

-v 表示目录映射关系(前者是宿主机目录,没有的时候会自动创建。后者是容器中的目录),

可以使用多个 -v 做多个目录或文件映射。

注意:最好做目录映射,挂载。在宿主机上做修改,然后共享到容器上,在容器中创建文件也会共享到宿主目录中。

在宿主本地创建的共享目录称为数据卷

-P 宿主端口:容器端口

将要上传的镜像改名:

[root@little ~ ]#docker tag nginx:latest 10.1.1.100:5500/nginx:v1.0

将镜像名字前面加ip:端口号/

上传镜像到私有仓库:

[root@little ~ ]#docker push 10.1.1.100:5000/nginx:v1.0

The push refers to repository [10.1.1.100:5000/nginx]

2acf82036f38: Pushed

9f65d1d4c869: Pushed

0f804d36244d: Pushed

9b23c8e1e6f9: Pushed

ffd3d6313c9b: Pushed

9eb82f04c782: Pushed

v1.0: digest: sha256:b08ecc9f7997452ef24358f3e43b9c66888fadb31f3e5de22fec922975caa75a size: 1570

开启的 registry容器进入:

[root@little docker_registry ]#docker exec -it myrepo /bin/sh

要用/bin/sh 不能用/bin/bash 否则报错

[root@little ~ ]#docker exec -it myrepo /bin/bash

OCI runtime exec failed: exec failed: container_linux.go:370: starting container process caused: exec: “/bin/bash”: stat /bin/bash: no such file or directory: unknown

6、数据卷和数据卷容器管理

[root@little ~ ]#docker run -d --name myrepo -v /opt/docker_registry:/data/registry --network=host registry

-v 表示目录映射关系(前者是宿主机目录,没有的时候会自动创建。后者是容器中的目录),

可以使用多个 -v 做多个目录或文件映射。

注意:最好做目录映射,挂载。在宿主机上做修改,然后共享到容器上,在容器中创建文件也会共享到宿主目录中。

在宿主本地创建的共享目录称为数据卷

我们一般会将容器内部挂载的目录称之为挂载点!

我们在创建容器时,数据卷、挂载的目录、或者容器内部的挂载点!其实都不用单独去创建!如果不存在,会自动创建的

先用create创建模板容器

docker create -v /data --name module ubuntu

模板容器称为数据卷容器

在基于这个模板的基础上创建容器:

docker run -it --volumes-from module --name myubuntu ubuntu /bin/bash

可以创建多个,基于模板创建的容器,共享/data目录数据

数据操作:

[root@little docker_registry ]#docker volume

Usage: docker volume COMMAND

Manage volumes

Commands:

create Create a volume

inspect Display detailed information on one or more volumes

ls List volumes

prune Remove all unused local volumes

rm Remove one or more volumes

端口映射:

种类:

1.随机映射: -P(大写)

docker run -itd -P --name mynginx nginx

这样,容器中开启的nginx服务会随机映射一个端口号给宿主,这样,在宿主机上就可以访问容器中nginx服务了

不映射访问不了

[root@little ~ ]#docker run -dit --name mynginx nginx

[root@little ~ ]#docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

1f3716a74b0d nginx “/docker-entrypoint.…” About a minute ago Up About a minute 80/tcp mynginx

8c8004fabf90 registry “/entrypoint.sh /etc…” About an hour ago Up About an hour myrepo

d317ba6b0346 ubuntu “/bin/bash” 5 hours ago Exited (0) 2 hours ago ubuntu

[root@little ~ ]#curl http://localhost

curl: (7) Failed connect to localhost:80; Connection refused

做端口映射:

[root@little ~ ]#docker run -dit -P --name mynginx nginx

ae6da75a85297cbd7364b2ea5920a70749b04a1deea01006dd1d30cc45ba4daa

[root@little ~ ]#docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

ae6da75a8529 nginx “/docker-entrypoint.…” 9 seconds ago Up 8 seconds 0.0.0.0:49153->80/tcp mynginx

8c8004fabf90 registry “/entrypoint.sh /etc…” About an hour ago Up About an hour myrepo

d317ba6b0346 ubuntu “/bin/bash” 6 hours ago Exited (0) 2 hours ago ubuntud317ba6b0346 ubuntu “/bin/bash” 6 hours ago Exited (0) 2 hours ago ubuntu

在在宿主机访问:

[root@little ~ ]#curl http://localhost:49153
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

可以访问了

2.指定映射: -p(小写) 宿主端口:容器端口

生产环境一般不使用随机映射,但是随机映射好处就是由docker分配,端口不会冲突

不管哪种映射都会影响性能,因为涉及到映射

[root@little ~ ]#docker run -dit -p 10.1.1.100:8080:80 --name mynginx nginx

cdc12824339abd9be410febaee1269e498576bdef0b21c4e4f484b446b525b03

指定映射的ip和端口号

不指定ip的话,默认是0.0.0.0所有ip都可以访问

[root@little ~ ]#curl http://10.1.1.100:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

可以访问

/usr/share/nginx/html

nginx镜像映射端口和数据卷:

docker run -itd -p 10.10.0.220:8022:80 -v /root/nginx/:/usr/share/nginx/html littlefun91/nginx_with_curl:v1.0

[root@220 ~ ]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

99d7f6814004 nginx:latest “/docker-entrypoint.…” 31 minutes ago Up 30 minutes 80/tcp busy_clarke

ba2c216bc95f littlefun91/nginx_with_curl:v1.0 “/docker-entrypoint.…” About an hour ago Up About an hour 10.10.0.220:8022->80/tcp determined_margulis

[root@little ~ ]#docker logs mycentos7

查看容器的标准输出

7、传递环境变量:

从宿主机向容器传递变量,云原生

语法:

docker run -e 环境变量key=环境变量value

[root@little ~ ]#docker run --rm -e E_EXPOT=100 nginx printenv

HOSTNAME=edd5b825a784

HOME=/root

PKG_RELEASE=1~buster

NGINX_VERSION=1.19.7

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

NJS_VERSION=0.5.1

E_EXPOT=100 这就是给容器传递的环境变量

PWD=/

传递多个参数用多个-e 环境变量key=环境变量value

[root@220 ~ ]# docker run -itd -e MY_HOME=“北京” -e MY_NAME=“景浩” nginx:latest

99d7f68140049e755e76a140b70682621c73343ae123ab2040848e34a058cde7

[root@220 ~ ]#

[root@220 ~ ]# docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

99d7f6814004 nginx:latest “/docker-entrypoint.…” 6 seconds ago Up 5 seconds 80/tcp busy_clarke

ba2c216bc95f littlefun91/nginx_with_curl:v1.0 “/docker-entrypoint.…” 44 minutes ago Up 43 minutes 10.10.0.220:8022->80/tcp determined_margulis

[root@220 ~ ]# docker exec -it busy_clarke /bin/bash

root@99d7f6814004:/# env

HOSTNAME=99d7f6814004

MY_HOME=北京

PWD=/

PKG_RELEASE=1~bullseye

HOME=/root

NJS_VERSION=0.7.3

TERM=xterm

SHLVL=1

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

NGINX_VERSION=1.21.6

MY_NAME=景

_=/usr/bin/env

docker比虚拟机火的原因

最主要的是虚拟机本身是一个信息孤岛,启动虚拟机时没办法向虚拟机传递参数

云原生,在启动之前传递一些参数,按根据环境变量的值启动jar包

docker inspect 查看容器运行信息


[root@220 ~ ]# docker inspect ba2c216bc95f


“HostConfig”: {

“Binds”: [

“/root/nginx/:/usr/share/nginx/html”

],


“PortBindings”: {

“80/tcp”: [

{

“HostIp”: “10.10.0.220”,

“HostPort”: “8022”

}

]

},


“Mounts”: [

{

“Type”: “bind”,

“Source”: “/root/nginx”,

“Destination”: “/usr/share/nginx/html”,

“Mode”: “”,

“RW”: true,

“Propagation”: “rprivate”

}

],

8、容器的生命周期:

1)检查本地是否存在指定的镜像,不存在就从公有仓库下载;

2)利用镜像创建一个容器,并启动该容器

3)分配一个文件系统给容器,并在只读的镜像层外面挂载一层可读写层;

4)从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中;

5)从网桥的地址池配置一个IP地址给容器;

6)执行用户指定的应用程序;

7)执行完毕后容器被自动终止;

默认docker的资源目录:

/var/lib/docker/

little docker ]#cd /var/lib/docker/

[root@little docker ]#ll

total 0

drwx–x–x 4 root root 120 Feb 20 14:48 buildkit

drwx-----x 2 root root 6 Feb 20 14:48 containers 容器信息

drwx------ 3 root root 22 Feb 20 14:48 image 镜像资源

drwxr-x— 3 root root 19 Feb 20 14:48 network

drwx-----x 3 root root 40 Feb 20 14:48 overlay2

drwx------ 4 root root 32 Feb 20 14:48 plugins

drwx------ 2 root root 6 Feb 20 14:48 runtimes

drwx------ 2 root root 6 Feb 20 14:48 swarm

drwx------ 2 root root 6 Feb 20 14:48 tmp

drwx------ 2 root root 6 Feb 20 14:48 trust

drwx-----x 2 root root 50 Feb 20 14:48 volumes

清理无用镜像:

docker image prune -a -f

9、docker cp命令详解

——在容器和主机之间复制文件/文件夹

docker cp命令作用

在容器和主机之间复制文件/文件夹

docker cp命令语法格式

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-

docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

源目录参数时,:后只能跟文件名,不能是以“/”开头的路径(eg:app/logs/cloud.log)

目标参数时,必须为文件不能是一个目录(eg:cloud.log)

重点

容器 container 可以是正在运行或已停止的容器

SRC_PATH 或 DEST_PATH可以是文件或目录

该命令会假定容器路径相对于容器的 /(根)目录

而主机路径则是相对于执行 docker cp 命令的当前目录

options 说明

option

作用

-a 存档模式(复制所有uid / gid信息)

-L 保持源目标中的链接

备注:暂时没发现这两个 options 有啥用

docker cp命令实操

1.从容器复制文件到主机

已存在的目录,当前目录,目录可以自己定

docker cp tomcat7:usr/local/tomcat/README.md ./

不存在的目录 会报错

docker cp tomcat7:usr/local/tomcat/README.md test/

已存在的文件,相当于改名后保存

docker cp tomcat7:usr/local/tomcat/README.md test.txt

2.从主机复制文件到容器,复制到容器的根目录下

docker cp test.txt tomcat7:/

源目录是pod 目录时,:后只能跟文件名,不能是以/ 开头的路径,

所以需要先用kubectl exec ssh到pod上,copy文件到工作目录下workdir, (此example种workdir为 /home/bbuser)

copy源文件在pod时,需要将目标文件放在容器的工作目录

相关文章
|
3天前
|
存储 应用服务中间件 nginx
手把手教你玩转docker(一)
手把手教你玩转docker
|
3天前
|
安全 Linux 数据库
Docker 入门详解
通过遵循这些步骤,你将能够快速入门 Docker 并开始探索容器化的世界,关注 V 哥,技术之路一起成长。
|
3天前
|
Ubuntu 网络协议 Docker
docker学习笔记1,入门到精通
docker学习笔记1,入门到精通
|
3天前
|
应用服务中间件 Shell nginx
docker学习笔记2,入门到精通
docker学习笔记2,入门到精通
|
3天前
|
Java Linux Docker
Docker入门到实战
Docker入门到实战、Centos7安装Docker
|
3天前
|
存储 Ubuntu API
【docker】入门
【docker】入门
41 0
|
3天前
|
Docker 容器
docker入门
docker入门
21 0
|
8月前
|
Docker 容器
Docker 入门(三)
Docker 入门(三)
57 0
|
11月前
|
存储 Kubernetes 安全
手把手教你入门 Docker
Docker 是一种容器化平台,它可以让开发人员和系统管理员轻松地创建、部署和运行应用程序。Docker 可以将应用程序及其依赖项打包成一个独立的容器,该容器可以在任何环境中运行,而不需要修改应用程序或其依赖项。Docker 的目标是帮助开发人员和系统管理员更快、更可靠地构建、部署和运行应用程序。
469 0
手把手教你入门 Docker
|
存储 Ubuntu Shell
Docker入门(一)
上一节中,我们了解到了Docker 的一些基本知识点,它的一些核心概念,Docker的使用安装等。此篇文章我们对 Docker 进行入门讲解
73 0
Docker入门(一)