What is a Docker
- Containers for applications
- Development, testing, operation and maintenance preferred container technology
- lightweight
- scalability
- Build once, share many times, run anywhere
The principle of
The most simple and wrong perception of Docker is that “Docker is a virtual machine with very good performance”.
Compared with traditional virtual machine, Docker technology is much more advanced. Specifically, Docker does not virtual a set of hardware on the host and then virtual an operating system, but allows the process inside the Docker container to run directly on the host (Docker can do the isolation of files and networks, etc.). As a result, Docker will be “lighter, faster, and can be created with more data on the same host”.
Docker has three core concepts: Image, Container, and Repository.
- The concept of mirroring will be familiar to programmers with a “good guy card” bent. But in contrast to iso images like Windows, images in Docker are layered and reusable, rather than simply a bunch of files stacked on top of each other (similar to the difference between a compressed source package and a Git repository).
- Container: A Container cannot exist without the support of images. It is a carrier of the image runtime (similar to the relationship between instances and classes). Based on the virtualization technology of Docker, it creates independent “Spaces” for containers, such as ports, processes, and files. Container is a “Container” that is isolated from hosts. Containers can communicate with hosts through ports, volumes, and networks.
- Repository: A Docker Repository is similar to a Git Repository, with a Repository name and tag. After the image is built locally, it can be distributed through the repository. Common Docker hubs include hub.docker.com/, cr.console.aliyun.com/, etc.
Tencent Yunyun server installed Docker
Tencent Cloud provides developer LABS to help users build Docker environment.
Install and configure Docker
Install the Docker
Adding Image Information
During the actual installation, installation cache or failure may occur due to network problems in the country.
# # official to mirror yum - config - manager - add - repo < https://download.docker.com/linux/centos/docker-ce.repo > # it is recommended to use ali cloud mirror to accelerate, Or too slow yum - config - manager - add - repo < http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo >Copy the code
The Docker package is included with the default centos-Extras software source. So to install Docker, just run the following yum command:
yum install docker-io -y
Directly install yum and check the version after the installation is successful
docker -v
Start the docker
service docker start
Setting boot
chkconfig docker on
Configuration Docker
Because it is slow to access Docker Hub in China, you can use the domestic image source provided by Tencent Cloud to speed up access to Docker Hub
Run the following commands in sequence
echo "OPTIONS='--registry-mirror=https://mirror.ccs.tencentyun.com'" >> /etc/sysconfig/docker
systemctl daemon-reload
service docker restart
Copy the code
An error
Docker simple operation
Download mirror
Download an official CentOS image to your local PC
docker pull centos
The downloaded image will appear in the image list
docker images
Run the container
Now we can operate in the container generated by the CentOS image we just downloaded.
Generate a centos image as a template container and use bash shell
docker run -it centos /bin/bash
At this point, you can see that the front end of the command line has changed to [root@(hash Id)], which indicates that we have successfully entered the CentOS container. Executing any command inside the container does not affect the host machine, as follows
mkdir -p /data/simple_docker
You can see that the simple_docker folder has been created in the /data directory
ls /data
Out of the container
exit
The simple_docker folder is not in the /data directory of the host machine, indicating that operations in the container do not affect the host machine
ls /data
Save the container
View all container information to obtain the container ID
docker ps -a
Then run the following command to save the image:
Docker commit -m=" note "your CONTAINER_ID your IMAGE
Change the information after -m to your own container information
Docker image operation command
Docker image command
Download the Docker image
docker pull centos
View all local mirrors
Docker images or Docker Image lS-a
Boot image
Docker run -it NAME:TAG /bin/bash // format
Docker run -it centos /bin/bash
Run Docker image (daemon mode)
Docker run -d
View mirror details
Docker inspect NAME:TAG //
Delete a Docker image
Docker image rm
Cleaning up images (temporary and unused images)
docker image prune
Creating a Mirror Alias
Docker tag IMAGEID{IMAGEID} REPOSITORY: tag // format
Docker tag ccbCEA8a6757 test:test
Docker container command
View all running containers
docker ps
docker ps -a
List all containers (including stopped containers)
docker ps -l
View all container ids
docker ps -a -q
Enter the running Docker container
Docker exec it {container ID} /bin/bash
Container renaming
Docker rename nginx-1 web-1 // Rename nginx-1 to web-1
Start an existing container
Docker start {container ID}
Stopping the specified container
Docker stop {container ID}
Deleting a specified container
Docker rm -f {container ID}
Delete stopped containers
docker container prune
The IP addresses of all containers
docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
View historical run logs of the Docker container
Docker logs {container name}
Docker logs -f -t {container ID} // -t displays time stamps
Docker file operation command
Copy files from the host to the Docker container
Sudo docker cp file path within {host} {container ID}, {} container file storage path
Copy files from the Docker container to the host
Sudo docker cp {docker ID}:{docker ID}
Docker data volume command
Create a Docker data volume
Docker volume create docker volume create
List all Docker data volumes
docker volume ls
Delete the specified Docker data volume
Docker volume rm
Delete unassociated (invalid) Docker data volumes
docker volume prune
docker volume rm $(docker volume ls -qf dangling=true)
Deleting a specified container
Docker rm -f {container ID}
Delete stopped containers
docker container prune
View historical run logs of the Docker container
Docker logs {container name}
Docker starts the image command
Docker run: Creates a new container and runs a command
grammar
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Copy the code
The OPTIONS:
- -a stdin: specifies the standard input and output types. The options are stdin, STDOUT, or STDERR.
- -d: Runs the container in the background and returns the container ID.
- -i: Runs the container in interactive mode, usually with -t.
- -p: indicates random port mapping. Ports inside a container are randomly mapped to ports on a host
- -p: specifies the port mapping. The format is host (host) port: container port
- -t: reassigns a pseudo-input terminal to the container. It is usually used together with -i.
- –name=”nginx-lb”: specify a name for the container;
- — DNS 8.8.8.8: Specifies the DNS server used by the container. The default DNS server is the same as the host server.
- –dns-search example.com: specify the container DNS search domain name, which is the same as the host by default.
- -h “Mars “: specifies the hostname of the container.
- -e username=” Ritchie “: Set the environment variable.
- –env-file=[]: reads environment variables from specified files;
- –cpuset=”0-2″ or –cpuset=”0,1,2″: bind the container to the specified CPU;
- -m: sets the maximum memory used by a container.
- – net = “bridge” : specify the container’s network connection type, support bridge/host/none/container: four types;
- –link=[]: Add link to another container;
- –expose=[]: Open a port or group of ports;
- –volume, -v: Binds a volume
The instance
Start a container in backend mode using the Docker image nginx: Latest and name the container mynginx.
docker run --name mynginx -d nginx:latest
Start a container in backend mode using mirroring nginx: Latest and map port 80 of the container to a random port on the host.
docker run -P -d nginx:latest
Start a container in backend mode, map port 80 of the container to port 80 of the host, and map the directory /data of the host to /data of the container.
docker run -p 80:80 -v /data:/data -d nginx:latest
Bind port 8080 of the container and map it to port 80 of localhost 127.0.0.1.
Docker run -p 127.0.0.1:80:8080/ TCP Ubuntu bash
Use the image nginx:latest to start a container in interactive mode and run the /bin/bash command inside the container.
docker run -it nginx:latest /bin/bash
Docker deploys the Vue project
- Use the Vue CLI to create a vue project, modify the created project, write a front-end interface request on the page, build a version of online resources, build a front-end engineering image based on the Nginx Docker image, and then based on the front-end engineering image, Start a container, vuenginxContainer.
- Start nodeWebServer, a container based on node images that provides a back-end interface.
- Modify the nginx configuration of VuenginxContainer to forward interface requests from front-end pages to NodeWebServer.
- A little refinement and improvement.
Create a VUE application
Without further ado, directions are given to create vuE-CLI3 projects. Note: There should be interface calls in the project
Packaging project
yarn build / npm run build
There is an additional dist folder in the project root directory
If you upload the dist directory to the server in its entirety, you can deploy it as a static resource site and have direct access to the project.
Build a VUE application image
Nginx is a high performance HTTP and reverse proxy server. Here we use nginx image as the base to build our VUE application image.
Pull the image of Nginx
docker pull nginx
You already have an Nginx image. We run the container based on an Nginx image
docker run --name=nginx -d -p 4030:80 nginx
Then access port 4030 in your browser to verify that the container is working properly
Create the nginx config configuration file
Create the nginx-docker folder in the root directory of the project. In this folder, create the default.conf file
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 /api/ { rewrite /api/(.*) /$1 break; Proxy_pass http://172.18.0.3:9728; } 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
Pointing to the homepage of the configuration file defines as/usr/share/nginx/HTML/index. The HTML, so we can for a while to build up the index. HTML documents and related static resource in/usr/share/nginx/HTML directory.
Add the Dockerfile file
FROM nginx
COPY dist/ /usr/share/nginx/html/
COPY nginx-docker/default.conf /etc/nginx/conf.d/default.conf
Copy the code
- Custom build images are built based on Dockerfile.
FROM nginx
This image is built based on nginx:latest image.COPY dist/ /usr/share/nginx/html/
Copy all files from the dist folder in the root directory of the project to /usr/share/nginx/html/.COPY nginx/default.conf /etc/nginx/conf.d/default.conf
Command will mean nginx – docker directory. The default conf copied to the/etc/nginx/conf. D/default. Conf, with local default. Conf configuration to replace nginx default configurations in the mirror.
Build the vUE application image based on the Dockerfile
After the file is uploaded, build the image of the Vue program (run the command to be careful not to lose the last “. )
Docker build -t dockervue. // -t specifies the name of the image. The image is built based on the Dockerfile of the current directory
Run the VUE container
Start the container based on the Dockervue image, run the following command:
docker run --name=dockervue -d -p 9020:80 dockervue
docker run
Start a container based on the image-p 3000:80
Port mapping, which maps port 3000 of the host to port 80 of the container-d
Background mode running--name
Container name view docker process
You can see the container is running and we are accessing port 9020 in the browser
So far, a static resource service has been deployed through the Docker container to access static resource files.
Build the Node interface service image
Obtaining node Image
docker pull node:alpine
Write Dockerfile to docker-like koA2 project
RUN mkdir -p /usr/dockerfile/web/node COPY. /usr/dockerfile/web/node Pm2 RUN NPM install pm2 -g # RUN NPM install pm2 CMD [" NPM ", "PRD "]Copy the code
Node_modules dependencies are installed directly through RUN NPM install when building the image, and a.dockerignore file is created in the project to ignore some files that are skipped directly:
node_modules
npm-debug.log
Copy the code
Build the mirror
docker build -t dockernode .
Start the Docker-Node container
-d indicates background execution, -p 55:9728 (9728:9728) indicates (local port: Docker run --name=dockernode -d -p 9728:9728 dockernodeCopy the code
Cross domain forwarding
You want to forward requests from the Vue container to the Node container. First, you need to know the IP address and port of the node container. Currently, it is known that the internal service of the node container listens on port 9728, and you also need to know the IP.
Query the IP address of the Nodeserver container
Docker inspect [containerId]
docker inspect 02277acc3efc
Find the Networks related configuration information:
You can also view the IP addresses of all containers
docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
Record the IP address of the Node service container, which will be used later when configuring nginx forwarding.
Modify the nginx configuration
- Nginx configurelocation to point to the node service default.conf (Nginx).
- Add a rewrite rule to redirect/API /{path} to the target service’s /{path} interface. Add to nginx-docker/default.conf file:
location /api/ {
rewrite /api/(.*) /$1 break;
proxy_pass http://172.17.0.2:8080;
}
Copy the code
After making the changes, I realized that the vue container runs on the image of Dockervue, and that the nginx configuration default.conf was built directly into the image. So if you want to change default.conf, you have to build a new image and run the new container based on the new image.
To improve the
Can you restart the container every time you change the configuration file to make the new configuration take effect? Yes.
Instead of copying the Nginx configuration to the image, mount it directly to the host and restart the container after each configuration change.
Modify the Dockerfile file
Modify the Dockerfile under vueclidemo project
FROM nginx
COPY dist/ /usr/share/nginx/html/
COPY nginx-docker/default.conf /etc/nginx/conf.d/default.conf
Copy the code
Will COPY nginx docker/default. Conf/etc/nginx/conf. D/default. Conf command delete, nginx configuration is through the mount command to mount on the host machine. COPY dist/ /usr/share/nginx/ HTML /, if you need to build a new image and start a new container every time the contents of the dist/ build are changed, you can remove this command and start the container by mounting it.
New run vUE application container
To start the container vuenginxnew directly from the nginx image, run the following command:
docker run \
-p 3000:80 \
-d --name vuenginxnew \
--mount type=bind,source=$HOME/SelfWork/docker/vueclidemo/nginx,target=/etc/nginx/conf.d \
--mount type=bind,source=$HOME/SelfWork/docker/vueclidemo/dist,target=/usr/share/nginx/html \
nginx
Copy the code
--mount type=bind,source={sourceDir},target={targetDir}
Mount the host’s sourceDir to the container’s targetDir directory.- The command running here is long, and if it is troublesome to re-type each time, we can save the entire command to one
shell
filevueapp.sh
Then execute the command directlysh vueapp.sh
.
This way, every time you change your Nginx configuration or rebuild your Vue application, you just need to restart the container to take effect immediately.