Lab 2 - Docker
Lab 2 - Docker
Lab 2 - Docker
• Prerequisites
• You don’t need to know anything about containers to
take this course, and no extensive programming
experience is required. However, you must know how
to run command-line tools from your workstation.
• If you don’t have Docker already, you need install
Docker Community Edition (CE) or use the use the
Play-with-Docker website. Docker installations are
available from store.docker.com for your Mac,
Windows, or Linux machine.
• Use the Docker CLI to run our first container.
1- Open a terminal on your local computer.
2- Run docker container run -t ubuntu top
– Use the docker container run command to run a
container with the ubuntu image, using
the top command. The -t flag allocates a pseudo-
TTY, which you need for the top to work correctly.
O/P –
• The docker run command will result first in a docker pull to
download the ubuntu image onto your host. Once it is
downloaded, it will start the container. The output for the
running container should look like this:-
app = Flask(__name__)
@app.route("/")
def hello():
return "hello world!"
if __name__ == "__main__":
app.run(host='0.0.0.0')
• This is a simple Python app that uses flask to expose a http
web server on port 5000 (5000 is the default port for flask).
Don’t worry if you are not too familiar with Python or Flask.
These concepts can be applied to an application written in any
language.
• Optional: If you have Python and pip installed, you can locally
run this app. If not, move on to the next step.
2. CREATE AND BUILD THE DOCKER IMAGE
• Now that you have built the image, you can run it to see that it
works.
1 - Run the Docker image.
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello Beautiful World!"
if __name__ == "__main__":
app.run(host='0.0.0.0')
• 2 - Now that your app is updated, you need to rebuild your
app and push it to the Docker Hub registry.First rebuild, this
time use your Docker Hub username in the build command.
• $ docker image build -t [username]/python-hello-world .
• Notice the “Using cache” for Steps 1-3. These layers of the
Docker image have already been built, and docker image
build will use these layers from the cache, instead of
rebuilding them.
• There is a caching mechanism in place for pushing layers too.
Docker Hub already has all but one of the layers from an
earlier push, so it only pushes the one layer that has changed.
• When you change a layer, every layer built on top of that will
have to be rebuilt. Each line in a Dockerfile builds a new layer
that is built on the layer created from the lines before it. This
is why the order of the lines in your Dockerfile is important.
We optimized our Dockerfile so that the layer that is most
likely to change (COPY app.py /app.py) is the last line of the
Dockerfile. Generally for an application, your code changes at
the most frequent rate. This optimization is particularly
important for CI/CD processes, where you want your
automation to run as fast as possible.
6. UNDERSTAND IMAGE LAYERS