preface

In the previous two articles, we passed Docker learning Tutorial for beginners: Basics

  1. What is a Docker?
  2. Why Docker?
  3. What kind of problem does Docker solve?

These three questions help you to have a better understanding of Docker. After that, the three core concepts of Docker are explained in the three Elements of Docker: Image, Container and Warehouse in the second article. Through the study of these two articles, I think we can understand what Docker is. This question has its own answer, so now that we’ve looked at these concepts, we’ll use Docker from the actual command line and be able to do some basic things.

No nonsense, directly on the code:

Docker installation:

Before using Docker, we need to install Docker first. Here, I only list the simplest way to install Docker. If you need other ways to install Docker, you can refer to the official document – Quick Start, which has more detailed installation tutorial.

Docker installation (yum) :

#Enter the yum command on terminal to install docker and related dependencies
yum install docker

#Start docker service: centos 7+
systemctl start docker 

#Start docker service: centos 6+
service docker restart

Copy the code

Verify that Docker is installed successfully:

#Pull docker Hell-world image from docker official repository
docker pull hello-world

#Run the Hello-world mirror
docker run hello-world
Copy the code

If Hello from Docker! It means that our Docker has been installed correctly

Note:

Docker official image of the source due to abroad, leading to low domestic many in this virtual machine learning docker classmate download mirror very very slow, ali cloud server speed can also be measured, such as download slower students can try to replace domestic ali cloud, or netease cloud image of the warehouse, need the students can search relevant tutorial to net.

Docker image operation:

Docker perfectly integrates Linux, so Docker command line style and Linux is relatively close, relatively easy to use, first of all, we first say the mirror-related commands:

#Search image:Docker Search Specifies the image name#Example: docker search centos Searches for centos related images.

#The default TAG for pulling the image to the local server is Lastet, the latest versionDocker pull image name :TAG
#Example: docker pull mysql or docker pull mysql:5.6 Pull the latest version or 5.6 version of mysql image

#View all mirrors:
docker images 

## View the top-level mirror
docker image ls

#View the middle tier mirror
docker image ls -a

#As mentioned in the previous basic chapter, Docker images are stored in layers. In order to speed up image construction and reuse resources, Docker makes use of middle-layer images. So after you've used it for a while, you might see some dependent mid-tier images, so you'll see a lot of unlabeled images, unlike the dangling images before, many of these unlabeled images are mid-tier images, which are dependent on other images. These tagless mirrors should not be deleted, otherwise the upper layer mirrors will fail due to lost dependencies. In fact, these images don't need to be deleted because, as mentioned earlier, the same layer is saved only once, and these images are dependencies of other images, so you don't have to save one more copy just because they're listed, and you'll need them anyway. As long as those dependent mirrors are deleted, those dependent mid-tier mirrors are also deleted.

#Lists partial mirrors. If not specified, the TAG part can be omittedDocker image ls ubuntu: 18.04
#Remove the mirrorDocker Image RM Image ID
#Deleting mirrors in batches:
docker image rm $(docker image ls -q redis) 

Copy the code

Execute the Docker images command as shown below:

The list contains the repository name, label, mirror ID, creation time, and occupied space.

Docker container operations

As we mentioned earlier, the relationship between images and containers is similar to the relationship between classes and instances. In Docker, we obtain a container by running images. Here, we take centos as an example:

Docker run [Option] [Image name or IMAGEID] Example: docker run -it centos-it Interactive terminal run
#For example, a lot of things like mysql images, we need this container to run in the background, we don't really need to go to the container terminal, we just need to replace it with-dTo start a daemon container:
docker run -d centos 
Copy the code

By executing the command above, we will automatically enter the container in interactive mode, as shown in the figure below:

As you can see, we have actually entered bin/bash inside the centos container, where we can enter the relevant commands to manipulate our container. If you simply want to create a container and are not in a hurry to start it, you can use the create command:

#Create a container but do not start it immediatelyDocker Creat Specifies the image name or image ID
#Example: the docker create centos

#The default docker container name is Scientist + his discovery. If we need to customize our own alias, such as mycentos, we need to add the --name optionDocker runit --name [alias] [mirror name]
#Docker run -it --name mycentos centos
Copy the code

When some applications, such as Tomcat, need to use a specific port to provide external services, we can use the -p option to configure port mapping between the host and the container. Take Tomcat as an example:

Docker run it -p 8899:8080 tomcat -p Configures port mappingCopy the code

This way, when we access IP :8899, we will see the tomcat home page in the browser, and the port has been correctly mapped to port 8080 in the container.

Of course, Docker also provides two options for us to exit the interactive terminal:

