sequence

Logically, every programmer should have a list of Docker skills. Docker skills like this important bonus points also add money how can not, but use less easily rusty, so I put my evernote docker skills list out

preface

In order for you to be able to hands-on test, I also say the installation method

  1. The installation

The old version docker

yum install docker -y
Copy the code

The latest version of Docker

cat >/etc/yum.repos.d/docker.repo <<EOF [dockerrepo] name=Docker Repository baseurl=https://yum.dockerproject.org/repo/main/centos/7 enabled=1 gpgcheck=1 gpgkey=https://yum.dockerproject.org/gpg EOF

yum install docker-engine -y
Copy the code
  1. Start and place startup items

Centos7.0 version

systemctl start docker
systemctl enable docker
Copy the code

Compatible with older versions

service docker start
chkconfig docker on
Copy the code

Docker use

View docker information

View the Docker version

docker version
Copy the code

Displays docker system information

docker info
Copy the code

mirror

Search the mirror

Docker Search [image]Copy the code

Download mirror

Docker pull [images] sudo docker pull ubuntu: 12.04 docker pull registry.hub.docker.com/ubuntu:12.04Copy the code

Release the mirror

Docker push [image]Copy the code

Remove the mirror

Docker RMI [image]Copy the code

View a local image (column directory)

docker images		
Copy the code

Historical mirror

Docker History [image]Copy the code

Remove the mirror

Docker RMI /IDCopy the code

Delete all stopped mirrors

docker rmi `docker images -q`
Copy the code

Delete all stopped mirrors

docker rmi $(docker images -q)
Copy the code

Forcibly delete all mirrors

docker rmi -f `docker images -q`
Copy the code

Forcibly delete all mirrors

docker rmi -f $(docker images -q)
Copy the code

Delete an unlabeled mirror

docker rmi $(docker images -q -f dangling=true)
Copy the code

Container operation

Create a container

Hello world output

Docker run [image] /bin/echo Hello worldCopy the code

Interactive entry container

Docker run -i -t /bin/bashCopy the code

Install the new program in the container

Docker run apt-get install -y app_nameCopy the code

Into the container

docker exec -it kali /bin/bash
Copy the code

Start the container and run in the background

docker run -it -d kali	
Copy the code

Ports can be mapped when the container is started

docker run -it -p 53:53 kali
Copy the code

Check the container

Lists the currently running containers

docker ps 	
Copy the code

List all containers

docker ps -a 	
Copy the code

Lists the containers that were last started

docker ps -l 	
Copy the code

View all container ids

docker ps -a -q
Copy the code

Container operation

Display container information

Docker inspect [container name]/IDCopy the code

Start the container

Docker start [container name]/IDCopy the code

Stop the container

Docker stop [container name]/IDCopy the code

Kill the container

Docker kill [container name]/IDCopy the code

Restart the container

Docker restart [container name]/IDCopy the code

Into the container

Docker attach [container name]/IDCopy the code

Viewing container Logs

Docker logs [container name]/IDCopy the code

View container modification operations

Docker diff [container name]/IDCopy the code

Display container process information

Docker top [container name]/IDCopy the code

Copies data from a container to a local location

docker cp ID:/path to_path
Copy the code

Copies data from a container to a local location

Docker cp [container name]:/path to_pathCopy the code

Alternative method of entering container

PID = docker inspect --format "{ {.State.Pid} }"[Container name]/ID nsenter -t$PID -u -i -n -p
Copy the code

Remove the container

Remove the container

Docker RM [Container]/IDCopy the code

Delete all stopped containers

docker rm `docker ps -a -q` 
Copy the code

Delete all stopped containers

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

Forcibly delete all containers

docker rm -f `docker ps -a -q` 	
Copy the code

Forcibly delete all containers

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

Export and import containers

Methods a

Save (save the image to a tar package; -o, –output=”” Write to an file )

Docker save [container name] -o [path]Copy the code

Load (Load an image in tar format; -i, –input=”” Read from a tar archive file )

Docker load -i [path]Copy the code
Method 2

Save the file as tar

Docker save [container name] > [path]/xxxx.tarCopy the code

Load tar file into docker

Docker load [container name] < [path]/xxxx.tarCopy the code

Dockerfile

