首先安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
设置阿里云镜像源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装 Docker-CE
yum -y install docker-ce
systemctl stop firewalld.service
setenforce 0
systemctl start docker.service
systemctl enable docker.service
设置阿里云镜像加速
1:打开阿里云官网—》搜索“镜像加速”
2:选择镜像加速器
3:针对centos操作:
1.安装/升级Docker客户端
推荐安装1.10.0以上版本的Docker客户端,参考文档 docker-ce
2.配置镜像加速器
针对Docker客户端版本大于 1.10.0 的用户
您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://z6o7pl02.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload //加载所有配置
systemctl restart docker //重启服务
做网络加速,开启路由转发
[root@server1 ~]# vi /etc/sysctl.conf
[root@server1 ~]# sysctl -p
net.ipv4.ip_forward = 1
[root@server1 ~]# systemctl restart docker
查看docker版本
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 19.03.13
API version: 1.40
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:03:45 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.13
API version: 1.40 (minimum version 1.12)
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:02:21 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.3.7
GitCommit: 8fba4e9a7d01810a393d5d25a3621dc101981175
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
搜素在线的某些镜像
[root@localhost ~]# docker search nginx
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 14063 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1912 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 795 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 131
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 115
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 105 [OK]
bitnami/nginx Bitnami nginx Docker Image 90 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 80 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 72 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 63 [OK]
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 45
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 40 [OK]
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 20
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 15
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 15
staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 14 [OK]
raulr/nginx-wordpress Nginx front-end for the official wordpress:f… 13 [OK]
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
mailu/nginx Mailu nginx frontend 8 [OK]
bitwarden/nginx The Bitwarden nginx web server acting as a r… 7
flashspys/nginx-static Super Lightweight Nginx Image 7 [OK]
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 6 [OK]
wodby/nginx Generic nginx 1 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 1 [OK]
下载在线镜像
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
852e50cd189d: Pull complete
571d7e852307: Pull complete
addb10abd9cb: Pull complete
d20aa7ccdb77: Pull complete
8b03f1e11359: Pull complete
Digest: sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
查看已有的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx new 570aaf110e72 3 hours ago 537MB
<none> <none> b4969f4efc5f 4 hours ago 215MB
nginx latest bc9a0695f571 2 days ago 133MB
centos 7 8652b9f0cb4c 13 days ago 204MB
centos latest 0d120b6ccaa8 3 months ago 215MB
paigeeworld/centos7 latest 4cbe7aa905e7 6 years ago 382MB
删除镜像:
查看所有镜像:
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
compose_nginx_nginx latest 7fb1eb8d06a3 About an hour ago 537MB
nginx new 570aaf110e72 3 days ago 537MB
<none> <none> b4969f4efc5f 3 days ago 215MB
nginx latest bc9a0695f571 5 days ago 133MB
centos 7 8652b9f0cb4c 2 weeks ago 204MB
centos latest 0d120b6ccaa8 3 months ago 215MB
paigeeworld/centos7 latest 4cbe7aa905e7 6 years ago 382MB
删除镜像:
[root@localhost ~]# docker rmi 570aaf110e72
Untagged: nginx:new
Deleted: sha256:570aaf110e72fdc8fecd79490f816f0cc38faef2bfcc7de680d7964156126c81
Deleted: sha256:76e9292f252055579770336c622a54290cc04c01a3854a7300e16189efe03a08
Deleted: sha256:c2671a7575833a9d723620e4fc25c11e47a2375a2eaa6b4ed755c81b1a7e2ad1
Deleted: sha256:d816936d9c2e0ac9a96f49f796cf66b3e58326693d907d805b0c12b94ea3dfba
Deleted: sha256:0f3c685ca034df036cc2d5618380376379b876ac885913dacd341c7729b44815
添加镜像标签:
[root@localhost ~]# docker tag nginx:latest nginx:yangzihan
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
compose_nginx_nginx latest 7fb1eb8d06a3 About an hour ago 537MB
<none> <none> b4969f4efc5f 3 days ago 215MB
nginx latest bc9a0695f571 5 days ago 133MB
nginx yangzihan bc9a0695f571
导出镜像
[root@localhost ~]# docker save -o /opt/nginx nginx:latest
[root@localhost ~]# ls /opt/
containerd nginx rh
载入镜像
[root@localhost ~]# docker load < /opt/nginx
Loaded image: nginx:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
compose_nginx_nginx latest 7fb1eb8d06a3 2 hours ago 537MB
<none> <none> b4969f4efc5f 3 days ago 215MB
nginx latest bc9a0695f571 5 days ago 133MB
nginx yangzihan bc9a0695f571 5 days ago 133MB
centos 7 8652b9f0cb4c 2 weeks ago 204MB
centos latest 0d120b6ccaa8 3 months ago 215MB
paigeeworld/centos7 latest 4cbe7aa905e7 6 years ago 382MB
容器操作
容器创建
[root@localhost ~]# docker create -it nginx:latest /bin/bash
f075c5779cc38f8e194bd1adc36700447ea3257f2af43541bfcf5bd4d17d9e14
查看容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f075c5779cc3 nginx:latest "/docker-entrypoint.…" 2 minutes ago Created quirky_rubin
7b0f72d38fcb compose_nginx_nginx "/run.sh" 2 hours ago Up 2 hours 0.0.0.0:1216->80/tcp, 0.0.0.0:1218->443/tcp compose_nginx_nginx_1
启动容器
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f075c5779cc3 nginx:latest "/docker-entrypoint.…" 5 minutes ago Up 8 seconds 80/tcp quirky_rubin
7b0f72d38fcb compose_nginx_nginx "/run.sh" 2 hours ago Up 2 hours 0.0.0.0:1216->80/tcp, 0.0.0.0:1218->443/tcp compose_nginx_nginx_1
RUN创建与启动同步
这里可以看见,当没有镜像时运行可以下载镜像
进入容器
[root@localhost ~]# docker exec -it babac4f85630 /bin/bash
root@babac4f85630:/usr/local/tomcat# exit
exit //退出容器
安装网络工具
[root@localhost ~]# yum -y install net-tools
Loaded plugins: fastestmirror, langpacks
删除容器
[root@localhost ~]# docker stop babac4f85630 //先停止
babac4f85630
[root@localhost ~]# docker rm babac4f85630 //再删除
babac4f85630
批量删除容器
[root@localhost ~]# docker ps -a | awk '{print "docker rm "$1"}' | bash
awk: cmd. line:1: {print "docker rm "$1"}
awk: cmd. line:1: ^ unterminated string
awk: cmd. line:1: {print "docker rm "$1"}
awk: cmd. line:1: ^ syntax error
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f075c5779cc3 nginx:latest "/docker-entrypoint.…" 27 minutes ago Up 22 minutes 80/tcp quirky_rubin
7b0f72d38fcb compose_nginx_nginx "/run.sh" 2 hours ago Up 2 hours 0.0.0.0:1216->80/tcp, 0.0.0.0:1218->443/tcp compose_nginx_nginx_1
#未停止的容器删不了