Docker入门与简单使用

前言:

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的Linux或Windows机器上。近几年来,Docker 在国内发展的如火如荼,特别是在互联网公司, Docker 的使用是十分普遍的,极大提高了应用的维护效率,降低了云计算应用开发的成本。本篇文章主要是带你入门Docker,介绍Docker的安装及简单使用。

1.安装Docker

想要学习Docker,我们首先要安装Docker,从 17.03 版本之后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),下面我们以CentOS系统为例,介绍Docker社区版的安装:

卸载旧版本 
旧版本的 Docker 称为 docker 或者 docker-engine ,使用以下命令卸载旧版本:

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

安装依赖包

#配置yum源
sudo yum-config-manager \
--add-repo \
https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo

#安装依赖包
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2

安装最新版本的 Docker CE

sudo yum-config-manager --enable docker-ce-edge
sudo yum makecache fast
sudo yum install docker-ce

启动 Docker CE

sudo systemctl enable docker
sudo systemctl start docker

建立 docker 用户组

sudo groupadd docker
sudo usermod -aG docker $USER

运行hello-world测试

$ docker run hello-world
 Unable to find image 'hello-world:latest' locally
 latest: Pulling from library/hello-world
 ca4f61b1923c: Pull complete
 Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
 Status: Downloaded newer image for hello-world:latest
 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,同样的,在Windows系统及macOS系统中安装Docker也是十分容易,下载Docker Desktop安装包即可安装使用,具体可参考下面官方文档:

https://docs.docker.com/docker-for-windows/install/
https://docs.docker.com/docker-for-mac/install/

2.常用命令介绍

学习Docker,我们首先要知道它的整体架构,这里简单介绍下Docker中三个基本概念:

  • 镜像(Image):Docker 镜像(Image),就相当于是一个 root 文件系统。比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu16.04 最小系统的 root 文件系统。
  • 容器(Container):镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
  • 仓库(Repository):仓库可看着一个代码控制中心,用来保存镜像。

镜像相关命令:

1)镜像的查找
docker search 镜像名(例如redis)

2)镜像的下载
docker pull 镜像名

3)查看本地的镜像列表
docker images

4)删除镜像
docker rmi 镜像ID

容器相关命令:

1)运行镜像为容器
docker run --name 容器的名字 -d 镜像的名字
-d 表示的是detached,意味着执行完这句命令后控制台将不会被阻碍,可以继续输入命令操作。

2)获取正在运行的容器列表
docker ps

  1. 获取所有容器列表 包含意见退出的
    docker ps -a

4)停止和启动容器
docker start/stop 容器名字/id

5)端口映射
需要将容器中运行的软件的端口映射到主机的端口,否则局域网内的主机是不能够访问的。
docker run -d -p 6378:6379 --name myRedis redis
-p:容器中的6379端口映射到主机的6378端口

6)删除容器
docker rm id

7)查看当前容器日志
docker logs name/id

8)登录容器
docker exec -it 容器名字 bash

-i:保证我们的输入有效
-t:会分配一个伪终端
登录访问当前容器,登陆后就可以在容器中进行常规的Linux命令操作,还可以使用exit命令退出登录。

总结:

本篇文章简单介绍了Docker的安装及常用命令,作为入门文章,希望对你有所帮助。其实Docker作为基础工具,还是推荐大家学习一下,比如你可以秒级启动一个MySQL实例,有新版本也可以用Docker运行来测试。下篇文章打算写下如何在Docker中运行及配置MySQL,期待下吧!

参考资料:

碎碎念:

最近双11即将到来,各大服务器厂商也有优惠活动,入门级的服务器88元即可买一年,腾讯云和阿里云都有此活动,推荐大家看看。买一台云服务器,你可以学习Linux,MySQL,Docker,Git等,还可以部署个人网站,需要的小伙伴可以买来测试玩玩哦!复制下面链接到浏览器打开即可进入官网页面购买。

阿里云:
https://www.aliyun.com/1111/2019/group-buying-share?ptCode=F6718C2981638C225DDA9F79172CF1F4647C88CF896EF535&share_source=copy_link

腾讯云:
https://cloud.tencent.com/act/double11/reserve?spread_hash_key=1isLgW

公众号.jpg

posted @ 2019-11-01 14:29  MySQL技术  阅读(389)  评论(0编辑  收藏  举报