Create your first Docker Container

Balaji Boominathan
techzap
Published in
5 min readMay 7, 2020

This is a very simple tutorial for getting started with Docker. I’ll try to keep this as simple as possible. In this, we are going to build a basic Flask application and dockerize the application. By the end of this tutorial, you’ll get familiar with Docker and a few Docker commands.

Docker man page

Overview of Docker

Docker is a platform for developing and running applications. It automates the deployment of applications. It’s a tool for running applications in an isolated environment. Docker makes it easy to share an application with all of its dependencies across different environments.

Docker Image

Image is a template for creating an environment. The Docker image contains the Operating System, Software, and application code. These are all packaged in a single file. Images are defined with the Dockerfile.

Dockerfile

Dockerfile is built into a docker image

The Dockerfile contains the steps that are needed to package your application(to create the image). These steps include configuring the Operating system, install the required packages or software, copy the files from one place to another.

Create the Flask application

Let’s dive into code

Flask is the micro web framework for building small web applications.

The directory structure is as follows:

flaskapp/
├─── Dockerfile
├─── app.py
├─── requirements.txt

Create a directory with the name of your choice. The command for creating a directory in Linux is,

$mkdir flaskapp

Navigate inside the directory you created. Now create a file named app.py and copy the following code to it.

app.py

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, This is my first Docker app!'
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')

This is a simple Hello world Flask app. If you want to learn about flask in detail, then visit this page and explore about flask. Let’s concentrate more on Docker now.

Then create the requirements.txt file. Add the following line to it.

requirements.txt

Flask==1.1.2

Now we are going to create a Dockerfile that dockerizes this application.

Write the Docker File

The Dockerfile should not have any extension.

If you haven’t downloaded the docker yet. Get the Docker from here. Detailed installation instructions can be found here.

Create a file called Dockerfile. This must start with capital letter D.

Dockerfile

FROM alpine:3.11RUN apk add --update pythonRUN apk add --update py-pipCOPY ./requirements.txt /app/requirements.txtCOPY . /appWORKDIR /appRUN pip install -r requirements.txtEXPOSE 5000CMD python app.py

Let’s look at the instructions line by line to have a better understanding

FROM alpine:3.11

This is our base image. There are a lot of images available in the Dockerhub. We choose alpine since it’s one of a lightweight image.

The FROM allows us to initialize the build over the base image. The number after the colon is the version number. A valid Dockerfile always starts with FROM keyword.

RUN apk --update add python
RUN apk add --update py-pip

These two lines are used to install the python and the pip package respectively. The RUN instruction will execute a new layer on top of the current image. This instruction is used to install packages and creating new directories.

COPY ./requirements.txt /app/requirements.txtCOPY . /app

The COPY instruction is used to copy the files from source(.) to the destination(/app). The ‘.’ represents the current directory. This basically copies the flask app into the image.

WORKDIR /app

The WORKDIR sets the working directory.

RUN pip install -r requirements.txt

This reads the requirements.txt file and installs the specified packages one by one on the host.

EXPOSE 5000

The EXPOSE exposes a port that is used by Flask. When you run the image you’ll get a container that container will run on this port.

CMD python app.py

CMD is the command that is executed when you start a container. Here, you are using the command to run your Python application. There can be only one CMD per Dockerfile. If you specify more than one, then the last CMD will take effect.

When you start a container this command is executed. There should be only one CMD instruction in the Dockerfile. If there is more than one, it’ll execute the last instruction.

Build the docker image

Enough of information. Let’s run the application

To build the Docker image, execute this command in the terminal in the directory we created.

$docker build -t flask-app .

The -t is used to name your image flask-app. The . , in the end, represents the current directory. When you execute this for the first time, it’ll have to download all of the layers that make up to build the image. After that, it’ll use the cache. If you’re not an administrator, try to run the command with the sudo.

docker build log

To run the application, execute this command

$docker run -p 5000:5000 flask-app

The -p is used to map the port running inside the container to your host. Here we’re mapping the port 5000. The value before the colon represents the port running on your host and the value after colon represents the port running inside the container.

Now, navigate to http://localhost:5000/ in your browser, you’ll see the “Hello, This is my first Docker app!” in the window.

Container

To know the container id enter the following command in your terminal.

$docker ps

This lists all the running containers in the Docker engine. This has information about containers like its ID, created time, status and on which port it’s running.

$docker ps -a

This will list the containers that have been stopped as well.

$docker stop [container name]

This will stop all the running containers.

I hope now you have a better understanding about Docker and how to write your Dockerfile. Keep Dockerizing more apps. Cheers!

Resources:

  1. https://docs.docker.com/get-started/
  2. https://docs.docker.com/reference/

--

--