Docker 是一种开源的容器化平台,它可以帮助开发者将应用程序及其依赖项打包成一个可移植的容器。Docker 容器是一个轻量级的独立环境,可以在任何支持 Docker 的系统上运行,例如本地开发机、云服务器或者数据中心。
Docker 的容器化技术解决了传统虚拟化技术的缺陷,通过共享操作系统内核来节省系统资源,同时提供更快的启动速度和更高的运行效率。Docker 容器还可以通过 Docker Hub 这个中央仓库来方便地分享和管理,可以让用户快速地获取和部署各种应用程序和服务。
本文介绍如何快速搭载Docker CE环境
1 安装Docker的依赖库 1.1 安装Docker的依赖库 1 [root@youfeng ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
1.2 添加Docker CE的软件源信息 1 [root@youfeng ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
1.3 安装Docker CE 1 2 [root@youfeng ~]# yum makecache fast [root@youfeng ~]# yum -y install docker-ce
1.4 启动Docker服务 1 2 3 [root@youfeng ~]# systemctl start docker # 设置Docker服务开机自启(可选) # systemctl enable docker
2 配置阿里云镜像仓库(镜像加速) Docker的默认官方远程仓库是hub.docker.com,由于“墙”的原因,没有科学上网的小伙伴下载一个Docker官方镜像可能会需要很长的时间,甚至下载失败。为此,阿里云容器镜像服务ACR提供了官方的镜像站点,从而加速官方镜像的下载。下面介绍如何使用阿里云镜像仓库。
2.1 登录阿里云镜像服务控制台 2.2 点击 [镜像工具] > [镜像加速器],可以看到阿里云为您提供了一个专属的镜像加速地址。 选择好下方操作文档中的系统配置文档,例如我使用的CentOS系统配置文档: 1 2 3 4 5 6 7 8 sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://******.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
复制至终端输入完成修改配置。
可以输入以下命令确认是否修改完成
1 2 3 4 [root@youfeng ~]# nl /etc/docker/daemon.json 1 { 2 "registry-mirrors": ["https://******.mirror.aliyuncs.com"] 3 }
如与上述配置文档地址一致,则修改完成
3 Docker基本使用 3.1 Docker的启动、停止、重启 3.1.1 查看Docker状态 1 [root@youfeng ~]# systemctl status docker
1 2 3 4 5 6 7 8 9 10 11 12 # 运行状态 [root@youfeng ~]# systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled) Active: active (running) since Wed 2023-04-05 13:04:18 CST; 1s ago Docs: https://docs.docker.com Main PID: 2301 (dockerd) Tasks: 7 Memory: 27.0M CGroup: /system.slice/docker.service └─2301 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
1 2 3 4 5 6 7 8 9 # 停止状态 [root@youfeng ~]# systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled) Active: inactive (dead) since Wed 2023-04-05 13:06:45 CST; 7s ago Docs: https://docs.docker.com Process: 2522 ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock (code=exited, status=0/SUCCESS) Main PID: 2522 (code=exited, status=0/SUCCESS)
3.1.2 启动Docker服务 1 [root@youfeng ~]# systemctl start docker
3.1.3 停止Docker服务 1 [root@youfeng ~]# systemctl stop docker
3.1.4 重启Docker服务 1 [root@youfeng ~]# systemctl restart docker
3.1.5 设置docker服务开机自启 1 [root@youfeng ~]# systemctl enable docker
3.2 Docker镜像 3.2.1 docker search [image]搜索镜像仓库 例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 [root@youfeng ~]# docker search mariadb NAME DESCRIPTION STARS OFFICIAL AUTOMATED mariadb MariaDB Server is a high performing open sou… 5340 [OK] phpmyadmin phpMyAdmin - A web interface for MySQL and M… 774 [OK] mariadb/server MariaDB Server is a modern database for mode… 51 [OK] mariadb/maxscale MariaDB MaxScale - The world's most advanced… 28 [OK] bitnami/mariadb Bitnami MariaDB Docker Image 175 [OK] circleci/mariadb CircleCI images for MariaDB 4 [OK] mariadb/xpand-single Single node MariaDB Xpand docker image, for … 3 bitnami/mariadb-galera 19 ibmcom/mariadb Docker Image for IBM Cloud Private-CE (Commu… 1 cimg/mariadb 0 rapidfort/mariadb RapidFort optimized, hardened image for Mari… 15 rapidfort/mariadb-ib RapidFort optimized, hardened image for Mari… 0 linuxserver/mariadb A Mariadb container, brought to you by Linux… 347 vmware/mariadb-photon 0 rapidfort/mariadb-official RapidFort optimized, hardened image for Mari… 0 ibmcom/mariadb-ppc64le Docker Image for IBM Cloud Private-CE (Commu… 1 ibmcom/mariadb-s390x 0 clearlinux/mariadb MariaDB relational database management syste… 3 [OK] bitnamicharts/mariadb 0 bitnamicharts/mariadb-galera 0 docksal/mariadb MariaDB service images for Docksal - https:/… 0 uselagoon/mariadb 0 blackflysolutions/mariadb Mariadb for Drupal/CiviCRM 0 [OK] drud/mariadb-local ddev mariadb local container 0 uselagoon/mariadb-drupal
建议选择STARS数较多的镜像
3.2.2 docker pull [image]拉取服务器镜像仓库里的镜像 例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [root@youfeng ~]# docker pull mariadb Using default tag: latest latest: Pulling from library/mariadb 7b1a6ab2e44d: Pull complete 034655750c88: Pull complete f0b757a2a0f0: Pull complete 4bbcce26bc5e: Pull complete 04f220ee9266: Pull complete 89c8a77f7842: Pull complete d1de5652303b: Pull complete ef669123e59e: Pull complete e5cec468d3a6: Pull complete b14b1ba1d651: Pull complete Digest: sha256:0f04ae6f30c5a3295fb7cc9be5780c15ff21d6028f999b19f5803114c1e8559e Status: Downloaded newer image for mariadb:latest docker.io/library/mariadb:latest
在不指定版本的情况下,默认优先下载最新版本。
3.2.3 docker push [image] 推送本地镜像到服务器 非个人仓库需要身份验证
3.2.4 docker images 查看本地镜像 1 2 3 [root@youfeng ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mariadb latest e2278f24ac88 17 months ago 410MB
3.2.5 docker images -a 查看本地所有镜像(包含历史) 1 2 3 [root@youfeng ~]# docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE mariadb latest e2278f24ac88 17 months ago 410MB
3.2.6 docker rmi [image]/[imageID] 删除镜像(可多选删除) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 # 删除redis和mariadb镜像 [root@youfeng ~]# docker rmi redis mariadb Untagged: redis:latest Untagged: redis@sha256:db485f2e245b5b3329fdc7eff4eb00f913e09d8feb9ca720788059fdc2ed8339 Deleted: sha256:7614ae9453d1d87e740a2056257a6de7135c84037c367e1fffa92ae922784631 Deleted: sha256:49c70179bc923a7d48583d58e2b6c21bde1787edf42ed1f8de9e9b96e2e88e65 Deleted: sha256:396e06df5d1120368a7a8a4fd1e5467cdc2dd4083660890df078c654596ddc1c Deleted: sha256:434d118df2e9edb51238f6ba46e9efdfa21be68e88f54787531aa39a720a0740 Deleted: sha256:2047f09c412ff06f4e2ee8a25d105055e714d99000711e27a55072e640796294 Deleted: sha256:13d71c9ccb39b206211dd1900d06aa1984b0f5ab8abaa628c70b3eb733303a65 Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f Untagged: mariadb:latest Untagged: mariadb@sha256:0f04ae6f30c5a3295fb7cc9be5780c15ff21d6028f999b19f5803114c1e8559e Deleted: sha256:e2278f24ac88b82f98ef58de4bf15c0b01df3de2f1fe835e2ea4350282d58700 Deleted: sha256:11e9bf96045889878ecb28fc7431af359878f2fa0bc9fcfd7a21de09feab2385 Deleted: sha256:3331ebe71406c0131f5a462951b87ad96d80d3d07ec309a2b40c8bbc6062e0ce Deleted: sha256:98fef66422fa8c83ef6cd25f69bf3c1df1115b91c77cfdc1c0b82439fa4b48b8 Deleted: sha256:74481f47fb22da394bea6c7b0707f2fd01e888ba16c4f48c4805803d7f32830b Deleted: sha256:ec96296944cc11a30b365e74768b5e8726e93cc2ab755a0ead282c66513591d5 Deleted: sha256:9ef89e12d1987b0b7d8081e1ecb9e868d00cc98b8783d816024f77fae996ce87 Deleted: sha256:fe65ce8f5cef9bd16097dd6b9b2484e6710a2ea3c5c2e948f16ceff5002d3c3f Deleted: sha256:715a1b962166ede06c7a0e87d068a4b686e6066e0eca5ecab6f4d6cfab2121fe Deleted: sha256:97ab3baee34d0c75ee10e65c63a06cbc87d20d695c17d14ad565d4ff1b8dc2ca Deleted: sha256:9f54eef412758095c8079ac465d494a2872e02e90bf1fb5f12a1641c0d1bb78b
3.2.7 定制个人镜像 在所需定制镜像目录下编写dockerfile,在该目录使用如下命令
1 docker build -t [image-name] .
请注意!( . ) 不可缺省,( . )实际上是在指定上下文的目录,docker build 命令会将该目录下的内容打包交给 Docker 引擎以帮助构建镜像。
3.3 Docker容器 3.3.1 docker run 可选参数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 -d, --detach=false 指定容器运行于前台还是后台,默认为false -i, --interactive=false 打开STDIN,用于控制台交互 -t, --tty=false 分配tty设备,该可以支持终端登录,默认为false -u, --user="" 指定容器的用户 -a, --attach=[] 标准输入输出流和错误信息(必须是以非docker run -d启动的容器) -w, --workdir="" 指定容器的工作目录 -c, --cpu-shares=0 设置容器CPU权重,在CPU共享场景使用 -e, --env=[] 指定环境变量,容器中可以使用该环境变量 -m, --memory="" 指定容器的内存上限 -P, --publish-all=false 指定容器暴露的端口 -p, --publish=[] 指定容器暴露的端口 -h, --hostname="" 指定容器的主机名 -v, --volume=[] 给容器挂载存储卷,挂载到容器的某个目录 --volumes-from=[] 给容器挂载其他容器上的卷,挂载到容器的某个目录 --name="" 指定容器名字,后续可以通过名字进行容器管理,links特性需要使用名字 ......以上仅为部分,详情可见官网
例如:
1 [root@youfeng ~]# docker run -d --name mymariadb -p 3306:3306 --restart=always -v /data/mysql/data:/var/lib/mysql -e MARIADB_ROOT_PASSWORD='you_password' mariadb
说明:如果需要 command 命令,写在 image(mariadb) 后面。
-d: 容器可以在后台运行
-p:把容器的3306端口映射到宿主机3306上
-v:主机的目录/data/mysql/data映射到容器的目录/var/lib/mysql
--name:给容器起个名字mymariadb,mariadb是你下载的镜像
--restart=always 容器退出时总是重启
-e 设置数据库密码作为容器密码
执行结果:
1 2 3 4 [root@youfeng ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2bc8e3987c69 mariadb "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp mymariadb
3.3.2 docker ps (-a) 查看docker容器 1 2 3 4 5 # docker ps 查看正在运行的容器 [root@youfeng ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2bc8e3987c69 mariadb "docker-entrypoint.s…" 10 minutes ago Up 10 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp mariadb
1 2 3 4 5 6 # docker ps -a 查看所有docker容器(包含历史) [root@youfeng ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bef22621b3f1 redis "docker-entrypoint.s…" 58 seconds ago Exited (0) 40 seconds ago myredis 2bc8e3987c69 mariadb "docker-entrypoint.s…" 10 minutes ago Up 10 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp mariadb
3.3.2 启动Docker容器 1 2 3 4 # docker start [CONTAINER ID/NAMES] [root@youfeng ~]# docker start myredis myredis
3.3.3 停止Docker容器 1 2 3 4 # docker stop [CONTAINER ID/NAMES] [root@youfeng ~]# docker stop myredis myredis
3.3.4 重启Docker容器 1 2 3 4 # docker restart [CONTAINER ID/NAMES] [root@youfeng ~]# docker restart myredis myredis
3.3.5 删除Docker容器 1 2 3 4 # docker rm [CONTAINER ID/NAMES] [root@youfeng ~]# docker rm myredis myredis
3.3.6 删除所有容器 1 2 3 4 5 # docker rm $(docker ps -a -q) [root@youfeng ~]# docker rm $(docker ps -a -q) 6fbeba914249 2bc8e3987c69
3.3.7 重命名容器 1 2 3 # docker rename 原容器名 新容器名 [root@youfeng ~]# docker rename redis myredis
3.3.8 进入Docker容器 1 2 3 4 5 6 7 8 9 10 # 进入容器 docker exec -it mariadb /bin/bash # 管理员权限进入容器 [root@youfeng ~]# docker exec -it -u root mariadb /bin/bash # 退出容器方式: # ctrl+d 退出容器且关闭 # ctrl+p+q 退出容器但不关闭
3.3.9 查看Docker容器日志 1 2 3 4 5 6 7 8 9 10 11 12 13 # docker logs -f -t --tail 行数 容器 [root@youfeng ~]# docker logs -f -t --tail 10 mariadb 2023-04-05T07:53:13.481298493Z 2023-04-05 7:53:13 0 [Note] InnoDB: 10.6.5 started; log sequence number 42479; transaction id 14 2023-04-05T07:53:13.481300979Z 2023-04-05 7:53:13 0 [Note] Plugin 'FEEDBACK' is disabled. 2023-04-05T07:53:13.481816311Z 2023-04-05 7:53:13 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool 2023-04-05T07:53:13.486927028Z 2023-04-05 7:53:13 0 [Warning] You need to use --log-bin to make --expire-logs-days or --binlog-expire-logs-seconds work. 2023-04-05T07:53:13.486940811Z 2023-04-05 7:53:13 0 [Note] Server socket created on IP: '0.0.0.0'. 2023-04-05T07:53:13.486945002Z 2023-04-05 7:53:13 0 [Note] Server socket created on IP: '::'. 2023-04-05T07:53:13.486948661Z 2023-04-05 7:53:13 0 [Warning] 'proxies_priv' entry '@% root@2bc8e3987c69' ignored in --skip-name-resolve mode. 2023-04-05T07:53:13.489189538Z 2023-04-05 7:53:13 0 [Note] InnoDB: Buffer pool(s) load completed at 230405 7:53:13 2023-04-05T07:53:13.489377322Z 2023-04-05 7:53:13 0 [Note] mariadbd: ready for connections. 2023-04-05T07:53:13.489384388Z Version: '10.6.5-MariaDB-1:10.6.5+maria~focal' socket: '/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
3.4 Docker数据拷贝 宿主机和容器之间的拷贝操作使用 docker cp 命令,并且无论容器是否启动都生效
3.4.1 文件从宿主机拷贝到容器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 # docker cp 宿主机文件路径 容器名:存放路径 [root@youfeng home]# echo "some mesage" > youfeng.txt [root@youfeng home]# docker cp /home/youfeng.txt mariadb:/home/ Successfully copied 2.048kB to mariadb:/home/ # 拷贝结果 [root@youfeng home]# docker exec -it mariadb bash root@0f9901c24b30:/# cd /home root@0f9901c24b30:/home# ls youfeng.txt root@0f9901c24b30:/home# nl youfeng.txt 1 some mesage
3.4.2 文件从容器拷贝到宿主机 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # docker cp 容器名:要拷贝的文件路径 宿主机存放路径 [root@youfeng home]# docker exec -it mariadb bash root@0f9901c24b30:/home# echo "new message" > youfeng_new.txt root@0f9901c24b30:/home# exit exit [root@youfeng home]# docker cp mariadb:/home/youfeng_new.txt /home/ Successfully copied 2.048kB to /home/ # 拷贝结果 [root@youfeng home]# ls youfeng_new.txt youfeng.txt [root@youfeng home]# nl youfeng_new.txt 1 new message