服务器 
首页 > 服务器 > 浏览文章

Docker简单入门使用教程

(编辑:jimmy 日期: 2024/9/24 浏览:3 次 )

前言:

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
3) 获取所有容器列表 包含意见退出的
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,期待下吧!

以上就是Docker简单入门使用教程的详细内容,更多关于Docker入门与使用的资料请关注其它相关文章!

上一篇:Linux date命令的使用
下一篇:在后台运行Linux命令的方法
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?