Basic knowledge of

What is a docker

Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container and then distribute them to any popular Linux machine, as well as virtualization. Containers are completely sandboxed and have no interface with each other. Docker can package the application in the way of container. Rapid deployment in different environments can be realized through the way of image. It can also be packaged once and shared for many times in the team. We package locally the programs that pass the compilation test into images, which can be quickly deployed in the server environment and sometimes solve problems caused by different development environments.

The mirror

Image is an executable package. Docker packages the application and dependencies into Images. It provides files such as programs, libraries, resources, and configurations required by the container for runtime, and also contains configuration parameters (such as anonymous volumes, environment variables, and users) prepared for runtime. This image file is used to generate Docker containers. There are two kinds of mirrors: base and personal. Basic images are provided by various vendors, such as Ubuntu images and Node images. Personal images are built and uploaded by individual developers. There is a Docker repository: hub.docker.com/search?q=&t…

The container

Containers are runnable instances of images, which by default are isolated from other containers and their hosts, with their own separate process space and network configuration. The container runs directly on the host and has no kernel of its own.

Docker and virtual machine

A VM is equivalent to the entire operating system. Installing and running software on this OPERATING system consumes large resources and is slow to start up. Docker, on the other hand, is not a complete operating system. The container runs directly on the host, has no kernel of its own, and has no virtual hardware. Each container has its own file system, which does not affect each other.

Data volume

-v File location on the host: file location in the container (similar to -p) Absolute path, relative path error message

Test 1 /bin/bash: After the image name is the command, here we want an interactive Shell, so /bin/bash is used.

Test 2

Named and anonymous mounts write only one is the location within the container

Data volume shares are copied, so one container is removed and other easy data remains.

Common commands

Grab image file to local, go to docker official provided image library to grab.

docker image pull hello-world
Copy the code

View the list of image files

docker image ls
Copy the code

Run the image file,

 docker container run hello-world
Copy the code

Run the docker container run command to generate a running container instance. In addition, the docker container run command finds that there is no specified image file locally. This is the docker image pull command.

View a list of running containers

Docker PS or Docker Container lsCopy the code

View all containers created (running or closing)

docker ps -a
Copy the code

I met DockerFile

//docker build construct image, -f pathname by which file to construct, -t construct image name, Generate your own runnable image in the current directory with the command //. Docker build -f dockerFile1 -t /liping/centosCopy the code

Each of these commands is a layer of the mirror

Docker inspect container ID for volume mounted anonymously

Dockerfile write command

Note: CMD runs at docker run time. RUN is in the Docker build.

Image search

 docker search nginx
Copy the code

Mirror pull

docker pull nginx
Copy the code

Publish an image for sharing

1, login Docker, login can be ignored this step

docker login
Copy the code

2. Label the local image. The default value of tag is latest

 docker image tag [imageName] [username]/[repository]:[tag]
Copy the code

3. Publish the image file

docker image push [username]/[repository]:[tag]
Copy the code

Docker builds nodeJS images

Create a hello-docker directory and create app.js

const http = require('http')
const PORT = 3000
const server = http.createServer((req,res) => {
    res.end('hello docker')
})
server.listen(PORT,() =>{
    console.log('running---',PORT)
})
Copy the code

Create package.json at the same level

{" name ":" hello - docker ", "version" : "1.0.0", "description" : ""," author ":" May ", "main" : "app. Js", "scripts" : {" start ": "node app.js" }, "dependencies":{} }Copy the code

Create a new Dockerfile at the same level

RUN mkdir -p /usr/src/nodejs # create a directory WORKDIR /usr/src/nodejs #Dockerfile Each directive in the Dockerfile command creates an image layer, and each image layer can be reused and cached without changes to the Dockerfile directive or copied project file. # RUN/COPY is layered, package.json is advanced, as long as it is not modified, Wouldn't reinstall the package COPY package. Json/usr/SRC/nodejs/package. The json RUN CD/usr/SRC/nodejs/RUN NPM I # all file COPY of the current directory to the Image /usr/ SRC/nodejs/expose 3000 CMD NPM startCopy the code

If you do this, you will need to download the NPM package every time you rebuild the image! Take full advantage of the Docker cache

COPY . /usr/src/nodejs/
RUN npm i
Copy the code

To build a hello-docker image, run the docker image build -t hello-docker command.

The -t parameter is used to specify the file name of the image. The last parameter is not omitted, indicating the directory of the Dockerfile file. When the image runs, it will execute the commands in the Dockerfile file one by one

After executing the above command, let’s take a look at the newly generated image file Hello-docker. Using the docker image command or in the Docker desktop application, you can see the newly generated image file Hello-docker

Run the container

After the image is successfully constructed, use the docker container run command to generate a container and make the image run.

docker container run -d -p 3001:3000 hello-docker

-d: indicates that the container runs in background, daemon mode (no interactive sessions, long-running, suitable for running applications and services)

– IT Runs interactively. You can exit the terminal only by entering exit. After exiting the terminal, the container is still running in the background.

-P: indicates port mapping. Port 3000 of the local machine is mapped to port 3001 of the container, so that the extranet can access our services through port 30001. Why map ports? Because each container in Docker is relatively independent and has its own internal IP. For some network applications running in a container to be accessible externally, ports need to be mapped to the host.

Hello-docker: is the name of our mirror

You can see the new container:

At this point, our Node.js service is already running in the Docker container virtual environment, curl http://localhost:30001 can be tested

Check the log

4ffceef152F4 is the container ID. Run the docker logs -f 4FFceef152F4 command

Go further and dynamically set environment variables

Modify package.json in the original Hello-Docker project

app.js

dockerfile

Can build a Docker image

docker image build . -t lp/hello-docker
Copy the code

Run the mirror

docker run -d -p 3000:3002 lp/hello-docker
Copy the code

Only one environment can be packaged according to CMD NPM run dev above.

Modify dockerfile

Build the image and pass in the environment variables

docker build --build-arg node_env=dev . -t lpp/hello-docker
Copy the code

run

docker run -d -p 3002:3003 lpp/hello-docker
Copy the code

See the log

Take it one step further

In the project, create a new DockerFile

WORKDIR /data/web/ # copy dist to /data/web/ # copy dist to /data/web/ /dist/ /data/web/ COPY nginx.template/TMP / Nginx. template CMD envsubst '$API_URL $POINT_URL' < / TMP /nginx.template  > /etc/nginx/nginx.conf && nginx -g "daemon off;"Copy the code

Add docker command to package.josn, copy nginx. template to TMP directory

Create a new docker_run.sh file in the script directory to set the environment variables

NPM run docker executes the statement in docker_run.sh and the docker service runs. Open the run terminal from the Docker desktop

The nginx.template content of our project is copied intact to the TMP folder

Nginx. conf uses nginx.template as the template and the environment variables have been replaced to see the actual nginx configuration file.

reference

Jartto. Wang / 2020/07/04 /…

Mp.weixin.qq.com/s/S7ksqF8z4…