Docker Hands-On Part 02
Tags: tutorial, docker, containers
Published: 2024-02-09 03:25:39

## 02. Working With Prebuilt Docker Images

**Overview**

Let's say you have been assigned a task to migrate a website from a server to a container. So how do we get a container? The easy way is to create a container from a prebuilt image. Since we are hosting a website we will need a web server. 

**This lab has 5 goals;**

1. Explore Docker Hub: We have to find the web server images we want to try. 
2. Get and View httpd: Retrieve the httpd image from Docker Hub, run a container from the image, and view the results. 
3. Run the Website in httpd: Get the website content, add it to an httpd container, and view the webpage. 
4. Get and View Nginx: Retrieve the nginx image from Docker Hub, run a container from the image, and view the results. 
5. Run the website in Nginx: Add the website content to an nginx container and view the webpage. 

When we hit the server from our browser, the web page will load. We want that same thing to happen with the containers. We'll load the website into an httpd container and then when we hit it, it will get back to the same website. Lastly, we'll host the site using nginx just like we did with httpd. 

**1. Explore Docker Hub**

Navigate to the Docker Hub and find the httpd container (latest). 

![httpd-container](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pq0ixkh1j9hrjol58cr6.png)

Step 01: Verify the docker service is running by using the below command. 

```
$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
```

Step 02: We can start by pulling the httpd docker from the Docker Hub. 

```
$ docker pull httpd
```

Step 03: To see the docker images run the below command. 

```
$ docker images
REPOSITORY    TAG       IMAGE ID       CREATED        SIZE
httpd         latest    59bcd61b45fd   2 weeks ago    167MB
```

Step 04: Now we need to start our container

the port will be 8080 on the server and port 80 will be connected in to the container. (-p 8080:80) 

-d --> detach mode

```
$ docker run --name httpd -p 8080:80 -d httpd:latest
016b3196078f73a0ea062024b4b6b82430a81aad1b02fe8329fa78997931fc

$ docker ps -a
CONTAINER ID   IMAGE          COMMAND              CREATED          STATUS          PORTS                                   NAMES
016b3196078f   httpd:latest   "httpd-foreground"   44 seconds ago   Up 43 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   httpd

```

Step 05: Now we can test it if the httpd is running using a web browser. 

Enter your server's public IP in the web browser. You can see it. 

Step 06: Pulling the content. 

GitHub Repo: [Link](https://github.com/RandiakM/content-widget-factory-inc.git)

We are using the git clone command to clone the repo. If you haven't installed git on your server, you need to [install](https://git-scm.com/download/linux).  

```
$ git clone https://github.com/RandiakM/content-widget-factory-inc.git

$ cd content-widget-factory-inc
```

Step 07: Now we need to mount this web app with our httpd docker. First, we need to stop the httpd docker. 

```
$ docker stop httpd
httpd

$ docker ps -a
CONTAINER ID   IMAGE          COMMAND              CREATED      STATUS                     PORTS     NAMES
016b3196078f   httpd:latest   "httpd-foreground"   2 days ago   Exited (0) 4 seconds ago             httpd

```

So if we are trying to mount this with the same name it will give an error, to avoid this we can remove the container for httpd. 

```
$ docker rm httpd
httpd

[ec2-user@ip-172-16-1-10 web]$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

```

Step 08: Mount the web with the container. 

```
$ docker run --name httpd -p 8080:80 -v /home/ec2-user/content-widget-factory-inc/web:/usr/local/apache2/htdocs:ro -d httpd:latest
f4eca276afa82dcc50424ad4a3d0ebcb344c946f6797aebfaaada4c2b432e54f


$ docker ps -a
CONTAINER ID   IMAGE          COMMAND              CREATED          STATUS          PORTS                                   NAMES
f4eca276afa8   httpd:latest   "httpd-foreground"   12 seconds ago   Up 11 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   httpd

```

Step 09: Now you can navigate "http://public_ip:8080/" to see the web app.