#Exit interactive command environment: This is a more elegant exit, exit container still running
Ctrl+P+Q

exit 
#Less elegantly, the container will also stop after exiting
Copy the code

If I regret it and suddenly want to change something in the container terminal, Docker provides the following two ways to enter the container interactive terminal:

Docker Attach [alias or IMAGEID]
## Recommend the second option
docker exec -it centos bash
##docker exec: Execute commands in a running container

Copy the code

Docker containers also support startup, restart, and delete operations:

#Stop container: Gentle, normal shutdownDocker stop [alias or IMAGEID]#case
docker stop mycentos

#Force stop container: unplugDocker kill [alias or IMAGEID]
#Start container:Docker start [alias or IMAGEID]#Restart container:Docker restart [alias or IMAGEID]#To delete containers, only stopped containers can be deletedDocker rm [alias or IMAGEID]Copy the code

If you want to see a running container, use the following command:

#View the running container
docker ps

#View all containers, including those that have been stopped
docker ps -a
Copy the code

Get the container output:

docker logs container 
Copy the code

Container data volume:

In some cases, we need to back up the data in the container to our host in real time. Similarly, Docker supports the establishment of shared directory between the container and the host. It is also very easy to use, just need to add a -v option.

#For example, after executing this command, we create a new file in the host directory, which will also appear in the container directory. Therefore, we can share the directory where mysql data is stored with the host in this way, so as to achieve the purpose of real-time backup data.Docker run -v/host directory :/ container directory centos /bin/bash 
#If you want to assign permissions to the directory in the container, just add ro,readThe abbreviation of read-onlyDocker run -v/host directory :/ inside directory :ro /bin/bash ro read-only, so the files created in the shared directory are read-only in the container 
#Check whether the container is successfully mounted:Docker inspect Container IDCopy the code

Docker image:

Run the commit command to create an image:

Most of the time, we will inevitably modify the container, such as the configuration files of Tomcat, mysql, etc., but if we configure another tomcat container with the same configuration file, we will have to modify the configuration file again. Docker can use the commit command to repackage the current container into an image. If you change the tomcat container port to 8081, then we use commit to package the container as an image. Then we run the new image and find that the tomcat default port in the image is 8081.

We customize the changes and want to save them as a mirror image. Remember, when we run a container (if we don’t use a volume), any file changes we make are recorded in the container storage layer. Docker provides a Docker commit command, which can save the storage layer of the container as an image. In other words, it is on the basis of the original image, and then superimpose the storage layer of the container, and form a new image. Later when we run the new image, we will have the last file changes from the original container.

Docker commit [options] < container ID or container name >[< warehouse name >[:< tag >]]
#Ex. :Docker commit --author "Tao Wang <[email protected]>" --message "modified default page" webServer nginx:v2
#Where --author is the author of the modification and --message is the content of the modification. This is similar to Git version control, except that this information can be left blank.
Copy the code

Use DockerFile to customize the image:

Even though Docker provides a COMMIT to make the image, it still feels a bit cumbersome. If we could write each layer of modification, installation, build, and operation commands into a script, and use this script to build and customize the image, the problems of unrepeatability, transparency, and volume mentioned above would all be solved. This script is called Dockerfile. A Dockerfile is a text file that contains instructions. Each Instruction builds a layer, so the content of each Instruction describes how the layer should be built.

Because DockerFile as a whole block of content is still a lot, so I plan to separate notes, this article as an introductory tutorial, will use some simple cases to help you get to know DockerFile, complex image construction, will be explained in a separate article.

Because our previous centos image only retained the core functions of centos, many common software did not, so we plan to make a simple centos image, on the basis of the original with vim editor and net-tools tools.

Create a new DockerFile and write the following:

WORKDIR $mypath // Specify the working directory RUN yum -y install vim // RUN the yum command to install vim RUN yum -y install net-tools // Run the yum command to install net-tools EXPOSE 80 // The default port to EXPOSE is 80 CMD /bin/bash // the command to start the container is automatically executed when the container is running. For example, when we docker run -it centos, we go directly to bashCopy the code

Then compile the image:

Docker build -f./DockerFile -t mycentos:1.3. -t New name: version -f file -d folderCopy the code

Then we execute docker images command, the new image we built will appear, mycentos:1.3, run this image, you will find that vim and Net-Tools can be used normally.

And then you’re done.

Let’s start with the technical summary:

In this article, we explained the use and explanation of the common Docker command line, as well as a simple introduction to DockerFile. In the next article, we will start from DockerFile and begin to understand in detail how to make and publish an image of our own.

Finally, the notes have been synced to Github(welcome star) : github.com/hanshuaikan…