Docker 技术学习

Docker 命令

list of container existed
docker container ls
list of running containers
docker ps
list of all containers
docker ps -a
enter container
docker exec -it d27bd3008ad9 /bin/bash
stop all running containers
docker stop $(docker ps -q)
remove all containers
docker rm $(docker ps -aq)
stop and remove containers
docker stop $(docker ps -q) & docker rm $(docker ps -aq)
docker system prune –all

Docker-Orientation, Containers

## List Docker CLI commands
docker container –help
## Display Docker version and info
docker –version
docker version
docker info
## Execute Docker image
docker run hello-world
## List Docker image
docker image ls
## List Docker containers (running, all, all in quiet mode)
docker container ls
docker container ls -all
docker container ls -aq
The portable image is define by something called a Dockerfile.
Define a container with Dockerfile
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host -r requirements.txt
# Make port 80 available to the world outside this container
# Define environment variable
# Run when the container launches
CMD ["python", ""]
Build the app
Create a Docker image. Use -t if you want to use the shorter option.
docker build --tag=friendlyhello .
Build image in docer
docker image ls
Note how the tag defaulted to latest. The full syntax for the tag option would be something like –tag=friendlyhello:v0.0.1.
Run the app
Mapping your machine’s port 4000 to the container’s published port 80 using -p:
docker run -p 4000:80 friendlyhello
Stop, remove the container
docker container stop <container_id>
docker container rm <container_id>
Share your image
A registry is a collection of repositories, and a repository is a collection of images.
docker build -t friendlyhello . # Create image using this directory's Dockerfile
docker run -p 4000:80 friendlyhello # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80 friendlyhello # Same thing, but in detached mode
docker container ls # List all running containers
docker container ls -a # List all containers, even those not running
docker container stop <hash> # Gracefully stop the specified container
docker container kill <hash> # Force shutdown of the specified container
docker container rm <hash> # Remove specified container from this machine
docker container rm $(docker container ls -a -q) # Remove all containers
docker image ls -a # List all images on this machine
docker image rm <image id> # Remove specified image from this machine
docker image rm $(docker image ls -a -q) # Remove all images from this machine
docker login # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag # Tag <image> for upload to registry
docker push username/repository:tag # Upload tagged image to registry
docker run username/repository:tag # Run image from a registry
About services
In a distributed application, different pieces of the app are called “services”.
Services are really just “containers in production.” A service only runs one image, but it codifies the way that image runs—what ports it should use, how many replicas of the container should run so the service has the capacity it needs, and so on. Scaling a service changes the number of container instances running that piece of software, assigning more computing resources to the service in the process.
version: "3"
    image: jieanyang/get-started:v0.0.1
      replicas: 5
          cpus: "0.1" # 10% of the cpu
          memory: 50M # RAM
          condition: on-failure
      - "4000:80"
      - webnet
Run your new load-balanced app
Before we can use the docker stack deploy command we first run:
docker swarm init
Now let’s run it. You need to give your app a name. It is set to getstartedlab:
docker stack deploy -c docker-compose.yml getstartedlab
Get the service ID for the one service in our application:
docker service ls
The command lets you view all services associated with the getstartedlab stack:
docker stack services getstartedlab
A single container running in a service is called a task. List the tasks for your service:
docker service ps getstartedlab_web
Either way, the container ID changes, demonstrating the load-balancing; with each request, one of the 5 tasks is chosen, in a round-robin fashion, to respond.
Scale the app
You can scale the app by changing the replicas value in docker-compose.yml, saving the change, and re-running the docker stack deploy command:
docker stack deploy -c docker-compose.yml getstartedlab
Docker performs an in-place update, no need to tear the stack down first or kill any containers.
Take down the app and the swarm
Take the app down
docker stack rm getstartedlab
Take down the swarm
docker swarm leave --force
docker stack ls # List stacks or apps
docker stack deploy -c <composefile> <appname> # Run the specified Compose file
docker service ls # List running services associated with an app
docker service ps <service> # List tasks associated with an app
docker inspect <task or container> # Inspect task or container
docker container ls -q # List container IDs
docker stack rm <appname> # Tear down an application
docker swarm leave --force # Take down a single node swarm from the manager
Docker Documentation
Docker Compose 配置文件详解
Windows 10下Docker使用经验谈


欢迎大家关顾我的Facebook主页「Y Life Book」以及我的公众微信号「不点语书」,我们下期见。

Leave a Comment

Your email address will not be published. Required fields are marked *