Using Docker

Keywords: PHP Docker Java network

Foundation of Docker Technology:

Namespace, the basis of container isolation, to ensure that container A cannot see container B. 6 namespace: User,Mnt,Network,UTS,IPC,Pid

cgroups, container resource statistics and isolation. cgroups: cpu,blkio,device,freezer,memory

unionfs, typical: aufs/overlayfs, the foundation of layered image implementation

Docker common commands

Container related operations

Note that there are two common parameters in run

docker run --name xxx -d -p 5000:5000 image address

Here write a - d to run in the background, or write - it, that is, run, and output console.writeln() in the code.

docker create # Create a container but do not start it
docker run # Create and start a container
docker stop # Stop the container and send a signal SIGTERM
docker start # Start a container in a stopped state
docker restart # Restart a container
docker rm # Delete a container
docker kill # Send signal to container, default SIGKILL
docker attach # Connect(Get into)To a running container
docker wait # Block to a container until it stops

Get container related information

docker ps # Display status as run( Up)Container
docker ps -a # Show all containers,Including in operation( Up)Of and out of(Exited)
docker inspect # Go deep inside the container to get all the information of the container
docker logs # View the container's log(stdout/stderr)
docker events # obtain docker Real time events for the server
docker port # Show port mapping for container
docker top # Display the process information of the container
docker diff # Show changes before and after the container file system

Export container

docker cp # Copy files or directories from the container
docker export # Export the entire file system of the container as one tar No bags layers,tag Such information

implement

docker exec # Execute a command in the container, which can be executed bash Enter interactive

Mirror operation

When using docker to package the packed files with docker, pay attention to

docker build -t package name..

docker images # Display a list of all local mirrors
docker import # From one tar Package creates an image, often with export Combined use
docker build # Use Dockerfile Create a mirror (recommended)
docker commit # To create an image from a container, you can package the modified container into an image
docker rmi # Delete a mirror
docker load # From one tar The package creates an image, and save Cooperative use
docker save # Save a mirror as a tar Bag, belt layers and tag information
docker history # Display the historical command to generate a mirror
docker tag # Create an alias for the image

Image warehouse operation

docker login # Log in to a registry
docker search # from registry Warehouse search image
docker pull # Download Image from warehouse to local
docker push # Make a mirror image push reach registry Warehouse

Get the Container IP address (the Container status must be Up)

docker inspect id | grep IPAddress | cut -d '"' -f 4

Get port mapping

docker inspect -f '{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' id

Get environment variable

docker exec container_id env

Kill all running containers

docker kill $(docker ps -q)

Delete old (created a week ago) containers

docker ps -a | grep 'weeks ago' | awk '{print $1}' | xargs docker rm

Delete stopped containers

docker rm `docker ps -a -q`

Remove all mirrors, be careful

docker rmi $(docker images -q)

So how to package docker? java is similar to netcore

Under java

Dockerfile
FROM frolvlad/alpine-oraclejdk8
COPY . /publish
#Set the working directory to the folder '/ publish', which is the default folder for container startup.
WORKDIR /publish
#Set 80 port for external exposure of Docker container
EXPOSE 8080
ENTRYPOINT ["java","-jar","boot-security-1.0.jar"]

Under netcore

"#Build our image based on 'microsoft/dotnet:1.0.0-core'
FROM microsoft/aspnetcore:latest
#Copy all files in the project publish folder to the publish folder in the docker container 
COPY . /publish

#Set the working directory to the folder '/ publish', which is the default folder for container startup.
WORKDIR /publish

#Set 80 port for external exposure of Docker container
EXPOSE 80

#Use 'dotnet HelloWebApp.dll' to run the application
ENTRYPOINT [""dotnet"", ""NetCore.dll""]"

A common set

#Enter the path of file packaging, including Dockerfile Under the directory
cd XXX
#Pack docker,Notice that there is a space .
docker build -t ImageName .
#Pack a dozen. tag
docker tag ImageName AliyunImageRegistryUrl
#docker Package pushed to Alibaba cloud image warehouse
docker push AliyunImageRegistryUrl
#Stop existing containers, if any
docker stop ContainerName
#Delete container
docker rm ContainerName
#Delete mirror image
docker rmi AliyunImageRegistryUrl
#Run the image, if not docker I will try to download it myself
docker run --name ContainerName -d -p 5000:5000 AliyunImageRegistryUrl

Posted by domino1 on Wed, 30 Oct 2019 13:18:40 -0700