In this example we will create a simple Docker container with a website running on NGINX.
Docker: You should have Docker installed on your machine. You can download and install it from the Docker website.
Online repository: You should have an account for an online container repository. In this example we're using Docker Hub.
Step 1: Write your application.
Before creating a container, you need an application to containerise. Write your application code and ensure it's functional. It's also a good practice to have a working directory with all necessary files, configurations, and dependencies.
In our case, this is our “to be containerized” website saved as index.html in a folder called My_Test_Website (~/ServerlessContainers/My_Test_Website/index.html):
<h1>It works on my machine...</h1>
<p>Not sure if it'll work on yours?</p>
<p>Try using containers!!</p>
Step 2: Dockerize your application.
To run your application as a container, you need to create a Docker image.
First, create a ‘Dockerfile’ in the root of your application directory to define the image.
# Use the existing, official NGINX image as the base.
# Copy the website content into NGINX's default web directory.
COPY My_Test_Website /usr/share/nginx/html
Build the docker image:
Now that we've created the docker file, it's time to build the image using the 'docker build' command!
In this example we'll also be adding a tag.
docker build -t serverlesscontainersdemo:latest
Please note that the name here also needs to be the same as your registry (if you want to push it to your container registry). For example:
docker build -t tilaa/serverlesscontainersdemo:latest
After running this, you now have a container image!
Step 3: Test run your Docker Container.
Now that our image was created, let's start the container based on that image using the 'docker run' command.
docker run -d -p 8080:80 serverlesscontainersdemo:latest
In this example, the NGINX container's port 80 is exposed on port 8080 of your host machine.
To verify that the container is up and running, and to see the status, you can run the 'docker ps' command.
Open a web browser and navigate to http://localhost:8080 or use the appropriate URL if you specified a different port. You should see your simple website running on NGINX.
Step 4: Clean up.
To stop and remove the Docker container, use the following commands:
docker stop <container-id>
docker rm <container-id>
You can find the <container-id> by running docker ps as shown in step 3.
Congratulations! You've created a container with a simple website running on NGINX.
Step 5: Upload to your online repository.
In order to upload your Docker image to our Serverless Containers platform, you'll need to use an online container registry.
In this example, we'll be using Docker Hub.
First, log in with your Docker Hub access token with read and write permissions:
Once you're logged in you can push your image to your repository.
docker push tilaa/serverlesscontainersdemo:latest
This will upload the image to the account and repository of the same name.
For further information on Docker and the Docker Hub, check out Docker docs.
The next step...
...is to upload the container to the Serverless Containers platform. To learn how to do this, check out the article as soon as it's released.