Dockerfile consists of a line of command statements and supports comment lines starting with #. Generally, Dockerfile is divided into four parts: basic image information, maintainer information, image operation instructions and container startup instructions

You must initially specify the name of the mirror on which you are based, followed by the recommended maintainer information. This is followed by mirror operation instructions, such as the RUN command, which executes the following command on the image. Each time the RUN command is RUN, a new layer is added to the image and committed. Finally, there is the CMD directive, which specifies the action commands to run the container

instruction

FROM

FROM <image>:<tag>
Copy the code

MAINTAINER

MAINTAINER <name>
Copy the code

RUN

RUN <command>
RUN ["/bin/bash", "-c", "echo hello"]
Copy the code

Each RUN command executes the specified command based on the current image and submits it as a new image. You can use \ to wrap lines when the command is long

CMD

Three formats are supported

  • CMD ["executable","param1","param2"]Exec execution, recommended;
  • CMD command param1 param2Run the command in /bin/sh for applications requiring interaction.
  • CMD ["param1","param2"]Default parameters provided to ENTRYPOINT;

Specifies the command to execute when starting the container. There can be only one CMD command per Dockerfile. If multiple commands are specified, only the last command will be executed.

If the user specified a command to run when starting the container, CMD commands are overridden

EXPOSE

Format for EXPOSE […] .

Tells the Docker server container the exposed port number for interconnecting systems. When starting the container, you need to pass -p, and the Docker master will automatically assign a port to forward to the specified port

ENV

ENV. Specifies an environment variable that will be used by subsequent RUN directives and held while the container runs

ENV PG_MAJOR 9.3 ENV PG_VERSION 9.3.4 RUN curl - SL http://example.com/postgres-$PG_VERSION.tar.xz | tar - xJC The/usr/SRC/postgress &&... ENV PATH /usr/local/postgres-$PG_MAJOR/bin:$PATHCopy the code

ADD

The format is ADD.

This command copies the specified into the container. Where can be a relative path to the directory where the Dockerfile resides; It could also be a URL; It can also be a tar file (automatically decompressed into a directory)

COPY

The format is COPY.

Copy localhost (relative to the directory where the Dockerfile resides) to the container. COPY is recommended when a local directory is used as the source directory

ENTRYPOINT

Two formats:

ENTRYPOINT ["executable"."param1"."param2"]
# (executed in shell).
ENTRYPOINT command param1 param2
Copy the code

Configure the commands executed after the container is started and cannot be overridden by the arguments provided by Docker Run.

There can only be one ENTRYPOINT per Dockerfile. When multiple entryPoints are specified, only the last ENTRYPOINT takes effect

VOLUME

The format is VOLUME [“/data”].

Create a mount point that can be mounted from a local host or other container. It is typically used to hold databases and data that needs to be held

USER

The format is USER daemon.

Specify the user name or UID to RUN the container, and subsequent runs will also use the specified user

WORKDIR

The format is WORKDIR /path/to/ WORKDIR.

Configure the working directory for subsequent RUN, CMD, and ENTRYPOINT directives.

Multiple WORKDIR directives can be used, and subsequent commands, if their arguments are relative paths, will be based on the path specified by the previous command. For example,

WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd
Copy the code

The final path is /a/b/c

ONBUILD

The format is ONBUILD [INSTRUCTION].

Configure the operation instructions to perform when the created image is used as the base image for other newly created images.

For example, Dockerfile creates the image image-a with the following content.

[...].  ONBUILD ADD . /app/src ONBUILD RUN /usr/local/bin/python-build --dir /app/src [...]Copy the code

If A new image is created based on image-a and the new Dockerfile uses FROM image-a to specify the base image, the ONBUILD directive will be executed automatically, which is equivalent to adding two directives later.

FROM image-A

#Automatically run the following
ADD . /app/src
RUN /usr/local/bin/python-build --dir /app/src
Copy the code

It is recommended to label images using the ONBUILD directive, such as Ruby :1.9-onbuild

other

I have one thing to say: “ALTHOUGH I am not good at writing articles, I have good skills. Like and follow, don’t miss it.”

If you find this article helpful, please like it and share it with us. If you are interested, you can also browse the author’s other articles. If you think this article is a waste of your time, please leave your comments in the comments section. If you have any questions, please leave a message. The author is willing to spend time and energy to find answers and discuss them together.