docker Dockerfile file to make its own image
- 2020-12-20 03:55:21
- OfStack
Create an empty directory
$ cd /home/xm6f/dev
$ mkdir myapp
$ cd myapp/
2.vim Dockerfile, which reads as follows:
## 1 A basic python Runtime environment
FROM python
## Set up working directory
WORKDIR /app
## Copies the contents of the current system folder to the container app directory
ADD . /app
## Install the necessary dependency packages
RUN pip install -r softwares.txt
## Open ports for outside container access
EXPOSE 80
EXPOSE 3088
EXPOSE 8080
EXPOSE 8066
## Define environment variables
ENV NAME HELLO
## Run the command
CMD ["python","app.py"]
3. Installation dependencies
vim softwares.txt, which reads as follows:
Flask
Redis
4.vim app.py, which reads 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 to see if the new student is an image
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myfirstapp latest 01ea1129a831 2 hours ago 699MB
7. Start image
$ docker run -p 4000:80 myfirstapp
It can also be run in the background:
$ docker run -d -p 4000:80 myfirstapp
8. Access 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 the browser to access services: http: / / 192.168.1.160:4000
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
## 1 A basic python Runtime environment
FROM python
## Set up working directory
WORKDIR /app
## Copies the contents of the current system folder to the container app directory
ADD . /app
## Install the necessary dependency packages
RUN pip install -r softwares.txt
## Open ports for outside container access
EXPOSE 80
EXPOSE 3088
EXPOSE 8080
EXPOSE 8066
## Define environment variables
ENV NAME HELLO
## Run the command
CMD ["python","app.py"]
0