Vagrant and Virtual Box to build Docker service and get started with Docker

Build a centos virtual machine in Vagrant and Virtual Box. We used Vagrant and Virtual Box to build a centos7 virtual machine. Today we built a docker service on the virtual machine we built, and learned some simple commands of docker to run Hello provided by docker Word mirroring. Before installing docker, if docker has been installed, use the following command to uninstall docker service first.

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

Then we can install the new Docker service. Before installing docker, we need to install docker's dependency packages: yum-utils, device-mapper-persistent-data, lvm2, and set up the docker warehouse. The command is as follows:

[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

Then we use yum to install docker, the command is: sudo yum install -y docker-ce docker-ce-cli containerd.io

So far, our docker has been installed, and then we need to start docker and test whether the docker is installed successfully, we can check the docker version through docker version, which is displayed as follows:

[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?

Print in the last line: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? Because docker has not been started, we can start docker and set docker to boot from start up:

[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.

When we are running docker, we can see the version information of the docker client and server. The information is as follows:

[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

After starting docker successfully, let's take a look at the commonly used commands of docker. The three basic commands of docker are as follows: pull the image, run the image and interact with the container.

docker images      查看镜像列表
docker pull        拉取镜像到本地
docker run         根据某个镜像创建容器
-d                 让容器在后台运行,其实就是一个进程
--name             给容器指定一个名字
-p                 将容器的端口映射到宿主机的端口
docker exec -it    进入到某个容器中并交互式运行

Next, we use docker to pull the first image from the mirror server, the Hello-World image, and then run the image service. The command to pull the image is as follows:

[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

As shown above, after we pull the image, use the docker images command again to view the pulled image file, and then we can use docker run to run the image and it will output the following content:

[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/

At this point, we have successfully installed Docker, learned the most basic commands of Docker, and ran the first Docker program. Later, we will use docker to install more services and learn more docker commands.

Guess you like

Origin blog.csdn.net/wk19920726/article/details/108430407