Containers 101 – Create a Container with Docker

Much like VMWare Fusion or Workstation, Docker is a tool to not only deploy containers but also to create new ones. Docker has an command line tool named Docker Compose that uses a file called a Docker file to tell the tool how you want the container image to be built. In this exercise we will build a simple web server container and insert a custom web page into it.

Pre-Reqs:

  • A Machine Running Docker see my guide here
  • Docker running, see my guide here

By default docker compose is not installed, run the following command to see

docker compose

We will install docker compose with a couple of commands. copy and paste the following and press enter

DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}

Copy and paste the following and press enter

mkdir -p $DOCKER_CONFIG/cli-plugins

Copy and paste the following and press enter

Make the files executable, copy and paste the following and press enter

chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose

Try the same command again and see that it is now installed

docker compose

Create a new directory to store our Dockerfile and web page files

cd /home

Make a new directory under home and change to it

mkdir webserver
cd webserver

Use the vi editor to create the web page file

vi index.html

Once in the vi editor press i on your keyboard to enter insert mode, then copy and paste the below text when you are done press the escape key on your keyboard then hold the shift key and press the z key twice to save it.

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
   body {
       width: 35em;
       margin: 0 auto;
       font-family: Tahoma, Verdana, Arial, sans-serif;
   }
</style>
</head>
<body>
<h1>Hello RKelly!</h1>
<p>If you see this page, the nginx web server container is
working</p>
</body>
</html>

When you are done, press the escape key on your keyboard once then hold the shift key and press the z key twice to save it.

make the index.html file executable with the following command

chmod 777 index.html

Create a Dockerfile with the following command

vi Dockerfile

Once in the vi editor press i on your keyboard to enter insert mode, then copy and paste the below text

FROM nginx:latest
COPY index.html /usr/share/nginx/html/index.html

when you are done press the escape key on your keyboard then hold the shift key and press the z key twice to save it.

Use the following command to list your new files

ls

Build the container with the following command, note that this command always looks for files in your current directory and the -t is to tag the container with the name webserver and don’t forget that trailing period at the end

docker build -t webserver .

Run the new container with the following command

docker run -d -p 80:80 webserver

Open a web browser to your Photon VM IP address with the following

You have successfully built your first container!

Now let’s go ahead and stop the container before the next lesson, type the following to get the container id

docker ps

Type the following then copy and paste the container id and press enter

docker stop containerid

In the next lesson we will push the container up to DockerHub so you can save it and run it again from anywhere.

Remember sharing is caring!

2 Replies to “Containers 101 – Create a Container with Docker”

  1. Pingback: Containers 101 – Push containers to Dockerhub | VMtoCloud.com

  2. Pingback: Containers 101 – Run a Contaner with Docker | VMtoCloud.com

Leave a Reply

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

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.