A few weeks ago practice, do not record really forget clean

Docker is the container technology, the specific introduction has a lot of, I don’t want to repeat, say some advantages

Why Docker

For me, the biggest advantage is that I can deploy different environments on one computer without worrying about their conflicts. The most common conflict is port occupation. Docker technology can easily avoid this problem, and it is easy to manage, I don’t need to deal with multiple environments locally

More broadly, there are many benefits

  • More efficient use of system resources

    As you’ll see below, even if I run multiple environments simultaneously on a normal computer, it doesn’t take too much resources from the host machine, and normal development can still take place, let alone a dedicated server host

  • Faster startup time

    Traditional VIRTUAL machine technology usually takes several minutes to start application services, while Docker container applications can be started in seconds or even milliseconds because they run directly in the host kernel and do not need to start the complete operating system. Greatly saving the development, testing, deployment time.

  • Consistent operating environment

    A common problem in development is environmental consistency. Because the development environment, test environment, and production environment are inconsistent, some bugs are not found in the development process. The image of Docker provides a complete runtime environment in addition to the kernel, ensuring the consistency of the application running environment, so that there will be no problem with this code on my machine.

  • Continuous delivery and deployment

    The most desirable thing for development and operations (DevOps) people is a single build or configuration that can run anywhere. With Docker, continuous integration, continuous delivery and deployment can be achieved by customizing application images. Developers can use Dockerfile for image building and Integration testing with Continuous Integration systems, while operations can quickly deploy the image directly into a production environment. Even automatic Deployment with Continuous Delivery/Deployment systems.

  • Easier migration

    Docker ensures the consistency of execution environment, making application migration easier. Docker can run on many platforms, whether physical machine, virtual machine, public cloud, private cloud, or even laptop, and its running results are consistent. Therefore, users can easily migrate an application running on one platform to another without worrying that the application will not run properly due to the change of the operating environment.

  • Easier maintenance and extension

    Docker uses layered storage and image technology, which makes it easier to reuse the repeated parts of the application, easier to maintain and update the application, and it is also very simple to further expand the image based on the basic image. In addition, Docker team maintains a large number of high-quality official images together with various open source project teams, which can be used directly in the production environment and further customized as a basis, greatly reducing the cost of image production of application services.

Docker installation and simple use

The basic things are not said, different operating system installation details see this Docker – from the beginning to practice. In this article, I mainly record some key points, and the specific operation on Windows system

Domestic mirror acceleration

The same as NPM, domestic network problems, the speed of pulling the mirror is relatively slow, so to configure the domestic mirror, you can use Docker official or other cloud service providers

  • Docker Official Registry Mirror China
  • Seven Ox cloud accelerator

I use Windows 10. The configuration method is to right-click the Docker icon in the lower right corner of the system and choose Settings. After opening the configuration window, choose Daemon from the navigation menu on the left. Enter the accelerator address registry.docker-cn.com in the Registry mirrors column, and click Apply to save. Docker will restart and Apply the configured image address.

Commonly used instructions

After Docker is installed, use CMD command to open the command control line to use Docker command. At present, I have only done some simple practices, and record the commands that I use more frequently

1. docker ps -a

Start by looking at which containers are used in the current installation

This command lists the status of all containers, their IDS, names, mirrors used, mapping addresses, etc. I personally think it is the most frequently used one

2. docker pull [NAME]

Similar to NPM, if you want to create a container, you need to first select the image and pull it to the local, find the image you need in Docker Hub, or use the Docker pull command to pull the image to the local after you make it. Take the official example image of NET Core

docker pull microsoft/dotnet-samplesCopy the code

After running the command, wait until the pull is complete. If you pull the same image again, it is updated

3. docker image ls

View the local mirror

The status of the local image will be displayed, including the Repository from which the image came, the label (usually the version number), the image ID, the creation time, and the image size

4. docker [image/container] rm [ID/NAME]

To delete an image or container, the rm instruction is the same for the container and the image, except that the image has no name

Docker image rm < repository name >:< tag >

If you want to delete a mirror by ID, you can either enter the full ID or use the short ID. The short ID is the first few characters of the ID. Generally, three or more images can be distinguished

Note that if there is a child container that depends on the image, it cannot be deleted, nor can it be deleted while the container is running, but you can add the -f parameter, and Docker will send SIGKILL to the container

Docker rm [ID/NAME] deletes the specified container

5. docker run []

Create and start a container. This command is important and has many commonly used parameters. The command mirrored by Microsoft is used as an example

docker run -it --rm -p 8000:80 --name aspnetcore_sample microsoft/dotnet-samples:aspnetappCopy the code
  • The -t option lets Docker assign a pseudo-tty and bind it to the container’s standard input
  • -I leaves the container’s standard input open
  • — Rm will use the previous IT parameter, mainly for testing purposes, after this parameter is added, when we close the terminal with Ctrl+C, the container will be deleted automatically
  • -p port mapping. 88:80 indicates that port 80 of the container is mapped to port 8000 of the host. That is, accessing port 8000 of my host is equivalent to accessing port 80 of our deployment environment
  • –name Name of the container
  • The last one is the name of the mirror, as mentioned earlier, < repository name >:< tag >

After executing this command, Docker will first check if there is any corresponding image, if not, it will automatically perform the pull operation. We pulled the official Microsoft Core application image earlier, here we start a Web sample image, so there will be a pull operation, need to wait for a while

Once created and started, visit http://localhost:8000 to see a sample site for Core

If the –rm parameter is deleted, the container will not be deleted after exiting. You can restart the container by running the start command below. If no parameter is used, the terminal will not be accessed again

If you have a problem you can use the first ps command to see the current state of the container

6. docker start [ID/NAME]

When we restart docker, under normal circumstances the container will be in Exited exit state, this time to start by the start command, ID as mentioned earlier, use long ID or short ID, name is to create the container is set by the –name parameter

Use the closed container example above

The container ID is normally started successfully and can continue to be accessed through port 80

7. docker exec

To enter the container, there is another instruction docker attach which is slightly different, for reasons explained below

Docker exec can be followed by multiple parameters, here mainly describes the -i -t parameter.

As mentioned in the previous run command, this is somewhat similar. With only the -i parameter, the interface does not have the familiar Linux command prompt because no pseudo-terminal is assigned, but the command execution result can still be returned. When the -i -t arguments are used together, you can see the familiar Linux command prompt.

If you use CD [directory name], you will not be able to identify it, so you can use the bash command to enter the Linux terminal to use Linux commands normally


The next section explains how to deploy on Docker. NET Core, no longer the official example, but how do you build an image locally from a Dockerfile and upload it to the repository if you go from a development environment to a deployed environment