The preface

As a love of learning front-end siege lion, in the current crazy volume of the environment ๐Ÿฑ, not a volumeDockerIs it a little difficult to say, plus now most of our front-end deployment projects areDockerSo now get in the car and follow Uplook lookBig old is welcome

  • Q: Can you tell me what you think about itDocker.DockerWhat can you do?
  • A:DockerIs a portable application container for automated testing and continuous integration and release

In the interview, you answered ๐Ÿ˜‚. Congratulations, you got it right, but it was not complete enough. Now let’s combine the document and Demo to see what Docker can do

concept

What is a Docker

Docker is like a container filled with all kinds of goods. In a large ship, goods can be laid out neatly. And all kinds of goods are standardized by containers, containers do not affect each other.

Some people think Docker is a virtual machine, but this idea is wrong

The Guest OS layer and Hypervisor layer are replaced by the Docker Engine layer. The Guest OS is a complete OS kernel installed on a VM. The Hypervisor layer of a VM can be simply regarded as a hardware virtualization platform. It exists as a kernel driver on the Host OS.

Three core concepts

The mirror(image)

Image is the basis of docker container creation. Docker image is similar to virtual machine image, which can be understood as a docker engine oriented read-only module, including file system

How to create an image

  1. useDockerfile BuildThe mirror
  2. pullDockerThe official image

The container(the container)

Containers are application running instances created from images and are isolated from each other. Think of the container as a simplified version of the Linux system environment (includingrooT permissions, process space, user space, network space, etc.), and application boxes packed with applications running on this environment.

You can use the docker create command to create a container, the created container is in the stopped state, you can use the docker start command to start it. You can also run the Docker run command to run a container directly from the image startup. Docker run = docker creat + docker start.

When creating and starting a container using Docker run, docker’s standard operations in the background include:

(1) Check whether the specified mirror exists locally. If not, download it from the public repository. (2) Create and start a container using the image. (3) Allocate a file system and mount a read-write layer outside the read-only mirror layer. (4) Bridge a virtual interface from the network bridge interface configured by the host machine to the container. (5) Configure an IP address from the address pool to the container. (6) Execute the application program specified by the user. (7) The container terminates after the execution.Copy the code

warehouse(Repository)

After Docker is installed, a set of local private warehouse environment can be set up through the officially provided Registry image.

Download the Registry image:

Basic operation

Install the Docker

linuxThe installationDocker

windowsThe installationdocker

Docker Desktop is recommended

Pull the mirror

# pull mirror
>>> docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
f3ef4ff62e0d: Pull complete
Digest: sha256:a0d9e826ab87bd665cfc640598a871b748b4b70a01a4f3d174d4fb02adad07a9
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

# View all local mirrors>>> Docker images REPOSITORY TAG IMAGE ID CREATED SIZE Ubuntu latest 597CE1600CF4 13 days ago 72.8MB Hello Latest 8b9d88b05a48 2 weeks ago 231MB centos latest 5d0da3dc9764 4 weeks ago 231MB docker/getting-started latest 083d7564d904 4  months ago 28MB# delete mirror
>>> docker rmi ubuntu
Untagged: ubuntu:latest
Untagged: ubuntu@sha256:a0d9e826ab87bd665cfc640598a871b748b4b70a01a4f3d174d4fb02adad07a9
Deleted: sha256:597ce1600cf4ac5f449b66e75e840657bb53864434d6bd82f00b172544c32ee2
Deleted: sha256:da55b45d310bb8096103c29ff01038a6d6af74e14e3b67d1cd488c3ab03f5f0d

Copy the code

Create a container

Create a container
>>> docker create --name my-ubuntu ubuntu
2da5d12e9cbaed77d90d23f5f5436215ec511e20607833a5a674109c13b58f48

# start container
>>>  docker start 2da5d

# View all containers
>>> docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED              STATUS                      PORTS     NAMES
2da5d12e9cba   ubuntu    "bash"    About a minute ago   Exited (0) 31 seconds ago             my-ubuntu

# delete container
>>> docker rm 2da5d

Create and enter the container
>>> docker run --name my-ubuntu2 -it ubuntu
root@552c7c73dcf6:/#
Once inside the container, you can execute scripts inside the container

Enter the running container
>>> docker exec -it 2703b1 sh
/ #

Copy the code

choreographyDockerfile

Dockerfile is a text file that creates all the commands of the image, including a sequence of instructions and instructions, each instruction to build a layer, through the docker build command, according to the content of the Dockerfile to build the image, so the content of each instruction, is to describe how to build the layer. With Dockefile, you can make your own docker image rules, just need to add or modify instructions on Dockerfile, you can generate docker image.

FROM ubuntu          Which image is the new image constructed based on
MAINTAINER Up_zhu    # Maintainer information
RUN yum install nodejs          Shell command to run when building the image
WORKDIR  /app/my-app            Set the working path
EXPOSE 8080                     Specifies the port on which the outside world interacts, that is, the port on which the container listens at runtime
ENV MYSQL_ROOT_PASSWORD 123456  Set the container environment variable
ADD ./config /app/config        Copy a file or directory to the image. If it is a URL or compressed package, it will be downloaded or decompressed automatically
COPY ./dist /app/my-app
VOLUME /etc/mysql               # define anonymous volume
Copy the code

In actual combat

Based on vite project mirroring, release

newDockerfile

FROM nginx
COPY ./dist/ /usr/share/nginx/html/
COPY ./nginx/default.conf /etc/nginx/conf.d/default.conf
Copy the code

newnginxThe configuration file

# nginx/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    access_log  /var/log/nginx/host.access.log  main;
    error_log  /var/log/nginx/error.log  error;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }}Copy the code

Hit the mirror

Viewing a Local Mirror

>>> docker images
REPOSITORY               TAG       IMAGE ID       CREATED              SIZE
my-vite                  latest    cc015756264b   About a minute ago   133MB
Copy the code

Start the container

You can now access the address to verify success

View locally running containers

At the end of the article

Is it Easy? We can see from the above, Docker function is very powerful, in addition, we can also pull some Ubuntu, Apache and other images, can also customize the image, published to Docker Hub.

Of course! This article introduces only the basic functions of Docker, xiaobian ability to this, still need to continue to learn ~

Refer to the link

Docker official documentation

Docker Hub

Docker Chinese Community

DockerInfo Chinese document