Write a Docker File

Ticker

6/recent/ticker-posts

Write a Docker File

Question As per recent requirements shared by the Nautilus application development team, they need custom images created for one of their projects. Several of the initial testing requirements are already been shared with DevOps team. Therefore, create a docker file /opt/docker/Dockerfile (please keep D capital of Dockerfile) on App server 1 in Stratos DC and configure to build an image with the following requirements:

a. Use ubuntu as the base image.

b. Install apache2 and configure it to work on 8081 port. (do not update any other Apache configuration settings like document root etc).


Please Note :-  Perform the below commands based on your question  server,  user name & other details might differ . So please read task carefully before executing. All the Best 👍

Solution:  

 1. At first login on app server as per the task & switch to root user

thor@jump_host ~$ ssh tony@stapp01

The authenticity of host 'stapp01 (172.16.238.10)' can't be established.

ECDSA key fingerprint is SHA256:GaYnenqm3e77zCLkLoooNMM8pM11VWhVwQxZX6oLHLg.

ECDSA key fingerprint is MD5:2a:84:c0:13:71:07:99:aa:1d:6b:7c:2f:f5:06:53:b6.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added 'stapp01,172.16.238.10' (ECDSA) to the list of known hosts.

tony@stapp01's password:

[tony@stapp01 ~]$ sudo  su -

 We trust you have received the usual lecture from the local System

Administrator. It usually boils down to these three things:

     #1) Respect the privacy of others.

    #2) Think before you type.

    #3) With great power comes great responsibility.

 [sudo] password for tony:

[root@stapp01 ~]#

2. Run the Below command to check existing docker images 

[root@stapp01 ~]# docker ps

CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

[root@stapp01 ~]#

[root@stapp01 ~]# docker images

REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

[root@stapp01 ~]#


3. Write a  Vi Dockerfile  in folder /opt/docker

 [root@stapp01 ~]# vi /opt/docker/Dockerfile

[root@stapp01 ~]# cat /opt/docker/Dockerfile

FROM ubuntu

ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update

RUN apt-get install apache2 -y

RUN sed -i "s/80/8081/g" /etc/apache2/ports.conf

EXPOSE 8081

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND", "-k", "start"]

[root@stapp01 ~]#


4. Run Build command to  create an image
 

[root@stapp01 ~]# cd /opt/docker/

[root@stapp01 docker]# docker build  -t apache2 .

Sending build context to Docker daemon  2.048kB

Step 1/7 : FROM ubuntu

latest: Pulling from library/ubuntu

a31c7b29f4ad: Pull complete

Digest: sha256:b3e2e47d016c08b3396b5ebe06ab0b711c34e7f37b98c9d37abe794b71cea0a2

Status: Downloaded newer image for ubuntu:latest

 ---> c29284518f49

Step 2/7 : ARG DEBIAN_FRONTEND=noninteractive

 ---> Running in 6817b823c853

Removing intermediate container 6817b823c853

 ---> 06cccbf37185

Step 3/7 : RUN apt-get update

 ---> Running in 6054670eca9c

Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]

Get:2 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]

Get:3 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [784 kB]

Fetched 18.6 MB in 4s (4862 kB/s)

Reading package lists...

Removing intermediate container 6054670eca9c

 ---> 77537976488b

Step 4/7 : RUN apt-get install apache2 -y

 ---> Running in 4106d0cb07f9

Reading package lists...

Building dependency tree...

Reading state information...

The following additional packages will be installed:

  apache2-bin apache2-data apache2-utils ca-certificates file krb5-locales

  libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap

Enabling conf security.

Enabling conf serve-cgi-bin.

Enabling site 000-default.

invoke-rc.d: could not determine current runlevel

invoke-rc.d: policy-rc.d denied execution of start.

Processing triggers for libc-bin (2.31-0ubuntu9.2) ...

Processing triggers for ca-certificates (20210119~20.04.1) ...

Updating certificates in /etc/ssl/certs...

0 added, 0 removed; done.

Running hooks in /etc/ca-certificates/update.d...

done.

Removing intermediate container 4106d0cb07f9

 ---> a63fe49818f1

Step 5/7 : RUN sed -i "s/80/8081/g" /etc/apache2/ports.conf

 ---> Running in 49057286a6ab

Removing intermediate container 49057286a6ab

 ---> 8b4f31581395

Step 6/7 : EXPOSE 8081

 ---> Running in 8105daeba87c

Removing intermediate container 8105daeba87c

 ---> 6b3abeba3fe4

Step 7/7 : CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND", "-k", "start"]

 ---> Running in 73a403c73683

Removing intermediate container 73a403c73683

 ---> 475b47ba1167

Successfully built 475b47ba1167

Successfully tagged apache2:latest

[root@stapp01 docker]#


5. Run the Below command to  run the docker container on the server

[root@stapp01 docker]#  docker run --name http -p 8081:8081 -d apache2

d9ee14f2bdfee58464143295f869713243b5d1a81c19a25759380a466b56c13d

[root@stapp01 docker]#


6.  Validate the task by docker ps  &   curl 

[root@stapp01 docker]# docker ps

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                    NAMES

d9ee14f2bdfe   apache2   "/usr/sbin/apache2ct…"   18 seconds ago   Up 11 seconds   0.0.0.0:8081->8081/tcp   http

[root@stapp01 docker]#

[root@stapp01 docker]# curl -Ik http://localhost:8081

HTTP/1.1 200 OK

Date: Sun, 25 Jul 2021 10:29:40 GMT

Server: Apache/2.4.41 (Ubuntu)

Last-Modified: Sun, 25 Jul 2021 10:25:30 GMT

ETag: "2aa6-5c7f00e53ba80"

Accept-Ranges: bytes

Content-Length: 10918

Vary: Accept-Encoding

Content-Type: text/html

 [root@stapp01 docker]#


7.  Click on Finish & Confirm to complete the task successfully

Happy Learning!!!!


Apart from this if you need more clarity,  I have made a  tutorial video on this, please go through and share your comments. Like and share the knowledge



Post a Comment

0 Comments

Latest Posts

Secure Root SSH Access Level 1 Linux KodeKloud Engineer Task Success