CentOS Docker 安装

100 阅读5分钟

第一种安装方式

1、yum包更新到最新

yum update

2、安装需要的软件包,yum-util提供yum-config-manager功能,另外两个是devicemapper驱动依赖的

yum install -y yum-utils device-mapper-persistent-data 1vm2

3、设金yum源

yum-config-manager --add-repo https://download. docker .com/linux/centos/docker-ce.repo

4、安装docker,出现输入的界面都按y

yum install -y docker-ce

5、查看docker版本,验证是否验证成功

docker -v

clipboard.png

第二种:

安装命令

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

设置国内镜像

vim /etc/docker/daemon.json

修改以下配置文件

{
    "registry-mirrors": [
        "https://registry.docker-cn.com""https://kfwkfulq.mirror.aliyuncs.com""https://pee6w651.mirror.aliyuncs.com",
        "https://21qq34jg.mirror.aliyuncs.com"
    ]
}

需要root权限,但是如果使用sudo命令,环境变量会变化

1 sudo groupadd docker #创建新用户组

2 sudo usermod -aG docker $USER #当前用户加入用户组

3 newgrp docker #刷新用户组权限

运行docker run hello-world

clipboard.png

镜像命令

docker image --help

Commands:

build Build an image from a Dockerfile #构建镜像

history Show the history of an image #查看镜像构建历史

import Import the contents from a tarball to create a filesystem image

inspect Display detailed information on one or more images #洞察: 查看详情

load Load an image from a tar archive or STDIN #导入

ls List images #列出

prune Remove unused images #移除未使用的

pull Pull an image or a repository from a registry #从仓库拉取

push Push an image or a repository to a registry #退到仓库

rm Remove one or more images #删除

save #导出 Save one or more images to a tar archive (streamed to STDOUT by default)

tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

例如:

docker image pull hello-world #拉取仓库镜像

docker image ls #列出所有镜像

docker image save hello-world -o hello.img #导出镜像为文件

docker image load -i hello.img #导入镜像为文件

网络命令

docker network --help

每一个容器它在创建运行时都会自动创建一个独立的网络

Commands:

connect Connect a container to a network #让容器加入一个网络

create Create a network #创建新的网络

disconnect Disconnect a container from a network #让容器退出一个网络

inspect Display detailed information on one or more networks #洞察

ls List networks #列出所有网络

prune Remove all unused networks #删除所有未使用网络

rm Remove one or more networks #删除指定网络

例如:

docker network create my_net #创建my_net网络

文件存储命令

卷(volume)是Docker持久化工具,文件存储

docker volume --help

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

容器命令

docker container --help

Commands:

attach Attach local standard input, output, and error streams to a running container

commit Create a new image from a container's changes

cp Copy files/folders between a container and the local filesystem

create Create a new container

diff Inspect changes to files or directories on a container's filesystem

exec Run a command in a running container

export Export a container's filesystem as a tar archive

inspect Display detailed information on one or more containers

kill Kill one or more running containers

logs Fetch the logs of a container

ls List containers

pause Pause all processes within one or more containers

port List port mappings or a specific mapping for the container

prune Remove all stopped containers

rename Rename a container

restart Restart one or more containers

rm Remove one or more containers

run Run a command in a new container

start Start one or more stopped containers

stats Display a live stream of container(s) resource usage statistics

stop Stop one or more running containers

top Display the running processes of a container

unpause Unpause all processes within one or more containers

update Update configuration of one or more containers

wait Block until one or more containers stop, then print their exit codes

例如:

docker container run (从镜像)创建 ,并启动容器

docker container ls 查看当前容器(可看到当前的启动容器CONTAINER ID)

根据CONTAINER ID进行以下操作

docker stop CONTAINER ID 停止容器

docker kill CONTAINER ID 强杀容器

docker restart CONTAINER ID 重启

docker logs CONTAINER ID 查看日志

docker运行参数

docker run

--name my_hello #指定容器名称

--rm #结束后自动删除

--net my_net #指定容器加入的网络

--volume ${PWD}:/tmp #把当前目录挂载到容器中

--it #进入容器内容,挂载终端

--d #让容器后台运行

hello-world

bash #容器要执行命令

clipboard.png

如上图所示,已经进入容器内部

建立测试网络

docker network create test_net

搭建selenium grid容器

docker run --net test_net --name my_slenium --rm -d -p 4444:4444 -p 6900:5900 --shm-size="2g" selenium/standalone-chrome:4.1.0-20211123

clipboard.png

启动UI录制容器

docker run --net test_net --name my_vidoe --rm -d -v /tmp/videos:/videos selenium/video:ffmpeg-4.3.1-20211123

clipboard.png

Docker 安装Nginx

下载镜像

docker pull nginx

建立容器

