docker官网
解决了环境迁移的痛点
Install Docker Engine on CentOS
centos7.iso下载步骤如下图所示
访问 cento官网
如参考其他文档安装centos7无法访问外网如百度,切换网络连接为NAT模式即可,本机环境是连接wifi上外网非网线模式。
确保可以访问外网如百度。
第一步
sudo yum install -y yum-utils
第二步
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
效果图
可能存在的问题: 在设置阿里云的时候没有出现 repo saved to /etc/yum.repos.d/docker-ce.repo ,可能是网络环境问题,只能先使用官方文档提供的路径配置。
sudo yum install docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
sudo docker run hello-world
sudo systemctl status docker
重新启动虚拟机查看docker状态发现dead,所以需要设置开机自启动
sudo systemctl enable docker
sudo docker version
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:
app* Docker App (Docker Inc., v0.9.1-beta3)
builder Manage builds
buildx* Docker Buildx (Docker Inc., v0.7.1-docker)
config Manage Docker configs
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
scan* Docker Scan (Docker Inc., v0.12.0)
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.
To get more help with docker, check out our guides at https://docs.docker.com/go/guides/
镜像没有仓库名或没有标签的镜像
虚悬镜像的查看命令
docker images -f dangling=true
[root@lzy test]# docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--add-host list Add a custom host-to-IP mapping (host:ip)
-a, --attach list Attach to STDIN, STDOUT or STDERR
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device list Block IO weight (relative device weight) (default [])
--cap-add list Add Linux capabilities
--cap-drop list Drop Linux capabilities
--cgroup-parent string Optional parent cgroup for the container
--cgroupns string Cgroup namespace to use (host|private)
'host': Run the container in the Docker host's cgroup namespace
'private': Run the container in its own private cgroup namespace
'': Use the cgroup namespace as configured by the
default-cgroupns-mode option on the daemon (default)
--cidfile string Write the container ID to the file
--cpu-period int Limit CPU CFS (Completely Fair Scheduler) period
--cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota
--cpu-rt-period int Limit CPU real-time period in microseconds
--cpu-rt-runtime int Limit CPU real-time runtime in microseconds
-c, --cpu-shares int CPU shares (relative weight)
--cpus decimal Number of CPUs
--cpuset-cpus string CPUs in which to allow execution (0-3, 0,1)
--cpuset-mems string MEMs in which to allow execution (0-3, 0,1)
-d, --detach Run container in background and print container ID
--detach-keys string Override the key sequence for detaching a container
--device list Add a host device to the container
--device-cgroup-rule list Add a rule to the cgroup allowed devices list
--device-read-bps list Limit read rate (bytes per second) from a device (default [])
--device-read-iops list Limit read rate (IO per second) from a device (default [])
--device-write-bps list Limit write rate (bytes per second) to a device (default [])
--device-write-iops list Limit write rate (IO per second) to a device (default [])
--disable-content-trust Skip image verification (default true)
--dns list Set custom DNS servers
--dns-option list Set DNS options
--dns-search list Set custom DNS search domains
--domainname string Container NIS domain name
--entrypoint string Overwrite the default ENTRYPOINT of the image
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
--expose list Expose a port or a range of ports
--gpus gpu-request GPU devices to add to the container ('all' to pass all GPUs)
--group-add list Add additional groups to join
--health-cmd string Command to run to check health
--health-interval duration Time between running the check (ms|s|m|h) (default 0s)
--health-retries int Consecutive failures needed to report unhealthy
--health-start-period duration Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)
--health-timeout duration Maximum time to allow one check to run (ms|s|m|h) (default 0s)
--help Print usage
-h, --hostname string Container host name
--init Run an init inside the container that forwards signals and reaps processes
-i, --interactive Keep STDIN open even if not attached
--ip string IPv4 address (e.g., 172.30.100.104)
--ip6 string IPv6 address (e.g., 2001:db8::33)
--ipc string IPC mode to use
--isolation string Container isolation technology
--kernel-memory bytes Kernel memory limit
-l, --label list Set meta data on a container
--label-file list Read in a line delimited file of labels
--link list Add link to another container
--link-local-ip list Container IPv4/IPv6 link-local addresses
--log-driver string Logging driver for the container
--log-opt list Log driver options
--mac-address string Container MAC address (e.g., 92:d0:c6:0a:29:33)
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
--mount mount Attach a filesystem mount to the container
--name string Assign a name to the container
--network network Connect a container to a network
--network-alias list Add network-scoped alias for the container
--no-healthcheck Disable any container-specified HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj int Tune host's OOM preferences (-1000 to 1000)
--pid string PID namespace to use
--pids-limit int Tune container pids limit (set -1 for unlimited)
--platform string Set platform if server is multi-platform capable
--privileged Give extended privileges to this container
-p, --publish list Publish a container's port(s) to the host
-P, --publish-all Publish all exposed ports to random ports
--pull string Pull image before running ("always"|"missing"|"never") (default "missing")
--read-only Mount the container's root filesystem as read only
--restart string Restart policy to apply when a container exits (default "no")
--rm Automatically remove the container when it exits
--runtime string Runtime to use for this container
--security-opt list Security Options
--shm-size bytes Size of /dev/shm
--sig-proxy Proxy received signals to the process (default true)
--stop-signal string Signal to stop a container (default "SIGTERM")
--stop-timeout int Timeout (in seconds) to stop a container
--storage-opt list Storage driver options for the container
--sysctl map Sysctl options (default map[])
--tmpfs list Mount a tmpfs directory
-t, --tty Allocate a pseudo-TTY
--ulimit ulimit Ulimit options (default [])
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
--userns string User namespace to use
--uts string UTS namespace to use
-v, --volume list Bind mount a volume
--volume-driver string Optional volume driver for the container
--volumes-from list Mount volumes from the specified container(s)
-w, --workdir string Working directory inside the container
-it 代表以命令模式进行交互,类linux终端输入命令
ubuntu:latest 版本号可不加
/bin/bash 容器内执行/bin/bash命令
–name string 设置容器名称
names 列已经设置为myu1
退出后查看发现容器状态依旧在线。
-d, --detach Run container in background and print container ID 在后台运行容器并打印容器ID
docker run -it redis 前台交互启动redis不合适,希望后台启动就行。
docker run -d redis 后台启动redis
exec命令连接回容器后执行exit,容器b530xx仍然在线。
attachexec命令连接回容器后执行exit,容器b530x已经不在线。
docker cp 容器id:文件路径 目标路径
docker cp b69c9dedf711:a.txt /
docker export b69c9dedf711 > abcd.tar
删除容器 b69c9dedf711
docker rm -f b69c9dedf711
cat abcd.tar | docker import - atguigu/ubuntu:3.7
运行镜像3081c84a2fce 之前用于复制的a.txt仍然存在
docker run -it 3081c84a2fce /bin/bash
docker ps
docker run -it ubuntu /bin/bash
apt-get update
apt-get -y install vim
运行容器ubuntu,执行更新操作
测试vim命令,并安装vim
测试vim命令成功
当仓库名称为大写时,会提示报错,repository name must be lowercase,仓库名称必须为小写
[root@lzy test]# docker commit -m="add vim cmd" -a="lzyy" 72f20a4fd7a9 ubuntuPlus:1.1
invalid reference format: repository name must be lowercase
[root@lzy test]# docker commit -m="add vim cmd" -a="lzyy" 72f20a4fd7a9 ubuntuplus:1.1
查看ubuntuplus镜像是否提交成功
启动新镜像(带vim命令的ubuntu),测试vim功能发现成功
docker run -d -p 5000:5000 -v /zzyyuse/myregistry/:/tmp/registry --privileged=true registry
启动ubuntu,测试ifconfig发现并未安装,执行命令更新
docker run -it ubuntu /bin/bash
apt-get update
安装net-tools后测试ifconfig命令,发现启动容器并不包含该命令
apt-get install net-tools
ifconfig
ctrl p+q
docker ps
docker commit -m="ifconfig ubuntu" -a="lzy" e7c2dc6ee923 ifcubuntu:1.2
本机ip用ifconfig命令查看,注意替换ip
curl -XGET http://ip:5000/v2/_catalog
将增强镜像ifcubuntu:1.2命名为私服库可以接受的形式
docker tag ifcubuntu:1.2 192.168.17.128:5000/ifcubuntu:1.2
docker push上传镜像
docker push 192.168.17.128:5000/ifcubuntu:1.2
修改配置文件
vim /etc/docker/daemon.json
systemctl restart docker
systemctl status docker
docker push 192.168.17.128:5000/ifcubuntu:1.2
拉取前先把本地镜像删除后拉取私服库中镜像
docker rmi -f 192.168.17.128:5000/ifcubuntu:1.2
docker pull 192.168.17.128:5000/ifcubuntu:1.2
数据卷的作用:实现宿主机和容器内数据交互,重要数据持久化和备份。
docker run -it --privileged -v /tmp/host_data:/tmp/docker_data --name=u3 ubuntu
挂载容器数据卷,在目录下新建aaa.txt
宿主机对应路径下自动生成aaa.txt
宿主机新建bbb.txt并修改aaa.txt内容
容器内自动生成bbb.txt和aaa.txt内容已经发生变化
docker inspect 2ec6fedb22d4
停止容器u3
另一个终端操作宿主机新建ccc.txt
容器重新启动后进入查看,发现ccc.txt已经生成
rw = read write ro =read only
默认读写规则为rw,读写规则限制容器向宿主机操作方向,如果设置只读则容器不能创建文件。
docker run -it --privileged -v /tmp/host_data2:/tmp/docker_data2:ro --name=u4 ubuntu
宿主机新建x.txt并写入内容
容器内可以看到宿主机创建的文件x.txt和新增的内容
操作步骤按视频操作即可,不再赘述,其中坑是修改配置文件。
docker pull mysql:5.7
启动之前先查看linux系统中是否启动mysql,否则映射端口会失败
ps -ef|grep mysql
docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
docker exec -it fa84bb0b4e6e /bin/bash
调用ifconfig查看ip后在windows系统使用navicate连接
启动mysql并挂载容器数据卷
docker run -d -p 3306:3306 --privileged=true
-v /bf/mysql/log:/var/log/mysql
-v /bf/mysql/data:/var/lib/mysql
-v /bf/mysql/conf:/etc/mysql/conf.d
-e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7
show variables like 'character%';
存在问题:需要指定自定义配置文件,挂载容器数据卷保存数据
第一步 拉取redis镜像
docker pull redis:6.0.8
第二步 创建自定义配置文件并修改,配置文件内容下载任意linux redis并复制其中的配置文件
第三步 必须修改如下图的两部分
修改配置文件后启动redis
docker run -p 6379:6379 --name myredis4 --privileged=true
-v /bf/redis/redis.conf:/etc/redis/redis.conf
-v /bf/redis/data:/data
-d redis:6.2.6 redis-server /etc/redis/redis.conf
如果启动后docker ps没有对应容器,用docker logs容器id查询日志报错信息。
我的报错原因是 之前下载的6.0.8版本,但是我的redis.conf是从6.2.6的redis中复制过来的,跨版本
的时候会有bug,将docker pull redis:6.2.6版本,或者从对应版本中取得配置文件。
修改前测试命令
退出后修改宿主机配置文件的databases = 10
重新启动redis后测试命令,发现配置文件修改成功
docker run --name docker_nginx -p 80:80 -d nginx