# Build your first Docker Container

In this blog, let’s dive into building your first Docker image. We aim to create an Ubuntu-based image with Python 3 installed in it.

## Why Build Your Docker Image?

Docker images are the foundation of containers. While public images on Docker Hub are convenient, building your image gives you control over the environment. This ensures consistency across your deployments and provides a deeper understanding of Docker's work.

Ready? Let’s get started!

**Set Up Docker**

First, make sure Docker is installed and running on your machine.

Follow this blog to install Docker on your Linux/WSL.

Verify installation by running:

```plaintext
docker --version
```

**Create a** `Dockerfile`

The `Dockerfile` is a simple text file with instructions to build your Docker image. Create a project directory to keep things tidy:

```plaintext
mkdir docker-python && cd docker-python
```

Now, create a file named `Dockerfile`:

PS: The file is Dockerfile with **no extension**

```plaintext
touch Dockerfile
```

**Write the Dockerfile**

Here’s what your `Dockerfile` should look like:

```plaintext
#Ubuntu as the base image  
FROM ubuntu:latest  

# Update and install Python 3  
RUN apt-get update && apt-get install -y python3
```

**Let’s examine the above file**

1. `FROM ubuntu:latest`: Starts with the latest Ubuntu base image.
    
2. `RUN`: Updates the package list and installs Python 3.
    

**Build the Image**

Time to turn the `Dockerfile` into a Docker image. Run:

```plaintext
docker build -t ubuntu-python3 .
```

**Command Explanation:**

* The `-t ubuntu-python3` flag tags the image with a name. In our case it’s `ubuntu-python3`
    
* The `.` specifies the build context (current directory).
    

If everything goes well, Docker will download the Ubuntu base image and install Python 3.

**Verify Your Image**

Check if your image is built:

```plaintext
docker images
```

You should see `ubuntu-python3` in the list.

**Run a Container**

Let’s test our image by running a container:

```plaintext
docker run ubuntu-python3
```

If everything goes well, your container should be running now.

Run the following command to confirm.

```plaintext
docker ps -a
```

Run the following command to connect to the docker container you just created

```plaintext
docker exec -it ubuntu-python3 /bin/bash
```

You should now see a “new terminal”. This is the terminal of your container.

Run `python -V` In the terminal, you will see the Python version.

If everything went well, you just built your first Docker image, ran it as a container and established a session with the container to run a command.
