Containers

Posted by Hitwalker on Tue, 30 Jul 2019 21:56:03 +0200

[TOC]

Containers are one or a group of applications that run independently.Docker containers are created by Docker mirroring.
The relationship between containers and mirrors is similar to objects and classes in object-oriented programming.

[info] Container names for the following commands can be replaced with container IDS

operation


# Create a container called mycon and pass in the parameter php:5.6-fpm
docker create --name mycon php:5.6-fpm

# Create and run ubuntu:15.10, output "Hello world"
docker run ubuntu:15.10 /bin/echo "Hello world"

# Create a container and enter it, creating a temporary name yourself if you don't specify a name
docker run -i -t ubuntu:18.10 /bin/bash

# Add d to create run and exit directly
docker run -itd ubuntu:18.10

# Open an interactive mode terminal in container mycon
docker exec -i -t mycon /bin/bash   

# Interactive execution of in-container/data/test.sh scripts in container mycon
docker exec -it mycon /bin/sh  /data/test.sh

# Exit Container
//Shortcut: Ctrl+P+Q
exit;

# Stop container operation, send signal SIGTERM
docker stop mycon

# Start a container in a stopped state
docker start mycon

# Restart a container
docker restart mycon

# Delete a container
doecker rm mycon

# Block into a container until it stops running
docker wait mycon 

# Pause all processes in the container
docker pause mycon

# Restore all processes in the container.
docker unpause mycon

# Find the pid of the target container and enter with nsenter
# Older versions of docker do not have exec commands
docker inspect -f {{.State.Pid}} 44fc0f0582d9  
nsenter --target 998 --mount --uts --ipc --net --pid

# Send signal to container, default SIGKILL
docker kill -s KILL mycon (-s Represents sending a signal to a container)

information

# Display containers in running state (Up)
docker ps
-a :Displays all containers, including those not running.For example: docker ps -a

# Go deep inside the container to get all the container information, for example: docker inspect mycon
docker inspect
-f :Specifies the template file for the return value.For example: docker inspect -f '{{.NetworkSettings.IPAddress}}' mycon (Get a running container mycon Of IP)
-s :Displays the total file size.
--type :Returns for the specified type JSON. 

# View the container's log (stdout/stderr)
docker logs
-f : Trace Log Output,For example: docker logs -f mycon(View Containers mycon Log output, container must be started)
--since :Show all logs for a start time
-t : presentation time stamp
--tail :List only the latest N Bar container log, for example: docker logs --since="2017-05-01" --tail=10 mycon(View Containers mycon The latest 10 logs from May 1, 2017.)

# Get real-time events for docker server
docker events
-f : Filter events based on conditions; for example: docker events -f "image"="mysql:5.6" --since="1466302400" (display docker Mirror as mysql:5.6 Related events after the date corresponding to this timestamp.)
--since : Show all events from the specified timestamp;For example: docker events --since="1466302400" (display docker All events after the date corresponding to this timestamp.)
--until : The running time is displayed until the specified time;

# Display port mappings for containers, for example: docker port mycon
docker port

# Displays the process information of the container, supporting ps parameters.For example, docker top Mycon
docker top

# Displays the changes in the container file system and checks for changes in the file structure within the container.For example: docker diff mycon
docker diff

Data Volume and Data Volume Container

Data still exists after deleting containers

# Creation and viewing of data volumes
# The default is to create on/var/lib/docker/volumes/of the host
docker volume create nginx-vol
docker volume ls
docker volume inspect nginx-vol

# Data Volume Mounting
docker run -itd --name=nginx-test --mount src=nginx-vol,dst=/usr/share/nginx/html nginx
docker run -itd --name=nginx-test -v nginx-vol:/usr/share/nginx/html nginx

# Data volume specifies mount directory/home/docker/wwwroot
# The directory can be created automatically in -v mode, but not in -mount mode.
docker run -itd --name=nginx-test --mount src=nginx-vol,dst=/usr/share/nginx/html nginx
docker run -itd --name=nginx-test --mount type=bind src=/home/docker/wwwroot,dst=/usr/share/nginx/html nginx
docker run -itd --name=nginx-test -v /home/docker/wwwroot:/usr/share/nginx/html nginx

If containers need to share some continuously updated data

# Create Mounted Volume Container
docker run -itd --name nginx-data  --mount src=nginx-vol,dst=/usr/share/nginx/html nginx
# New Container Associated Data Volume Container
docker run -itd --volumes-from nginx-data --name nginx-1 nginx
docker run -itd --volumes-from nginx-data --name nginx-2 nginx
# Data Volume Common for Three Containers

Export Import

# Copy a file or directory outward from a container
docker cp 

    -L :Keep links in source and target

    docker cp /www/test mycon:/www/  # Copy host/www/test directory to container mycon/ww directory

    docker cp /www/test mycon:/www   # Copy the host/www/test directory to the container mycon and rename the directory to WWW

    docker cp mycon:/www /tmp/test    # Copy the / WW directory from the container mycon to the host's / tmp/test directory

# Export the entire file system of the container as a tar package without layers, tag s, and so on
    # Export container must stop container first
docker export 
    -o :Write the input to a file.For example: docker export -o mysql-`date +%Y%m%d`.tar a404c6c174a2  #Save the container with id a404c6c174a2 as a tar file by date.

    # Docker import <file path> <container name>
    docker import ./ubuntu1810.tar ubuntu1810:tag

[info] Note that docker import import import container entry is a new image to view through docker images, container files lose all metadata and history compared to docker save command, only the state of the container at that time is saved, which is equivalent to a virtual machine snapshot.

Topics: Linux Docker Nginx Ubuntu MySQL