docker修炼手册

版权声明:如需转载或引用,请注明出处。 https://blog.csdn.net/weixin_39278265/article/details/82753645

前言

本文旨在根据docker官网最新[指导手册](https://docs.docker.com/v17.12/) ,整理出一份“修炼手册”来。

##一、 如何安装docker?

1 docker 总共两个版本

1)Community Edition (CE). 适合开发者和小规模团队。is ideal for developers and small teams looking to get started with Docker and experimenting with container-based apps
2)Enterprise Edition (EE). 适合企业和IT团队。is designed for enterprise development and IT teams who build, ship, and run business critical applications in production at scale

2 docker支持的平台

1)Desktop(桌面系统)

  • Docker for Mac (macOS)
  • Docker for Windows (Microsoft Windows 10)

奇怪:为什么没有Ubuntu???
答:原来Ubuntu属于3)服务器一类

2)Cloud(云平台)

  • Amazon Web Services
  • Microsoft Azure
  • IBM Cloud (Beta)

3)Server(服务器)

  • CentOS
  • Oracle Linux
  • Red Hat Enterprise Linux
  • SUSE Linux Enterprise Server
  • Ubuntu
  • Microsoft Windows Server 2016

3 docker 还有 docker cloud这个产品,感觉很酷

Docker Cloud
You can use Docker Cloud to automatically provision and manage your cloud instances.

所以,到底如何安装?
这里我也不多说了,在官网上都有指导的,根据指导做就行:https://docs.docker.com/v17.12/install/

##二、Docker配置(setup)第一部分——https://docs.docker.com/v17.12/get-started/

###1 docker概念
The use of Linux containers to deploy applications is called containerization. Containers are not new, but their use for easily deploying applications is.

容器化是核心概念。

好处:

  • Flexible: Even the most complex applications can be containerized.
  • Lightweight: Containers leverage and share the host kernel.
  • Interchangeable: You can deploy updates and upgrades on-the-fly.
  • Portable: You can build locally, deploy to the cloud, and run anywhere.
  • Scalable: You can increase and automatically distribute container replicas.
  • Stackable: You can stack services vertically and on-the-fly.

这样来看的话,确实比虚拟机方便好多。Containers leverage and share the host kernel.

2 image和container的概念

A container is launched by running an image. An image is an executable package that includes everything needed to run an application–the code, a runtime, libraries, environment variables, and configuration files.

一个镜像运行一个容器。一个镜像包含了所有运行需要的包、环境等等。

A container is a runtime instance of an image–what the image becomes in memory when executed (that is, an image with state, or a user process). You can see a list of your running containers with the command, docker ps, just as you would in Linux.

一个容器是一个镜像的实例。

3 container和virtual machine

A container runs natively on Linux and shares the kernel of the host machine with other containers. It runs a discrete process, taking no more memory than any other executable, making it lightweight.

By contrast, a virtual machine (VM) runs a full-blown “guest” operating system with virtual access to host resources through a hypervisor. In general, VMs provide an environment with more resources than most applications need.

4 这里还可以部署Kubernetes

Kubernetes on Docker for Mac is available in 17.12 Edge (mac45) or 17.12 Stable (mac46) and higher.
Kubernetes on Docker for Windows is available in 18.02 Edge (win50) and higher.

网址:https://docs.docker.com/v17.12/docker-for-windows/kubernetes/

具体信息:
Kubernetes is only available in Docker for Windows 18.02 CE Edge. Kubernetes support is not included in Docker for Windows 18.02 CE Stable.

See Docker for Windows > Getting started to enable Kubernetes and begin testing the deployment of your workloads on Kubernetes.

5 基本指令

docker --version 查看版本
docker version或者docker info 查看详细信息。

docker run hello-world 运行hello-world 这个image,如果本地没有,那么docker会自动去hub上下载。

docker image ls 列出所有你下载的image

docker container ls --all 或者 docker container ls 或者 docker container ls -aq 列出所有容器(记住:容器是镜像的实例)。

## List Docker CLI commands
docker
docker container --help


## Display Docker version and info
docker --version
docker version
docker info


## Execute Docker image
docker run hello-world


## List Docker images
docker image ls


## List Docker containers (running, all, all in quiet mode)
docker container ls
docker container ls --all
docker container ls -aq

三、Docker配置(setup)第二部分——https://docs.docker.com/v17.12/get-started/part2/

