Life is like a play, and if you are not an actor?
I recently played Docker and it feels good. I will share some basic usages of docker in this record. This content will be supplemented by my continued in-depth study of docker, and everyone is welcome to correct it.
The advantages of Docker are not elaborated here. The purpose of docker is very wide. I recently prepared to use it to build a test environment (vulnerability test environment, development test environment, etc.), and can also be used to deploy distributed projects (which can greatly utilize the server). Resources), of course, there are many uses for docker, which are not detailed here.
- docker client and server (daemon)
- docker image (image)
- docker container (container)
The docker container is built on top of the image. We can understand image as a defined class, and container is an instance. A class can instantiate many instances. Similarly, docker can also run multiple containers on the image. Containers can be the same or they can be customized. The docker client and server can run on the same host or different.
The registry is used to store the user’s image. It is divided into public and private. The public registry operated by docker is called the docker hub. Users can register their accounts on the docker hub and share and save their own images.
Docker can run on linux, mac, windows.
Go to the official website to download the installation package: https://www.docker.com/products/docker#/mac
After the download is complete, install it directly. After the installation is complete, run docker run hello-world. If no error is reported, the installation is successful.
Replace the mirror source (fill in the domestic mirror source):
Start the docker daemon:
Boot from boot:
Alibaba Cloud image is recommended, address: http://dev.aliyun.com/search.html
After registering an account to log in, enter the console accelerator, you will get a mirror address, add the address to the /etc/default/docker file, and restart the docker service.
The docker basic commands include docker operations, mirror operations, container operations, and other related operations. The following lists some commonly used commands. For more details, please refer to the official documentation, or use the –help command.
View docker information
View docker version
Local images are saved in the /var/lib/docker directory.
View the local mirror list:
Other mirroring operations:
Create a container:
- –rm Delete the container once the process exits
- -ti enter interactive mode
- Mirror name of the ubuntu container
- /bin/bash command to run
- -d running in the background
- -p mapped port, pre-local, post-container
- –name custom name
Note: After the container is created, an ID is returned, which is randomly generated.
Check the container running status:
View container specific information:
More detailed than the ps-a command, including network information, configuration information, etc., can be matched with -forma, such as:
View the process inside the container:
Enter the inside of the container:
Or you can use:
The exec command can execute commands inside the container. The above code indicates that a new shell is created in the container.
Exit the container:
Restart the container:
Container Network Management: (Thanks @DarkEvil for sharing)
- host mode, use dockerrun with –net=host specified (docker uses the same network as the host, the network card ip seen in the container is the ip on the host)
- container mode, use –net=container:container_id/container_name (multiple containers use the same network, see the ip is the same)
- none mode, specified with –net=none (in this mode, no network will be configured)
- bridge mode, specified with –net=bridge
- Default mode, this network mode is not required to specify the default. (This mode assigns a separate Network Namespace to each container. Similar to vmware’s nat network mode. All containers on the same host will be on the same network segment and can communicate with each other.)
The dockerfile can be used to dynamically generate new images. For example, we have pulled a basic centos image. Now we need to install some software in this image so that we can run the project code we prepared smoothly. Then we can use the following two schemes:
Create a container on the centos image, enter the container interactive interface, manually install some required software, configure the environment. When all the changes have been made, create a new image using docker commit container-id newimagename. Then use the new image to create the container and run our project code.
The so-called automation, that is, you do not need to enter the container to manually enter the command to configure, everything is automatically processed when the container is running, then this will use the dockerfile. The dockerfile is simply a configuration file. The docker container handles the contents of the file at runtime, such as installing software, modifying environment variables, running programs, and so on. The advantage of using dockerfile is that it is very convenient to modify the contents of the configuration file to achieve the effect of creating a dynamic image.
We need to create a directory to store the Dockerfile, the directory name can be arbitrarily selected, in this directory to create a Dockerfile. Here I create an instance based on centos, the container will automatically open a python webserver (local monitor 8080 port) after running.
Write in the Dockerfile:
The dockerfile syntax is similar to MakeDown. The basic content is as follows:
- FROM based base image name
- MAINTAINER maintainer information
- RUN running commands (installing software, etc.)
- CMD command to run when starting the container (only one can be written)
The grammar is more than this, more content, you can refer to the official documentation.
Go to the directory where the Dockerfile is located and run:
At this point, run docker images -a view, you will find an additional image, the name is centos_test, the tag is 01
If there is a problem with the dockerfile, an error will be reported during the build. At this point, you can use the docker run container id to enter the container in the final state for debugging.
Run the container on this image:
At this point, turn on the unit’s 127.0.0.1:80
Each instruction must be in uppercase letters, such as FROM, RUN, and followed by a content, the docker file will execute the content in order from top to bottom.
Role: set the working directory, similar to cd
You can override the container working directory with the -w parameter
Role: set the environment variables in the container
Can be covered with -e.
Role: Specify what kind of user to run the container
Can be overridden with docker -u.
Both add and copy are used to add files to the image. The difference is that copy can only copy files without decompression.
docker has a lot of content, these are just some of the most basic usages, and this article will continue to be updated