# 启动 docker 服务
systemctl start docker
systemctl enable docker
# 查看当前系统 Docker 信息
docker info
# 拉取 docker 镜像
docker pull image_name
# 从 Docker hub 上下载某个镜像
docker pull centos:latest
# 查看宿主机上的镜像,Docker 镜像保存在 / var/lib/docker 目录下:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 1b30b36ae96a 8 days ago 372MB
zabbix/zabbix-web-nginx-mysql centos-4.0-latest 8be5f91b2fa1 3 weeks ago 415MB
zabbix/zabbix-server-mysql centos-4.0-latest 8e5becf45c4e 3 weeks ago 326MB
# 删除镜像
docker rmi image_name/image_id
docker rmi zabbix/zabbix-web-nginx-mysql:centos-4.0-latest 或者 docker rmi 8be5f91b2fa1
# 查看当前有哪些容器正在运行
docker ps
# 查看所有容器
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b30307ad65be zabbix/zabbix-web-nginx-mysql:centos-4.0-latest "docker-entrypoint.sh" 7 days ago Exited (255) 8 minutes ago 443/tcp, 0.0.0.0:8080->80/tcp zabbix-web-nginx-mysql
0ad822cd52b7 zabbix/zabbix-server-mysql:centos-4.0-latest "docker-entrypoint.sh" 7 days ago Exited (255) 8 minutes ago 0.0.0.0:10051->10051/tcp zabbix-server-mysql
d01c89a112f7 mysql:5.7 "docker-entrypoint.s…" 7 days ago Exited (255) 8 minutes ago 3306/tcp, 33060/tcp mysql-server
# 启动、停止、重启容器命令:
docker start container_name/container_id
docker stop container_name/container_id
docker restart container_name/container_id
# 后台启动一个容器后,如果想进入到这个容器,可以使用 attach
docker attach container_name/container_id
# 删除容器
docker rm container_name/container_id
# 查看容器日志
docker logs -f container_name/container_id
# 查看容器 IP 地址
docker inspect container_name/container_id
# 进入容器
docker exec -it container_name/container_id bash
# 从 Docker 容器与宿主机相互传输文件
[root@localhost tmp]# docker cp --help
Usage: docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
Copy files/folders between a container and the local filesystem
docker cp zabbix_config.sql mysql-server:/tmp
docker cp mysql-server:/tmp/zabbix_config.sql /tmp
# 批量删除所有已经退出的容器
docker rm -v $(docker ps -aq -f status=exited)
# docker help
[root@centos7lined1 wangao]# docker help
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-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:
config Manage Docker configs
container Manage containers
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
使用USTC源加速
地址:https://docker.mirrors.ustc.edu.cn/
说明:Docker Hub 镜像缓存
使用说明
Linux
对于使用 upstart 的系统(Ubuntu 14.04、Debian 7 Wheezy),在配置文件 /etc/default/docker
中的 DOCKER_OPTS
中配置Hub地址:
DOCKER_OPTS="--registry-mirror=https://docker.mirrors.ustc.edu.cn/"
重新启动服务:
sudo service docker restart
对于使用 systemd 的系统(Ubuntu 16.04+、Debian 8+、CentOS 7), 在配置文件 /etc/docker/daemon.json
中加入:
{ "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"] }
重新启动 dockerd:
sudo systemctl restart docker
macOS
-
打开 “Docker.app”
-
进入偏好设置页面(快捷键
⌘,
) -
打开 “Daemon” 选项卡
-
在 “Registry mirrors” 中添加
https://docker.mirrors.ustc.edu.cn/
-
点击下方的 “Apply & Restart” 按钮
Windows
在系统右下角托盘 Docker 图标内右键菜单选择 Settings
,打开配置窗口后左侧导航菜单选择 Daemon
。在 Registry mirrors
一栏中填写地址 https://docker.mirrors.ustc.edu.cn/
,之后点击 Apply 保存后 Docker 就会重启并应用配置的镜像地址了。
检查 Docker Hub 是否生效
在命令行执行 docker info
,如果从结果中看到了如下内容,说明配置成功。
Registry Mirrors: https://docker.mirrors.ustc.edu.cn/