1 关于dockerfile的介绍

In the past, if you were to start writing a Python app, your first order of business was to install a Python runtime onto your machine. But, that creates a situation where the environment on your machine needs to be perfect for your app to run as expected, and also needs to match your production environment.


With Docker, you can just grab a portable Python runtime as an image, no installation necessary. Then, your build can include the base Python image right alongside your app code, ensuring that your app, its dependencies, and the runtime, all travel together.


These portable images are defined by something called a Dockerfile.

这个是真的方便。只需要定义一个dockerfile,就能够轻松配置实验环境,太酷了。

2 Define a container with Dockerfile (用一个dockerfile来定义一个容器)

Dockerfile defines 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 need 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 behaves exactly the same wherever it runs.

意思就是dockerfile定义了你需要的一些依赖之类的。

3 上传你的docker

docker login 登录,要输入密码的。

docker tag image username/repository:tag 给选中的image打上标签(tag) 标签就是:username/repository:tag

docker image ls

docker push username/repository:tag 上传(push)你的docker镜像
docker tag defects4j-ubuntu dalewushuang/defects4j-ubuntu 记得改标签,不然传不上去
docker push dalewushuang/defects4j-ubuntu:latest 上传

load和save都在这里:https://blog.csdn.net/wangdaoge/article/details/52639123

docker run -p 4000:80 username/repository:tag 上传之后,就可以这样运行了。

docker build -t friendlyhello . # Create image using this directory’s Dockerfile
docker run -p 4000:80 friendlyhello # Run “friendlyname” mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello # Same thing, but in detached mode
docker container ls # List all running containers
docker container ls -a # List all containers, even those not running
docker container stop # Gracefully stop the specified container
docker container kill # Force shutdown of the specified container
docker container rm # Remove specified container from this machine
docker container rm $(docker container ls -a -q) # Remove all containers
docker image ls -a # List all images on this machine
docker image rm # Remove specified image from this machine
docker image rm $(docker image ls -a -q) # Remove all images from this machine
docker login # Log in this CLI session using your Docker credentials
docker tag username/repository:tag # Tag for upload to registry
docker push username/repository:tag # Upload tagged image to registry
docker run username/repository:tag # Run image from a registry

我发现docker rmi -f 在删除image上面是真的好用!

4 保存你的docker image

docker save image_name > image_name.tar

5 有关pull指令

https://docs.docker.com/engine/reference/commandline/pull/

docker pull [OPTIONS] NAME[:TAG|@DIGEST]

docker pull ubuntu:14.04

6 有关复制cp命令——https://docs.docker.com/engine/reference/commandline/cp/

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

7 修改docker pull的路径(win 10系统下)

https://blog.csdn.net/StemQ/article/details/53150939 这个必须要是win 10 旗舰版,企业版,我的是windows10家庭版,没有hyper-v,扎心了。

https://blog.csdn.net/u013948858/article/details/80811986 这个比较靠谱

https://my.oschina.net/ykbj/blog/1595328 这个很接地气,但是我感觉自己的docker不是完整版的,好像有问题。

四、在docker下运行Ubuntu 14.04

先要下载:
docker pull ubuntu:14.04

然后运行:
docker run --name testByDale -it

$ docker run --name test -it debian


root@d6c0fe130dba:/# exit 13
$ echo $?
13
$ docker ps -a | grep test
d6c0fe130dba debian:7 “/bin/bash” 26 seconds ago Exited (13) 17 seconds ago

This example runs a container named test using the debian:latest image. The -it instructs Docker to allocate a pseudo-TTY connected to the container’s stdin; creating an interactive bash shell in the container. In the example, the bash shell is quit by entering exit 13. This exit code is passed on to the caller of docker run, and is recorded in the test container’s metadata

查看容器使用的CPU,内存:
docker stats

五、重新分配CPU

1)docker run --name simfix --cpus 4 --memory 3GB --memory-swap -1 defects4j-ubuntu

2)docker container ls -a

3)docker cp ./ubuntu/SimFix.tar simfix:/home/deheng/

4)docker start simfix

5)docker exec -it simfix bash

6) vim /etc/profile
export TZ=America/Los_Angeles
source /etc/profile

失败。

原来我的docker是在toolbox里面运行的,很难受。

猜你喜欢

转载自blog.csdn.net/weixin_39278265/article/details/82753645