Docker 学习笔记

Docker 学习笔记

Docker 能处理的事情包括:

  • 隔离应用依赖
  • 创建应用镜像并进行复制
  • 创建容易分发的即启即用的应用
  • 允许实例简单、快速的扩展
  • 测试应用并随后销毁它们

Docker 背后的想法是创建软件程序可移植的轻量容器,让其可以在任何安装了 Docker 的机器上运行,而不用关心底层操作系统。

Docker 两个最重要的概念,镜像和容器。

  • 镜像
    Docker 的镜像类似虚拟机的快照,但更轻量。创建 Docker 的镜像有几种方式,多数是在一个现有镜像基础上创建新镜像。从头创建一个镜像,可以考虑在一个镜像的基础上创建它的子镜像。有两种方式这样做,一是在一个文件中指定一个基础镜像及需要完成的修改,另一种是通过运行一个镜像,对其进行修改并提交。
  • 容器
    容器与虚拟机一样,是隔离的,它们拥有一个唯一的 ID 和唯一的供人阅读的名字。容器对外公开服务是必要的,因此 Docker 允许公开容器的特定端口。与虚拟机相比,容器有一个很大的差异,它们被设计用来运行单进程,无法很好地模拟一个完整的环境。
  • 数据卷
    数据卷让你可以不受容器生命周期影响进行数据持久化。它们表现为容器内的空间,但实际保存在容器之外,从而允许你在不影响数据的情况下销毁、重建、修改、丢弃容器。Docker 允许你定义应用部分和数据部分,并提供工具让你可以将它们分开。卷是针对容器的,你可以使用同一个镜像创建多个容器并定义不同的卷。卷保存在运行 Docker 的宿主文件系统上。
  • 链接
    容器启动时,会被分配一个随机的私有IP,其容器可以使用这个IP地址与其进行通讯。要开启容器间通讯,Docker允许在创建一个新容器时引用其它现存容器。在刚创建的容器里,被引用的容器将获得一个别名。我们就说,这两个容器链接在了一起。

命令

# 从公共 registry 下载一个镜像
docker pull ubuntu:latest
# 列出镜像
docker images
# 新列出镜像命令
docker image ls
# 从镜像上创建一个容器
# --rm: 告诉 Docker 一旦运行的进程退出就删除容器,这在运行测试时非常有用,可免除杂乱
# -ti: 告诉 Docker 分配一个伪终端并进入交互模式,不要在生产容器中打开这个标志
# ubuntu 指定镜像
# /bin/bash 要运行的命令
# 运行 run 命令时,可指定链接、卷、端口、窗口名称,如果没有提供,Docker 会分配一个默认名称
docker run --rm -ti ubuntu /bin/bash
# 在后台运行一个容器
# 输出分配的 ID
docker run -d ubuntu ping 8.8.8.8
docker ps
# 进入正在运行的容器的交互界面
docker exec -ti 容器名称 /bin/bash
# 测试 docker 安装情况
docker version
docker ps
docker run hello-world
# create image
docker build -t name .
# create and start nginx container
# -p map machine's port 4000 to container's port 80
# access the service through port 4000
docker run -d -p 4000:80 --name webserver nginx
# run docker
docker run -p 4000:80 name
docker run -d -p 4000:80 name
# stop container
docker stop name
docker container stop <hash>
# force shutdown
docker container kill <hash>
# start nginx container
docker start webserver
# list all running containers
docker ps
docker container ls
# list all containers including stopped
docker ps -a
docker container ls -a
# stop and remove running container
docker rm -f webserver
# remove container
docker container rm <hash>
# remove all containers
docker container rm $(docker container ls -a -q)
# remove an image
docker rmi imageId/imageName
docker image rm imageId
#remove all images
docker image rm $(docker image ls -a -q)

镜像仓库相关

docker login
# tag image for upload to registry
docker tag <image> username/repository:tag
# upload
docker push username/repository:tag
# run image from a registry
docker run username/repository:tag

Dockfile

Dockerfile will define what goes on in the environment inside your container. Access to resources like networking interfaces and disk drives is virtualized inside this environment, which is isolated from the rest of your system, so you have to map ports to the outside world, and be specific about what files you want to “copy in” to that environment. However, after doing that, you can expect that the build of your app defined in this Dockerfile will behave exactly the same wherever it runs.

Dockerfile 就是一个镜像的配置文件

# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

如果服务器前面有代理,可能会阻塞网络连接,使用 ENV 指定代理。

# Set proxy server, replace host:port with values for your servers
ENV http_proxy host:port
ENV https_proxy host:port
posted @ 2017-12-16 22:35  我的娃会叫爸爸啦  阅读(219)  评论(0编辑  收藏  举报