#不建议(参数多)# docker run --name some2-nginx -v /www/serever/nginx:/www/server/nginx/html:ro -d -p 8080:80 -p 8081:80 nginx

建立容器

docker run -d --name my-nginx -p 8081:80 nginx

clipboard.png

进入容器

docker exec -it my-nginx /bin/bash

clipboard.png

nginx 部署

首先,创建目录 nginx, 用于存放后面的相关东西

mkdir -p /opt/nginx/html /opt/nginx/logs /opt/nginx/conf /opt/nginx/ssl

拷贝容器内 Nginx 默认配置文件和静态文件到本地创建的挂载目录,容器 ID 可以查看 docker ps 命令输入中的第一列:

docker cp 88a5cb9e51b2:/etc/nginx/nginx.conf /opt/nginx/conf //把默认的配置文件也拷贝过来,方便直接使用

docker cp 88a5cb9e51b2:/etc/nginx/conf.d/default.conf /opt/nginx/conf/conf.d //把默认的配置文件也拷贝过来,方便直接使用

docker cp 88a5cb9e51b2:/usr/share/nginx/html/index.html /opt/nginx/html //把默认的静态文件也拷贝过来,方便直接使用

docker cp 88a5cb9e51b2:/usr/share/nginx/html/50x.html /opt/nginx/html //把默认的静态文件也拷贝过来,方便直接使用

  • html: 目录将映射为 nginx 容器配置的虚拟目录。
  • logs: 目录将映射为 nginx 容器的日志目录。
  • conf: 目录里的配置文件将映射为 nginx 容器的配置文件。
  • ssl: 目录为后面作https配置用,为可选项。

部署命令

docker run -d -p 8081:80 --name nginx -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/conf/conf.d:/etc/nginx/conf.d nginx

如果加上ssl配置

docker run -d -p 80:80 -p 443:443 --name nginx -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/conf/conf.d:/etc/nginx/conf.d -v /opt/nginx/ssl:/etc/nginx/ssl nginx
  • -p 8081:80: 将容器的 8081 端口映射到主机的 80 端口。
  • -p 4433:443: 将容器的 4433 端口映射到主机的 443 端口。(可选项,配置https时加上)
  • --name nginx:将容器命名为 nginx。
  • -v /opt/nginx/html:/usr/share/nginx/html:将我们自己创建的 html目录挂载到容器的 /usr/share/nginx/html。
  • -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:将我们自己创建的 nginx.conf 挂载到容器的 /etc/nginx/nginx.conf。
  • -v /opt/nginx/logs:/var/log/nginx:将我们自己创建的 logs 挂载到容器的 /var/log/nginx。
  • -v /opt/nginx/conf/conf.d:/etc/nginx/conf.d: 将conf.d目录挂载,后面进行二级域名反向代理映射端口时用,不配做这个后面配置不生效
  • -v /opt/nginx/ssl:/etc/nginx/ssl:将我们自己创建的 ssl目录 挂载到容器的 /etc/nginx/ssl,此项为可选项,要使用https证书时加上。

\

创建启动并挂载 Nginx

docker run -d --name my-nginx -p 8081:80 -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/logs:/var/log/nginx nginx

clipboard.png

记得重启容器 docker restart [容器id]

下载仓库镜像 ( 从中央仓库拉取最新的 registry 仓库)

docker pull registry

创建仓库文件夹 ( 存储本地镜像)

mkdir -pv /opt/docker/registry

启动镜像

docker run -d -p 5000:5000 -v /opt/docker/registry:/var/lib/registry --restart=always --name registry registry

安装docker-compose

www.cnblogs.com/darren1112/… 操作地址及命令

1、下载二进制文件

wget github.com/docker/comp…

2、移动文件

mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose

3、赋予可执行权限

chmod +x /usr/local/bin/docker-compose

4、创建软链

ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

5、测试是否安装成功

docker-compose --version

docker安装mysql数据库

在 / 目录下执行 mkdir -p /opt/mysql/{conf.d,mysql} 可以根据喜好改名,主要是挂载一些配置文件。

执行以下命令:

docker run --name mysql --restart=always -p 3306:3306 -v /opt/mysql/conf.d:/etc/mysql/conf.d -v /opt/mysql/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql

如果你的镜像库中没有mysql,docker会自动下载,注意,默认下载最新版本,可以通过加 -d mysql : <版本号> 的方式,或者提前pull指定版本的数据库安装

进入容器

docker exec -it mysql bash

进入数据库环境

mysql -p

创建数据库用户

create user 'wuyi'@'%' identified by '123456';
grant all on *.* to 'wuyi'@'%';
flush privileges;
alter user 'wuyi'@'%' identified by '123456' password expire never;
alter user 'wuyi'@'%' identified with mysql_native_password by '123456';
flush privileges;

clipboard.png

clipboard.png