Docker is needed for development in recent work. This article demonstrates several demos from scratch. If you have never touched Docker before, you can follow the operation step by step to deepen your understanding of Docker.
For more, please try to see it at 💻
This article was originally written by HelKyle, DJI front-end development engineer, and shared in Nuggets
What problem does Docker solve
No matter how big or small your company is, the development and production environments are at odds with each other. Some developers may use Windows, some may use Mac, and the production environment may use Linux, running multiple applications at the same time, each relying on a different version of Node, and different services may use the same port. So we often hear this question: “Local is good, why not online?”
(My own experience: early on in the development of a front-end project, I had to set up a LEMP environment locally, followed tutorials for days, and didn’t write a single line of front-end code. Later, because of some “puzzling” problems, repeatedly reinstalled several times.)
After using Docker, we can quickly build the environment through a command of the configuration file, and can achieve isolation from other services, do not affect each other, through examples to explain.
The demo environment
This article is executed on macOS 10.14.1, Docker Version 18.09.0
Concept: Mirror vs. container
An image is a sequence of historical actions. Each action on an image generates new read-only levels. For example, writing, committing, and removing content to a container results in two historical read-only levels, like Git commits. Anything you do in the writable layer is fine, and if you don’t commit, changes will be lost when the container is deleted, a bit like git staging.
(Personal understanding, not necessarily correct)
Install the Docker
Mac installation docker
brew cask install docker
Copy the code
Other environments to install Docker see here.
Once the installation is complete, run Hello-world to try it out.
$ docker run hello-world
Copy the code
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
Copy the code
This command will connect to the local Docker service, Docker service detects that there is no local hello-world image, so go to the Docker image market to download the image, and then create a new container, run the specific command, hello from Docker!
This article will not explain the basic commands of Docker, but will start with examples.
Start the nginx service
docker run -d -p 80:80 --restart=always nginx:latest
Copy the code
-d enables the container to run in the background. -p specifies the port mapping. Port 80 of the host is mapped to port 80 of the container.
Since I don’t have nginx:latest locally, I will download it from the mirror market first. Once you’ve started, go to http://localhost:80 and you’ll immediately see the welcome page for Nginx.
If you want to modify the welcome page, go to the Container Modify page.
docker exec -it 4591552a4185 bash
Copy the code
Exec performs some operations on the container. -it enables the container to accept standard input and allocates a pseudo tty 4591552A4185 is the only flag for the newly started nginx container to specify that the program to interact with is bash
Nginx default file path is/usr/share/nginx/HTML/index. The HTML, echo write content can be used directly.
echo '<h1>Hello Docker<h1/>' > /usr/share/nginx/html/index.html
Copy the code
CTRL + D exits the container and re-accesses localhost:80 to see the Hello Docker.
Each modification requires manual entry into the container, which is too tedious. Besides, as mentioned by 👆, direct modification to the container will not be durable, and data will be lost if the container is deleted.
(Since the previous demo already occupied port 80, let’s kill it first.)
docker kill 4591552a4185
Copy the code
Docker provides the function of data mount, that is, you can specify certain paths in the container to be mapped to the host machine, modify the command, add -v parameter, and start a new container.
docker run -d -p 80:80 -v ~/docker-demo/nginx-htmls:/usr/share/nginx/html/ --restart=always nginx:latest
Copy the code
/ /docker-demo/nginx-htmls /nginx-htmls /nginx-htmls /nginx-htmls /nginx-htmls /nginx-htmls /nginx-htmls
Open http://localhost:80 again and you’ll also see Hello Docker.
Next we will use Node + Redis + Docker to make a PV DEMO.
Run the command:
docker run -d -p 6379:6379 -v ~/docker-demo/redis:/data redis:latest
Copy the code
Start a redis container and persist the data to the ~/docker-demo/redis directory. (For performance, Redis does not write data to disk in real time.)
Start a Node server with KOA and connect to Redis, incrementing the counter with each access /.
const Redis = require('ioredis');
const Koa = require('koa');
const Router = require('koa-router'); const router = new Router(); const app = new Koa(); Const redis = new redis (' redis://127.0.0.1:6379/0 '); router.get('/', async (ctx, next) => {
await next();
await redis.incr('pv');
const current = await redis.get('pv');
ctx.body = `current pv: ${current}`;
});
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(3000);
Copy the code
Visit http://localhost:3000 to see the output.
Visualization of Redis data using Medis is recommended.
OK, functional development of the development environment is completed, and delivery operation and maintenance is online. We assume that the build environment will start four Node services, one Redis service, and one Nginx for the load.
At this time, we need to build our Node service as a mirror, add dockerFile file.
# Based on the latest Node image
FROM node:latest
Copy all files in the current directory to the target image /app/
COPY . /app/
Change the working directory
WORKDIR /app/
# yarn click to install dependencies
RUN ["yarn"]
Start node Server
ENTRYPOINT ["node"."index.js"]
Copy the code
See more dockerfile instructions here
To build it locally, run the docker build. –tag=pv command and see the new image you just built through Docker images.
Docker: Docker-compose: Docker-compose: Docker-compose: Docker-compose Add the docker-comemess. yml file to the project directory.
Use Docker-compose version 2.2
version: "2.2"
# define services
services:
redis:
image: redis:latest
volumes:
- "~/docker-demo/pv/data/:/data/"
web:
Zoom in 4 times and there will be four node servers
scale: 4
build: .
Add environment variables
environment:
- REDIS_HOST=redis://redis:6379/0
# dependencies
depends_on:
- redis
nginx:
image: nginx:latest
depends_on:
- web
- redis
ports:
- 80:80
volumes:
- "./default.conf:/etc/nginx/conf.d/default.conf"
Copy the code
See more docker-compsoe instructions here
REDIS_HOST=redis://redis:6379/0 REDIS_HOST=redis: 6379/0
const redis = new Redis(process.env.REDIS_HOST);
Copy the code
Redis ://redis:6379/0 The first redis is the protocol and the second redis is the service host. Services can communicate with each other through host.
Make a copy of the default.conf file in the nginx container
upstream web {
server pv_web_1:3000;
server pv_web_2:3000;
server pv_web_3:3000;
server pv_web_4:3000;
}
server {
#...
location / {
proxy_pass http://web;
}
#...
}
Copy the code
New upstream service web, where PV is the file name of my project, web is the service name defined in docker-compose file, 1-4 is the serial number automatically given by docker when scale comes out. Once started, nginx’s request to visit http://pv_web_1:3000 reaches the first Web container.
Let’s compose up!
Ok, now go to http://localhost:80
So far, we have deployed the application, which automatically increments the number of PVS per visit by one and randomly assigns them to different containers through nginx load balancing. 🎉 🎉 🎉
conclusion
This article demonstrates some docker operations related to the front-end, from which we can see that it brings great convenience to software development, testing and deployment. This is just the tip of the iceberg. For more comprehensive learning, see Docker – from getting started to getting started.
Examples are also intended for learning purposes only and should not be used directly in a production environment. At the same time, part of the content is personal understanding, no authority, in-depth study of Docker, you should look at the detailed documents.
This article was originally written by HelKyle, DJI front-end development engineer, and shared in Nuggets
Related articles
- The official documentation
- Docker – From getting started to practice