docker入门

docker入门

Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。

1 检查系统内核

[root@localhost ~]# uname -r
3.2.03.2.0

如果内核版本低于3.0(该值根据docker版本决定)请升级Linux内核,内核升级方法请见:http://blog.csdn.net/jlh912008548/article/details/72943741

2 docker 安装

2.1 CentOS 安装 docker

  1. 首先关闭selinux:setenforce 0
  2. sed -i ‘/^SELINUX=/c\SELINUX=disabled’ /etc/selinux/config
  3. 在Fedora EPEL源中已经提供了docker-io包,下载安装epel:rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
  4. 设置docker yum:sed -i ‘s/^mirrorlist=https/mirrorlist=http/’ /etc/yum.repos.d/epel.repo
  5. yum安装docker-io:
    yum -y install docker-io

当然也可前往阿里镜像站下载:[https://mirrors.aliyun.com/docker-engine/yum/repo/main/](https://mirrors.aliyun.com/docker- engine/yum/repo/main/ “https://mirrors.aliyun.com/docker-engine/yum/repo/main/“)

使用service docker start 命令启动docker

2.2 Ubuntu 安装 docker

  1. 首先使用su命令切换到root 用户
  2. 使用apt-get update命令保证apt-get为最新版本
  3. 使用apt-get install -y docker.io命令安装docker

使用该方式所安装的docker有可能不是最新版本,如果要安装最新版本请使用curl -s https://get.docker.com|sh 命令

3 docker实践

3.1 docker Helloworld

考虑下载速度,此处使用163提供的镜像

下载地址:

docker pull hub.c.163.com/library/hello-world:latest

运行hello-world:

使用docker images 查看镜像列表,使用docker run 命令运行镜像

root@jarome:~# docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
hub.c.163.com/library/hello-world   latest              7a5a2d73abce        4 months ago        1.84 kB
root@jarome:~# docker run hub.c.163.com/library/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.
 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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

root@jarome:~# 

3.2 docker创建nginx镜像并通过浏览器访问

下载镜像:

docker pull hub.c.163.com/library/nginx:latest

启动nginx:

nginx 使用文档详情见:https://c.163.com/hub#/m/repository/?repoId=2967

在nginx文档中有这样一个命令:$ docker run –name some-nginx -d -p 8080:80 some-content-nginx

该命令的意思是:使用-p命令给制定端口,将当前电脑的8080端口映射到nginx 的80端口中。注意some-content-nginx为镜像名称,启动步骤如下
1.docker images查看镜像名称
2.docker run –name some-nginx -d -p 8080:80 REPOSITORY(REPOSITORY 为镜像名称)
3.此时通过主机的8080端口可访问nginx的80端口,在浏览器中输入127.0.0.1:8080即可访问Nginx
3.docker ps 查看该镜像是否已启动。
4.docker kill CREATED、docker stop CREATED(CREATED为镜像进程id) ,停止镜像

root@jarome:~# docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
hub.c.163.com/library/nginx         latest              25c6ec04df86        6 weeks ago         109.4 MB
hub.c.163.com/library/hello-world   latest              7a5a2d73abce        4 months ago        1.84 kB
root@jarome:~# docker run --name some-nginx -d -p 8080:80 hub.c.163.com/library/nginx    
a36e8632b7ffa31487ba1e15a8d5a7c5f4cf88693dd7a34d62fc8960e12f3629
root@jarome:~# docker ps
CONTAINER ID        IMAGE                                COMMAND                CREATED              STATUS              PORTS                  NAMES
a36e8632b7ff        hub.c.163.com/library/nginx:latest   "nginx -g 'daemon of   About a minute ago   Up About a minute   0.0.0.0:8080->80/tcp   some-nginx     
root@jarome:~# docker kill a36e8632b7ff
a36e8632b7ff
root@jarome:~# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
root@jarome:~# 

docker exec 命令的使用:

docker exec 命令进入容器(exec Run a command in a running container)
使用示例:docker exec -it 5c2282ae2534 bash (5c2282ae2534为该镜像的进程id)

root@jarome:~# docker exec --help

Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container

  -d, --detach=false         Detached mode: run command in the background
  --help=false               Print usage
  -i, --interactive=false    Keep STDIN open even if not attached
  -t, --tty=false    
root@jarome:~# docker exec -it 5c2282ae2534 bash
root@5c2282ae2534:/# 
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页