Docker basics - about installation, common instructions and initial experience of image making

Why use docker

Docker is a 轻量级virtual machine, which solves the problem of environment configuration during service migration and deployment. For example, common web services rely on tools such as jdk, Tomcat, and databases. The migration project needs to be reconfigured on the new machine, which is not only troublesome, but also may be misconfigured.

It would be nice if the whole service could be packaged together with the external environment it depends on, which is what docker does. He packaged the configured software into a new imagemachine and started itimage

Of course, what I understand is only a small part. Docker also has applications in providing elastic cloud services and building microservice architectures.

Finally, compared with traditional virtual machines, docker occupies very few resources, and docker itself only consumes the overhead of one process of the system. A virtual machine, such as VMware, simulates a complete operating system.

Install docker-ce

Docker is divided into community version and enterprise version, which docker-cemeans community version. The following demonstrates the instructions for using repository(library) installation docker-ce. In addition, the author uses the root account by default. If it is not the root account, please add a command before each sudocommand

Note: The author's system version is Ubuntu 16.04LST

installation instructions

# 升级apt包
apt-get updat

#安装必要的工具,使apt可以基于htpps协议使用repository
apt-get install apt-transport-https ca-certificates curl \
software-properties-common

#安装docker官方GPG key 并验证
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

#如果成功,会显示pub uid sub等字段信息
apt-key fingerprint 0EBFCD88

#将docker安装源加入ppa
add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable";

#刷新apt源
apt-get update

#安装docker-ce,如果前面准备工作都做好了就可以执行安装了
apt-get install docker-ce

#测试是否安装成功
docker run hello-world

startup and shutdown

service docker start
service docker stop

Modify mirror source

The official mirror library address is abroad, and our docker pulltime will be very slow, and even the timeout cannot be pulled down. For mainland users, it is officially recommended to modify the mirror source address.

method one

Pass in when starting the daemon --registry-mirror:

docker --registry-mirror=https://registry.docker-cn.com daemon

Method Two

Modify the configuration file, modify /etc/docker/daemon.json(create it yourself if you don't have it), and add the following key-value pairs:

"registry-mirrors": ["https://registry.docker-cn.com"]

Finally, restart after changing:

service docker restart

Commonly used mirror addresses in China

Docker official China area:https://registry.docker-cn.com

NetEase:http://hub-mirror.c.163.com

ustc:https://docker.mirrors.ustc.edu.cn

image 和 container

imageand containerare two important elements of docker.

Image

image——Mirror, that is, the binary file formed after the docker packaged and configured environment is integrated. For example, on the basis of other people's Ubuntu image, we have added our own Tomcat server and jdk, and then packaged, this is the custom image

The official defines a library on the docker hub library, which contains images provided by the official, we can directly pull it down through the docker command

# 拉取redis镜像
docker image pull redis

Later, we will give some cheat sheets about imageand containercommon commands. This is mainly to introduce the concept

container

container - container, he is an instance of image. Analogous to java, image is like an Class<T>object, and container is like a Classgenerated instance object T. An image template can generate countless container instances, and these containers have the same environment and are independent of each other

# 以redis镜像为模板,创建一个容器叫test-redis
docker container run --name test-redis -d redis

Common cheat sheets

# 搜索redis镜像
docker seacher redis

#下载镜像,library是官方库,不写的话默认pull官方库
docker pull library/redis

#列出所有的镜像
docker images

#删除redis镜像,这个redis就是镜像名称
docker image rm redis

#删除所有的镜像
docker image rm $(docker image ls -a -q)
#启用redis image作为一个容器,名称为test-redis
docker run --name test-redis -d redis

#查看运行中的容器
docker ps
#或者
docker container ls

# 查看所有的容器(运行的和停止的)
docker ps -a

#关闭容器,test-redis是容器名,填写容器id也可以
docker stop test-redis
#或者
docker container kill e494

#启动容器id为e494的容器,其实就是前面的redis
docker start e494

#删除容器
docker container rm e494

#删除所有停止的容器
docker rm $(docker ps -a -q)

#启用镜像时将内部端口映射到Linux 系统的端口上,这里将容器的6379端口映射到本机的6478端口上
docker run -d -p 6478:6379 --name test-redis redis

#注1:如果是在虚拟机上的容器,其实虚拟机还要再映射一次端口到真实主机的端口上

#注2:如果是局域网,很不幸的,还需要将私有ip映射到公网ip上。。。

#注3:如果没有公网ip,很不幸的,可能需要借助花生壳之类的工具提供公网ip,进行内网穿透orz
# 查看e494容器的运行日志
docker log e494

#进入e494这个容器的内部bash界面操作
docker exec -it e494 bash

Deploy applications based on docker

Deploying an application, that is, making our App into a mirror-Image, can be run everywhere. To make an image, you need to write Dockerfile, this is a text file, and docker generates an image based on this file

Dockerfile main instructions

# 继承一个官方的基础镜像,这里是Python,tag = 2.7-slim
FROM python:2.7-slim

# 设置镜像工作目录
WORKDIR /app

# 拷贝当前目录文件到 镜像/app目录下
ADD . /app

# RUN表示运行一个Linux指令
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# 设置容器对外端口
EXPOSE 80

# 设置环境变量
ENV NAME World

#让容器像一个可执行程序一样运行
ENTRYPOINT ["/bin/echo"]

#启动镜像时执行的指令,这是在image完成后的动作,一个dockerfile只能有一个cmd
CMD ["python", "app.py"]

Deploy a spring boot project using docker

  • 制作 Dockerfile ash
    `` bash

    dockerfile

    Download the base image java:8

    FROM java:8

Copy dockerdemo.jar (our springboot package file) to the mirror directory and rename it to app.jar

ADD dockerdemo.jar app.jar

The external port of the mirror package is 8080

EXPOSE 8080

Run the command java -jar /app.jar when the container is enabled

ENTRYPINT ["java","-jar","/app.jar"]


- 编译镜像

将Dockerfile和jar包一起放在`/var/app`文件夹下,运行指令

luzj/docker is the image name, followed by . indicates that the directory of the Dockerfile is in the current directory

docker build -t luzj/docker .


- 查看镜像库

docker images

![docker镜像查询](https://images2018.cnblogs.com/blog/758765/201804/758765-20180430173306784-170015821.png)

- 运行镜像

docker run -d --name mydocker -p 8080:8080 luzj/docker
```

  • accesshttp://192.168.11.128:8080/index
    access container

refer to

docker official documentation

Docker China official image acceleration

Spring Boot in action

Getting Started with Docker

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325099950&siteId=291194637