Docker Quick Start-common docker commands

Source: Internet
Author: User
Tags docker ps docker cp docker hub
Docker Quick Start-common docker commands 1. Introduction to docker commands 1. Introduction to docker commands

You can run the sudo docker or sudo docker HELP command to view the docker command list.
Dockerfile instructions

Docker container command
As docker container technology continues to evolve, there are already 41 docker sub-commands, and complex parameter configurations will be available for core sub-commands (such as run. Docker commands can be divided into four parts based on functions and scenarios.

2. docker User Group

Is the docker daemon bound to a Unix? Docker container bash command .The Default owner is root. Other users can use the sudo command to access the socket file. To avoid entering sudo every time you run docker commands, you can create a docker user group and add the corresponding users to the docker group. When the docker process is started, the socket is set to be read and written by users in the docker group.
Before creating a user group docker, docker command for running containers ,check whether there is a docker group in the user group.
cat /etc/group | grep docker
Create a docker Group
sudo groupadd docker
Add users to the docker Group
sudo usermod -aG docker user
Restart the docker Service
sudo systemctl restart docker
Run docker info
docker info
Modify/var/run/docker. Sock File Permissions
sudo chmod a+rw /var/run/docker.sock

Docker commands to run a container

3. docker Local Image Storage

By default, docker is stored at/var/lib/docker.
The docker configuration file in rhel7/centos7 is as follows:
/Usr/lib/systemd/system/docker. Service ?.
Modify the docker. service file and use the -- Graph parameter to specify the storage location.
sudo vim ?/usr/lib/systemd/system/docker.service

Execstart =/usr/bin/dockerd-current and add the following content:
???????????--graph /home/user/docker
After modification, reload the configuration file
sudo systemctl daemon-reload
Restart the docker Service
sudo?systemctl ?restart docker

Docker command from container

II. Environment Information commands 1. docker info

Docker info: displays the docker system information, including the number of images and containers.
docker info [OPTIONS]

2. docker version

Docker version: displays the docker version information.
docker version [OPTIONS]

Iii. Local image management commands 1. docker Images

Docker images :? List local images.
docker images [OPTIONS] [REPOSITORY[:TAG]]
Options description:
-A: lists all local images (including the intermediate image layer. By default, the intermediate image layer is filtered out );
-- Digests: displays the abstract information of the image;
-F: displays images that meet the conditions;
-- Format: Specifies the template file of the returned value;
-- No-trunc: displays the complete image information;
-Q: only the image ID is displayed.
View the list of local images.
sudo docker images
List the images whose repository is Ubuntu in the local image.
docker images ubuntu

2. docker RMI

Docker RMI :? Delete one or more local images.
docker rmi [OPTIONS] IMAGE [IMAGE...]
Options description:
-F: Force Delete;
-- No-prune: The image is removed by default when the image is not removed;
Forcibly Delete the local image Ubuntu: V4.
docker rmi -f ubuntu:v4

3. docker tag

Docker Tag :? Mark the local image as a repository.
docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]
Mark Ubuntu: 15.10 As a Ubuntu: V3 image.
docker tag ubuntu:15.10 ubuntu:v3
View the image with the tag Ubuntu: V3 in the local image repository
docker images ubuntu:v3

4. docker build

Docker build? Command is used to create an image using dockerfile.
docker build [OPTIONS] PATH | URL | -
Options description:
-- Build-Arg = []: Set the variable when the image is created;
-- CPU-Shares: sets the CPU usage weight;
-- CPU-period: limits the cpu cfs cycle;
-- CPU-quota: limits the cpu cfs quota;
-- Cpuset-CPUs: indicates the cpu id used;
-- Cpuset-MEMS: Specifies the memory ID used;
-- Disable-content-Trust: Ignore verification. It is enabled by default;
-F: Specifies the dockerfile path to be used;
-- Force-RM: deletes intermediate containers during image setting;
-- Isolation: Container isolation technology is used;
-- Label = []: sets the metadata used by the image;
-M: sets the maximum memory size;
-- Memory-Swap: set the maximum value of swap to memory + swap. "-1" indicates unlimited swap;
-- No-Cache: the cache is not used during image creation;
-- Pull: update the new version of the image;
-- Quiet,-Q: Quiet Mode. After success, only the image ID is output;
-- RM: After the image is set, the intermediate container is deleted;
-- SHM-size: set the size of/dev/SHM. The default value is 64 m;
-- Ulimit: ulimit configuration.
-- Tag,-T :? The image name and tag, usually in name: tag or name format. You can set multiple tags for an image in one build.
-- Network :? Default. Set the network mode of the Run Command during build
Use the dockerfile in the current directory to create an image with the label Ubuntu: V1.
docker build -t ubuntu:v1 .
Use URL? Github.com/creack/docker-firefox? To create an image.
docker build github.com/creack/docker-firefox
You can also use the location of the-F dockerfile file:
docker build -f /path/to/a/Dockerfile .
The docker daemon checks the syntax of dockerfile before executing the commands in dockerfile. If a syntax error occurs, an error message is returned.

