Get Docker commands in one article

Get Docker commands in one article

Mirror warehouse operation

syntax

# Log in to the mirror warehouse docker login [OPTIONS] [SERVER] Copy code

OPTIONS description:

  • -u: login user name
  • -p: login password
# Sign mirroring warehouse docker logout [OPTIONS] [SERVER] Copy code
# Search mirrored in the warehouse docker search [OPTIONS] TERM Copy code

OPTIONS description:

  • --automated: Only list images of the automated build type
  • --no-trunc: show the complete mirror description
  • -f <filter condition>: List the mirrors whose number of favorites is not less than the specified value
# Pulling warehouse Mirror docker pull [OPTIONS] NAME[:TAG|@DIGEST] Copy code

OPTIONS description:

  • -a: pull all tagged mirrors
  • --disable-content-trust: Ignore the verification of the mirror, it is enabled by default
#Upload the local mirror to the mirror warehouse, you must first log in to the mirror warehouse docker push [OPTIONS] NAME[:TAG] Copy code

OPTIONS description:

  • --disable-content-trust: Ignore the verification of the mirror, it is enabled by default

Instance

Log in to Docker Hub

docker login -u user name -p password copy the code

Log out of Docker Hub

docker logout copy code

Find all mirrors whose name contains java from Docker Hub and the number of favorites is greater than 10

docker search -f stars = 10 java duplicated code

Parameter Description:

NAME: The name of the mirror repository source

DESCRIPTION: The description of the mirror

OFFICIAL: Is docker officially released

stars: similar to the star in Github, which means like or like

AUTOMATED: automatic construction

Download the latest version of java image from Docker Hub

docker pull java copy code

Download all mirrors of REPOSITORY as java from Docker Hub

docker pull -a java copy the code

Upload the local mirror myapache:v1 to the mirror warehouse

docker push myapache: v1 copy the code

Local mirror management

syntax

# Lists local mirror docker images [OPTIONS] [REPOSITORY[:TAG]] Copy code

OPTIONS description:

  • -a: List all local mirrors (including the intermediate image layer, by default, filter out the intermediate image layer)
  • --digests: Display summary information of the mirror
  • -f: display the mirror image that meets the conditions
  • --format: Specify the template file of the return value
  • --no-trunc: display complete mirror information
  • -q: Only display the mirror ID
# Deleting (one or more) local mirror docker rmi [OPTIONS] IMAGE [IMAGE...] Copy code

OPTIONS description:

  • -f: force deletion
  • --no-prune: do not remove the process image of the image, it is removed by default
# Mark local mirror, be classified as a warehouse docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG] Copy code
# View the specified mirrored create history docker history [OPTIONS] IMAGE Copy code

OPTIONS description:

  • -H: Print the image size and date in a readable format, the default is true
  • --no-trunc: display the complete submission record
  • -q: Only list the submission record ID
# The build command is used to create a mirror using Dockerfile docker build [OPTIONS] PATH | URL |- Copy code

OPTIONS description:

  • --build-arg=[]: set the variables when mirroring is created
  • --cpu-shares: set the weight of cpu usage
  • --cpu-period: Limit the CPU CFS cycle
  • --cpu-quota: Limit CPU CFS quota
  • --cpuset-cpus: Specify the CPU id used
  • --cpuset-mems: Specify the memory id used
  • --disable-content-trust: Ignore verification, enabled by default
  • -f: Specify the Dockerfile path to be used
  • --force-rm: Delete the intermediate container during the mirroring process
  • --isolation: Use container isolation technology
  • --label=[]: set the metadata used by the mirror
  • -m: set the maximum memory
  • --memory-swap: Set the maximum value of Swap to memory + swap, "-1" means unlimited swap
  • --no-cache: The process of creating a mirror does not use cache
  • --pull: try to update the new version of the mirror
  • --quiet, -q: Quiet mode, only the mirror ID will be output after success
  • --rm: delete the intermediate container after setting the mirror successfully
  • --shm-size: set the size of/dev/shm, the default value is 64M
  • --ulimit: Ulimit configuration
  • --squash: compress all operations in the Dockerfile into one layer
  • --tag, -t: name and tag of the image, usually name:tag or name format can set multiple tags for one image in one build
  • --network: The default default is to set the network mode of the RUN instruction during the build
#Save the specified image as a tar archive file docker save [OPTIONS] IMAGE [IMAGE...] Copy code

OPTIONS description:

  • -o: output file
# Into the use of docker save command to export the image docker load [OPTIONS] Copy code

OPTIONS description:

  • --input, -i: specify the imported file, instead of STDIN
  • --quiet, -q: streamline output information
# Create a mirror from the archive docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]] Copy code

