The front Docker basic course

Why learn Docker?

Each learn a thing, we must be based on a requirement to learn, we all know, is the most troublesome software development environment to configure, develop good, very uncomfortable deployment problems, so in order to ensure the development, testing, deployment, consistent environment, and effective deployment so I chose container technology rather than VM, while Docker is based on the Linux open-source project container technology, and its mantra is: "build once, run everywhere", with a light weight, speed, community activists, and high scalability .

installation

I point to enter the official website to install
Docker support Linux, Mac, Window, direct download network install Quguan on the line

Quick Start

Learning new skills requires a Hello World, let's quickly begin to experience Docker
directly into the terminal to start it!

# 拉取nginx镜像
docker pull nginx
# 创建一个nginx容器
docker run -d --name test-nginx -p 3000:80 nginx


Then open localhost: 3000 to access the page to the familiar nginx

It is not very simple? That we are going to talk about the specific composition of Docker

镜像 image

Mirroring is a binary file, which has an application-dependent, and only through it, it is possible to generate Docker containers, equivalent to the same die, the same image file may generate multiple instances of the container

For how to make a mirror image, in general, we are all generated by the base image processing others in our own image, rather than starting from scratch, but we can also here to share our image, which is why we choose one of its reasons, we can enjoy the community contribution

Mirroring commonly used commands

# 列出本机的所有 image 文件。
$ docker image ls
# 拉取镜像
$ docker pull [imageName]
# 删除 image 文件
$ docker image rm [imageName]

Construction of a mirroring Dockerfile

We have already used the nginx mirrored, and this time we are trying to build its own image with Dockerfile
create a new folder, inside a new index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Hello World</title>
  </head>
  <body>
    <h1>Hello World!</h1>
  </body>
</html>

Then create a file Dockerfile

# 声明基于 nginx 最新的镜像 这里说一下镜像名:后面的是标签 默认是latest
FROM nginx:latest
# 把刚才 index.html 复制到 nginx 的 html 路径去
COPY index.html /usr/share/nginx/html/index.html
# 声明暴露80端口
EXPOSE 80

Run command

#  生成一个名为 nginx 标签为0.0.1 的镜像 ,注意最后还有一个 .
docker image build -t nginx:0.0.1 .
# 根据刚生成的镜像 启动容器
docker run -d --name test-nginx1 -p 3001:80 nginx:0.0.1

This time again visit locahost: 3001 is not the default nginx visit the page
here explain the -p parameter is a 80 port mapping to the native container port in 3001

Container container

Container commonly used commands

# 查看正在运行的容器
docker ps
# 查看所有创建过的容器(运行或者关闭)
docker ps -a
# 停止容器
docker stop [container]
# 启动容器
docker start [container]
# 删除容器
docker rm [container]
# 查看后台运行的日志
docker logs [containe]

These containers can first use the command operations at the container we created earlier, try
it on one of our own to build a mirror instead of the default page nginx, that if we want to change how to do? In addition to building a new image, we also can be modified directly into the container

docker container exec -it [containe] /bin/bash

This command into the shell of the container
, of course, the development of real time, certainly can not go so modified, just like the previous port mapping can also be a directory inside the container also mapped out to achieve common share

Data Volume Volume

Back to our building in front of the mirrored folder, and then start a nginx container (and container port name used here to create a fast start with a container of the same, ask someone to review the vessel command to delete it and then create)

docker run -d --name test-nginx -v $PWD/index.html:/usr/share/nginx/html/index.html -p 3000:80  nginx

We opened locahost: 3000 to modify the contents of index.html Hello Volume, refresh the page to see the content of our modified

What we learned in this section can be developed based on the use of docker, then there are on the network, a plurality of container services, free and then more

Network Networking

To be continued ...

Combination docker-compose

To be continued ...

to sum up

A brief look at how to use the Docker and basic commands, if students want to read a book, you can see the first book of this Docker

Guess you like

Origin www.cnblogs.com/cnyball/p/12370122.html