Creating apache2 webserver container from Docker file
Tags: devops, linux, docker
Published: 2023-09-28 18:16:46

Now we are going to create a Docker file with ubuntu apache2

 step1. Create a directory and change into the directory

```
mkdir apache2 && cd apache2
```

step2. create a Docker file 

> before we create a docker file there are 2 commands -Docker cmd and Docker file cmd (always in capital letter)

```
vim Dockerfile
```

```
kannan@kannan-PC:~/apache2$ cat Dockerfile 
FROM ubuntu
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update -y
RUN apt-get install apache2 -y
RUN apt-get install apache2-utils -y
RUN apt-get clean
COPY index.html /var/www/html/
EXPOSE 80
CMD ["apache2ctl","-D","FOREGROUND"]

```

step3. create a index.html file 

```
vim index.html
```

```
kannan@kannan-PC:~/apache2$ cat index.html 
<h1> Welcome to Dockerfile </h1>

<p> apache2webserver container from Dockerfile<p>

```
show the list of files created on the directory

step4. build the Dockerfile

```
kannan@kannan-PC:~/apache2$ docker build -t apache .
[+] Building 2.1s (12/12) FINISHED                                                                          docker:default
 => [internal] load .dockerignore                                                                                     0.0s
 => => transferring context: 2B                                                                                       0.0s
 => [internal] load build definition from Dockerfile                                                                  0.0s
 => => transferring dockerfile: 272B                                                                                  0.0s
 => [internal] load metadata for docker.io/library/ubuntu:latest                                                      2.1s
 => [auth] library/ubuntu:pull token for registry-1.docker.io                                                         0.0s
 => [1/6] FROM docker.io/library/ubuntu@sha256:aabed3296a3d45cede1dc866a24476c4d7e093aa806263c27ddaadbdce3c1054       0.0s
 => [internal] load build context                                                                                     0.0s
 => => transferring context: 31B                                                                                      0.0s
 => CACHED [2/6] RUN apt-get update -y                                                                                0.0s
 => CACHED [3/6] RUN apt-get install apache2 -y                                                                       0.0s
 => CACHED [4/6] RUN apt-get install apache2-utils -y                                                                 0.0s
 => CACHED [5/6] RUN apt-get clean                                                                                    0.0s
 => CACHED [6/6] COPY index.html /var/www/html/                                                                       0.0s
 => exporting to image                                                                                                0.0s
 => => exporting layers                                                                                               0.0s
 => => writing image sha256:629df6a8eee42dc69156ed0adf87aae8344ef996529889b8c2643e228ef4b0ee                          0.0s
 => => naming to docker.io/library/apache                                                                             0.0s

```

step5.Run the docker container on detached mode and assign the port 

```
kannan@kannan-PC:~/apache2$ docker run -d --name apachecontainer -p 8083:80 apache
55d764edab40de1e27401effae8977b04f4c1509cdb09a650bb6a1f510206c67

```
 

step6. check the docker container

```
kannan@kannan-PC:~/apache2$ docker ps -a
CONTAINER ID   IMAGE         COMMAND                  CREATED         STATUS         PORTS                                   NAMES
55d764edab40   apache        "apache2ctl -D FOREG…"   6 seconds ago   Up 6 seconds   0.0.0.0:8083->80/tcp, :::8083->80/tcp   apachecontainer

```

step7. Now check the app on the your local machine

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8rogmmpbhknyma3pqlsz.png)
  

**Now we are going to login to the container which we have created**
 Under these container we can install the tools we need eg:vim etc.,

```
kannan@kannan-PC:~/apache2$ docker exec -it 55d764edab40 /bin/bash
root@55d764edab40:/# cat /etc/o
opt/        os-release  
root@55d764edab40:/# cat /etc/os-release 
PRETTY_NAME="Ubuntu 22.04.3 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

```

## Docker cmds

```
docker --help
docker images
docker rmi <IMAGE ID> or <IMAGE NAME>
docker ps
docker ps -a
docker stop <CONTAINER ID> or <CONTAINER NAME>
docker start <CONTAINER ID> or <CONTAINER NAME>
docker restart <CONTAINER ID> or <CONTAINER NAME>
docker rm <CONTAINER ID> or <CONTAINER NAME>
docker search <IMAGE>
docker pull <IMAGE>
docker volume ls
docker network ls
docker stats
docker stats --all
docker inspect <NAME> <ID>
docker image prune
docker container prune
docker build -t <IMAGE NAME> .
docker run -d --name <CONTAINER NAME> -p <PORT> <IMAGE NAME>
```