5. docker history

Docker history :? View the creation history of the specified image.
docker history [OPTIONS] IMAGE
Options description:
-H: print the image size and date in readable format. The default value is true;
-- No-trunc: displays the complete submission record;
-Q: only the submitted record IDs are listed.
View the creation history of the local image Ubuntu: V3.
docker history ubuntu:v3

6. docker save

Docker save :? Saves the specified image as a tar archive file.
docker save [OPTIONS] IMAGE [IMAGE...]
Options description:
-O: output file.
Run Ubuntu: v3to generate my_ubuntu_v3.tar.
docker save -o my_ubuntu_v3.tar ubuntu:v3

7. docker Import

Docker import :? Create an image from an archive file.
docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Options description:
-C: Use the docker command to create an image;
-M: description text when submitting the request;
Create an image from the mirror file my_ubuntu_v3.tar and name it Ubuntu: v4
docker import my_ubuntu_v3.tar ubuntu:v4

Iv. Container operation command 1. docker PS

Docker PS :? List containers
docker ps [OPTIONS]
Options description:
-A: displays all containers, including those that are not running.
-F: filter the displayed content based on the conditions.
-- Format: Specifies the template file of the returned value.
-L: displays the recently created container.
-N: list the n recently created containers.
-- No-trunc: the output is not truncated.
-Q: In silent mode, only the container number is displayed.
-S: displays the total file size.

dokcer ps
Lists information about all running containers.
docker ps -n 5
Lists information about the five most recently created containers.
docker ps -a -q
Lists All created container IDs.

2. docker inspect

Docker inspect :? Obtain the metadata of the container/image.
docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Options description:
-F: Specifies the template file of the returned value.
-S: displays the total file size.
-- Type: Return JSON for the specified type.
docker inspect mysql:5.6
Obtain the metadata of the image MYSQL: 5.6.
docker inspect --format=‘{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}‘ mymysql
Obtain the IP address of the running container mymysql.

3. docker top

Docker top: view the running process information in the container. ps command parameters are supported.
docker top [OPTIONS] CONTAINER [ps OPTIONS]
When the container is running, the/bin/bash terminal may not be used to execute the TOP Command interactively, and the container may not have the top command. You can use docker top to view the running processes in the container.

docker top mymysql
View the process information of the container mymysql.
for i inDocker PS | grep up | awk '{print $1 }';do echo \ &&docker top $i; done
View the process information of all running containers.

4. docker attach

Docker attach: connect to a running container.
docker attach [OPTIONS] CONTAINER
The container to attach must be running. You can connect to the same container to share the screen.
If the container is currently running bash, The CTRL-C exits the input of the current row without exiting; if the container is currently running the process at the foreground, such as the access to the output nginx. log, the CTRL-C not only causes the container to exit, but also stops the container. Attach can bring the -- sig-proxy = false option to ensure that the CTRL-D or CTRL-C does not close the container.
docker attach --sig-proxy=false mynginx
The container mynginx directs access logs to standard output and connects to the container to view access information.

5. docker events

Docker events :? Obtain real-time events from the server
docker events [OPTIONS]
Options description:
-F: filter events based on conditions;
-- Since: displays all events after the specified timestamp;
-- Until: the running time is displayed until the specified time;
Docker events -- since = "1467302400"
Displays all events of docker after July 15, July 1, 2016.
docker events -f "image"="mysql:5.6" --since="1467302400"
Show events related to the docker image as MySQL: 5.6 after January 1, July 1, 2016
If the specified time is seconds, you need to convert the time to a timestamp. If the time is a date, you can use it directly, for example, -- since = "2016-07-01 ".

