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;
- Explore Docker Hub: We have to find the web server images we want to try.
- Get and View httpd: Retrieve the httpd image from Docker Hub, run a container from the image, and view the results.
- Run the Website in httpd: Get the website content, add it to an httpd container, and view the webpage.
- Get and View Nginx: Retrieve the nginx image from Docker Hub, run a container from the image, and view the results.
- 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).
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.