OPTIONS description:

  • -c: apply docker command to create a mirror
  • -m: description text when submitting

Instance

View the list of local mirrors

docker imagescopy code

List REPOSITORY in the local mirror as

ubuntu
List of mirrors

docker images ubuntu copy the code

Forcibly delete the local mirror

runoob/ubuntu:v4

docker rmi -f runoob/ubuntu: v4 copy the code

Will mirror

ubuntu: 15.10
Mark as
runoob/ubuntu:v3
Mirror image

docker tag ubuntu: 15.10 runoob/ubuntu : v3 copy the code

View the creation history of the local mirror runoob/ubuntu:v3

docker history runoob/ubuntu: v3 copy the code

Use the current directory

Dockerfile
Create a mirror with the label
runoob/ubuntu:v1

docker build -t runoob/ubuntu:v1 .Copy code

Use URL

github.com/creack/docker-firefox
of
Dockerfile
Create a mirror

docker build github.com/creack/docker-firefox copy the code

Can also pass

-f
Specify
Dockerfile
File location

docker build -f/path/to/ a/Dockerfile. duplicated code

Execute in the Docker daemon

Dockerfile
Before the instruction in the
Dockerfile
Perform a syntax check and return when there is a grammatical error

docker build -t test/myapp. Sending build context to Docker daemon 2.048 kB Error response from daemon: Unknown instruction: RUNCMD Copy code

Generate my_ubuntu_v3.tar file from the mirror runoob/ubuntu:v3

docker save -o my_ubuntu_v3.tar runoob/ubuntu: v3 copy the code

Create an image from the image archive file my_ubuntu_v3.tar and name it runoob/ubuntu:v4

docker import my_ubuntu_v3.tar runoob/ubuntu: v4 copy the code

Container operation

syntax

# Listed container running docker ps [OPTIONS] Copy code

OPTIONS description:

  • -a: Display all containers, including those that are not running
  • -f: Filter the displayed content based on conditions
  • --format: Specify the template file of the return value
  • -l: display recently created containers
  • -n: List recently created n containers
  • --no-trunc: do not truncate the output
  • -q: Silent mode, only the container number is displayed
  • -s: display the total file size
# Get metadata container/mirror docker inspect [OPTIONS] NAME|ID [NAME|ID...] Copy code

OPTIONS description:

  • -f: Specify the template file of the return value
  • -s: display the total file size
  • --type: return JSON for the specified type
# View process information running in the container to support the ps command parameters docker top [OPTIONS] CONTAINER [ps OPTIONS] Copy code
#Connect to the running container docker attach [OPTIONS] CONTAINER Copy code
# Get real-time events from the server docker events [OPTIONS] Copy code

OPTIONS description:

  • -f: filter events based on conditions
  • --since: show all events after the specified timestamp
  • --until: the running time is displayed until the specified time
# Get Logs container docker logs [OPTIONS] CONTAINER Copy code

OPTIONS description:

  • -f: trace log output
  • --since: show all logs at a certain start time
  • -t: display timestamp
  • --tail: Only list the latest N container logs
# Blocked from running until the container is stopped, and then print out its exit code docker wait [OPTIONS] CONTAINER [CONTAINER...] Copy code
#Export the file system as a tar archive file to STDOUT docker export [OPTIONS] CONTAINER Copy code

OPTIONS description:

  • -o: write the input to the file
# Lists the port mapping specified container, or find the PRIVATE_PORT NAT to public-facing port docker port [OPTIONS] CONTAINER [PRIVATE_PORT[/PROTO]] Copy code

Instance

List all running container information

$ docker ps CONTAINER ID IMAGE COMMAND ... PORTS NAMES 09b93464c2f7 nginx:latest "nginx -g'daemon off" ... 80/tcp, 443/tcp myrunoob 96f7f14e99ab mysql:5.6 "docker-entrypoint.sh" ... 0.0.0.0:3306->3306/tcp mymysql Copy code

Output details:

CONTAINER ID: Container ID

IMAGE: The image used

COMMAND: The command to run when the container is started

CREATED: The creation time of the container

STATUS: container status

There are 7 states:

  • created
  • restarting (restarting)
  • running
  • removing (migrating)
  • paused
  • exited
  • dead

PORTS: The port information of the container and the connection type used (tcp\udp)

NAMES: automatically assigned container name

List the information of the 5 recently created containers

docker ps -n 5 duplicated code

List all created container IDs

docker ps -aq copy the code

Get the meta information of mirror mysql:5.6

docker inspect mysql: 5.6 Copy the code

Get the IP of the running container mymysql

docker inspect --format = '{{range .NetworkSettings.Networks}} {{. IPAddress}} {{end}}' mymysql copy the code

View the process information of the container mymysql