6. docker logs

Docker logs :? Get container logs
docker logs [OPTIONS] CONTAINER
Options description:
-F :? Tracking log output
-- Since: displays all logs at a certain start time.
-T :? Display Timestamp
-- Tail: only list the latest n container logs

docker logs -f mynginx
Tracking and viewing the log output of container mynginx
docker logs --since="2016-07-01" --tail=10 mynginx
View the latest 10 logs of the container mynginx after January 1, July 1, 2016.

7. docker wait

Docker wait :? It is blocked until the container stops, and then the exit code is printed.
docker wait [OPTIONS] CONTAINER [CONTAINER...]
docker wait CONTAINER

8. docker Export

Docker export: export the file system as a tar archive file to stdout.
docker export [OPTIONS] CONTAINER
Options description:
-O: Write the input content to a file.

docker export -o mysql-Date + % Y % m % d.tar a404c6c174a2
Save the container with ID a404c6c174a2 as a tar file by date

9. docker Port

Docker port: list the port ing of the specified container, or find a public port that Nat private_port.
docker port [OPTIONS] CONTAINER [PRIVATE_PORT[/PROTO]]
docker port mymysql
View the port ing of the container mynginx.

5. Container lifecycle management command 1. docker run

Docker run: Create a new container and run a command
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Options description:
-A stdin :? Specifies the type of standard input/output content. Optional values: stdin, stdout, and stderr;
-D :? Run the container in the background and return the container ID;
-I :? Run containers in interactive mode, usually used together with-T;
-P :? Port ing. Format: host port: Container Port
-T :? Re-allocate a pseudo-input terminal for the container, which is usually used together with-I;
-- Name = "nginx-lb ":? Specify a name for the container;
-- DNS 8.8.8.8 :? Specifies the DNS server used by the container, which is consistent with the host by default;
-- DNS-search example.com :? Specify the DNS search Domain Name of the container, which is consistent with the host by default;
-H "Mars ":? Specify the container hostname;
-E username = "Ritchie ":? Set environment variables;
-- Env-file = []:? Reads environment variables from a specified file;
-- Cpuset = "0-2" or -- cpuset = "0, 1, 2 ":? Bind the container to the specified CPU for running;
-M: sets the maximum memory usage for containers;
-- Net = "bridge ":? Network Connection Type of the specified container. Supportedbridge/host/none/container:<name|id>?Four types;
-- Link = []:? Add a link to another container;
-- Expose = []:? Open a port or a group of ports;

docker run --name mynginx -d nginx:latest
Use docker image nginx: Latest to start a container and name the container mynginx
docker run -P -d nginx:latest
Start a container in later mode using image nginx: Latest and map port 80 of the container to the random port of the host.
docker run -p 80:80 -v /data:/data -d nginx:latest
Use the image nginx: Latest to start a container in later mode, map port 80 of the container to port 80 of the host, and map the directory/data of the host to/data of the container.
docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
Bind the container port 8080 to the port 80 of the local host 127.0.0.1.
docker run -it nginx:latest /bin/bash
Use the image nginx: Latest to start a container in interactive mode and execute the/bin/bash command in the container.

2. docker start

Docker start? : Start one or more stopped containers
docker start [OPTIONS] CONTAINER [CONTAINER...]
docker start myapp
Start the stopped container MyApp

3. docker stop

Docker stop? : Stop a running container
docker stop [OPTIONS] CONTAINER [CONTAINER...]
docker stop myapp
Stop running container MyApp

4. docker restart

Docker restart? : Restart the container
docker restart [OPTIONS] CONTAINER [CONTAINER...]
docker restart myapp
Restart the container MyApp

5. docker kill

Docker kill? : Kill a running container.
docker kill [OPTIONS] CONTAINER [CONTAINER...]
Options description:
-S: sends a signal to the container

docker kill -s KILL mynginx
Kill the running container mynginx

6. docker RM

