Docker basic tutorial

preface

Tip: the following is the main content of this article. The following cases can be used for reference

1, Initial Docker

1.1 Docker

  • How does Docker solve the compatibility problems of large projects with complex dependencies and different component dependencies?
  1. Docker allows development to package applications, dependencies, function libraries, and configurations together to form a portable image
  2. Docker applications run in containers and use sandbox mechanism to isolate each other
  • How does Docker solve the problem of differences in development, testing and production environments?
  1. The Docker image contains a complete running environment, including the system cabinet function library, and only depends on the system's Linux kernel, so it can run on the task Linux operating system

2, Install Docker

2.1 installation

	1. Uninstall the old version first
		yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine
	2. Download the required installation package
		yum install -y yum-utils
	3. install docker Alibaba cloud image
		yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
	4. to update yum Package index
		yum makecache fast
	5. install docker
		yum install docker-ce docker-ce-cli containerd.io 
		yum install -y  docker-ce #ce is a free community edition
		Remember: turn off the firewall,otherwise Docker Various ports will be used,It's troublesome for us to manually take open ports one by one
		#Turn off the firewall systemctl stop firewalld
		#Permanently close the firewall systemctl disable firewalld
			
	6. start-up docker
		systemctl start docker
	7. View current installation docker edition
		docker version
	8. function helloworld,The image will be downloaded
		docker run hello-world 
	9. Query the currently downloaded helloworld image
		docker images(image) 

2.2 uninstalling Docker

	yum remove docker-ce docker-ce-cli containerd.io
	rm -rf /var/lib/docker
	rm -rf /var/lib/containerd

2.3 Docker command

2.3.1 viewing docker version

  • docker version
  • docker info

3, Mirror command

An image is equivalent to a class, and a container is equivalent to an instance of a class

3.1 view all images on the local host

  • docker images
  • Optional
  • docker -images -a # view all images
  • docker -images -q # view the ID of all images (quit)

3.2 package the image / decompress the image compression package

  • Package image: docker save -o package name. tar image name: [TAG version]
  • Unzip the image package: docker load -i package name. tar

3.2 Docker image warehouse

  • docker search image name
  • Or go to the website to search the image warehouse of DockerHub docker

3.3 downloading images

  • docker pull image name

3.4 image deletion

  • docker rmi -f container id force forced deletion
  • docker rmi -f container id container id delete multiple images

3.5 startup vessel

Create and start the container

  • docker run [optional parameter] image (image name) / bin/bash
  • Optional parameters
  • -i keep the container running. Usually used with t
  • -d run the container in guard (background) mode and run in background mode
  • -it runs interactively and enters the container to view the content
  • – name name the container
  • The container created by id is generally a guard container
  • -p port mapping: map the host port to the container port. The left side of the colon is the host port and the right side of the colon is the container port. When accessing the host port, you can access the container port
  • bash: the command executed after entering the container. bash is a Linux terminal interactive command

3.6 list all running containers

  • docker ps
  • Optional - a list the currently running containers + bring out the historically running containers

3.7 exit container

  • exit exits the container and stops
  • Ctrl+P+Q container does not stop exiting

3.8 delete container

  • docker rm container id

3.9 starting and stopping containers

The start here refers to the container that starts and stops

  • docker start container id
  • docker restart container id
  • docker stop container id
  • docker kill container id
  • docker pause container id container that is paused
  • docker pause container id cancels the paused container

3.10 viewing logs

  • -t: Time stamp
  • -f: Continuous output
  • -nums outputs several log contents
  • Docker logs - TF -- tail num container id

3.11 viewing process information

  • docker top container id

3.12 viewing container metadata [common]

  • docker inspect container id

3.12 enter the currently running container [common]

  • either-or
  • docker exec -it container id /bin/bash # enter the container and open a new terminal (the container will not close after exiting)
  • The docker attach container id # enters the executing terminal of the container and will not start a new terminal

