Use of docker two

Use of docker two

1. container operation supplement

The first command executed to create a container is /bin/bash, such as:

docker create -it --name=mycentos1 centos:7/bin/bash
#After the container is created, the first line of command/bin/bash command executed when running, bash can make the Linux system ram there, so after we execute docker start container id/container name, the container will always run in the background, if we Change this command to something else and the container will automatically stop after executing the following commands after starting.
-i: means running the container
-t: indicates that the container will enter its command line after it is started. After adding these two parameters, log in after the container is created. That is, a pseudo terminal is allocated.
--name: Name the created container.
-d: Add the -d parameter after run, it will create a guardian container to run in the background (so that the container will not be automatically logged in after the container is created, if only the -i -t two parameters are added, it will automatically enter after creation container).

-p: Represents port mapping, the former is the host port, and the latter is the mapping port in the container. You can use multiple -p to do multiple port mapping
-v: Represents the directory mapping relationship (the former is the host directory, the latter is the directory mapped to the host), you can use multiple -v to do multiple directory or file mapping. Note: It is best to do directory mapping, make changes on the host, and then share to the container.

Container operation

docker start container id/container name

Create and run

docker run -di --name=Container name mirror: version number#Run in the background
docker run -di --name=mypython python:3.6
docker run -idt --name=container name mirror: version number
docker run -idt --name=mypython python:3.6 #Foreground running
The python-t command can enter python equivalent:/bin/bash

Enter the container

docker exec -it container id command in the container
docker exec 85768eb78045 ps -a #Execute ps -a on the container and return to the host after execution
# method one
docker exec -it container id/bin/bash #When entering the container, executing/bin/bash will tamping the Linux system in the container and enter the container

# Enter the Python container
docker exec -it container ID python
#Of course, if you enter a container such as python mysql, you can also execute docker exec -it container id/bin/bash first, and then execute python, mysql, etc.

#Method Two
#attach: Enter the container and connect to the first process
docker attach container ID

#Method Three
# ssh connection, need to install ssh service on the container

Delete container

docker rm container ID/container name


docker run: create and run         
docker exec: container execution command (usually used to: enter the container) docker exec -it container id/container name/bin/bash

File copy

# Copy files from the host to the container
docker cp 1.txt centos7:/home # Copy 1.txt under the current path to/home of the container

# Copy files from the container to the host
docker cp centos7:/home/1.txt 1.txt # Copy 1.txt from the container home path to the current path

2. Network and data volume

Map the host directory to the container

docker run -di --name=mycentos -v/root/test:/home centos:7#The host test folder is mapped to the inside of the container (multiple mappings are possible)

Port Mapping

docker run -di --name=myredis -p 6378:6379 redis#Map 6378 of the host to port 6379 of the container (multiple can be mapped)

View container information

docker inspect container name (container ID) #docker inspect container name to view the detailed information of the container
docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql#View the ID of the container mysql

3. backup migration

Save the container as an image

#docker commit container name mirror name
docker commit centos7 my_image_centos7

Package the image as a compressed file

#Compress my_image_centos7 image into centos_my.tar
docker save -o centos_my.tar my_image_centos7

Restore compressed files to mirror images

docker load -i centos_my.tar

4. dockerfile

Dockerfile is a script composed of a series of commands and parameters. These commands are based on the basic image and finally create a new image. Dockerfile is similar to the requirements in the project, which contains the installation commands and other content of the dependencies that we need to build the image. After we build the dockerfile file, we only need to transplant the file to another docker to run it to generate what we need. Mirror.

# We need to create a file named Dockerfile
# File file name must be called Dockerfile

FROM python:3.6 #Build based on the image of python:3.6
MAINTAINER abc #Create a folder named abc in the new mirror
RUN pip install django==1.11.9 -i Replace the source with Douban source and install Django
RUN mkdir/home/abc #Run under the abc folder

WORKDIR/home #Set the working folder to/home

Execute the following command to start building the image

docker build -t='django_img'. # Build the django_img image

5. create a private warehouse

We can create a private warehouse to store our private images. The essence of the private warehouse is a web project linux+registry (a web service written in other languages). The specific creation steps are as follows:

# 1 docker pull registry pulls the registry image
# 2 docker run -di --name=registry -p 5000:5000 registry to run the container
# 3 Browser access: returns empty
#Modify daemon.json


# 4 Restart the docker service (all containers are stopped)
systemctl restart docker

# 5 Start the registry container
docker start registry

# 6 mark mirror
docker tag django_img

# 7 Upload image (private warehouse)
docker push
#8 Visit from the browser again
Can see the mirror just uploaded

# In the future, as long as daemon.json is configured at the same time, and then pull the mirror, the priority is to pull from the private warehouse, not going abroad
Reference: Use of docker 2-Cloud + Community-Tencent Cloud