Docker RM: delete one or more containers.
docker rm [OPTIONS] CONTAINER [CONTAINER...]
Options description:
-F: Use the sigkill signal to force a running container to be deleted.
-L: remove the network connection between containers, rather than the container itself.
-V: Delete the volume associated with the container

docker rm -f db01 db02
Force Delete containers db01 and DB02
docker rm -l db
Remove the connection from container nginx01 to container db01. The connection name is dB.
docker rm -v nginx01
Delete the nginx01 container and delete the volume attached to the container

7. docker pause

Docker pause? : Pause all processes in the container.
docker pause [OPTIONS] CONTAINER [CONTAINER...]
docker pause db01
Pause the database container db01 to provide services.

8. docker unpause

Docker unpause? : Restore all processes in the container
docker unpause [OPTIONS] CONTAINER [CONTAINER...]
docker unpause db01
Restore the database container db01 to provide services.

9. docker create

Docker create: Create a new container without starting it
docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
docker create --name myapp nginx:latest
Use a docker image nginx: Latest to create a container and name the container MyApp

10. docker Exec

Docker Exec: Execute the command in the running container
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Options description:
-D: Separation mode: run in the background
-I: Keep stdin open even if there is no attachment
-T: assign a Pseudo Terminal.

docker exec -it mynginx /bin/sh /root/runoob.sh
Execute the/root/runoob. Sh script in interactive mode in container mynginx
docker exec -it mynginx /bin/bash
Enable an interactive terminal in container mynginx

Vi. docker Hub service commands 1. docker Login

Docker login :? Log on to a docker image repository. If the image repository address is not specified, the docker hub is the official repository by default.
Docker logout :? Log out of a docker image repository. If the image repository address is not specified, the docker hub is the official repository by default.

docker login [OPTIONS] [SERVER]docker logout [OPTIONS] [SERVER]

Options description:
-U: Login User Name
-P: logon Password
Log on to docker Hub
Docker login-u username-P Password

2. docker logout

Log out of docker Hub
docker logout

3. docker pull

Docker pull :? Pull or update a specified image from the image repository
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Options description:
-A: Pull all tagged Images
-- Disable-content-Trust: Ignore image verification. It is enabled by default.
Download the latest Java image from docker hub.
docker pull java
Download all images whose repository is Java from docker hub.
docker pull -a java

4. docker push

Docker push :? To upload a local image to the image repository, you must first log on to the image repository.
docker push [OPTIONS] NAME[:TAG]
Options description:
-- Disable-content-Trust: Ignore image verification. It is enabled by default.

Upload the local image myapache: V1 to the image repository.
docker push myapache:v1

5. docker search

Docker search :? Search for images from docker Hub
docker search [OPTIONS] TERM
Options description:
-- Automatic: only images of the Automatic Build type are listed;
-- No-trunc: displays the complete image description;
-S: list the images with no less than the specified number of favorites.

Search for all images whose names contain Java and whose number is greater than 10 from docker Hub
docker search -s 10 java

7. Container rootfs operation command 1. docker commit

Docker commit: create a new image from the container.
docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Options description:
-A: the author of the submitted image;
-C: Use the dockerfile command to create an image;
-M: description text when submitting the request;
-P: When commit is enabled, the container is paused.
Save the container a404c6c174a2 as a new image and add the submitted information and instructions.
docker commit -a "scorpio.com" -m "my apache" a404c6c174a2 mymysql:v1

2. docker CP

Docker CP: used to copy data between containers and hosts.

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

Options description:
-L: Keep the link in the source target

Copy the host/www/docker directory to the/WWW directory of the container 96f7f14e99ab.
docker cp /www/docker 96f7f14e99ab:/www/
Copy the host/www/docker directory to the 96f7f14e99ab container, and rename the directory to www.
docker cp /www/docker 96f7f14e99ab:/www
Copy the/WWW directory of the 96f7f14e99ab container to the/tmp directory of the host.
docker cp 96f7f14e99ab:/www /tmp/

3. docker diff

Docker DIFF :? Check for changes to the file structure in the container
docker diff [OPTIONS] CONTAINER
View the file structure changes of the container mymysql.
docker diff mymysql

Docker Quick Start-common docker commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.