一、centos安装docker
本实验是以centos操作系统。
bash
# 移除旧版本docker
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
# 配置docker yum源。
sudo yum install -y yum-utils
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 安装 最新 docker
sudo yum install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
# 启动& 开机启动docker; enable + start 二合一
systemctl enable docker --now
# 配置加速
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://mirror.ccs.tencentyun.com"
]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
# 使用命令查看是否正确启动
docker ps
二、使用docker部署nginx
使用docker部署nginx,修改主页,进行发布。
(1)下载镜像
相关命令
bash
# 检索
docker search nginx # 这个命令我在腾讯云服务器上执行,获取不到结果,弄了半天也没弄好
# 下载
docker pull
# 列表
docker images
# 删除
docker rmi
(2)启动容器
相关命令
bash
# 运行
docker run
# 查看
docker ps -a
# 停止
docker stop
# 启动
docker start
# 重启
docker restart
# 状态
docker stats
# 日志
docker logs
# 进入
docker exec
# 删除
docker rm
# 后台启动
docker run -d --name mynginx -p 80:80 nginx
# 交互进入容器
docker exec -it mynginx bash
# 进入到指定目录
cd /usr/share/nginx/html
# 修改index.html
(3)保存镜像
相关命令
bash
# 提交
docker commit
# 保存
docker save -o mynginx.tar mynginx:v1.0
# 加载
docker load -i mynginx.tar
(4)分享社区
bash
# 登录
docker login
# 打标签
docker tag oldname yourname/newname
# 推送
docker push yourname/newname
使用卷映射启动
bash
docker run -d --name app03 -p 90:80 -v ngconf:/etc/nginx -v /app/nghtml:/usr/share/nginx/html nginx
使用卷映射,可以避免在使用目录映射时,由于外部目录为空,导致映射到容器内部也为空,使得容器启动失败。当然,可以先在外部建立好相关映射目录和文件,再使用目录映射。另外一种方法是使用卷映射,卷映射也是使用-v参数,但是不同点是卷映射不以"/"开头,而是直接使用自定义的卷名。映射之后,这个卷名在外部/var/lib/docker/volumes目录下。可以理解为使用目录映射时,容器内部以外部目录为准,使用卷映射时,外部目录以容器内部目录为准。
查看所有卷:
bash
docker volume ls
查看卷的详情
bash
docker volume inspect volumename
查看某个容器的细节
bash
docker container inspect [app01]
通过上面的命令,可以看到指定容器在内部的网络地址。
创建网络
bash
docker network create mynet
列出网络
bash
docker network ls
启动容器,加入自定义网络
bash
docker run -d -p 80:80 --name app02 --network mynet nginx
绑定自定义的网络之后,就可以使用容器名进行稳定的访问
bash
# 进入容器
docker exec -it app02 bash
curl http://app02:80
三、redis主从同步集群
使用如下镜像bitnima/redis
使用bitnami创建容器创建主节点master:
bash
docker run -d -p 6379:6379 -v /app/rd1:/bitnami/redis/data -e REDIS_REPLICATION_MODE=master -e REDIS_PASSWORD=123456 --network mynet --name redis01 bitnami/redis
创建完之后,使用docker ps -a 发现没有正常运行
bash
[root@VM-24-5-centos ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
50cdd220d951 bitnami/redis "/opt/bitnami/script..." 48 seconds ago Exited (1) 47 seconds ago redis01
981cb63c6752 mynginx:v1.0 "/docker-entrypoint...." 45 minutes ago Up 45 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp app02
查看容器的日志
bash
[root@VM-24-5-centos ~]# docker logs 50c
redis 16:20:05.04 INFO ==>
redis 16:20:05.04 INFO ==> Welcome to the Bitnami redis container
redis 16:20:05.04 INFO ==> Subscribe to project updates by watching https://github.com/bitnami/containers
redis 16:20:05.04 INFO ==> Submit issues and feature requests at https://github.com/bitnami/containers/issues
redis 16:20:05.04 INFO ==> Upgrade to Tanzu Application Catalog for production environments to access custom-configured and pre-packaged software components. Gain enhanced features, including Software Bill of Materials (SBOM), CVE scan result reports, and VEX documents. To learn more, visit https://bitnami.com/enterprise
redis 16:20:05.04 INFO ==>
redis 16:20:05.04 INFO ==> ** Starting Redis setup **
redis 16:20:05.06 INFO ==> Initializing Redis
redis 16:20:05.06 INFO ==> Setting Redis config file
redis 16:20:05.08 INFO ==> Configuring replication mode
redis 16:20:05.10 INFO ==> ** Redis setup finished! **
redis 16:20:05.11 INFO ==> ** Starting Redis **
1:C 27 Sep 2024 16:20:05.126 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:C 27 Sep 2024 16:20:05.126 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 27 Sep 2024 16:20:05.126 * Redis version=7.4.0, bits=64, commit=00000000, modified=1, pid=1, just started
1:C 27 Sep 2024 16:20:05.126 * Configuration loaded
1:M 27 Sep 2024 16:20:05.126 * monotonic clock: POSIX clock_gettime
1:M 27 Sep 2024 16:20:05.127 * Running mode=standalone, port=6379.
1:M 27 Sep 2024 16:20:05.127 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 27 Sep 2024 16:20:05.127 * Server initialized
1:M 27 Sep 2024 16:20:05.127 # Can't open or create append-only dir appendonlydir: Permission denied
大概的意思就是所映射的文件夹该容器没有权限访问,将映射到主机的目录的权限进行修改。
bash
chmod -R 777 /app
重新启动
bash
docker restart 50c
再次查看,发现容器正常启动了。
接下来,启动从节点slave:
bash
docker run -d -p 6380:6379 -v /app/rd1:/bitnami/redis/data -e REDIS_REPLICATION_MODE=slave -e REDIS_MASTER_HOST=redis01 -e REDIS_MASTER_PORT_NUMBER=6379 -e REDIS_MASTER_PASSWORD=123456 -e REDIS_PASSWORD=123456 --network mynet --name redis02 bitnami/redis
bash
[root@VM-24-5-centos ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b3e51d271341 bitnami/redis "/opt/bitnami/script..." 27 seconds ago Up 26 seconds 0.0.0.0:6380->6379/tcp, :::6380->6379/tcp redis02
50cdd220d951 bitnami/redis "/opt/bitnami/script..." 14 minutes ago Up 11 minutes 0.0.0.0:6379->6379/tcp, :::6379->6379/tcp redis01
981cb63c6752 mynginx:v1.0 "/docker-entrypoint...." 59 minutes ago Up 59 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp app02
然后使用vscode的redis插件或者redis客户端进行连接,如果使用腾讯云服务器进行连接,需要先开放对应的端口,否则链接失败。
向主机或者从机写入数据,另外一个会进行数据同步:
四、启动MySQL容器
bash
docker run -d -p 3306:3306 -v /app/myconf:/etc/mysql/conf.d -v /app/mydata:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0.37-debian
五、使用docker compose
下面是使用命令行的方式启动
bash
# 创建网络
docker network create blog
# 查看网络是否创建成功
docker network ls
# 批量删除之前的容器
docker rm -f $(docker ps -aq)
# 创建MySQL容器
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -e MYSQL_DATABASE
=wordpress -v mysql-data:/var/lib/mysql -v /app/myconf:/etc/mysql/conf.d --restart always --name my
sql --network blog mysql:8.0
# 启动WordPress
docker run -d -p 8080:80 -e WORDPRESS_DB_HOST=mysql -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=123456 -e WORDPRESS_DB_NAME=wordpress -v wordpress:/var/www/html --restart always --name wordpress-app --network blog wordpress:latest
浏览器访问ip:8080
配置完毕之后使用配置的账号和密码进入后台:
使用compose进行管理:
参考文档:Compose file reference | Docker Docs
使用compose的方式启动,编辑compose.yaml文件
bash
name: myblog
services:
mysql:
container_name: mysql
image: mysql:8.0
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=123456
- MYSQL_DATABASE=wordpress
volumes:
- mysql-data:/var/lib/mysql
- /app/myconf:/etc/mysql/conf.d
restart: always
networks:
- blog
wordpress:
image: wordpress
ports:
- "8080:80"
environment:
WORDPRESS_DB_HOST: mysql
WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: 123456
WORDPRESS_DB_NAME: wordpress
volumes:
- wordpress:/var/www/html
restart: always
networks:
- blog
depends_on:
- mysql
volumes:
mysql-data:
wordpress:
networks:
blog:
使用docker compose 批量启动
bash
# 删除运行的容器
docker rm -f $(docker ps -aq)
# 删除之前的卷
docker volume rm mysql-data wordpress
# 删除之前的网络
docker network rm blog
# 使用compose启动
docker compose -f compose.yaml up -d
# 输出如下
[+] Running 5/5
✔ Network myblog_blog Created 0.0s
✔ Volume "myblog_mysql-data" Created 0.0s
✔ Volume "myblog_wordpress" Created 0.0s
✔ Container mysql Started 0.3s
✔ Container myblog-wordpress-1 Started 0.5s
网络名和卷名都会拼接一个name作为前缀。如果没有给定容器名,则拼接后缀添加数字作为区分。另外,如果compose.yaml文件做了修改,那么再次使用上面的compose启动命令,compose会自动判断应用是否发生变化,如果发生变化才会进行更新,重新启动,如果没有变化,则保持原样。
使用compose关闭应用
bash
docker compose -f compose.yaml down
上述命令会关闭应用,并删除网络,但是不会删除卷,也就是数据会被保存。如果要移除镜像,并且删除卷,可以使用如下命令:
bash
docker compose -f compose.yaml down --rmi all -v
六、制作镜像Dockerfile
常用指令:
|------------|-----------|
| FROM | 指定镜像基础环境 |
| RUN | 运行自定义命令 |
| CMD | 容器启动命令或参数 |
| LABEL | 自定义标签 |
| EXPOSE | 指定暴露端口 |
| ENV | 环境变量 |
| ADD | 添加文件到镜像 |
| COPY | 复制文件到镜像 |
| ENTRYPOINT | 容器固定启动命令 |
| VOLUME | 容器数据卷 |
| USER | 指定用户和用户组 |
| WORKDIR | 指定默认工作目录 |
| ARG | 指定构建参数 |
查看镜像层级:
bash
docker image history mynginx:v1.0
IMAGE CREATED CREATED BY SIZE COMMENT
5227c77e8efb 36 hours ago nginx -g daemon off; 1.12kB update index.html
<missing> 6 weeks ago CMD ["nginx" "-g" "daemon off;"] 0B buildkit.dockerfile.v0
<missing> 6 weeks ago STOPSIGNAL SIGQUIT 0B buildkit.dockerfile.v0
<missing> 6 weeks ago EXPOSE map[80/tcp:{}] 0B buildkit.dockerfile.v0
<missing> 6 weeks ago ENTRYPOINT ["/docker-entrypoint.sh"] 0B buildkit.dockerfile.v0
<missing> 6 weeks ago COPY 30-tune-worker-processes.sh /docker-ent... 4.62kB buildkit.dockerfile.v0
<missing> 6 weeks ago COPY 20-envsubst-on-templates.sh /docker-ent... 3.02kB buildkit.dockerfile.v0
<missing> 6 weeks ago COPY 15-local-resolvers.envsh /docker-entryp... 336B buildkit.dockerfile.v0
<missing> 6 weeks ago COPY 10-listen-on-ipv6-by-default.sh /docker... 2.12kB buildkit.dockerfile.v0
<missing> 6 weeks ago COPY docker-entrypoint.sh / # buildkit 1.62kB buildkit.dockerfile.v0
<missing> 6 weeks ago RUN /bin/sh -c set -x && groupadd --syst... 113MB buildkit.dockerfile.v0
<missing> 6 weeks ago ENV DYNPKG_RELEASE=2~bookworm 0B buildkit.dockerfile.v0
<missing> 6 weeks ago ENV PKG_RELEASE=1~bookworm 0B buildkit.dockerfile.v0
<missing> 6 weeks ago ENV NJS_RELEASE=1~bookworm 0B buildkit.dockerfile.v0
<missing> 6 weeks ago ENV NJS_VERSION=0.8.5 0B buildkit.dockerfile.v0
<missing> 6 weeks ago ENV NGINX_VERSION=1.27.1 0B buildkit.dockerfile.v0
<missing> 6 weeks ago LABEL maintainer=NGINX Docker Maintainers <d... 0B buildkit.dockerfile.v0
<missing> 6 weeks ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 6 weeks ago /bin/sh -c #(nop) ADD file:a9a95cfab16803be0... 74.8MB