Recently, the company has been promoting containerization and k8s, and the project will be changed to Docker deployment. There are several node projects in charge of the project, so you can only learn Docker from scratch.
install
Docker supports windows, MAC, Linux, tutorial reference Docker Installation Tutorial.
Docker is recommended for Mac and Linux systems.
Usually development, I use the vscode editor, docker plug-in can be installed conveniently. Search docker in the plug-in store, and after installation, we can easily manage Docker images and containers.
Rapid use
First let's experience Docker.
In peacetime work, if our computer development environment is Windows, one day, we hope to do something in Linux environment. What should we do? Most people choose to install a ubuntu system on a virtual machine (without a cloud server). But before installing the virtual machine, you have to download several G images, configure some parameters in VMware, and finally wait for at least ten minutes for system installation. By the time you install a ubuntu system, it's estimated that you've wasted several hours.
However, with Docker, you only need a few minutes!
# Pull up ubuntu image docker pull ubuntu # Create a ubuntu container and interact with the terminal docker run -it --name my-ubuntu --rm ubuntu /bin/bash
Once created successfully, you go into a ubuntu system, where you can now do whatever you want.
Note: Although Ubuntu is currently in the container, you can only imagine it as a streamlined version of ubuntu, so there are many common commands that need to be installed by yourself.
curl -v bilibili.com
Running curl command directly will prompt that the command does not exist
# Install curl apt-get update apt-get install -y curl
After the installation is complete, you can use the curl command
Exit container
exit
Basic concepts
- Image: Similar to a mirror in a virtual machine. There are two kinds of mirror: basic mirror and personal mirror. Basic mirrors are provided by major manufacturers, such as ubuntu mirrors and node mirrors. Personal images are uploaded by individual developers.
- Container: Similar to a lightweight sandbox. Containers are created based on mirrors, and Ubuntu mirrors do not interact with us in any way. We want an environment to run ubuntu, so we create a container based on Ubuntu mirrors.
- Repository: Similar to the code repository, this is the mirror repository, where Docker centralizes the storage of mirror files.
We can make an analogy like this:
# Download source code git clone deepred5/app # Start app npm run start
# Pull mirror image docker pull deepred5/app # Create container docker run deepred5/app
Docker is based on c/s architecture: we execute Docker commands in Client, and the last created Container and Image run in Server.
# You can view server and client information docker info
Image
Frequently used commands
# Lookup mirror docker search ubuntu # Pull the image of a specific tag version (latest by default) docker pull ubuntu:18.0.4 # View all local images downloaded docker images # delete mirror docker rmi ubuntu:18.0.4
Constructing mirrors
We usually build personal images based on basic images. Mirrors are constructed from a single instruction (Dockerfile)
Let's build a node-pm2 image with node and pm2:
Create a node-pm2 directory and create a new Dockerfile
mkdir node-pm2 cd node-pm2 touch Dockerfile
Edit Dockerfile
# Basic Mirror Based on Noe11 FROM node:11 # Some metadata, such as author information LABEL maintainer="deepred5 <deepred5@gamil.com>" # Install pm2 RUN npm install pm2 -g --registry=https://registry.npm.taobao.org # Exposure container port EXPOSE 80 443
Create our own mirror deepred5/node-pm2 based on this Dockerfile
docker build -t deepred5/node-pm2:1.0 .
Notice the last one.
Look at our own mirror
# You can see the deepred5/node-pm2 image docker images
Start a container based on deepred5/node-pm2 image
docker run -it deepred5/node-pm2:1.0 /bin/bash
After entering the container, we run pm2 -v and you can see that pm2 has been installed successfully.
Upload mirroring
If we want our locally built images to be used by others, we need to upload them to the warehouse. Sign in dockerhub Register an account.
# Log in to your account and enter your username and password docker login # Upload mirroring docker push deepred5/node-pm2:1.0
Note: When deepred5/node-pm2 is changed to your username/node-pm2, you need to reconstruct a mirror of your username/node-pm2 before uploading it to dockerhub
Container
Usually we are dealing with containers.
# Create my-ubuntu containers based on Ubuntu images. If there is no Ubuntu image locally, you will go to docker pull to download it first. docker run -it ubuntu:latest --name my-ubuntu /bin/bash
Parametric interpretation:
-i
: Allow you to enter standard input into the container (STDIN) Interaction
-t
: Specify a pseudo terminal or terminal in the new container.
--name
: Container name, default is random name
/bin/bash
: Commands executed immediately after starting the container# Stop container docker stop my-ubuntu # Starting container docker start my-ubuntu # Delete container docker rm my-ubuntu # Delete all containers docker rm `docker ps -aq`
# View the running container docker ps # View all created containers (run or close) docker ps -a
The docker start my-ubuntu start container, although the container is running, but we can not enter the container.
How do I get into the container again?
docker exec -it my-ubuntu /bin/bash
Two Ways of Container Operation
- Interactive Running (-it)
- Daemon Running (No Interactive Session, Long Running, Suitable for Running Applications and Services) (-d)
It can be analogized as follows:
node index.js: Interactive Running
pm2 start index.js: daemon operation
Most of the time, it's running daemonized containers.
# Start the container and it closes immediately. docker run ubuntu /bin/bash # The container is started and the interactive terminal is opened. Only input exit can exit the terminal. After exit, the container is still running in the background. docker run -it ubuntu /bin/bash # Start the container and run in the background, exporting hello world every 1s docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
View Container Log
docker run -d --name my_container ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
# View the logs running in the background docker logs my_container # Real-time monitoring (similar to tail-f) docker logs -f my_container # Get the last 10 lines docker logs --tail 10 my_container # Real-time view of recent logs docker logs --tail 0 -f my_container # Add a timestamp docker logs -t my_container
Nginx
Nginx is the most commonly used static server on the front end.
docker run -d --name my-nginx -p 8888:80 nginx
Visit http://localhost 8888 / You can see the familiar welcome page
Parametric interpretation:
- d: As explained in the Basic Chapter, guardianship mode of operation
- p: Port mapping. 8888:80 means mapping local port 8888 to port 80 of the container
Why map ports? Because each container in Docker is relatively independent and has its own internal ip. For some network applications running in containers to be accessible externally, ports need to be mapped to the host.
docker port my-nginx
80/tcp - > 0.0.0.0:8888 to see the port of the mapping
What if we want to modify the content of the Nginx welcome page?
The easiest way to think about it is to go into the container and modify the index.html in the / usr/share/nginx/html directory.
# Enter the nginx container docker exec -it my-nginx /bin/bash
However, this method is not very expansive. If there are multiple Nginx containers, do we need to go into the containers one by one to modify it?
Then the concept of Volume will be introduced.
Volume
Similar to port mapping, we can map the directory inside the container to the directory of the host computer to realize sharing and reuse between containers.
New my-nginx directory, new index.html
mkdir my-nginx cd my-nginx touch index.html
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <h1>hello world</h1> </body> </html>
docker run --name nginx-test \ --rm -p 8888:80 \ -v $PWD:/usr/share/nginx/html \ -d nginx
Tip: If the command line is too long, you can write on multiple lines using symbols
Visit http://localhost 8888/Already changed!
Parametric interpretation:
- v: $PWD:/usr/share/nginx/html means that the / usr/share/nginx/html in the container is mapped to the current directory, i.e., my-nginx directory. So the index.html returned by nginx becomes our local index.html.
We can try to create a new 1.html locally and then access it. http://localhost 8888/1.html can also see the output.
Similarly, if we want to modify the configuration of Nginx in the container, we can also map the container's / etc/nginx/conf.d / locally and then create a new configuration mydefault.conf locally.
To review the basics, we want to build a local mirror based on Nginx. The default welcome page content is our newly created index.html.
In my-nginx directory, create a new Docker file
FROM nginx # Copy the current index.html to the container's / usr/share/nginx/html/index.html COPY ./index.html /usr/share/nginx/html/index.html EXPOSE 80
Docker build-t my-nginx. build mirror
Docker run-d -- rm-p 4445:80 my-nginx creates containers, accesses http://localhost 4445 can see the effect.
Redis
We can also run Redis in Docker.
docker pull redis docker run -d --name my-redis -p 6389:6379 redis
Enter the container and connect to redis
# Enter my-redis container and execute redis-cli command in the container docker exec -it my-redis redis-cli
So we can connect to redis and execute the corresponding redis command.
# Set name set name tc # Get name get name
Because we mapped port 6379 of the container to port 6389 of the local machine, we can also directly connect redis in the container locally.
# You need to install redis-cli locally redis-cli -h 127.0.0.1 -p 6389 # Return to tc get name
summary
We learned the basic concepts of Docker image and container, the usage of port mapping (-p) and directory mapping (-v), and how to use Nginx and Redis in Docker. stay Next article Here, we will continue to introduce Docker's actual combat.