1. Introduce Docker

  • Docker Chinese community documentation

  • Docker is an open source software deployment solution.

  • Docker is also a lightweight application container framework.

  • Docker can package, publish, and run any application.

  • Docker is a client-server (C/S) architecture.

Docker consists of three basic concepts: Image, Container, and Repository.

  • Image

    • Docker’s image concept is similar to an image in a virtual machine. It is a read-only template, a separate file system that contains the data needed to run the container and can be used to create new containers.

    • For example, an image can contain a complete Ubuntu operating system environment with only MySQL or other applications installed.

  • Container

    • A Docker container is a running instance created by a Docker image, similar to a VM, which can be started, stopped, and deleted.

    • Each container is isolated from the other, and the container will run the specific application, containing the code for the specific application and the required dependency files.

  • Repository

    • Docker’s repository function is similar to Github’s and is used to host images.

2. Basic usage

2.1 Start and Stop

# start docker
$ sudo service docker start
# restart docker
$ sudo service docker restart
# stop
docker$ sudo service docker stopCopy the code

2.2 Docker Image Operations

# display mirror list
$ sudo docker image ls
# pull mirror from warehouse

# Official mirror$sudo Docker image pull image name or sudo docker image pull library/ image name# pull the latest official image$sudo Docker image pull Ubuntu or sudo Docker image pull Library/Ubuntu $sudo docker image pull Ubuntu :16.04 or sudo Docker image pull library/ubuntu 16.04# specify a specific version image

# pull mirror from warehouse
$ sudo docker image pull delron/fastdfs  A distributed file server systemCopy the code



* IMAGEID: IMAGEID * CREATED: the date the image was CREATED (not the date the image was obtained) * SIZE: the SIZE of the imageCopy the code



2.3 Deleting a Mirror

$sudo docker image rm Image name or image ID $sudo docker image rm Hello -world $sudo docker image rm fce289e99eb9Copy the code



Docker container operations

3.1 Container List

# View the running container
$ sudo docker container ls
# View all containers
$ sudo docker container ls --allCopy the code



3.2 Creating a Container

$sudo docker run [option] Image name [command passed into the startup container] The options are as follows: * -i Runs the container in Interactive mode. * -t indicates that the container will enter its command line after it is started. After adding these two parameters, the container creation can be logged in. That is, assign a dummy terminal. * --name specifies the name of the created container. * -v indicates directory mapping, that is, host directory: directory in a container. Note: It is best to do directory mapping, make changes on the host, and then share them on the container. *-dA daemon container is created to run in the background (so that the container is not automatically logged in after being created). * -p indicates port mapping, that is, host port: port in container. * --network=host Maps the network environment of the host to the container so that the network of the container is the same as that of the host.Copy the code

3.3 Interactive Container

Linux commands can be executed freely in the container, which is an Ubuntu environment, and the container is stopped when the exit command is executed. For a container that needs to run for a long time, we can create a daemon container. When the exit command is executed inside the container to exit, the container stops.

$ sudo docker run -it --name=ubuntu1 ubuntu /bin/bash
Open the daemon container
$ sudo docker run -dit --name=ubuntu2 ubuntu
Enter the container internal interaction environment
$ sudo docker exec-it Container name or container ID the first command executed after entering $sudo dockerexec -it ubuntu2 /bin/bashCopy the code

3.4 Stopping and starting containers

# stop container$sudo docker container stop Specifies the container name or ID# kill the container
$ sudo docker container killContainer name or container ID# start container$sudo docker container start Specifies the container name or ID# delete container$sudo docker Container Rm Indicates the container name or IDCopy the code

Note: running containers cannot be deleted directly, they must be stopped.

3.5 Creating a Container Image

  • To ensure that the configured environment can be reused, the container can be mirrored.

# Mirror the container$sudo docker commit Container name Image nameImage package backup$sudo docker save -o Saves the file name of the image# image decompression$sudo docker load -i File path/backup fileCopy the code

4 Install FastDFS using Docker

FastDFS introduction

  • C language prepared an open source lightweight distributed file system.

  • The functions include file storage, file access (file upload, file download), and file synchronization, which solves the problems of large-capacity storage and load balancing. It is especially suitable for online services with file as the carrier, such as photo album website, video website and so on.

  • Tailor-made for the Internet, with full consideration of redundant backup, load balancing, linear expansion and other mechanisms, and pay attention to high availability, high performance and other indicators.

  • It can help us build a set of high-performance file server cluster, and provide file upload, download and other services.

4.1 Obtaining a FastDFS Image

