文章目录
- Docker的环境安装
- 移除旧版本的docker
- 更新apt安装包索引
- 安装docker
- 测试是否安装成功
- 不使用sudo启动docker
- 安装docker-compose
- 镜像拉取加速
- 创建仓库
- 创建Dockerfile文件
- 纯命令行
- c++编译生成的执行文件
- 构建镜像
- 运行镜像
- 删除所有镜像
- docker运行异常
- 登录
- 推送镜像
- 在其他电脑使用镜像
- 拉取镜像
- 运行镜像
- 使用docker-compose
- 编写docker-compose.yml
- 构建
- 运行
- 在浏览器中管理镜像
- 创建用户
- 连接本地docker环境
- 查看镜像状态
Docker的环境安装
移除旧版本的docker
$ sudo apt-get remove docker docker-engine docker.io docker-ce
更新apt安装包索引
$ sudo apt-get update
安装docker
方法一
$ wget -qO- https://get.docker.com/ | sh
方法二
$ sudo apt-get install -y docker.io
$ systemctl start docker
$ systemctl enable docker
$ docker version
测试是否安装成功
$ sudo docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
不使用sudo启动docker
$ sudo groupadd docker
$ sudo gpasswd -a ${USER} docker
Adding user heyw to group docker
$ sudo service docker restart
$ newgrp --help
Usage: newgrp [-] [group]
$ newgrp - docker
$ docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
(注意:需要重启系统)
安装docker-compose
使用 Docker Compose 可以轻松、高效的管理容器,它是一个用于定义和运行多容器 Docker 的应用程序工具
$ sudo curl -L https://github.com/docker/compose/releases/download/1.17.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ docker-compose -v
如果太慢无法安装,可尝试使用python安装
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -U docker-compose
镜像拉取加速
wilson@ubuntu:~$ sudo mkdir -p /etc/docker
[sudo] password for wilson:
wilson@ubuntu:~$ sudo tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors":["https://f3lu6ju1.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors":["https://f3lu6ju1.mirror.aliyuncs.com"]
}
wilson@ubuntu:~$ sudo systemctl daemon-reload
wilson@ubuntu:~$ sudo systemctl restart docker
创建仓库
创建Dockerfile文件
纯命令行
FROM busybox
CMD echo "hello, Wilson!"
Busybox是一个开源项目,遵循GPL v2协议。Busybox将众多的UNIX命令集合进一个很小的可执行程序中,可以用来替代GNU fileutils、shellutils等工具集。Busybox中各种命令与相应的GNU工具相比,所能提供的选项比较少,但是也足够一般的应用了。Busybox主要用于嵌入式系统。
c++编译生成的执行文件
需要使用静态编译
g++ -static -o hello Main.cpp
Dockerfile
FROM alpine
ADD hello /hello
CMD [ "./hello" ]
构建镜像
docker build -t 用户名/仓库名 Dockerfile所在文件夹
在当前例子中,是当前文件夹,即 docker build -t wilsonhe2019/hello .
$ docker build -t wilsonhe2019/hello .
Sending build context to Docker daemon 2.048kB
Step 1/2 : FROM busybox
latest: Pulling from library/busybox
bdbbaa22dec6: Pull complete
Digest: sha256:6915be4043561d64e0ab0f8f098dc2ac48e077fe23f488ac24b665166898115a
Status: Downloaded newer image for busybox:latest
---> 6d5fcfe5ff17
Step 2/2 : CMD echo "hello, Wilson!"
---> Running in d3ba11b3df32
Removing intermediate container d3ba11b3df32
---> 5a8186438fd5
Successfully built 5a8186438fd5
Successfully tagged wilsonhe2019/hello:latest
运行镜像
$ docker run wilsonhe2019/hello
hello, Wilson!
删除所有镜像
docker rmi -f $(docker images -qa)
docker运行异常
提示
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. ...
重启docker
sudo service docker restart
如果需要开机自动恢复,那么执行下面的命令
systemctl enable docker.service
登录
如果要推送到docker hub的话,需要先登录
$ docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: wilsonhe2019
推送镜像
$ docker push wilsonhe2019/hello
The push refers to repository [docker.io/wilsonhe2019/hello]
195be5f8be1d: Mounted from library/busybox
latest: digest: sha256:98c379d7de38b5e84157aeb80551d504439ddbea2e376a7010f1b15f7b7cb254 size: 527
在其他电脑使用镜像
拉取镜像
$ docker pull wilsonhe2019/hello
Using default tag: latest
latest: Pulling from wilsonhe2019/hello
bdbbaa22dec6: Pull complete
Digest: sha256:98c379d7de38b5e84157aeb80551d504439ddbea2e376a7010f1b15f7b7cb254
Status: Downloaded newer image for wilsonhe2019/hello:latest
docker.io/wilsonhe2019/hello:latest
运行镜像
$ docker run wilsonhe2019/hello
hello, Wilson!
使用docker-compose
编写docker-compose.yml
version: '3'
services:
hello:
image: "wilsonhe2019/hello"
restart: always
hello-portainer:
image: portainer/portainer
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./hello-data/portainer/data:/data
restart: always
ports:
- 9000:9000
portainer可以让你在网页中统一管理镜像
构建
$ docker-compose build
hello-portainer uses an image, skipping
hello uses an image, skipping
运行
$ docker-compose up
Starting hello_hello-portainer_1 ...
Starting hello_hello_1 ...
Starting hello_hello-portainer_1
如果要在后台运行,命令需要加上-d
$ docker-compose up
在浏览器中管理镜像
打开浏览器访问 http://localhost:9000/
创建用户
连接本地docker环境
查看镜像状态