











Docker 能处理的事情包括:
Docker 背后的想法是创建软件程序可移植的轻量容器,让其可以在任何安装了 Docker 的机器上运行,而不用关心底层操作系统。
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
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
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。