# #Day12  🐳 Dockerizing a Python Program🐍

"Previously, we have seen how to install Docker. Now, start by navigating to the 'example' folder to write the first Dockerfile and then move on to the next example.

Let's create an image of a simple Python program.

First, let's create a file named [app.py](http://app.py). We can use the vim command for this:

`vim app.py`

then it open text editor

`print("Hello World")`

save it exit

"Here, I have a simple program, but you can use a more complex program if needed."

now let's create an Dockerfile for it

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701070879177/2c46f8e5-76ff-40c5-a9ea-e69baf1d11b5.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701071053589/5edbab78-52fa-48cc-ae12-1a6959d0c1a8.png align="center")

**FROM ubuntu:latest**

`FROM ubuntu:latest` in a Dockerfile is the way to specify the base image for your Docker container.

The `FROM` instruction initializes a new build stage and sets the base image for subsequent instructions in the Dockerfile. In this case, `ubuntu:latest` is the base image, and `latest` refers to the most recent version of the Ubuntu image available on the Docker Hub.

**WORKDIR /app:** This sets the working directory inside the container to `/app`. This is where subsequent commands will be executed, and it's a good practice to have a designated working directory.

**COPY . /app:** This copies the contents of the current directory on your host machine (where the Dockerfile is located) to the `/app` directory inside the container. This assumes that your application files, including [`app.py`](http://app.py), are in the same directory as your Dockerfile.

**RUN apt-get update && apt-get install -y python3 python3-pip:** This updates the package list and installs Python 3 and pip inside the container. The `-y` flag automatically assumes "yes" to prompts, which is useful for automated builds.

**ENV NAME World:** This sets an environment variable named `NAME` with the value "World." Environment variables are often used to configure applications.

**CMD \["python3", "**[**app.py**](http://app.py)**"\]:** This specifies the default command to run when the container starts. In this case, it runs the Python script [`app.py`](http://app.py). This assumes that the Python script is in the `/app` director

esc+:wq+enter

`docker build -t areefann567/my-first-docker-image:latest .`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701071002862/38ffbceb-f1fe-4ea9-857c-d2e08c41dea2.png align="center")

**docker build:** This command is used to build a Docker image from a Dockerfile.

**\-t areefann567/my-first-docker-image:latest:** The `-t` flag is used to specify a name and optionally a tag for the image. In this case, the image will be named `areefann567/my-first-docker-image` with the tag `latest`. This is a common convention to use `latest` as the tag for the most recent version of the image.

The dot (`.`) at the end specifies the build context, which is the current directory. The build context includes the Dockerfile and any files referenced in the Dockerfile using commands like `COPY`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701071121359/81670b33-c4a5-4b13-92a1-030dca99ec87.png align="center")

`docker run`: This command is used to run a Docker container.

use `-it` together, you're essentially telling Docker to run the container in an interactive mode with a pseudo-TTY(This makes the container behave like a real terminal, and you can see the output of commands as if they were executed directly on your machine.)

let's push this image to docker hub

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701071188818/7df46e46-3070-4dd6-a030-0826d261eb60.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701071244968/8a9dab3a-a212-40eb-b024-2cac92a5e43a.png align="center")

"Now, using the `docker pull` command, we can retrieve our image whenever we need it."

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701071404542/81e96772-110a-4c11-8989-45646f42189a.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701071443819/a29bc7c0-67ca-4caf-b971-35d66a491bd9.png align="center")

The `docker push` command is used to push a Docker image to a container registry, making it available for others to pull and use. In your case, you're attempting to push the image `areefann567/my-first-docker-image` with the `latest` tag.

let's know some docker commands

`docker images` command is used to list all the Docker images that are currently stored on your local machine.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701071281461/a5b97ca7-ddaf-4e40-8646-bf1f7340841c.png align="center")

`docker ps` command is used to display a list of running Docker containers on your system.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701071363714/2599e98d-dc6b-4583-8cd7-e2e638d10eb8.png align="center")

`docker ps -a` To see all containers (including those that have exited)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701071748999/74ded21d-a368-4975-88a3-d9962b665862.png align="center")

remove the stop/exited containers permanently

`docker rm <container id or Name>`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1701071782254/20ecf519-e397-41c5-bae3-a7d6f14cb78e.png align="center")
