Part-2 container (Container) Introduction
The Docker architecture has three levels, from high to low as follows:
- Stack (Stack)
- Services (Service)
- Containers (Container)
These are now exposed in the layer of the container, similar to the underlying foundation; the service is what functions/services are implemented on this basis; higher-level stacks define some of the interactions within these services.
Benefits of Docker
The development of a thing in the past, after the development of the machine to the server to deploy, related to the configuration of various environments, a variety of software-dependent installation, very troublesome; Docker is equivalent to providing a class of container services, packing these things up, providing standard service, you carry containers to this ship, that pier, He can be put there to provide good service; To pack these things, of course, configuration is required, and the configuration file is called Dockerfile; that is: Dockerfile determines the environment in the container, and this environment is isolated from the system environment.
Docker example
- Create a Dockerfile file under a folder with the following contents:
# Use an official Python runtime as a parent imageFROM python:2.7-slim# Set the working directory to /appWORKDIR /app# Copy the current directory contents into the container at /appADD . /app# Install any needed packages specified in requirements.txtRUN pip install --trusted-host pypi.python.org -r requirements.txt# Make port 80 available to the world outside this containerEXPOSE 80# Define environment variableENV NAME World# Run app.py when the container launchesCMD ["python", "app.py"]
- Create Requirements.txt and app.py files, respectively, with the contents of
FlaskRedis
from flask import flaskfrom redis import Redis, Rediserrorimport osimport socket# Connect to Redisredis = Redis (host= "Redis", Db=0, socket_connect_timeout=2, socket_timeout=2) app = Flask (__name__) @app. Route ("/") def hello (): try: visits = REDIS.INCR ("counter") except Rediserror:visits = "<i>cannot Connect to Redis, counter dis abled</i> "html ="
- Compile (build),
docker build -t friendlyhello .
- Run the app,
docker run -p 4000:80 friendlyhello
- View
docker image ls
, you can see Friendlyhello this image
- Browser Access localhost:4000, you can see the app we wrote with Flask
- You can use the background mode to start the app, and the command is
docker run -d -p 4000:80 friendlyhello
- Background mode-enabled apps can be used
docker container stop container_id
to turn off
- Look at the containers that Docker is running,
docker ps
and ps
like Linux, you can add -a
parameters to view all
Share Docker Imagesgit corresponds to have github,docker corresponding to have Dockerhub, register a Dockerhub account first, then can share;
docker login
Login account
docker tag image
To hit the label, similar todocker tag friendlyhello wang/get-started:part2
docker push username/repository:tag
, to push the mirror
- Pull and then run the mirror
docker run -p 4000:80 username/repository:tag
Getting Started with Docker (MAC Environment)-Part 2 container (container)