安装官网参考文档
https://docs.docker.com/engine/install/centos/
配置阿里云镜像
开启 docker
systemctl start docker
重启 docker 服务
systemctl restart docker
关闭 docker
systemctl stop docker
遇到问题
[root@192 ~]# systemctl stop docker
Warning: Stopping docker.service, but it can still be activated by:
docker.socket
原因
https://stackoverflow.com/questions/47489631/warning-stopping-docker-service-but-it-can-still-be-activated-by-docker-socke
这是因为除了docker.service单元文件,还有一个docker.socket单元文件…docker.socket这是用于套接字激活。
该警告意味着:如果你试图连接到docker socket,而docker服务没有运行,系统将自动启动docker。
解决
systemctl stop docker.socket
补充
键启动所有docker 容器:docker start $(docker ps -a | awk ‘{ print $1}’ | tail -n +2)
键关闭所有docker 容器:docker stop $(docker ps -a | awk ‘{ print $1}’ | tail -n +2)
键删除所有docker 容器:docker rm $(docker ps -a | awk ‘{ print $1}’ | tail -n +2)
键删除所有docker 镜像: docker rmi $(docker images | awk ‘{print $3}’ | tail -n +2)
查看版本信息
docker version
结果如下
Client: Docker Engine - Community
Version: 20.10.17
API version: 1.41
Go version: go1.17.11
Git commit: 100c701
Built: Mon Jun 6 23:05:12 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.17
API version: 1.41 (minimum version 1.12)
Go version: go1.17.11
Git commit: a89b842
Built: Mon Jun 6 23:03:33 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.6
GitCommit: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
runc:
Version: 1.1.2
GitCommit: v1.1.2-0-ga916309
docker-init:
Version: 0.19.0
GitCommit: de40ad0
更加详细的docker信息,系统级别的镜像和容器
docker info
查看镜像
[root@192 /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 10 months ago 13.3kB
搜索
docker search mysql
docker 分层下载
[root@192 /]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709
Status: Downloaded newer image for mysql:latest
// 真实地址
docker.io/library/mysql:latest
删除镜像
指定 id 删除
[root@192 /]# docker rmi -f c20987f18b13
删除全部
[root@192 /]# docker rmi -f $(docker images -aq)
[root@192 /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker run -d --name nginx01 -p:8080:80 nginx
-d 后台运行
–name 容器命名
-p 暴露端口 8080(Linux) 80(nginx01 容器)
[root@192 /]# docker images
[root@192 /]# docker run -d --name nginx01 -p:8080:80 nginx
a059f5a198b7f7aef69a66447c86fecf402e0c0fd15aaa166a49e1d8d8c56e2d
[root@192 /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a059f5a198b7 nginx "/docker-entrypoint.…" 12 seconds ago Up 10 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp nginx01
[root@192 /]# curl localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@192 /]#
关闭之后,遇到的问题
Conflict. The container name “/nginx01” is already in use by container “a059f5a198b7f7aef69a66447c86fecf402e0c0fd15aaa166a49e1d8d8c56e2d”. You have to remove (or rename) that container to be able to reuse that name.
解决方法
// 列出所有的容器
docker ps -a
// 停止容器
docker stop <CONTAINER ID|NAME>
// 停止多个容器
docker rm <CONTAINER ID|NAME> <CONTAINER ID|NAME>
// 停止所有容器
docker rm $(docker ps -aq)
再次运行docker run -d --name nginx01 -p:8080:80 nginx
,curl localhost:8080
就好了
systemctl status firewalld.service
关闭防火墙服务
systemctl stop firewalld.service
查看虚拟机 ip 地址
ifconfig
虚拟机下可以访问
当容器关闭之后就不可以在访问了,再删除容器
Docker Machine 是一种可以让您在虚拟主机上安装 Docker 的工具,并可以使用 docker-machine命令来管理主机。
Docker Machine 也可以集中管理所有的 docker 主机,比如快速的给 100 台服务器安装上 docker。
基于 Docker 安装 Nginx,启动运行,外网访问,映射配置文件
Windows中无法访问Docker中Nginx的解决方案
docker machine 的 GitHub 地址
docker run -d -p 9000:9000 \
--restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer