百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

新手快速入门Docker,轻松掌握Docker安装与使用

off999 2025-01-13 16:49 23 浏览 0 评论

安装

使用官方安装脚本自动安装

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

手动安装

CentOS 7 (使用yum进行安装)

卸载旧版docker

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils
# Step 2: 添加软件源信息 (推荐使用下面阿里云源)
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 添加阿里云源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# 开启一些选项 (这一步可跳过)
sudo yum-config-manager --enable docker-ce-nightly
sudo yum-config-manager --enable docker-ce-test
# 关闭选项
sudo yum-config-manager --disable docker-ce-nightly

# 安装 Docker-CE
sudo yum install docker-ce docker-ce-cli containerd.io

验证

sudo docker version

启动docker

sudo systemctl start docker

读者福利:关注小编+转发文章+私信【项目】获取整理好的100+个Java项目视频+源码+笔记

配置镜像加速器

可以上阿里云获取个人加速地址

https://dev.aliyun.com/
https://cr.console.aliyun.com/cn-shenzhen/instances/mirrors


修改
daemon配置文件/etc/docker/daemon.json使用加速器

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://e3b4lt8s.mirror.aliyuncs.com"]
}
EOF

重启

sudo systemctl daemon-reload
sudo systemctl restart docker

hello world

docker run hello-world

命令

查看帮助

