# Creating a Docker image using Dockerfile and Launch Container

Create directory for Dockerfile

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723614142174/b9599146-0168-40c6-9641-ca809e3c60b6.png align="center")

Create Docker file, Name should be `Dockerfile`

```plaintext
#Start with an #Ubuntu base image
FROM ubuntu

#Add metadata with a Name Label
LABEL Name="Dinesh"

#Update packages and Install Apache2
RUN apt-get update && apt-get install apache2 -y
```

```plaintext
vi Dockerfile
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723614356788/439729be-f48b-4103-b14f-81b255587aa9.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723615142402/721f4c82-0064-4ac2-97dd-b336046fcdba.png align="center")

**Build the Docker Image :**

```plaintext
docker build -t myimage:v1.0 .
```

* `docker build`: Invokes Docker's build process.
    
* `-t myimage:v1.0`: Tags the resulting image with the name `myimage` and version `v1.0`.
    
* `.`: Uses the current directory as the build context, meaning it looks for a Dockerfile in the current directory to build the image.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723615308032/ec58a9bb-5ee5-44a4-a407-feff14ee42fe.png align="center")

**Verify the Image Creation :**

```plaintext
docker images
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723615379144/f6ef07b0-d824-43bc-919a-0ebc20b33471.png align="center")

Now, i need to install vim & git in inside docker image.

We have two option, one for build new image with new Dockerfile. Another one, build new image from existing image `myimage:v1.0` , Now, i will create new image from existing image.

Need add image name in `FROM` existing Docker image with tag & Which things need to install (like, vim,git) add in `RUN` line

```plaintext
vi Dockerfile
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723616867973/cd3c2a24-d32d-47cf-a6d3-cb95fbff64d8.png align="center")

Now, Build the new Docker Image &gt; Need to change `Image name` or `Tag` for new image.

I have changed tag `v2.0`

```plaintext
docker build -t myimage:v2.0 .
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723616745051/4660fa74-6d2f-416b-830c-666c2932b282.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723616784803/929e591a-429c-4dfa-88bf-845825147e70.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723616821228/a555f658-6c2b-4e81-a83b-d58b3bf19270.png align="center")

**Verify the Image Creation :**

```plaintext
docker images
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723617000348/5618f03a-0fba-445e-8cee-78a620288b9a.png align="center")

**<mark>Launching a Docker Container using Docker Image:</mark>**

**Already, we have created two images, i will use this docker image** `myimage:v2.0` for launch docker container

Step 1 : Verify the Available Docker Images

```plaintext
docker images
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723618134292/7efb7d1f-f95d-44bc-b369-41eefc0f7c13.png align="center")

Step 2: Launching a Docker Container

```plaintext
docker run -itd -p "8080:80" myimage:v2.0
```

* `docker run`: Runs a new container from a specified image.
    
* `-it`: Runs the container in interactive mode with a terminal (though the `-d` flag will detach it, so `-it` is not necessary here).
    
* `-d`: Runs the container in detached mode (in the background).
    
* `-p "8080:80"`: Maps port 80 inside the container to port 8080 on the host machine.
    
* `myimage:v2.0`: Specifies the image to use for the container, with the tag `v2.0`.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723618425718/ce4d613b-5364-4bc1-bf21-b4a8a4db8380.png align="center")

Step 3: Verify the Running Container

```plaintext
docker ps
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723618517098/5abb33ed-e69c-4aa3-b5cc-2573f5cc4852.png align="center")
