Problem scenario
You want to create your own Dockerfile. You also want to invoke reserved words such as FROM, RUN, COPY, WORKDIR, ADD, and LABEL. You want to then create a Docker image from it. You want to ultimately create a working Docker container from that image. How do you do all of this?
Solution
1. In a given directory, create a file called extra.txt. It can have nothing in it and be created with a "touch" command.
2. In this directory create a file calle hello.py with this as its content:
from flask import Flask
app = Flask(__name__)
@app.route('/hi')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
3. In this directory create a file called Dockerfile with the content below:
FROM ubuntu:latest
LABEL Remarks="www.continualintegration.com created this Dockerfile"
RUN apt-get update
RUN apt-get install -y python3 python-pip
RUN apt-get clean all
RUN pip install flask
ADD hello.py /tmp/hello.py
# This stanza expects a hello.py file to be in the same directory as the Dockerfile itself.
COPY extra.txt /tmp/extra.txt
# This stanza expects an extra.txt file to be in the same directory as the Dockerfile itself.
EXPOSE 5000
ENV HOME /home/
# This stanza will set Docker container's home directory in container's env settings
WORKDIR /tmp/
# The default directory when you enter the container will be the directory set above
CMD ["python","/tmp/hello.py"]
4. Run this command (but replace "foobar" and "contint" as you see fit):
docker build -t foobar:contint .
5. Replace abcd1234 below with the alphanumeric value that was displayed (associated with the newly created image ID):
docker create -it abcd1234 bash
# Run the above command with the substituted abcd1234 / container ID.
6. The above command should produce a new container. The container ID should be displayed. Run this command, but substituted wxyz6789 with the container ID you just created:
docker start wxyz6789
7. Run this command, but substitute wxyz6789 with the container ID you just started:
docker exec -it wxyz6789 bash