# Pull the image from the warehouse and install it online
$ sudo docker image pull delron/fastdfs
Local image installation mode$sudo docker load -i File path /fastdfs_docker.tarCopy the code

4.2 Starting the Tracker container

Create an FDFS directory in /var and map it to the /var/fdfs/tracker directory on the host

sudo mkdir /var/fdfs
sudo chmod 777 /var/fdfs
sudo docker run -dit --name tracker --network=host -v /var/fdfs/tracker:/var/fdfs delron/fastdfs trackerCopy the code

4.3 Starting a Storage Container

  • TRACKER_SERVER= IP address of Tracker :22122 (do not use 127.0.0.1 for the IP address of Tracker).

  • We map the running directory of storage to the /var/fdf/storage directory of the host machine.

 sudo docker run -dti --name storage --network=host -e TRACKER_SERVER=192.168103.158.:22122 -v /var/fdfs/storage:/var/fdfs delron/fastdfs storageCopy the code

Note: If you cannot restart the storage container, delete the fdfs_storaged. Pid file in the /var/fdfs/storage/data directory and retry the storage.

5. Advanced use

You can add docker commands to sudo groups, reboot some Linux, and then you don’t have to add sudo every time you run a command.

sudo usermod -a -G docker $USERCopy the code

5.1 Use of warehouse

How to configure a local Docker repository

Docker pull Registry Downloads the Registry imageCopy the code

Configuring container permissions:

Vim /etc/docker-daemon. json Changes as follows: {"registry-mirrors": ["http://74f21445.m.daocloud.io"]."insecure-registries": ["192.168.8.14:5000"]}Copy the code

Then here is an example of creating a mirror in a local repository and restoring it.

# restart docker
 sudo service docker start
 Start the warehouse container
 docker run -d -p 5000:5000 registry
 # mark mirrorDocker tag ubuntu - mini 192.168.8.14:5000 / ubuntu 16.04 - the mini# docker tag Old image name New image name
 # submit mirrorDocker push 192.168.8.14:5000 / ubuntu 16.04 - the mini# dropdown mirrorDocker pull 192.168.8.14:5000 / ubuntu 16.04 - the miniCopy the code

5.2 Network Management

Introduction to Port Mapping By default, the network between a container and a host is isolated. You can use port mapping to map ports in a container to a port on a host. This allows us to access the contents of the container through the host’s IP +port.

docker run -d-p --name [container name] [Image name] Docker run-d-p [host IP address]::[Container port] --name [Container name] [Image name] Docker run-d -P nginx  # Starting an nginx with a capital P randomly assigns a port, and a lowercase P specifies a port mappingCopy the code

Note: Production scenarios generally do not use random mappings, but the advantage of random mappings is that they are assigned by Docker, ports do not conflict, and either mapping will affect performance because it involves mappings.

Docker network commands are as follows:

~# docker network help. connect Connect a container to a network create Create a network disconnect Disconnect a container from a network inspect Display detailed information on one or more networks ls List networks prune Remove all unused networks rm Remove  one or more networksCopy the code
Docker network mode

Bridge mode: Simply put: it is to wear a vest, under the banner of the host, do their own thing. The default mode of Docker will automatically configure its own network information when Docker container is started. All containers of the same host are under the same network and can communicate with each other. This is similar to the NAT mode of our vmware virtual machine. It uses the host network card for communication, because it involves network conversion, so it will cause resource consumption, network efficiency will be low.

Host mode: To put it simply, it means to occupy the magpie nest and use the things of the host computer to do their own things. Containers communicate using the IP address of the host. Features: The container and host share the network.

Docker run --net= host-dit --name nginx-1 nginxCopy the code

Container mode: A network of containers is used between newly created containers, similar to a LAN. Features: Containers and containers share networks.

None mode: This mode is the purest and does not do any network configuration for you, allowing for maximum customization.

Simple use of Dockerfile

Dockerfile(similar to shell script, execute batch operation, operation and maintenance bosses commonly used. Here’s how to use Dockerfile.

FROM ubuntu # Use an Ubuntu image
# RUN apt-get update && apt-get install redis-server -y # RUN apt-get update && apt-get install redis-server -y
# ADD a.txt /home
# ADD data /home
# RUN mkdir /home/add
# COPY Adds files or directories
# COPY data.tar /home
# ADD data.tar /home/ ADD # Unzip the package automatically
# Change the file directory
# WORKDIR /etc
Execute the command when running a new mirror resource
ENTRYPOINT echo hello python
Copy the code

summary

There are many more about the use of Docker, limited by space, not more explanation. For example, data volume, data volume container, compose, etc. If you are interested, you can find out by yourself.