docker --help
Usage:	docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.docker")
  -c, --context string     Name of the context to use to connect to the daemon (overrides DOCKER_HOST
                           env var and default context set with "docker context use")
  -D, --debug              Enable debug mode
  -H, --host list          Daemon socket(s) to connect to
  -l, --log-level string   Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info")
      --tls                Use TLS; implied by --tlsverify
      --tlscacert string   Trust certs signed only by this CA (default "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  context     Manage contexts
  engine      Manage the docker engine
  image       Manage images
  network     Manage networks
  node        Manage Swarm nodes
  plugin      Manage plugins
  secret      Manage Docker secrets
  service     Manage services
  stack       Manage Docker stacks
  swarm       Manage Swarm
  system      Manage Docker
  trust       Manage trust on Docker images
  volume      Manage volumes

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  build       Build an image from a Dockerfile
  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
  events      Get real time events from the server
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  history     Show the history of an image
  images      List images
  import      Import the contents from a tarball to create a filesystem image
  info        Display system-wide information
  inspect     Return low-level information on Docker objects
  kill        Kill one or more running containers
  load        Load an image from a tar archive or STDIN
  login       Log in to a Docker registry
  logout      Log out from a Docker registry
  logs        Fetch the logs of a container
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Remove one or more images
  run         Run a command in a new container
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  search      Search the Docker Hub for images
  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
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
  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
  version     Show the Docker version information
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker COMMAND --help' for more information on a command.

查看指定命令帮助

docker images --help
Usage:	docker images [OPTIONS] [REPOSITORY[:TAG]]

List images

Options:
  -a, --all             Show all images (default hides intermediate images)
      --digests         Show digests
  -f, --filter filter   Filter output based on conditions provided
      --format string   Pretty-print images using a Go template
      --no-trunc        Don't truncate output
  -q, --quiet           Only show numeric IDs

常用命令

查看本地镜像

docker images
docker images -a # 查看本地所有镜像(包括悬空镜像)
docker images -q # 查看本地镜像的镜像ID信息
docker images -qa
docker images --digests # 显示镜像的摘要写信息
docker images --no-trunc # 查看完整的镜像信息

搜索镜像

docker search nginx

搜索点赞数>30的镜像

docker search -s 30 nginx
docker search --filter=stars=20 nginx

列出自动构建的镜像

docker search --automated nginx
docker search --filter=is-automated=true nginx

拉取镜像

docker pull nginx == docker pull nginx:latest
docker pull nginx:3.2

删除镜像

docker rmi hello-world

强制删除镜像

docker rmi -f hello-world

删除多个镜像

docker rmi -f hello-world nginx

删除全部镜像

docker rmi -f $(docker images -qa)
1

容器命令

容器不但可以包含软件, 容器也可以包含一个操作系统

docker search centos

新建并运行容器

docker run

查看帮助

docker run --help
# Usage:	docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

运行容器

docker run -it centos
docker run -it centos /bin/bash

-i: 以交互模式启动容器, 和-t搭配使用

-t: 为容器重新分配一个输入终端

docker run -it 0d120b6ccaa8

指定容器新名字

docker run -it --name mycentos centos

查看正在运行的容器

docker ps

查看正在运行+历史运行过的容器

docker ps -a

查看上一次运行的容器

docker ps -l
docker ps -lq

查看最近2次运行的容器

docker ps -n 2

关闭并退出容器

exit

只退出不关闭容器

ctrl+p+q 

重启启动容器

docker start ID
docker start 04ac5ae5d5fb

停止容器

docker stop ID
docker stop 04ac5ae5d5fb

强制停止容器

docker kill ID

删除已停止的容器

docker rm ID

强制删除容器

docker rm -f ID

一次性所有容器

docker rm -f $(docker ps -qa)
1

以守护进程启动容器

docker run -d  centos # [这样会马上退出]

不退出

docker run -d centos /bin/sh -c "while true; do echo hello soul; sleep 2; done"

查看容器日志

docker logs ID
docker logs -f -t --tail 100 ID

查看容器内运行的进程

docker top ID
docker top e7574dd446d6

查看容器内部细节

docker inspect ID

进入正在运行的容器

docker attach ID
docker exec -t ID /bin/bash

直接容器执行命令

docker exec -t  ID ls -l /tmp

从容器拷贝文件到主机上

# docker cp 容器ID:容器内路径 目的主机路径
docker cp 04ac5ae5d5fb:/mnt/test.log /data1

生成新镜像副本

# docker commit 提交容器副本成为一个新的镜像
# docker commit -m="描述" -a="作者" 容器ID 镜像名:版本
docker commit -m="my tomcat" -a="soul" 04ac5ae5d5fb soul/go:1.0

运行自定义镜像

docker run -it -p 7777:8080 soul/tomcat:1.0

指定端口运行

# docker run -it -p 主机端口:容器端口 tomcat
docker run -it -p 8081:8080 tomcat

浏览器访问

http://localhost:8080

随机分配主机端口

docker run -it -P tomcat

后台运行

docker run -d -p 6666:8080 tomcat

指定容器数据卷启动 (等于主机目录挂载到容器指定目录)

# docker run -it -v 主机目录:容器目录 镜像名
docker run -it -v /mydata:/mydata centos

只读权限

docker run -it -v /mydata:/mydata:ro centos

容器数据卷共享

docker run -it --name dc01 soul/centos
docker run -it --name dc02 --volumes-from dc01 soul/centos
docker run -it --name dc03 --volumes-from dc01 soul/centos

Redis运行容器

docker run -p 6379:6379
-v /data1/redis/data:/data
-v /data1/redis/conf/redis.conf:/usr/locak/etc/redis/redis.conf
-d redis:5.0.10 redis-server /usr/local/etc/redis/redis.conf
--appendonly yes

MySQL运行容器

docker run -p 12345:3306 --name mysql 
-v /data1/mysql/conf:/etc/mysql/conf.d
-v /data1/mysql/logs:/logs
-v /data1/mysql/data:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=123456
-d mysql:5.6

备份数据库

docker exec 04ac5ae5d5fb sh -C 'exec mysqldump --all-databases -uroot -p"123456"' > /data1/all-databases.sql

读者福利:关注小编+转发文章+私信【项目】获取整理好的100+个Java项目视频+源码+笔记

提交镜像到阿里云

登录阿里云Docker Registry

$ sudo docker login --username=uisoul registry.cn-shenzhen.aliyuncs.com

用于登录的用户名为阿里云账号全名,密码为开通服务时设置的密码。

您可以在访问凭证页面修改凭证密码。

从Registry中拉取镜像

$ sudo docker pull registry.cn-shenzhen.aliyuncs.com/uisoul/gocentos:[镜像版本号]

将镜像推送到Registry

$ sudo docker login --username=uisoul registry.cn-shenzhen.aliyuncs.com
$ sudo docker tag [ImageId] registry.cn-shenzhen.aliyuncs.com/uisoul/gocentos:[镜像版本号]
$ sudo docker push registry.cn-shenzhen.aliyuncs.com/uisoul/gocentos:[镜像版本号]

请根据实际镜像信息替换示例中的[ImageId]和[镜像版本号]参数。

作者:IM魂影
原文:imsoul.blog.csdn.net/article/details/110531103

相关推荐

大文件传不动?WinRAR/7-Zip 入门到高手,这 5 个技巧让你效率翻倍

“这200张照片怎么传给女儿?微信发不了,邮箱附件又超限……”62岁的张阿姨对着电脑犯愁时,儿子只用了3分钟就把照片压缩成一个文件,还教她:“以后用压缩软件,比打包行李还方便!”职场人更懂这...

电脑解压缩软件推荐——7-Zip:免费、高效、简洁的文件管理神器

在日常工作中,我们经常需要处理压缩文件。无论是下载软件包、接收文件,还是存储大量数据,压缩和解压缩文件都成为了我们日常操作的一部分。而说到压缩解压软件,7-Zip绝对是一个不可忽视的名字。今天,我就来...

设置了加密密码zip文件要如何打开?这几个方法可以试试~

Zip是一种常见的压缩格式文件,文件还可以设置密码保护。那设置了密码的Zip文件要如何打开呢?不清楚的小伙伴一起来看看吧。当我们知道密码想要打开带密码的Zip文件,我们需要用到适用于Zip格式的解压缩...

大文件想要传输成功,怎么把ZIP文件分卷压缩

不知道各位小伙伴有没有这样的烦恼,发送很大很大的压缩包会受到限制,为此,想要在压缩过程中将文件拆分为几个压缩包并且同时为所有压缩包设置加密应该如何设置?方法一:使用7-Zip免费且强大的文件管理工具7...

高效处理 RAR 分卷压缩包:合并解压操作全攻略

在文件传输和存储过程中,当遇到大文件时,我们常常会使用分卷压缩的方式将其拆分成多个较小的压缩包,方便存储和传输。RAR作为一种常见的压缩格式,分卷压缩包的使用频率也很高。但很多人在拿到RAR分卷...

2个方法教你如何删除ZIP压缩包密码

zip压缩包设置了加密密码,每次解压文件都需要输入密码才能够顺利解压出文件,当压缩包文件不再需要加密的时候,大家肯定想删除压缩包密码,或是忘记了压缩包密码,想要通过删除操作将压缩包密码删除,就能够顺利...

速转!漏洞预警丨压缩软件Winrar目录穿越漏洞

WinRAR是一款功能强大的压缩包管理器,它是档案工具RAR在Windows环境下的图形界面。该软件可用于备份数据,缩减电子邮件附件的大小,解压缩从Internet上下载的RAR、ZIP及其它类...

文件解压方法和工具分享_文件解压工具下载

压缩文件减少文件大小,降低文件失效的概率,总得来说好处很多。所以很多文件我们下载下来都是压缩软件,很多小伙伴不知道怎么解压,或者不知道什么工具更好,所以今天做了文件解压方法和工具的分享给大家。一、解压...

[python]《Python编程快速上手:让繁琐工作自动化》学习笔记3

1.组织文件笔记(第9章)(代码下载)1.1文件与文件路径通过importshutil调用shutil模块操作目录,shutil模块能够在Python程序中实现文件复制、移动、改名和删除;同时...

Python内置tarfile模块:读写 tar 归档文件详解

一、学习目标1.1学习目标掌握Python内置模块tarfile的核心功能,包括:理解tar归档文件的原理与常见压缩格式(gzip/bz2/lzma)掌握tar文件的读写操作(创建、解压、查看、过滤...

使用python展开tar包_python拓展

类Unix的系统,打包文件经常使用的就是tar包,结合zip工具,可以方便的打包并解压。在python的标准库里面有tarfile库,可以方便实现生成了展开tar包。使用这个库最大的好处,可能就在于不...

银狐钓鱼再升级:白文件脚本化实现GO语言后门持久驻留

近期,火绒威胁情报中心监测到一批相对更为活跃的“银狐”系列变种木马。火绒安全工程师第一时间获取样本并进行分析。分析发现,该样本通过阿里云存储桶下发恶意文件,采用AppDomainManager进行白利...

ZIP文件怎么打开?2个简单方法教你轻松搞定!

在日常工作和生活中,我们经常会遇到各种压缩文件,其中最常见的格式之一就是ZIP。ZIP文件通过压缩数据来减少文件大小,方便我们进行存储和传输。然而,对于初学者来说,如何打开ZIP文件可能会成为一个小小...

Ubuntu—解压多个zip压缩文件.zip .z01 .z02

方法将所有zip文件放在同一目录中:zip_file.z01,zip_file.z02,zip_file.z03,...,zip_file.zip。在Zip3.0版本及以上,使用下列命令:将所有zi...

如何使用7-Zip对文件进行加密压缩

7-Zip是一款开源的文件归档工具,支持多种压缩格式,并提供了对压缩文件进行加密的功能。使用7-Zip可以轻松创建和解压.7z、.zip等格式的压缩文件,并且可以通过设置密码来保护压缩包中的...

取消回复欢迎 发表评论: