Docker in plain English part 1. Building and running Docker containers
Tags: docker, automation
Published: 2021-01-24 13:55:43

---
title: Docker in plain English part 1. Building and running Docker containers
published: true
description: Let's host a static website inside a docker container
tags: 
- docker
- automation
cover_image: https://user-images.githubusercontent.com/630705/105617832-752b7200-5dd9-11eb-804e-296a90f800af.jpg
canonical_url: https://www.harrisgeo.me/blogs/docker-in-plain-english-part-1-building-and-running-docker-containers
---

Docker is a great tool and it is really useful for automating our workflows. I have been using Docker for many years, yet sometimes I find myself forgetting the basic commands.

The scope of these series is to give some really simple examples of how to use Docker. In this article we will dockerise a static website. For those not familiar with the Docker vocabulary, dockerise is when we put some app into a Docker container. Before we start, let's download and install the docker CLI from the [official website](https://www.docker.com/products/docker-desktop).

## The static website

Here's a dead simple website that we are going to dockerise.

```html
<html>
  <head>
    <title>Simple website</title>
  </head>
  <body>
    <h1>Hello there</h1>
    <p>
      I am a simple website and I live inside a Docker container.
    </p>
  </body>
</html>
```

Let's copy that code, open our terminal / code editor and paste it into an `index.html` file. Now let's create a Dockerfile.

## The Dockerfile

We can now create a new file with the name `Dockerfile` and add the following code into it.

```docker
FROM php:7.0-apache
COPY . /var/www/html/
```

What we're doing here is we're telling the container to install apache. Then the next step is to copy everything from the current directory and put it into `/var/www/html/` which is the directory apache will look at for HTML (and not only) files. Now we have everything that we want in order to build our Docker image.

## Building the Docker image

We have configured what the Docker image is going to contain so let's build it.

```bash
docker build -t static-website .
```

The `-t` flag stands for tag and is a good practice that will simply make our lives so much easier when looking at long lists of Docker images.

The first time we run that command, it is going to take a few seconds or minutes depending on our internet connections. That is because Docker has to download the image we specified in the Dockerfile.

We can see all of the available images with the `docker images` command. Our command line should look like this.

```bash
➜  docker images
REPOSITORY           TAG          IMAGE ID       CREATED              SIZE
static-website       latest       3152d04a164f   About a minute ago   368MB
php                  7.0-apache   aa67a9c9814f   2 years ago          368MB
```

Now that we have a Docker image for our website, we can spin up some Docker containers.

## Running our Docker container

To spin up our Docker container we simply run the following command.

```bash
docker run -d -p 4000:80 --name my-website static-website
```

Wow there's a lot of things happening here. Let me quickly explain what's going on.

- `-d` is a flag that is telling Docker to detach that container from the process and run in the background. If we don't include that flag then killing the root process or in other words closing the terminal will also stop the container
- `-p` is a flag that stands for port. This where we tell docker what port to make that container available on in the outside world and then map it to the port used internally. In this case apache uses port **80** and we want to make that available on the port **4000** of our localhost
- `--name` is quite straightforward. It is the name we want to give to our Docker container
- the final argument `static-website` refers to the Docker image we want to use to run the Docker container

You may notice that once we run that command a long string is returned. That string is the id of the container and we can use it to stop it. However due to the amount of output that might be generated while running that command, we can also view all of the containers that are currently running with `docker ps`. That should look like this.

```bash
➜  docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                            NAMES
9254225687cd   static-website    "docker-php-entrypoi…"   11 minutes ago   Up 11 minutes   3000/tcp, 0.0.0.0:8001->80/tcp   my-website
```

Now if we open [http://localhost:4000](http://localhost:4000) we should be able to see our static website which runs inside a Docker container we just created! 🎉

## Stopping containers

While experimenting with Docker we might have noticed that we have created multiple images and containers. Given the example from above, to stop the container we can do the following.

```bash
docker stop 9254225687cd
# or
docker stop my-website
```

This is where I find that naming containers comes in quite handy. When stopping a container, that does not mean that we have removed it. Running `docker start my-website` should run it again.

> Tip: Remember that `docker ps` will only show us the containers that are currently running. To see everything, including the ones that are stopped we can run `docker ps -a`

## Cleaning up Docker containers and images

All of the images and containers we have created can take a lot of space. Now let's talk about how to clean things up. Running `docker ps -a`, we can view and then copy the ids of the containers we want to remove.

```bash
docker rm ac6f4b61de14
```

> Tip: If you have more than once containers you want to remove you don't have to remove them one by one. You can pass all of them in the docker remove command like `docker rm id1 id2 ...`.

There are also more commands that can help with cleaning up. However, they can be subject to different opinions of whether you should or should not use them so let's not talk about them yet. For the Docker images the logic is quite similar.

```bash
docker image rm edec9bdcc8d2
```

## Aaaand that's it 🙌

Congratulations we now know how to get started with our Docker journey. Next step I would recommend to do is to try and Dockerise our React or you Node.js apps. The getting-started page of the [official docs](https://docs.docker.com/get-started/02_our_app/) have some great examples.

In the next part we are going to talk about spinning up multiple containers using `docker-compose`.

Please subscribe to my newsletter if you enjoyed this post and you would like to get notified when new ones come out.

*Did you enjoy this content? [Subscribe to my newsletter](https://tinyletter.com/harrisgeo88) and get notified when I post new stuff.*