3.13 copy files from the container to the host

  • docker cp container id: where is the file path copied

3.14 Docker deployment Nginx

	1. docker pull nginx download NGINX
	2. docker images see NGINX image
	3. docker run -d --name nginx01 -p Host port:Container internal port mirroring
		The port mapping relationship inside the container can be accessed by accessing the host port
	4. request curl localhost:Host port

Port exposure concept

3.15 commit image

The docker commit submission container becomes a new mirror copy

  • docker commit -m = "submitted description" - a = "author" container id target image name: [TAG version]

4, Data volume

  • Data volume: a virtual directory that points to a directory in the host file system

4 basic syntax of data volume

	docker volume [command]:
		docker volume The command is a data volume operation,According to the command command To determine the next step:
			create: Create a volume
			inspect: Show one or more volume Information about
			ls: List all volume
			prune: Delete unused volume
			rm: Delete one or more specified volume

4.1 mounting data volumes

  • Method 1: directly use the command to mount
	docker run -it -v Host Directory: Container directory

4.2 anonymous mount

  • -v path in container
	-P :Random mapping port
	docker run -d -P --name nginx01 -v /etc/nginx nginx

4.3 named mount

  • -v volume name: path in container
	docker run -d -P --name nginx02 -v juming-nginx:

4.4 viewing volumes

  • docker volume inspect volume name

4.5 DockerFile

  • dockerfile: the build file used to build the docker image
  • Mirror structure:
    1. Image is the packaging of application and required system function library, environment, configuration and dependency
instructionsexplain
FROMSpecify base instruction
ENVSet the environment variable, which can be used in later instructions
COPYCopy the local file to the specified directory of the image
ADDAdd file
CMDContainer start command
RUNExecute Linux shell commands, which are generally commands during installation
EXPOSESpecifies the port on which the container listens when running. It is for the image user
ENTRYPOINTThe startup command applied in the image, which is called when the container runs

4.5.1 custom DockerFile file

# Specify base mirror
FROM centos:7
#Add jar package rename
ADD springboot.jar app.jar
#Container start command
CMD ["java","-jar","app.jar"]
  • After setting the dockerfile above, build the dockerfile image
  • Build command: docker build -f dockerfile path - t(tag indicates version) new image name: custom version. (. Indicates dockerfile addressing path)
    Or build command: docker build -t(tag indicates version) javaweb:1.0. (. Indicates that dockerfile is in the current file directory)
  • Build a java project as an image based on the java:8-alpine image
# Specify base mirror
FROM java:8-alpine

# Copy package of java project
COPY ./docker-demo.jar /tmp/app.jar

# Exposed port
EXPOSE 8090

# Entry, start command of java project
ENTRYPOINT java -jar /tmp/app.jar
  • Build command: docker build -t javaweb:2.0. (. Indicates that dockerfile is in the current file directory)
  • Run custom image: docker run --name web -p 8090:8090 -d javaweb:1.0

4.6 Compose

Docker Compose It is a tool for arranging multi container distributed deployment, providing commands to centrally manage the complete development cycle of container applications, including services
 Build, start and stop. Use steps:
  • step
  1. Using Dockerfile to define running environment image
  2. Use docker-compose.yml to define the services that make up the application
  3. Run docker compose up to start the application
  • Install docker compose
# Compose now fully supports Linux, Mac OS and Windows. We need to install Docker before installing compose. Now let's take
 The compiled binary package is installed in Linux In the system.
curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-
`uname -m` -o /usr/local/bin/docker-compose
# Set file executable permissions
chmod +x /usr/local/bin/docker-compose
# View version information
docker-compose -version
  • Uninstall docker compose
# For binary package installation, delete the binary file
rm /usr/local/bin/docker-compose

Tags: Java Linux Operation & Maintenance Docker

Posted on Fri, 15 Oct 2021 17:06:17 -0400 by pkallberg21