前言: linux
Docker 是一个开源的应用容器引擎,让开发者能够打包他们的应用以及依赖包到一个可移植的镜像中,而后发布到任何流行的Linux或Windows机器上。近几年来,Docker 在国内发展的如火如荼,特别是在互联网公司, Docker 的使用是十分广泛的,极大提升了应用的维护效率,下降了云计算应用开发的成本。本篇文章主要是带你入门Docker,介绍Docker的安装及简单使用。redis
想要学习Docker,咱们首先要安装Docker,从 17.03 版本以后分为 CE(Community Edition: 社区版) 和 EE(Enterprise Edition: 企业版),下面咱们以CentOS系统为例,介绍Docker社区版的安装:docker
卸载旧版本
旧版本的 Docker 称为 docker 或者 docker-engine ,使用如下命令卸载旧版本:shell
$ sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine
安装依赖包 ubuntu
#配置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 CEwindows
sudo yum-config-manager --enable docker-ce-edge sudo yum makecache fast sudo yum install docker-ce
启动 Docker CE centos
sudo systemctl enable docker sudo systemctl start docker
创建 docker 用户组 浏览器
sudo groupadd docker sudo usermod -aG docker $USER
运行hello-world测试 bash
$ 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/
学习Docker,咱们首先要知道它的总体架构,这里简单介绍下Docker中三个基本概念:
镜像相关命令:
1)镜像的查找
docker search 镜像名(例如redis)2)镜像的下载
docker pull 镜像名3)查看本地的镜像列表
docker images4)删除镜像
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,期待下吧!
参考资料:
碎碎念:
最近双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