All HowTo's Kubernetes & Docker

Docker Quick Start (5 minutes to a running web site)

This article demonstrates how to get a web site running using Docker. We’re starting with a Linux server (doesn’t matter which distribution). We’re going to pull down an Docker image from the public repository, modify it’s “index” file, and expose it – making it publicly accessible. I’ve added a little FAQ at the end of this artile.

To make it interesting, we’re going to run a php “index” file: “index.php”.

Let’s start by installing Docker:

Ubuntu:

https://docs.docker.com/engine/install/ubuntu/

CentOS:

https://docs.docker.com/engine/install/centos/

Now we can pull down an image from the public repository. This image is basically a template for us to manipulate for our own needs. It will run just fine without any modifications, but the site will be the stock-standard “apache” page. But because we want our own “index.php” to execute, we’re going to modify it.

docker pull php:8.0-apache

Let’s see our images. So far there should be just one:

docker images

The output:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/php       8.0-apache          475b1a998b6f        5 days ago          455 MB

Create a directory and create a file in it called “Dockerfile”:

mkdir ~/docker
cd ~/docker
vi Dockerfile

Put the following into the “Dockerfile”:

FROM php:8.0-apache
COPY ./index.php /var/www/html/
EXPOSE 80

Now create your “index.php” file in the same location as the “Dockerfile”. You can see from the above Dockerfile contents that we’re referring to it. Here’s my example “index.php” file:

<?php
echo "This is a php script!";
?>

Now we’re ready to build our new Docker image. Once built, we can run it. Tip: If we didn’t “poll” the image in the earlier step, Docker would automatically pull it down as a result of the following “build” command.

# I'm calling my image "my-web-server" but you can call it whatever you like...
docker build . -t my-web-server

Let’s see our list of images now:

docker images

The output:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my-web-server         latest            6798fce89abd        32 seconds ago      455 MB
docker.io/php       8.0-apache          475b1a998b6f        5 days ago          455 MB

Now run it. This turns our image into a container. A container can be stopped and restarted. All containers come from an image. The ‘-d’ puts it into the background. The ‘-p’ allow us to specify a port to expose it on – the port that we’ll visit.

docker run -d -p 5000:80 my-web-server

Confirm it’s running:

docker container ls

The output:

docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
7600e6e385cd        my-web-server       "docker-php-entryp..."   4 seconds ago       Up 3 seconds        0.0.0.0:5000->80/tcp   mystifying_wing

Test it. Visit that exposed port “5000”. Of course, we could expose it on any other port including “80”.

curl http://localhost:5000

Let’s stop our container:

# Where "7600e6e385cd" is the name of our container - from the "docker container ls" command above...
docker stop 7600e6e385cd

We can start it again:

docker start 7600e6e385cd 

We can remove/delete the container:

docker stop 7600e6e385cd 
docker container rm 7600e6e385cd 

We can remove/delete the original image and the image we created from the original image. Start by listing the images

docker images

The output:

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
my-web-server       latest              92e2324e62da        4 minutes ago       455 MB
docker.io/php       8.0-apache          475b1a998b6f        5 days ago          455 MB

Now we delete them:

docker images rm 92e2324e62da 475b1a998b6f

And we’re done.

FAQ:
Q. Can I log into a Docker container while it’s running?
A. Yes, you use use the command “docker exec -it 7600e6e385cd /bin/bash” (where “7600e6e385cd” is the container ID) and you will be given the prompt for that container.

Q. If I log into a Docker container and make changes, will those changes persist?
A. Any changes will persist between “docker stop” and “docker start” commands, but they will not persist after a “docker run” command. Start and stop operations are safe. But Run operations create a new container with a new ID leaving all previous/other containers untouched.

Q. Does “docker run” destroy previously started containers originating form the same image?
A. No, running “docker run” creates a new and unique container with a unique ID.

Leave a Reply

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