在Vagrant与Virtual Box 搭建centos虚拟机一篇我们使用Vagrant和Virtual Box搭建了一个centos7的虚拟机,今天我们在搭建的虚拟机上搭建docker服务,并且学习docker的一些简单命令,运行docker提供的Hello Word镜像。在安装docker之前,如果已经安装完docker吗,使用下面的命令先卸载docker服务。
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
然后我们可以安装新的Docker服务,在安装docker之前,我们要安装docker的依赖包:yum-utils、device-mapper-persistent-data、lvm2,并且设置docker仓库,命令如下:
[root@localhost ~]# sudo yum install -y yum-utils \
> device-mapper-persistent-data \
> lvm2
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
然后我们使用yum安装docker,命令为:sudo yum install -y docker-ce docker-ce-cli containerd.io
到此为止,我们的docker已经安装完毕,然后我们需要启动docker,并且测试docker是否安装成功,我们通过docker version可以查看docker的版本,显示如下:
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 19.03.12
API version: 1.40
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:46:54 2020
OS/Arch: linux/amd64
Experimental: false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
在最后一行打印:Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?是因为docker还没有启动,我们可以启动docker,并且将docker设置为开机自启动:
[root@localhost ~]# systemctl start docker && systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
我们在运行docker,可以看到docker 客户端和服务端的版本信息,信息如下:
[root@localhost ~]# docker version
Client: Docker Engine - Community
Version: 19.03.12
API version: 1.40
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:46:54 2020
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.12
API version: 1.40 (minimum version 1.12)
Go version: go1.13.10
Git commit: 48a66213fe
Built: Mon Jun 22 15:45:28 2020
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.2.13
GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
runc:
Version: 1.0.0-rc10
GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
docker-init:
Version: 0.18.0
GitCommit: fec3683
在启动docker成功之后,我们接下来看一下docker的常用命令,如下为docker最基础的三个命令,分别为拉取镜像,运行镜像和与容器交互
docker images 查看镜像列表
docker pull 拉取镜像到本地
docker run 根据某个镜像创建容器
-d 让容器在后台运行,其实就是一个进程
--name 给容器指定一个名字
-p 将容器的端口映射到宿主机的端口
docker exec -it 进入到某个容器中并交互式运行
接下来我们使用 docker从镜像服务器拉取第一个镜像,Hello-World镜像,然后运行该镜像服务。拉取镜像命令如下:
[root@localhost ~]# docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:7f0a9f93b4aa3022c3a4c147a449bf11e0941a1fd0bf4a8e6c9408b2600777c5
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 8 months ago 13.3kB
如上所示,我们拉取镜像之后,再次使用docker images命令可以查看到拉取的镜像文件,然后我们可以使用docker run运行该镜像会输出一下内容:
[root@localhost ~]# docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
至此,我们就成功安装了Docker,并且学习了Docker最基本的命令,运行了第一个Docker程序,后面我们将使用docker安装更多服务,并且学习docker的更多命令。