In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "docker container command and use". In daily operation, I believe many people have doubts about docker container command and use. Xiaobian consulted all kinds of information and sorted out simple and easy operation methods. I hope to help you answer "docker container command and use" doubts! Next, please follow the small series to learn together!
docker subcommands overview
subcommand classification subcommand
Docker environment info, version
Container lifecycle management create, exec, kill, pause, restart, rm, run, start, stop, unpause
Mirror repository commands login, logout, pull, push, search
image management build, images, import, load, rmi, save, tag, commit
Container operations attach, export, inspect, port, ps, rename, stats, top, wait, cp, diff, update
Container resource management volume, network
System log information events, history, logs
Get subcommand details
docker COMMAND_NAME -help
Docker container operations
Docker daemons bind to multiple addresses
-H tcp://IP:PORT -H unix:///home/docker/docker.sock
Running container based on base image: [base registry is Ubuntu]
docker run -i -t ubuntu /bin/bash
-i Ensure that STDIN in the container is open
-t Assign a pseudo-tty terminal to the created container
--name CONTAINER_NAME Container naming
-d Create a daemon container
tagged ubuntu images ubuntu:12.04
vessel start-stop
docker start|stop CONTAINER_NAME
docker ps View started containers
-a View all containers
docker ps -n -x displays the last x containers, whether they are running or stopped
docker attach CONTAINER_NAME Attach to the specified container
docker logs COMTAINER_NAME Get the logs of the container
-f parameter to monitor docker logs, similar to tail -f
-t Each log is timestamped.
--tail N Get the last N lines of the log
--tail 0 -f to track the latest logs of a container without having to read the entire log
View processes within containers
docker top CONTAINER_NAME
Running processes inside containers
docker exec -d CONTAINER_NAME COMMAND
-d needs to run a background process
docker exec -i -t CONTAINER_NAME /bin/bash Create a TTY for the executing process and capture STDIN. Not terminating the container after exit is equivalent to creating a new bash reply.
docker run --restart=always ….
--restart checks the container's exit code and decides whether to restart the container based on it, always Docker automatically restarts the container regardless of the container's exit code;on-failure also accepts an optional restart count parameter--restart=on-failure:5 Up to 5 restarts
Deep into the container
docker inspect CONTAINER_NAME performs a detailed inspection of the container and returns its configuration information, including name commands, network configuration, and a lot of useful data
-f|-format='{{ .State. Running}}' View log content, multiple containers can be created at the same time
delete a container
docker rm CONTAINER_NAME
docker rm 'docker ps -a -q' delete all
-q returns only the ID of the container
After Docker is installed,/var/lib/docker stores docker images, containers, and container configurations.
Docker image operations
List all docker images
docker images
pull images from the nearest region
docker pull registry_name|registry_name:xxx
Find Mirrors
docker search registry_name
Docker Hub has two types of repositories:
User warehouse and top-level warehouse
User repositories are created by docker users
The top-level warehouse is managed by people inside docker.
Jamturol/puppet
Jamturol: User name
puppet: Warehouse name
build images
Use docker commit . Login (Docker Hub) Operation Build
Using docker build and Dockerfile files
docker commit CONTAINER_NAME| ID jamturol/apache2 commits only the part that creates the difference between the image of the container and the current state of the container
Dockerfile file
# version: 0.01 Comments
FROM BASE_REGISTRY # BASE_REGISTRY Base mirror as new mirror
MAINTAINER xxx, "xxx@gmail.com" #Mirror author and author's email address
RUN echo 'xxx'> /path/to/file #Each RUN command creates a new mirror layer
RUN 。。。The # RUN command is executed in shell using the command wrapper/bin/sh -c. If shell is not supported or you do not want shell running, you can use the RUN command in exec format. RUN [ "apt-get","install","-y","nginx" ]
。。。
EXPOSE 80 # Docker Applications within this container will use the specified port of the container. Multiple EXPOSE directives can be specified to expose multiple ports externally.
docker build -t="jamturol/static_web" . #Run Dockerfile
- tSet repository and name for new mirror
-t="jamturol/static_web:v1" Set a label for the image,"Image name: label", Docker will automatically set a latest label for the image if no label is specified
--no-cache Skip caching, use the latest version for each package
ENV REFRESHED_AT 2017-07-05 #Set an environment variable named REFRESHED_AT
Explore how mirrors are constructed
docker history REGISTRY_NAME|ID
Starting a container from a new image
docker run -d -p 80 --name static_web jamtuol/static_web nginx -g "deamon off;"
Run nginx in foreground mode, -p exposes which network ports to the outside world
docker ps -l View port mapping
docker port ID 80 View container port mapping
-P uppercase exposes all ports set by EXPOSE in Dockerfile
-p IP:PORT1:PORT2 Bind the container port2 to the host port1
CMD Specifies the command to run when the container starts, CMD ["/bin/bash","-l"]
You can override CMD commands with docker run
Any arguments specified in the ENTRYPOINT docker run command line are passed as arguments to the command specified in the ENTRYPOINT directive again
ENTRYPOINT ["/usr/sbin/nginx"]
docker run--entrypoint flag overrides ENTRYPOINT directive
WORKDIR sets up a working directory inside the container. The programs specified by ENTRYPOINT and CMD will be executed under this directory. Multiple can be set.
docker run -w /var/log …
-w Overwrites working directory at runtime
Environment variables set by ENV, with a value of $variable name, will be persisted into any container created from our mirror
USER specifies what user the mirror will run as
USER nginx can also be UID, specifying user and group
USER UID
USER user:group
VOLUME ["/opt/project"] Create a mount point named/opt/project based on any containers created by this image, or specify multiple volumes
VOLUME ["/opt/project","/data"]
ADD software.lic /opt/application/software.lic copies files and directories from the build environment to the image. Docker is used to determine whether the file source is a directory or a file by the characters at the end of the destination address parameter when installing the application.
COPY conf.d /etc/apache2 Copy files from the local conf.d directory to the/etc/apache2/directory. copy only cares about copying local files in the build context, not extracting and decompressing files. If the destination location does not exist, Docker will automatically create the required directory structure.
ONBUILD can add triggers to mirrors. When a mirror is used as a base mirror for other mirrors, triggers in that mirror will execute. Triggers will insert new instructions during the build process. These instructions can be considered to be specified immediately after FROM. Triggers can be any build instruction.
ONBUILD ADD . /opp/src
ONBUILD RUN cd /app/src && make
Image packaging and import related operations
Package a mirror image
docker save REPO_NAME:TAG > filename.tar
docker load < filename.tar Load an image
removing mirroring
docker rmi jamtuol/static_web
docker run -v $PWD/website:/var/www/html/website:ro ….
-v specifies the source directory of the volume (directory of the local host) and the destination directory in the container. The two directories are separated by: . If the destination directory does not exist, it will be automatically created. ro or rw determines the read and write status.
docker run --link redis:db ...
The--link flag creates a parent-child connection between two containers, requiring two parameters: the name of the container to be connected and the alias of the connected container.
docker run -p 4567 --name webapp --link redis:db -t -i -v $PWD/webapp:/opt/webapp jamturol/sintra /bin/bash
Docker can be forced to allow only connected containers to communicate with each other. You need to add--icc=false when starting the docker daemon to turn off communication between all disconnected containers. Connected containers must run on the same docker host
docker run add--dns or--dns-search configure dns separately for a container
RUN shell.sh Run Shell Script
Build can be performed with multiple files
Dockerfile, shell.sh, var.txt, etc.
docker run --privileged ...
Docker privileged mode can be enabled, allowing containers to run with (almost) all the capabilities their hosts have, including some kernel features and device access
docker wait CONTAINER_NAME Wait for program exit to get return code
docker run --cidfile=/tmp/CONTAINERID.txt
Let docker intercept the container id and save it to the file specified by the--cidfile option
docker run --rm ... Automatically deletes containers after the container process runs out, useful for one-time containers and disposable containers
When VOLUME is specified in Dockerfile, there is an external mount point in the mirror boot container corresponding to run. You can see through inspect, if not specified, there is no external mount point
Dockerfile used for
RUN for var in v{v.. 8};do touch $DIRS/$var; done # DIRS is an environment variable specified by ENV
serf is a decentralized service discovery and orchestration solution characterized by lightweight and high availability, with fault tolerance features
docker run -p 53:53/udp ... in the form of UDP.
docker run -h CONTAINER_host_name 。。。
docker supports multiple SDKs in various languages
At this point, the study of "docker container command and use" is over, I hope to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.