4

docker 快速上手 - 鱼与鱼

 2 years ago
source link: https://www.cnblogs.com/gongyanzh/p/16325117.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

docker 快速上手

Docker 属于 Linux 容器的一种封装,提供简单易用的容器使用接口

图片

安装 docker

$ sudo yum install -y yum-utils

$ sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo
# 增加阿里源
$ sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  1. Install the latest version of Docker Engine, containerd, and Docker Compose or go to the next step to install a specific version:

    $ sudo yum install docker-ce docker-ce-cli containerd.io docker-compose-plugin
    

    If prompted to accept the GPG key, verify that the fingerprint matches 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35, and if so, accept it.

    安装指定版本

    $ yum list docker-ce --showduplicates | sort -r
    
    docker-ce.x86_64  3:18.09.1-3.el7                     docker-ce-stable
    docker-ce.x86_64  3:18.09.0-3.el7                     docker-ce-stable
    docker-ce.x86_64  18.06.1.ce-3.el7                    docker-ce-stable
    docker-ce.x86_64  18.06.0.ce-3.el7                    docker-ce-stable
    
    $ sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io docker-compose-plugin
    
  2. Start Docker.

    $ sudo systemctl start docker
    
  3. Verify that Docker Engine is installed correctly by running the hello-world image.

    $ sudo docker run hello-world
    

    This command downloads a test

  4. 避免每次输入sudo, 可以把用户加入docker用户组

    1、输入
    $ sudo groupadd docker
    显示groupadd: group 'docker' already exists
    
    2、将docker账户给与权限
    sudo gpasswd -a <用户名> docker
    例如: sudo gpasswd -a xiangyuan docker
    
    3、重启docker
    sudo service docker restart
    
    4、刷新docker成员
    newgrp - docker
    
docker version
docker info

官方文档 https://docs.docker.com/engine/install/centos/#set-up-the-repository

https://www.runoob.com/docker/centos-docker-install.html

docker 使用

三个核心部件

  • dockerfile
  • image
  • container

image

# 列出本机的所有 image 文件。
$ docker image ls

# 删除 image 文件
$ docker image rm [imageName]

container

# 列出本机正在运行的容器
$ docker container ls

# 列出本机所有容器,包括终止运行的容器
$ docker container ls --all

#新建容器,每运行一次,就会新建一个容器。同样的命令运行两次,就会生成两个一模一样的容器文件
$ docker container run hello-world 

# 命令,它用来启动已经生成、已经停止运行的容器文件。
$ docker container start

# 强行终止
$ docker container kill

$ docker container rm [containerID]

# 查看 docker 容器的输出,即容器里面 Shell 的标准输出
$ docker container logs [containerID]

Dockerfile

Dockerfile 文件。它是一个文本文件,用来配置 image。Docker 根据 该文件生成二进制的 image 文件

docker image build  -t koa-demo:0.0.1 .  # 命令创建 image 文件

# -t参数用来指定 image 文件的名字,后面还可以用冒号指定标签。如果不指定,默认的标签就是latest。最后的那个点表示 Dockerfile 文件所在的路径,上例是当前路径,所以是一个点。

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK