Deploy your first Apache Docker container

How to deploy Apache on Docker container?

We discussed the basics of Docker containerisation in my previous article. before starting with this, please read the basics.

Please refer to this link for more details > An introduction note to Docker containers.

Here we are going to create a container with Apache installed. As I explained in aforementioned article, the basic requirement to launch a container is Docker image. We need a Dockerfile with proper instructions to build the image.

Here we go.. Docker can build images automatically by reading the instructions from this file. This is a normal text file contains all the commands to build an image. You have to maintain a specific format to write Dockerfile.

The “docker build” command will build an image from Dockerfile.

Deploying Apache

1. create a directory “Apache”

# mkdir Apache
# cd Apache

2. Create Dockerfile with proper instructions

arunlal@linux:~/myDockerLab/Apache$ cat Dockerfile

FROM ubuntu:14.04
MAINTAINER [email protected]
RUN apt-get update -y && apt-get upgrade -y
RUN apt-get install -y apache2 
EXPOSE 80
CMD ["apache2ctl", "-D", "FOREGROUND"]

Next we need to build the image using this docker file. We can use “docker build” command to build the image.

docker build -t myapache:ver0.1 .

Output should be like

runlal@linux:~/myDockerLab/Apache$ docker build -t myapache:ver0.1 .
Sending build context to Docker daemon  2.048kB
Step 1/6 : FROM ubuntu:14.04
 ---> 67759a80360c
Step 2/6 : MAINTAINER [email protected]
 ---> Using cache
 ---> 085f987d65d5
Step 3/6 : RUN apt-get update && apt-get upgrade
 ---> Using cache
 ---> ae140ec22b7b
Step 4/6 : RUN apt-get install -y apache2
 ---> Using cache
 ---> 485b5f9955a3
Step 5/6 : EXPOSE 80
 ---> Using cache
 ---> 1e9860e549e9
Step 6/6 : CMD apache2ctl -D FOREGROUND
 ---> Using cache
 ---> 7003e959ed73
Successfully built 7003e959ed73
Successfully tagged myapache:ver0.1

You can check the images using the following command:

docker images

Output

arunlal@linux:~/myDockerLab/Apache$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myapache            0.0.1               7003e959ed73        26 hours ago        257MB
myapache            ver.1               7003e959ed73        26 hours ago        257MB
myapache            ver0.1              7003e959ed73        26 hours ago        257MB

Yup we created the image from our Dockerfile. Next step is creating and starting the container using this image. We can use “docker run” command to create a container.

docker run -it --name My_Apache_Test -d -p 1881:80 7003e959ed73

Here, My_Apache_Test is the container name, 1881 is the port for Apache on that container, 7003e959ed73 is the image ID.

You can check the status using “docker ps” command.

arunlal@linux:~/myDockerLab/Apache$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
f170a8e403ff        7003e959ed73        "apache2ctl -D FOR..."   11 seconds ago      Up 10 seconds       0.0.0.0:1881->80/tcp   My_Apache_Test

Use “docker ps -a” to list active and inactive containers.

That’s it!! You started the container with Apache installed. You can check the status by accessing it on browser with 1881 port.

IP:1881

For local host: http://localhost:1881/

How to start/stop a docker container?

You can simply manage these operations using start and stop arguments with “docker” command.

arunlal@linux:~/myDockerLab/Apache$ docker stop My_Apache_Test
My_Apache_Test
arunlal@linux:~/myDockerLab/Apache$ docker start My_Apache_Test
My_Apache_Test

How to enter into a docker container?

To enter into the container just we created:

docker exec -it My_Apache_Test /bin/bash

Example:

arunlal@linux:~/myDockerLab/Apache$ docker exec -it My_Apache_Test /bin/bash
root@f170a8e403ff:/# 
root@f170a8e403ff:/# 
root@f170a8e403ff:/# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 14.04.5 LTS
Release:	14.04
Codename:	trusty
root@f170a8e403ff:/# hostname
f170a8e403ff
root@f170a8e403ff:/# w
 19:44:59 up  4:36,  0 users,  load average: 1.03, 1.26, 1.15
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT

That’s it!

An introduction note to Docker containers.

What is a Linux container?

Basic: Containerisation helps to isolate processes. You can run your App/Services as an isolated process, running from a distinct image that provides all files necessary to support the processes.

Basically Linux containers are OS level virtualisation technique for running multiple isolated Linux systems (containers) on a control host using a single Linux kernel.

More, What is a Linux container?

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

One thought on “Deploy your first Apache Docker container

Leave a Reply

Your email address will not be published. Required fields are marked *