Docker
Please provide the content you would like translated to English. docker change source sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' Please provide the content you would like translated to English. "registry-mirrors": ["https://o6ljoubs.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker Please provide the content you would like translated to English. Dock worker Docker objects Images: an executable file of Docker that includes all the code content, dependencies, environment variables, and configuration files needed to run an application. Wait. Containers: Instances of images that have been run. Network: The method of network access between external or containerized environments, such as host mode and bridge mode. Data Volumes: A shared storage method between the container and the host, as well as between containers, similar to a shared file directory between a virtual machine and the host. The description file for a Docker image is the Dockerfile, which includes the following instructions.
- FROM defines the base image ●MAINTAINER Author
- RUN Execute Linux commands
- ADD Add file/directory
- ENV environment variable
- CMD run process
Basic command format of Docker:
1 Basic Format
2 systemctl [parameters] docker
#Parameter Details:
start Start the service
stop close
restart
status: Status
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
-a, --attach=[] Attach to running container (started with docker run -d)
-c, --cpu-shares=0 Set the CPU weight of the container for use in CPU sharing scenarios.
--cap-add=[] Add capabilities, see details at: http://linux.die.net/man/7/capabilities
--cap-drop=[] Drop capabilities, see list at: http://linux.die.net/man/7/capabilities
--cidfile="" Write the container PID value to a specified file after running the container, a typical usage in monitoring systems.
--cpuset="" Set which CPUs the container can use, this parameter can be used to dedicate CPU to the container.
-d, --detach=false Specify whether the container runs in the foreground or background
--device=[] Add host devices to the container, equivalent to device passthrough
--dns=[] Specify the DNS servers for the container
--dns-search=[] Specify the DNS search domain for the container, which is written to the /etc/resolv.conf file of the container.
-e, --env=[] Specify environment variables that can be used in the container
--entrypoint="" Override the image's entrypoint
--env-file=[] Specify the environment variable file, with each line containing one environment variable.
--expose=[] Specify the ports exposed by the container, i.e., modify the exposed ports of the image.
-h, --hostname="" Specify the hostname of the container
-i, --interactive=false Open STDIN for console interaction
--link=[] Specify the association between containers, using IP, env, etc., information from other containers.
--lxc-conf=[] Specify the container configuration file, only used when --exec-driver=lxc is specified.
-m, --memory="" Specify the memory limit for the container
--name="" Specify the container name, subsequent container management can be done by name, and the links feature requires using the name.
--net="bridge" Container network settings, to be detailed
-P, --publish-all=false Specify the ports exposed by the container, to be detailed later.
-p, --publish=[] Specify the ports to expose on the container, details to follow
--privileged=false Specifies whether the container is a privileged container, which has all capabilities.
--restart="" Specify the restart policy for the container after it stops, to be detailed later.
--rm=false Specify that the container should be automatically removed after it stops (does not support containers started with docker run -d).
--sig-proxy=true Set the proxy to accept and process signals, but SIGCHLD, SIGSTOP, and SIGKILL cannot be proxied.
-t, --tty=false Allocate a tty device, which can support terminal login
-u, --user="" Specify the user for the container
-v, --volume=[] Mounts a storage volume to the container at a specific directory.
--volumes-from=[] Mount volumes from another container to this container at a specific directory. -v Host:Container Directory
-w, --workdir="" Specify the working directory for the container
Parameter explanation:
-i interactive command operation
-t start a terminal docker image prune - Remove unused images
Docker installation
perl
wget -O /etc/yum.repos.d/Centos-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
Enable traffic forwarding in the Linux kernel
```bash
cat <<EOF > /etc/sysctl.d/docker.conf
net.bridge.bridge-nf-call-ip6tables=1 net.bridge.bridge-nf-call-iptables=1 net.ipv4.conf.default.rp_filter=0 net.ipv4.conf.all.rp_filter=0 net.ipv4.ip_forward=1 EOF
Load parameters to modify the kernel, configuration file
Follow the commands in the following order
[root@yc_docker01 ~]# modprobe br_netfilter [root@yc_docker01 ~]#
[root@yc_docker01 ~]#
[root@yc_docker01 ~]# sysctl -p /etc/sysctl.d/docker.conf
Use yum to quickly install Docker #Preconfigure the yum repository
1. Aliyun's built-in repository
2. Aliyun's Docker-specific repo repository
curl -0 /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo curl -0 /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
Update yum cache
[root@yc_docker01 ~]# yum clean all && yum makecache
Now you can install Docker directly using yum.
Yum Installation
$ sudo yum install docker-ce-20.10.6 -y
Check available versions in the source
$ yum list docker-ce --showduplicates | sort -r
If an old version needs to be installed
sudo yum install -y docker-ce-18.09.9
#If you want to uninstall yum remove -y docker-XXx
Try to start, using Docker now.
Image accelerator
- Modify the Docker configuration file, we choose the Qiniu Cloud mirror site. Change
[root@yc_docker01 ~]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh
sh -S http://f1361db2.m.daocloud.io
[root@docker01 ~]# cat /etc/docker/daemon.json
{"registry-mirrors": ["https://reg-mirror.qiniu.com"]} #Can also
Configure Source Acceleration
https://cr.console.aliyun.com/en-us/hangzhou/instances/mirrors
mkdir -p /etc/docker
vi /etc/docker/daemon.json
{"registry-mirrors" : [ "https://8xpk5wnt.mirror.aliyuncs.com" ] } 2. Restart and set to start automatically at boot. #Set to start automatically at boot systemctl enable docker systemctl daemon-reload Sure, please provide the content you would like translated to English.
perl
Review the image again
[root@yc_docker01 ~]# docker image ls
Run the nginx image, which will start a specific container. Then, an nginx service will run inside this container.
The command to run the well image is as follows:
docker run parameters
The name/ID of the image
# -d run container in the background
# -p 80:80
Port mapping, Host port: Container port, When you access this port on the host, you are accessing it inside the container.
Ports inside the container
The output is as follows: The `docker run` command will return a container ID.
[root@yc_docker01 ~]# docker run -d -p 80:80 nginx
1a033aef64fec 78acc9007dc 2d7c8e4ed9849ca00286676583777758a91fc84f
Check if the container is running.
[root@yc_docker01 ~]# docker ps
# Try stopping the container to see the result
docker stop container_id
# Stop the specific container process
docker stop 1a033aef64fe
# [root@yc_ docker01 ~]# docker start 1a033aef64fe
Export image using docker save
docker load to import an image
You are transferring image files between your company and colleagues.
Export the image as a compressed file.
Methods for persisting Docker images or containers
Docker images and containers can be exported in two ways.
docker save #ID or #Name
docker export #ID or #Name
The difference between docker save and docker export
For the Docker Save method, it will save all history records of that image.
For the Docker Export method, no history is retained, i.e., there is no commit history.
`docker save` saves an image, while `docker export` saves a container.
`docker load` is used to load an image package, while `docker import` is used to load a container package. However, both will be restored as images.
`docker load` cannot rename the loaded image, while `docker import` can specify a new name for the image.
save command
docker save [options] images [images...]
Example
docker save -o nginx.tar nginx:latest
Or
docker save -o nginx.tar nginx:latest
Where `-o` and `>` indicate output to a file, `nginx.tar` is the target file, and `nginx:latest` is the source image name (name:tag).
load command
docker load [options]
Example
docker load -i nginx.tar
or
docker load < nginx.tar
Where -i and < indicate input from a file. It will successfully import the image and related metadata, including tag information.
export command
docker export [options] container
Example
docker export -o nginx-test.tar nginx-test
Export as tar
docker export #ID or #Name > /home/export.tar
Where `-o` indicates output to a file, `nginx-test.tar` is the target file, and `nginx-test` is the source container name (name).
import command
docker import [options] file|URL|- [REPOSITORY[:TAG]]
Example
docker import nginx-test.tar nginx:imp
or
cat nginx-test.tar | docker import - nginx:imp
# Run the container and enter it
# Parameter explanation: -i interactive command operation -t open a terminal afb6fca791e0 is the image ID bash enter container
After that, the executed command
[root@yc_docker01 ~]# docker run -it afb6fca791e0 bash
# Check the distribution version inside the container
[root@ff1c937d0497 /]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
Enter the running container using `docker exec`
[root@yc_docker01 ~]# docker exec -it 1a033aef64fe bash
docker search image_name:tag tag is the specific version label
docker search centos
Download Docker image
docker pull centos # 默认的是 centos:latest
docker pull centos:7.8.2003
# Check the storage path of Docker images
[root@yc_docker01 ~]# docker info | grep Root
Docker Root Dir: /var/lib/docker
# Use different images to generate containers
#-it starts an interactive terminal
Remove the container when it exits.
[root@yc_docker01 ~]# docker run -it --rm
CentOS
balh
docker images # lists the image IDs
-q --quiet List only the ID
# Format Display Image
# This is the template language for Docker, --format
[root@yc_docker01 ~]# docker images --format "{{ .ID}}--{{ .Repository}}"
Display in table format and beautify.
docker images --format
"table {{ .ID}}\t{{ .Repository}}\t{{.Tag}}"
# According to the image ID, name, summary, etc.
#Deleted images must not have any dependent container records
docker rmi hello-world
#Delete container record
[root@yc_docker01 ~]# docker rm f1635b8377e5
f1635b837 7e5
Specify the first three digits of the ID.
docker rmi d11
Please provide the content you would like translated to English.
Basic Docker commands
Clean up with one command:
docker kill $(docker ps -q);
docker rm $(docker ps -a -q);
docker rmi $(docker images -q -a)
Remove none
docker images | grep none | awk '{ print $3 }'
docker rmi -f $(docker images | grep none | awk '{ print $3 }')
docker images | grep none
## Image Management
Usage of batch deleting images, rm
#Batch delete images, use with caution
docker rmi, docker images -a
#Batch Delete Containers
docker rm, docker ps -aq
# Export Image
For example, the default CentOS image does not provide vim functionality. After running this container, install vim inside the container.
# Then submit the image, export it as a compressed file, and you can send it to others for use.
# Learn in the Container Management Section >
docker commit
# Command to export the image
[root@yc_docker01 ~]# docker image save centos:7.8.2003 >
/opt/centos7.8.2003.tgz
# Import image command
[root@yc_docker01 ~]# docker image load -i /opt/centos7.8.2003.tgz
View detailed information about the image
# Check the information of the Docker service
docker info
docker image inspect image_id
docker image inspect afb
Container Management
perl docker run Equals creation + startup docker run image_name, if the image does not exist locally, it will download the image online. Note: The process inside the container must run in the foreground; otherwise, the container will exit directly. When deploying a container yourself and running it, the command must not be used. Run in the background, run in the foreground as well. If nothing is done inside the container, the container will also hang up. There must be a process running in the foreground inside the container. We run the nginx base image without running any program, so the container directly dies. #Playing with Containerized Applications
1. Run a crashed container, (learn Docker containers from bad examples)
docker run centos:7.8.2003 This approach will generate multiple independent container logs, and since there are no programs running inside the containers, they will hang.
2. Run the container, enter the container, and execute a command inside the container
[root@yc_docker01 ~]# docker run -it centos:7.8.2003 sh
sh-4.2#
3. Start a container to run a program for you, in the foreground, which will block one terminal.
[root@yc_docker01 ~]# docker run centos:7.8.2003
ping baidu.com
4. Run a live container that can be seen with docker ps
-d parameter, to run the container in the background (from the host's perspective)
Return container ID
[root@yc_docker01 ~]# docker run -d centos:7.8.2003 ping baidu.com 608dd3
5. Enrich the parameters for running Docker
[root@yc_docker01 ~]# docker run -d --name pythonav centos:7.8.2003 ping pythonav.cn
-d run in the background
#--rm automatically removes the container after it exits
--name Give the container a name
#6. How to view container logs and refresh them
[root@yc_docker01 ~]# docker logs -f
docker logs f2598cb26363 | tail -5
7. Enter the running container space
[root@yc_docker01 ~]# docker exec -it f2598cb26363 bash
8. View detailed information about the container for advanced debugging
docker container inspect container_id
9. Port Mapping for Containers
Run the nginx container in the background, name it, and map port 85 of the host to port 80 of the container. [root@yc_docker01 ~]# docker run -d --name yuchao_ nginx -p 85:80 nginx
View container
9.1 View the port forwarding situation of the container
docker port container_id
[root@yc_docker01 ~]# docker port 2e73fac44507
#Random Port Mapping -P Randomly access an idle port on the host and map it to an open port inside the container
[root@yc_doc ker01 ~]# docker run -d --name yuchao nginx_random -P nginx
10. Committing the Container
Run the basic centos:7.8.2003, install vim inside the container, then commit the new image
New image, containers run from it will have vim installed by default
[root@yc_docker01 ~]# docker run -it centos:7.8.2003 bash
#Submit Container docker commit container_id new_image_name Sure, please provide the content you would like translated to English.
Dockerfile Instruction Learning
perl
WHO is the mother of this image? (Specify the base image)
MAINTAINER tells others who is responsible for taking care of it? (Specify maintainer information, which can be omitted.)
RUN What do you want it to do (add RUN in front of the command)
ADD Add the host's file to the container and added an automatic decompression function.
# RUN tar -zxf /opt/xx.tgz # Error! The tgz file does not exist! !
The COPY instruction is the same as ADD, both of which copy files from the host to the container. COPY simply copies.
WORKDIR I am cd, I just put on makeup today (set the current working directory)
VOLUME give it a place to store luggage (set up the volume, mount the host directory)
EXPOSE It specifies the port to be opened (the port exposed externally).
CMD Run, brother! (Specify what the container should do after startup)
Sure, please provide the content you would like translated to English.
Dockerfile practice
Requirements: Build an Nginx image using a Dockerfile, and after running the container, the generated page should display "Super Brother Takes You to Learn Docker."
Please provide the content you would like translated to English.
# 1. Create Dockerfile, note the filename, it must be this one.
[root@yc_docker01 learn_docker]# pwd
/learn_docker
[root@yc_docker01 learn_docker]# vi Dockerfile
FROM nginx
RUN echo ' <meta charset=utf8 >Brother Chao takes you to run the nginx service with Docker.' >
/usr/share/nginx/html/index.html
# 2. Build Dockerfile
docker build
# 3. Modify Image Name
[root@yc_dockeE01 learn_docker]# docker tag b4200a856253 my_nginx
# 4. Run the image
Please provide the content you would like translated to English.
Running shell commands via CMD will also be converted to the shel1 form.
For example
CMD echo %PATH%
will be converted to
CMD ["sh", "-C", "echo $PATH"]
Sure, please provide the content you would like translated to English.
# ADD
ADD chaoge.tgz /home/
RUN
linux command (xxx modification command)
# Run a command inside the container to start the program
What specific parameters does this image execute when running container instances?
CMD ["parameter1", "parameter2"]
CMD ["/bin/bash"]
CMD ["cat", "/etc/os-release"]
The command executed when the container is running.
# Equivalent to direct operation in the command line: docker run -it centos
cat /etc/os-release
Please provide the content you would like translated to English.
Put the concept of installing and starting Nginx on the host into the Dockerfile.
1. RUN yum install nginx
2. MODIFY the RUN configuration file with sed
3. RUN systemctl start nginx The programs inside the container must run in the foreground; otherwise, your container will not start.
4. The correct one should be CMD ["nginx" ,"-g", "daemon off;"]
## ENTRYPOINT and CMD
Both serve the same purpose as CMD, which is to specify the program and its arguments to be run when the container starts.
When ENTRYPOINT is specified, the semantics of the CMD instruction change (CMD cannot specify arguments).
But instead, the content of CMD is passed as an argument to the ENTRYPOINT instruction.
## ARG and ENV
Set environment variables like ARG and ENV
The difference is that the ENV variable can be used both during image build and container runtime.
ARG is just a variable that needs to be set for building the image and disappears at container runtime.
## VOLUME
At runtime, containers should ensure that no data is written to the storage layer. Data generated within the container should be recommended to be mounted and written to
On the host machine, perform maintenance.
mount /mnt
VOLUME /data
Mount the /data folder inside the container as an anonymous volume automatically when the container is running. Any data written to
The operations of writing data to the directory will not be recorded by the container, ensuring the stateless concept of the container's storage layer.
Dockerfile 1
FROM centos
VOLUME [" /data1", " /data2"]
When this container is running, these two directories are automatically mapped to the host machine's directories.
docker inspect command to view
docker inspect 5 b12bf9d63e9
The way container data is mounted, through the Dockerfile, specifying the VOLUME directory.
2. Directly set the directory to be mapped and mounted using the docker run -v parameter.
## EXPOSE
Specify the port services provided by the container runtime to the outside.
Help users of this image quickly understand the business of one port in the container.
docker port container
docker run -p host_port:container_port
docker run -P # The purpose is to map random host ports to container ports.
# Get the IP inside the container
docker inspect --format '{{ .NetworkSettings.IPAddress }}' ed4e6852f620
WORKDIR
Used for switching directories in a Dockerfile, changing the working directory
WORKDIR /opt
USER
Used to change the environment, used to switch users
USER root
Please provide the content you would like translated to English.
RUN curl -0 /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo;
RUN curl -0 /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo;
RUN yum makecache fast;
RUN yum install python3-devel python-pip -y
RUN pip3 install -i https://pypi.douban.com/simple flask
Please provide the content you would like translated to English.
# Get the IP address inside the container
docker inspect --format '{{ .NetworkSettings.IPAddress }}' ed4e6852f620
Postscript
4: Installation of Docker
rm -rf /etc/yum.repos.d/local.repo
curl -0 /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
sed -i 's#download\.docker\.com#mirrors\.tuna\.tsinghua\.edu\.cn/docker-ce#g' /etc/yum.repos.d/docker-ce.repo
sudo yum install docker-ce -y
docker run -d -P 1 422:22 centos6.9 ssh:v1 /usr/sbin/sshd -D
Steps to upload Docker to a private repository:
### 1: Tag the image
docker tag centos6.9:v1 10.0.0.11:5000/centos6.9:v2
### 2: Upload Image
docker push 10.0.0.11:5000/centos6.9:v2
Error:
The push refers to repository [10.0.0.11:5000/centos6.9 ssh]
Get https://10.0.0.11:5000/v2/: http: server gave HTTP response to HTTPS client
Solution:
vi /etc/docker/daemon.json
I "registry-mirrors": ["https://registry.docker-cn.com"],
"insecure-registries": ["10.0.0.11:5000"]
systemctl restart docker
## Image Acceleration
Sure, please provide the content you would like translated to English.
sudo tee /etc/docker/daemon.json > /dev/null << EOF
Please provide the content you would like translated to English.
"registry-mirrors": [
"https://dc.j8.work"
"https://dockerproxy.com"
"https://docker.zhai.cm"
]
}
EOF
Please provide the content you would like translated to English.
# registry with basic authentication
yum install httpd-tools -y
mkdir /opt/registry-var/auth/ -p
htpas swd
-bbn oldboy 123456 >> /opt/registry/var/auth/htpasswd
docker run -d -p 5000:5000 --restart=always -v /opt/registry-var/auth/:/auth/
/opt/myregistry: /var/lib/registry -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry"
"Realm" -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd registry (image name)
6:00 PM
Solution to restart Docker service and have all containers exit
Method
docker run --restart=always
Method two: "live-restore": true
Docker server configuration file /etc/docker/daemon.json reference
Please provide the content you would like translated to English.
"registry-mirrors": ["http://b7a9017d.m.daocloud.io"],
"insecure-registries": ["10.0.0.11:5000"],
"live-restore": true
}
20: Docker Network Types
None: No network functionality is configured for the container, --net=none
Container: Shares Network Namespace with another running container,-
net=container: containerID
Host: Share the Network Namespace with the host, --net=host
Bridge:
The NAT network model designed by Docker
21: Docker Cross-Host Communication with macvlan
Add eth1
# Create macvlan network
docker network create --driver macvlan --subnet 172.16.1.0/24 --gateway 172.16.1.1 -o parent=eth1 jiage
Set the eth1 network card to promiscuous mode
ip link set eth1 promisc on
## Create a container using the macvlan network
docker run -it --network jiage --ip=172.16.1.3 busybox:latest /bin/sh
22: Docker Cross-Host Communication with Overlay
http://www.cnblogs.com/CloudMan6/p/7270551.html
1) Preparation work
On docker01:
docker run -d -p 8500:8500 --restart=always -h consul --name consul progrium/consul -server -bootstrap
docker02, 03_:
vim /etc/docker/daemon.json
"hosts": ["tcp://0.0.0.0:2376", "unix:///var/run/docker.sock"],
"cluster-store": "consul://10.0.0.11:8500",
"cluster-advertise": "10.0.0.11:2376"
}
systemctl restart docker
2) Create an overlay network
systemctl restart docker
2) Create an overlay network
docker network create --subnet=172.16.3.0/24 -d overlay o11
3) Start container testing
docker run -it --network o11 --name oldboy01 busybox:latest /bin/sh
## Docker Compose
linux
Installation on Linux is also very simple; you can directly download the pre-compiled binary file from the official GitHub Release. For example, download the corresponding binary package for a 64-bit Linux system.
Sure, please provide the content you would like translated to English.
$ sudo curl -L https://github.com/docker/compose/releases/download/1.25.5/docker-compose-\((uname -s)-\)(uname -m) > /usr/local/bin/docker-compose
3 sudo chmod +x /usr/local/bin/docker-compose
Sure, please provide the content you would like translated to English.
The core is services.
It's actually the relationship between services.
Check if the installation was successful
docker-compose --version
Start
docker-compose up
The basic usage format of the docker-compose command is
docker-compose [-f=<arg>...] -p [options] [COMMAND] [ARGS...]
2. Command Options
-f, --file FILE Specify the Compose template file to use. The default is docker-compose.yml. Multiple files can be specified.
-p, --project-name NAME Specify the project name. By default, the directory name where it is located will be used as the project name.
--x-networking Use the pluggable network backend feature of Docker
--x-network-driver DRIVER specifies the driver for the network backend, default is bridge
--verbose Output more debug information.
-v, --version Print the version and exit.
2. down command
Purpose: Used to stop all docker-compose services.
3. exec command.
Function: Enter the container
docker-compose exec redis bash
ps command
Purpose: Used to display all containers currently running with docker-compose.
docker-compose ps
resume
^ docker-compose unpause [SERVICE...]'
Resume the service that is in a paused state.
docker-compose logs service_id to view the service logs
volumes:
# Declare the automatically created volume names used by the above service
tomcatwebapps01: # Declare the volume name, compose will automatically create this volume name but will prepend the project name.
external: # Use custom volume name
false # true indicates that the specified volume name will be used. Note: -- Once an external custom volume name is used to start the service, it must be manually created beforehand.
tomcat webapps02 :
mysql_data:
mysqlconf:
redisdata:
portainer_data:|
networks:
# 定义服务使用的桥
hello: # Define the bridge name used by the above service, which is created by default as bridge
external: .
true # Use an externally specified bridge. Note: The bridge must exist.
(2) Docker-Compose.yml file
YAML is a concise unmarked language. YAML is data-centered, using spaces, indentation, and line breaks to organize data, thereby making the representation more concise and readable.
The commonly used configuration items in Docker-Compose.yml are as follows:
- environment: Define environment variables and configurations
- image: Start the container from the specified image
- build: Define image generation
- ports: Define port mapping
- depends_on: defines dependency relationships
● volumes: Mount a directory or an existing data volume container
- context: Specify the file path of the Dockerfile
- args: Specify build arguments
- target: Define the Dockerfile for the specified build stage
- command: The `command` can be used to override the default command executed after the container starts.
- container_name: Specify a custom container name instead of the generated default name.
(3) Common Docker Compose Commands
- docker-compose up -d nginx: Build and start the Nginx container.
- docker-compose exec nginx bash: Log in to the Nginx container.
- docker-compose down: Remove all Nginx containers and images.
- docker-compose ps: Display all containers.
- docker-compose restart nginx: Restart the Nginx container.
- docker-compose rm nginx: Remove the container (the container must be stopped before removal).
● docker-compose stop nginx: Stop the Nginx container.
● docker-compose start nginx: Start the Nginx container.
Detailed command parsing: https://blog.csdn.net/qq_36148847/article/details/79427878
Standard example of a YAML Docker Compose configuration file
version: "3"
services:
redis:
image: redis:alpine
ports:
- "6379"
networks:
- front-end
deploy:
replicas: 2
update_config:
parallelism: 2
delay: 10 seconds
restart_policy:
condition: on-failure
db:
image: postgres:9.4
volumes:
- db-data:/var/lib/postgresql/data
networks:
- backend
deploy:
placement:
constraints: [node.role == manager]
vote:
image: dockersamples/examplevotingapp_vote:before
ports:
- 5000:80
networks:
front-end
depends\_on:
- Redis
deploy:
replicas: 2
update_config:
parallelism: 2
restart_policy:
condition: on-failure
result:
image: dockersamples/examplevotingapp_result:before
ports:
- 5001:80
networks:
- backend
depends\_on:
- database
deploy:
replicas: 1
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
worker:
image: dockersamples/examplevotingapp_worker
networks:
front end
- backend
deploy:
mode: replicated
replicas: 1
labels: [APP=VOTING]
restart_policy:
condition: on-failure
delay: 10 seconds
max_attempts: 3
window: 120s
placement:
constraints: [node.role == manager]
visualizer:
image: dockersamples/visualizer:stable
ports:
"8080:8080"
stop_grace_period: 1m30s
volumes:
"/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints: [node.role == manager]
networks:
frontend:
backend:
volumes:
db-data:
docker-compose deployment of MySQL configuration
version: '3'
services:
mysql:
image: mysql
environment:
MYSQL_ROOT_PASSWORD=123456
MYSQL_DATABASE=wordpress
volumes:
- $PWD/conf:/etc/mysql/conf.d
- $PWD/logs:/logs
- $PWD/data:/var/lib/mysql
container_name: db
ports:
"3306:3306"
Detailed Explanation of Docker's Configuration File daemon.json
vim /etc/docker/daemon.json
```json
{
"registry-mirrors": ["https://r9xxm8z8.mirror.aliyuncs.com"]
}
"insecure-registries": ["172.16.213.38:5000"], "live-restore": true } The first line above is for Alibaba Cloud Docker Image Acceleration. The second line is the address of the local Docker image repository without SSL. The third line is that you must send a SIGHUP signal to the daemon process to reload the configuration. Extension: If you want to configure multiple public or private image repositories, do the following: Please provide the content you would like translated to English. "registry-mirrors": ["https://r9xxm8z8.mirror.aliyuncs.com", "https://registry.docker-cn.com"] "insecure-registries": ["172.16.213.38:5000", "172.16.213.39:5000"] } This way, both public and private image repositories can be used simultaneously.
systemctl restart docker.service # Restart Docker to proceed
Error occurred when running Docker-compose up:
ERROR: yaml.scanner.ScannerError: mapping values are not allowed in this context
"in './docker-compose.yml', line 2, column 9"
Solve
This error occurred because the YAML format is incorrect. Google's definition of the YAML format is too strict; each colon must be followed by a space.
Error: Writing file './docker-compose.yml' is invalid because: services.jenkins.volumes contains an invalid type, it should be an array.
ERROR: The Compose file './docker-compose.yml' is invalid because: services.jenkins.volumes contains an invalid type, it should be an array
Unsupported config option for services.mysql: 'MYSQL_ROOT_PASSWORD'
The type of services.mysql.environment is invalid; it should be an object or an array.
There should be a space between the - and the value, and the value should be a string. Please refer to the docker compose volume documentation.
You should add a space after the - in your docker-compose file.
Mounting folders and files in Docker images
Check /data/docker/conf/conf.d and find that it is empty. Because Docker cannot mount files, it can only mount folders, so the default.conf file needs to be copied again.
docker cp test:/etc/nginx/conf.d/default.conf /data/docker/nginx/conf/conf.d
Please provide the content you would like translated to English.
The cp command is for files. If it's an executable sh file, can we consider mounting the directory or the parent directory?
The container cannot ping Baidu.
Enter the container with `docker run -it ID bash --network=host`
When building Docker, if there is an error in installing yum, the yum source configuration is fine. Add host during the build.
docker build . --network=host -t