This is the seventh day of my participation in the More text Challenge. For details, see more text Challenge
- 1.Docker Quick Start
- 2.Docker Quickstart 2
- 3.Docker Quickstart 3
- 4.Docker Quick Start
Multi-container application
In general, we refer to multiple services such as mysql, front-end services, Nginx, back-end services, queue services, Redis, etc. We recommend multiple containers for these services because:
- It may be necessary to extend the API and the front end in a different way than the database
- Individual containers can be independently updated to the version
- While you can use a container for your database locally, you might want to use managed services for your database in a production environment. We don’t want to ship the database engine with the application, nor do we need to
- Running multiple processes will require a process manager (the container starts only one process), which increases the complexity of container start/close
start
- Multiple containers to access each other need to be created on the same network (non-local network/LAN, where each container has its own IP address)
docker
network
docker network create todo-app
Copy the code
- Start a MySQL container and attach it to the network,
--network-alias mysql
Specifies a network alias, useful later
docker run -d \
--network todo-app --network-alias mysql \
-v todo-mysql-data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=secret \
-e MYSQL_DATABASE=todos \
mysql:5.7
Copy the code
If you use PowerShell
docker run -d ` --network todo-app --network-alias mysql ` -v todo-mysql-data:/var/lib/mysql ` -e MYSQL_ROOT_PASSWORD=secret '-e MYSQL_DATABASE=todos' mysql:5.7Copy the code
Note: Volumes are automatically created for todo-mysql-data
- Test connection to mysql
docker exec -it <mysql-container-id> mysql -p
Copy the code
- Enter the password to view the database:
SHOW DATABASES;
mysql> SHOW DATABASES;+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | todos | + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- + 5 rows in the set (0.00 SEC)Copy the code
The toDOS database was created successfully
Connect to MySQL
- use
nicolaka/netshoot
Image starts a new container, making sure it is connected to the same network
docker run -it --network todo-app nicolaka/netshoot
Copy the code
- Find hostname
mysql
theIP
address
dig mysql
; <<>> DiG 9.16.16 <<>> mysql
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52110
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
;; QUESTION SECTION:
;mysql. IN A
;; ANSWER SECTION:
mysql. 600 IN A 172.18.0.2
;; Query time: 3 msec
;; SERVER: 127.0.0.11#53(127.0.0.11)
;; WHEN: Wed Jun 23 06:11:23 UTC 2021
;; MSG SIZE rcvd: 44
Copy the code
!!!!!!!!! Mysql = 172.18.0.2; mysql = 172.18.0.2; mysql = 172.18.0.2
Using mysql
- The specified
mysql
Environment variable, switch toapp
Directory, view1.Docker Quick Start
cd xx/app
ls
Dockerfile node_modules package.json spec src yarn.lock
Copy the code
Create a container
docker run -dp 3000:3000 \ -w /app -v "$(pwd):/app" \ --network todo-app \ -e MYSQL_HOST=mysql \ -e MYSQL_USER=root \ -e MYSQL_PASSWORD=secret \ -e MYSQL_DB=todos \ node:12-alpine \ sh -c "yarn install && yarn run dev"Copy the code
If you use PowerShell
docker run -dp 3000:3000 ` -w /app -v "$(pwd):/app" ` --network todo-app ` -e MYSQL_HOST=mysql ` -e MYSQL_USER=root ` -e MYSQL_PASSWORD=secret ` -e MYSQL_DB=todos ` node:12-alpine ` sh -c "yarn install && yarn run dev"Copy the code
Check log, connect to mysql
Yarn install V1.22.5 [1/4] Resolving Packages... Success Already up-to-date. Done in 0.59s. yarn run V1.22.5$ nodemon src/index.js
[nodemon] 1.19.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] starting `node src/index.js`
Waiting for mysql:3306.
Connected!
Connected to mysql db at host mysql
Listening on port 3000
Copy the code
The connection to mysql was successful
- Go to http://localhost:3000/ to add a few
- Check whether the mysql database exists
docker exec -it <mysql-container-id> mysql -p todos
Enter password:
mysql> select * from todo_items;+--------------------------------------+---------+-----------+ | id | name | completed | +--------------------------------------+---------+-----------+ | 987d2be8-49fa-4fef-a0c1-4faf2fcb003e | a s d f | 0 | | bdd05e38-76b8-47c9-b408-22fa1b00b58b | 111 | 0 | +--------------------------------------+---------+-----------+ 2 rows In the set (0.00 SEC)Copy the code
Up to this point, we’ve managed to create a network that includes both mysql and Node services, and the code is hot.
Quick query of common commands
View image: docker image ls View container: docker ps Monitor logs: docker logs-f < container-id >