In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to define a mirror image with Dockerfile". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Container preparation work
Install Docker, version 1.13 or above.
Give your Docker environment a quick test to make sure everything is ready:
Docker run hello-world
Your new development environment
In the past, if you wrote an pyhton application, your first step was to install the python development environment on your machine. But you need the running environment on your machine to match the application perfectly, and you also need to match the production environment.
With docker, you can migrate a convenient python runtime as a mirror without installation. Then, by building an image that contains the python runtime environment and your application code, you can ensure that your code and the runtime environment are perfectly integrated and running.
These convenient images are defined by a file called DockeFile.
Define a mirror through Dockerfile
Dockerfile defines what environment to run inside the container. Access to resources such as network interfaces and disk drives is virtualized in this environment, isolated from the rest of the system, so you need to map ports to the outside world and identify which files to "copy" to the running environment. However, after doing this, you can expect that the build of the application defined in this Dockerfile will behave exactly the same at run time.
Dockerfile
Create an empty folder. Use the cd command to go to the newly created folder, create a file named Dockerfile, copy the following to the file, and save it.
# 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 8 Define environment variableENV NAME World# Run app.py when the container launchesCMD ["python", "app.py"]
This Dockerfile is associated with two files that we haven't created yet, namely app.py and requirements.txt. Let's create.
Application part
Create 2 files, requirements.txt and app.py, and put them in the same folder as Dockerfile. This completes our application, and you can find it easy to create an application with. When the above Dockerfile is built into an image, app.py and requirements.txt are added to the image through the Add command, and the Expose command can expose the port, which can be accessed through http.
Requirements.txtFlaskRedisapp.pyfrom 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 = "cannot connect to Redis Counter disabled "html =" Hello {name}! "\" Hostname: {hostname} "\" Visits: {visits} "return html.format (name=os.getenv (" NAME "," world "), hostname=socket.gethostname (), visits=visits) if _ _ name__ =" _ _ main__ ": app.run (host='0.0.0.0', port=80)
Now we see that pip install-r requirements.txt installs the Flask and Redis libraries for Python, and the application prints the environment variable NAME and the output of calling socket.gethostname (). Finally, because Redis is not running (because we only have the Python library installed, not Redis itself), we should expect that trying to use it here will fail with an error message.
That's the point! You do not need Python or any requirements.txt files on your system, nor do you need to install or run this image on your system. It looks like you don't really use Python and Flask to build an environment, but you already have it.
Build application
We are ready to build the application. Make sure you have the highest permissions on the newly created files. The following is via the ls command, and the following should be displayed:
$lsDockerfile app.py requirements.txt
Now run the build command. Create a Docker image with the-t tag to give the image a friendly name.
Docker build-t friendlyhello.
Where is the image you built? In your machine's local Docker image registry. "
$docker image lsREPOSITORY TAG IMAGE IDfriendlyhello latest 326387cea398 runs the application
Run the application and use-p to map the machine's port 4000 to the container's published port 80:
Docker run-p 4000 purl 80 friendlyhello
You should see a message on http://0.0.0.0:80 that Python is serving your application. But the message comes from inside the container, and it does not know how to map the container's port 80 to 4000
In a browser, http://localhost:4000 can view what is displayed on a web page.
You can also view the same content through the curl command:
$curl http://localhost:4000Hello Worldwide Hostname: 8fc990912a14Visits: cannot connect to Redis, counter disabled
The purpose of this port remapping of 4000 docker run 80 is to demonstrate the difference between EXPOSE in Dockerfile and content published using docker run-p. In the next steps, we just need to map port 80 on the host to port 80 in the container and use http:// localhost.
Press CTRL+C to end the process.
You can now have the application run in a back-end process, in detached mode.
Docker run-d-p 4000UR 80 friendlyhello
You can get the container ID of the application and stop the application through the container ID. The container is running in the background. You can use the docker container ls command to view the abbreviated container ID:
$docker container lsCONTAINER ID IMAGE COMMAND CREATED1fa4ab2cf395 friendlyhello "python app.py" 28 seconds ago
Now to end the process with the docker containner stop command, you need to use container id, as follows:
Docker container stop 1fa4ab2cf395 shares your image:
To demonstrate the portability of the container we just created, we upload the image we built and run it anywhere else. After all, when you want to deploy a container to a production environment, you need to know how to push the registration warehouse.
A registration repository is a collection of repositories, and a repository is a collection of images-a bit like a GitHub repository, but the code has been created. Registering an account on the repository can create many repositories. Docker CLI uses Docker's public registry by default.
Log in to dokcer Id
If you do not already have an Docker account, please register an account on the website cloud.docker.com. Write down your user name.
Log in to the Docker public registry on your local computer.
$docker login tag image
The command to associate the local image with the repository in the registry is username / repository:tag. This tag is optional, but recommended because it is the mechanism used by the registration authority to provide versions of Docker images. Provide the repository for the context and mark it with a meaningful name, such as get-started:part2. This places the image in the startup repository and marks it as part2.
Now, put it together to mark the image. Run the dock tag image with your user name, repository, and tag name to upload the image to the destination you want. The syntax of the command is:
Docker tag image username/repository:tag
For example:
Docker tag friendlyhello john/get-started:part2
Run the docker image ls command to view the image of your new tag.
$docker image lsREPOSITORY TAG IMAGE ID CREATED SIZEfriendlyhello latest d9e555c53008 3 minutes ago 195MBjohn/get-started part2 d9e555c53008 3 minutes ago 195MBpython 2.7-slim 1c7128a655f6 5 days ago 183MB. Push image
Upload an image of your tag to the warehouse
Docker push username/repository:tag
When completed, the results of this upload will be released to the public. If you log in to Docker Hub, you can see the new image there with its pull command.
Get and run the image from the remote repository
From now on, you can use docker run and use this command to run your application on any machine:
Docker run-p 4000 purl 80 username/repository:tag
If the image is not on the local machine, docker pulls it from the repository.
$docker run-p 4000 john/get-started:part2' locallypart2 80 john/get-started:part2Unable to find image 'john/get-started:part2' locallypart2: Pulling from john/get-started10a267c67f42: Already existsf68a39a6a5e4: Already exists9beaffc0cf19: Already exists3c1fe835fb6b: Already exists4c9f1fa8fcb8: Already existsee7d8f576a14: Already existsfbccdcced46e: Already existsDigest: sha256:0601c866aab2adcc6498200efd0f754037e909e5fd42069adeff72d1e2439068Status: Downloaded newer image for john/get-started:part2 * Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
No matter where docker run is executed, it will extract your image and all the dependencies in Python and requirements.txt, and run your code. They are all in a neat package, and you don't need to install any Docker on the host
Command review
Here is a list of the basic Docker commands for this page, as well as some related commands, if you want to explore before continuing.
Ocker build-t friendlyhello. # Create image using this directory's Dockerfiledocker run-p 4000 List all running containersdocker container ls 80 friendlyhello # Run "friendlyname" mapping port 4000 to 80docker run-d-p 4000 friendlyname 80 friendlyhello # Same thing, but in detached modedocker container ls # List all running containersdocker container ls-a # List all containers Even those not runningdocker container stop # Gracefully stop the specified containerdocker container kill # Force shutdown of the specified containerdocker container rm # Remove specified container from this machinedocker container rm $(docker container ls-a-Q) # Remove all containersdocker image ls-a # List all images on this machinedocker image rm # Remove specified image from this machinedocker image rm $(docker image ls-a-Q) # Remove all images from this machinedocker login # Log in this CLI session using your Docker credentialsdocker tag username/repository:tag # Tag for upload to registrydocker push username/repository:tag # Upload tagged image to registrydocker run username/repository:tag "how to define an image with Dockerfile" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.