How to create your own image using Dockerfile

How to create your own image using Dockerfile

1. Create an empty directory

$ cd /home/xm6f/dev 
$ mkdir myapp
$ cd myapp/

2.vim Dockerfile, the content is as follows:

## A basic python runtime environment FROM python
## Set the working directory WORKDIR /app
## Copy the current system folder contents to the container's app directory ADD ./app
## Install necessary dependency packages RUN pip install -r softwares.txt
## Open port for access outside the container EXPOSE 80
EXPOSE 3088
EXPOSE 8080
EXPOSE 8066
## Define environment variable ENV NAME HELLO
## Run command CMD ["python","app.py"]

3. Install dependencies

vim softwares.txt, the content is as follows:

Flask
Redis

4.vim app.py, the content is as follows:

from flask import Flask
from redis import Redis, RedisError
import os
import socket
# Connect to Redis
redis = 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 disabled</i>" 
 html = "<h3>Hello {name}!</h3>" \
   "<b>Hostname:</b> {hostname}<br/>" \
   "<b>Visits:</b> {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)

5. Compile

$ docker build -t myfirstapp .

6. Check that a new image has been generated

$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myfirstapp latest 01ea1129a831 2 hours ago 699MB

7. Start the image

$ docker run -p 4000:80 myfirstapp

It can also be run in the background:

$ docker run -d -p 4000:80 myfirstapp

8. Access to the Services

# curl http://localhost:4000
<h3>Hello world!</h3><b>Hostname:</b> a6655d0d7e74<br/><b>Visits:</b> <i>cannot connect to Redis, counter disabled</i>

Or use a browser to access the service: http://192.168.1.160:4000

9. View the currently running image

$ docker ps
CONTAINER ID MAGE COMMAND CREATED STATUS PORTS NAMES
2db45cab2bb4 myfirstapp "python app.py" 2 minutes ago Up 2 minutes 0.0.0.0:4000->80/tcp elastic_wilson

10. Stop mirroring

$ docker stop 2db45cab2bb4

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of CMD and ENTRYPOINT commands in Dockerfile
  • How to write the best Dockerfile
  • Detailed explanation of COPY and ADD commands in Dockerfile
  • A detailed introduction to the Dockerfile image building file and related commands in Docker
  • Detailed explanation of using Dockerfile to build MySQL image and implement data initialization and permission setting
  • Detailed explanation of the specific use of the ENV instruction in Dockerfile
  • Dockerfile usage examples
  • Dockerfile to create the official Tomcat image and detailed explanation of image usage
  • Summary of common commands in Dockerfile
  • How to deploy SpringBoot project using Dockerfile
  • How to use Dockerfile to create a mirror of the Java runtime environment
  • Introduction to Dockerfile instructions ADD and COPY
  • Detailed explanation of multi-stage (multi-stage build) in Dockerfile
  • Docker Basics: Detailed Explanation of Dockerfile Commands
  • How to deploy nodejs service using Dockerfile
  • Dockerfile instructions explained
  • A brief introduction to the Dockerfile instruction VOLUME
  • Dockerfile simple introduction

<<:  MySQL recursion problem

>>:  js uses cookies to remember user page operations

Recommend

Nginx local directory mapping implementation code example

Sometimes you need to access some static resource...

Node script realizes automatic sign-in and lottery function

Table of contents 1. Introduction 2. Preparation ...

Do you know all 24 methods of JavaScript loop traversal?

Table of contents Preface 1. Array traversal meth...

What to do if you forget the initial password of MySQL on MAC

The solution to forgetting the initial password o...

Detailed explanation of Vue filter implementation and application scenarios

1. Brief Introduction Vue.js allows you to define...

JavaScript regular verification password strength implementation method

exhibit design Password strength analysis The pas...

How to install MySQL 8.0.13 in Alibaba Cloud CentOS 7

1. Download the MySQL installation package (there...

The textarea tag cannot be resized and cannot be dragged with the mouse

The textarea tag size is immutable Copy code The c...

WeChat applet implements sorting function based on date and time

I recently took over a small program project, and...

Implementation of CSS3 button border animation

First look at the effect: html <a href="#...

Summary of xhtml block level tags

* address - address * blockquote - block quote * c...

Implementation of MySQL select in subquery optimization

The following demonstration is based on MySQL ver...

Summary of MySQL time statistics methods

When doing database statistics, you often need to...

Vue implements weather forecast function

This article shares the specific code of Vue to r...

About the problem of offline installation of Docker package on CentOS 8.4

The virtual machine used is CentOS 8.4, which sim...