This is the 10th day of my participation in the More text Challenge. For more details, see more text Challenge
An overview
Because docker is easy to install/use, I tried to use it in daily development and testing. The service/application is independent, isolated from the local environment, and non-invasive.
Tip
1. View files in the Docker container
Grammar:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...] ls [-alrtAFR] [name...]# etc: cat
docker exec nginx ls /etc
docker exec nginx cat /etc/hosts
Copy the code
2. View the IP address of the container
2.1 Method 1, will display itself and (–link) soft connected container IP, not recommended, cumbersome operation
View the container ID, port, and name
docker ps
Copy the code
Into the container
Docker exec -it < container ID/container name> /bin/shCopy the code
Check the hosts
cat /etc/hosts
Copy the code
2.2. Method 2: The information is long and needs to be found
Docker inspect < container ID >Copy the code
2.3. Method 3, display IP only, recommended
Docker inspect -f '{{range. NetworkSettings.Net works}} {{. IPAddress}} {{end}}' < containers id/name >#orDocker inspect -f '{{. NetworkSettings. IPAddress}}' < containers id/name ># 172.17.0.3
Copy the code
2.4. Method 4, obtain all container names and IP addresses, recommended
docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(docker ps -aq)
#or
docker inspect --format='{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
Copy the code
Using the docker-compose command, then:
docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
Copy the code
Bashrc, vi ~/.bashrc, vi ~/.bashrc
function docker_ip() {
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $1
}
function dockeriplist() {
docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
}
Copy the code
Run source ~/.bashrc (this command must be executed every time the terminal restarts)
# IP docker_ip < container ID/container nameCopy the code
Step 3 Enter the container
Docker exec -it < container ID/container name> /bin/bashCopy the code
4. The Volumn management
Docker volume inspect edc-nginx-vol // Create a custom docker volume docker volume ls // View all container volumes docker volume inspect edc-nginx-vol // View details about a specified volume docker volume rm <volumeid> // Delete a customized data volume. The volume is not automatically deleted after the container is deletedCopy the code
reference
language
1. The Python 3.5
Create a demo. Py file in ~/python/demo (native MAC, win) directory.
#! /usr/bin/python
print("Hello, World!");
Copy the code
Switch to the directory above
CD ~/python docker run -v $PWD/demo:/usr/src/demo -w /usr/src/demo Python :3.5 Python demo.py Hello, World!Copy the code
Cons: You have to create a container every time
instructions
-v, $PWD/demo:/usr/src/demo: mounts the demo in the current directory on the host to /usr/src/demo of the container. -w, /usr/src/demo: specifies the /usr/src/demo of the container as the working directory
2. PHP + MySQL + Nginx => dnmp(Docker Nginx MySQL PHP)
- create
~/nginx/conf/conf.d
directory - add
~/nginx/conf/conf.d/php7.conf
File, code as follows:
server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { fastcgi_pass php:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /www/$fastcgi_script_name; include fastcgi_params; }}Copy the code
Create php7, MySQL, nginx containers
# phpdocker run --name php7 -v ~/nginx/www:/www -d \ -v ~/nginx/php7/conf/conf.d:/usr/local/etc/php/conf.d:ro \ PHP: 7.4.20 - FPM
#Mysql: port 3307 is used on the local computer. Mysql is also used on the local computer
docker run -itd --name mysql8 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql
# nginx
docker run --name nginx -p 8083:80 -d \
-v ~/nginx/www:/usr/share/nginx/html:ro \
-v ~/nginx/conf/conf.d:/etc/nginx/conf.d:ro \
--link php7:php \
nginx
Copy the code
/nginx/ WWW/phpinfo.php
echo phpinfo();
Copy the code
Visit: http://localhost:8083/phpinfo.php
Installing the PHP extension
Go into the PHP container
docker exec -it php7 /bin/bash
Copy the code
The installation
docker-php-ext-install pdo pdo_mysql mysqli
Copy the code
/ Users/woojufon/nginx/php7 / conf/conf. Add docker d directory – PHP – ext ini:
extension=mysqli.so
extension=pdo_mysql.so
Copy the code
Restart the PHP7 container
testphp
+MySQL
Get MySQLIP:
Docker inspect -f '{{. NetworkSettings. IPAddress}}' mysql8 172.17.0.3Copy the code
Test the connection
$servername = "172.17.0.3";
$username = "root";
$password = "123456";
// Create a connection
$conn = new mysqli($servername.$username.$password);
// Check the connection
if ($conn->connect_error) {
die("Database connection error:" . $conn->connect_error);
}
echo "Mysqli database connection successful";
? >
<br />
try {
$pdo = new pdo("Mysql: host = 172.17.0.3; dbname=mysql"."root"."123456");
} catch (PDDException $e) {
echo "Database connection error";
}
echo "Pdo database connection successful";
? >
Copy the code
Test success
The mysqli database connection succeeds. The pDO database connection succeedsCopy the code
Github has a similar open source project: github.com/yeszao/dnmp writers remember to pay for advertising
other
- Front-end project engineering (refer to previous article)
- go && go web
Dockerfile
# source image
FROM golang:latest
# Set the working directory
WORKDIR $GOPATH/src/goweb
# Add server go project code to docker container
ADD . $GOPATH/src/goweb
# Go mod initialization
RUN go mod init
# go Build the executable
RUN go build .
# Expose port
# EXPOSE 8080
# Run the docker command
ENTRYPOINT ["./goweb"]
Copy the code
main.go
package main
import (
"fmt"
"net/http"
)
func handlerHello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello Go docker")}func main(a) {
http.HandleFunc("/", handlerHello)
http.ListenAndServe(": 8080".nil)}Copy the code
Create a mirror image
docker image build -t goweb .
Copy the code
Create a container
Docker run -d --name golang -p 8080:8080 -- IP 172.172.0.10 -v ~/go/webapp/:/ WWW /webapp goweb. Docker run -d --name golang -p 8080:8080 -- IP 172.172.0.10 -v ~/go/webapp/:/ WWW /webapp gowebCopy the code
Go to http://localhost:8080