docker top mymysql View process information of all running containers ```shell for i in `docker ps |grep Up|awk'{print $1}'`do echo/&&docker top $i done Copy code

The container mynginx points the access log to the standard output and connects to the container to view the access information

docker attach --sig-proxy = false mynginx duplicated code

Show docker all events after July 1, 2016

docker events --since = "1467302400" copy the code

Show that the docker image is mysql:5.6 Related events after July 1, 2016

docker events -f "image" = " mysql: 5.6" --since = "2016-07-01" copy the code

Track and view the log output of the container mynginx

docker logs -f mynginx copy the code

View the latest 10 logs of the container mynginx from July 1, 2016

docker logs --since = "2016-07-01" --tail = 10 mynginx duplicated code

Save the container with id a404c6c174a2 as a tar file by date

docker export -o mysql-`date +% Y% m% d`.tar a404c6c174a2 duplicated code

Container life cycle management

syntax

# Create a new container and run a command docker run [OPTIONS] IMAGE [COMMAND] [ARG...] Copy code

OPTIONS description:

  • -a stdin: specify the standard input and output content type, optional STDIN/STDOUT/STDERR
  • -d: Run the container in the background and return the container ID
  • -i: Run the container in interactive mode, usually used with -t
  • -P: random port mapping, the internal port of the container is randomly mapped to the port of the host
  • -p: Specify port mapping, the format is: host (host) port: container port
  • -t: reassign a pseudo input terminal for the container, usually used at the same time as -i
  • --name="nginx-lb": Specify a name for the container
  • --dns 8.8.8.8: Specify the DNS server used by the container, the default is the same as the host
  • --dns-search example.com: Specify the container DNS search domain name, the default is the same as the host
  • -h "mars": Specify the hostname of the container
  • -e username="ritchie": set environment variables
  • --env-file=[]: read environment variables from the specified file
  • --cpuset="0-2" or --cpuset="0,1,2": Bind the container to the specified CPU to run
  • -m: Set the maximum memory usage of the container
  • --net="bridge": Specify the network connection type of the container, support bridge/host/none/container: four types
  • --link=[]: add a link to another container
  • --expose=[]: open a port or a group of ports
  • --volume, -v: bind a volume
# Activate one or more of the containers has been stopped docker start [OPTIONS] CONTAINER [CONTAINER...] # Stop a running container docker stop [OPTIONS] CONTAINER [CONTAINER...] # Restart container docker restart [OPTIONS] CONTAINER [CONTAINER...] Copy code
# Kill a running container docker kill [OPTIONS] CONTAINER [CONTAINER...] Copy code

OPTIONS description:

  • -s: send a signal to the container
# Delete one or more containers docker rm [OPTIONS] CONTAINER [CONTAINER...] Copy code

OPTIONS description:

  • -f: Forcefully delete a running container through the SIGKILL signal
  • -l: Remove the network connection between the containers, not the container itself
  • -v: delete the volume associated with the container
# Suspend all processes in the vessel docker pause CONTAINER [CONTAINER...] # Restore all processes in the vessel docker unpause CONTAINER [CONTAINER...] Copy code
# Create a new container but do not start it docker create [OPTIONS] IMAGE [COMMAND] [ARG...] Copy code
# Execute commands in running container docker exec [OPTIONS] CONTAINER COMMAND [ARG...] Copy code

OPTIONS description:

  • -d: separate mode: run in the background
  • -i: Keep STDIN open even if it is not attached
  • -t: allocate a pseudo terminal

Instance

Use the docker image nginx:latest to start a container in background mode and name the container mynginx

docker run --name mynginx -d nginx: latest copy the code

Use the mirror nginx:latest to start a container in background mode and map port 80 of the container to a random port on the host

docker run -P -d nginx: latest copy the code

Use the mirror nginx:latest to start a container in background mode, map the container's port 80 to the host's port 80, and map the host's directory/data to the container's/data

docker run -p 80:80 -v/data: /data -d nginx: latest copy the code

Bind the 8080 port of the container and map it to the 80 port of the local host 127.0.0.1

docker run -p 127.0.0.1:80:8080/tcp ubuntu bash copy the code

Use the mirror nginx:latest to start a container in interactive mode and execute the/bin/bash command in the container

docker run -it nginx: latest/bin /bash duplicated code

Start the stopped container myrunoob

docker start myrunoob copy the code

Stop the running container myrunoob

docker stop myrunoob copy the code

Restart the container myrunoob

docker restart myrunoob copy the code

Kill the running container mynginx

docker kill -s KILL mynginx copy the code

Forcibly delete containers db01, db02

docker rm -f db01 db02 copy the code

Remove the connection of container nginx01 to container db01, the connection name is db

docker rm -l db copy the code

Delete the container nginx01, and delete the data volume mounted by the container

docker rm -v nginx01 copy the code

Delete all stopped containers

docker rm $ (docker ps -a -q ) Copy the code

Suspend database container db01 to provide services

docker pause db01Copy code

Restore the database container db01 to provide services

docker unpause db01 copy the code

Use the docker image nginx:latest to create a container and name the container myrunoob

docker create --name myrunoob nginx: latest copy the code

Execute the/root/runoob.sh script in the container in interactive mode in the container mynginx

docker exec -it mynginx/bin/sh /root/runoob.sh duplicated code

Open an interactive terminal in the container mynginx

docker exec -i -t mynginx/bin/ bash duplicated code

View the container ID that is already running

docker ps -a copy the code

Container rootfs command

syntax

# Create a new image from the container docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] Copy code

OPTIONS description:

  • -a: Submitted mirror author
  • -c: Use Dockerfile instructions to create a mirror
  • -m: description text when submitting
  • -p: When committing, pause the container
#For data copy between the container and the host docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH Copy code

OPTIONS description:

  • -L: keep the link in the source target
# Change the file structure of the container inspection docker diff [OPTIONS] CONTAINER Copy code

Instance

Save the container a404c6c174a2 as a new image, and add the submitter's information and description information

docker commit -a "runoob.com" -m " my apache" a404c6c174a2 mymysql: v1 copy the code

Copy the host/www/runoob directory to the/www directory of the container 96f7f14e99ab.

docker cp/www/runoob 96f7f14e99ab: /www/duplicated code

Copy the host/www/runoob directory to the container 96f7f14e99ab, and rename the directory to www.

docker cp/www/runoob 96f7f14e99ab: /www copy the code

Copy the/www directory of the container 96f7f14e99ab to the/tmp directory of the host.

docker cp 96f7f14e99ab:/www/tmp /copy the code

Check the file structure changes of the container mymysql.

docker diff mymysql copy the code

Other commands

syntax

# Display Docker system information, and a container comprising a number of mirrors docker info [OPTIONS] Copy code
docker version [OPTIONS] Copy code

OPTIONS description:

  • -f: Specify the template file of the return value
# Ctrl+p+q Exit the container and make the container run in the background ^p^q Copy code

Instance

View

Docker
system message.

docker infocopy code

display

Docker
Version Information

docker versioncopy code

docker --help

Commands: attach Attach local standard input, output, and error streams to a running container Connect local standard input, output, and error streams to the running container build Build an image from a Dockerfile Build the image from the Dockerfile commit Create a new image from a container's changes Create a new image from changes in the container cp Copy files/folders between a container and the local filesystem Copy files/folders between a container and the local file system create Create a new container Create a new container diff Inspect changes to files or directories on a container's filesystem Check for changes to files or directories in the container file system events Get real time events from the server Get real-time events from the server exec Run a command in a running container Run a command in a running container export Export a container's filesystem as a tar archive Export the file system of the container as a tar archive history Show the history of an image Show the history of an image images List images List images import Import the contents from a tarball to create a filesystem image Import content from the compressed package and create a file system image info Display system-wide information Display information about the entire system inspect Return low-level information on Docker objects Return the underlying information of the Docker object kill Kill one or more running containers Kill one or more running containers load Load an image from a tar archive or STDIN Load an image from a compressed archive or STDIN login Log in to a Docker registry Log in to the Docker registry logout Log out from a Docker registry Log out from the registry logs Fetch the logs of a container Grab logs from the container pause Pause all processes within one or more containers Suspend all processes in one or more containers port List port mappings or a specific mapping for the container List the port mapping or specific mapping of the container ps List containers List all containers pull Pull an image or a repository from a registry Pull an image or a repository from the registry push Push an image or a repository to a registry Push an image or repository to the registry rename Rename a container Rename a container restart Restart one or more containers Restart one or more containers rm Remove one or more containers Remove one or more containers rmi Remove one or more images Remove one or more mirrors run Run a command in a new container Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) Save one or more images as a compressed archive (default is to STDOUT) search Search the Docker Hub for images Search for images in Dockers Hub start Start one or more stopped containers Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics Display real-time stream of container resource usage statistics stop Stop one or more running containers Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE Create a tag TARGET_IMAG pointing to SOURCE_IMAGE top Display the running processes of a container Show all running processes in a container unpause Unpause all processes within one or more containers Unsuspend all processes in one or more containers update Update configuration of one or more containers Update the configuration of one or more containers version Show the Docker version information Display the version information of the container wait Block until one or more containers stop, then print their exit codes Block until one or more containers are stopped, then print their exit codes Run'docker COMMAND --help' for more information on a command. Copy code