Creating, pushing, and deploying container images

with Amazon Lightsail

Amazon Lightsail is the easiest way to get started on AWS. It offers virtual servers (instances), storage, databases, content delivery network (CDN) distributions, and networking for a low, predictable price. With Lightsail, you can start small and scale as you grow.

Lightsail now offers container services. For more information on this latest launch, please read this blog. In this tutorial, we show you how to create a container image on your local machine using a Dockerfile. After your image is created, you can then push it to your Lightsail container service to deploy it.

Get started with Amazon Lightsail for free. 

About this Tutorial
Time 10-20 minutes      
Cost Free
Use Case Compute
Products Amazon Lightsail
Level 200
Last Updated November 11, 2020

Step 1: Prerequisites

Complete the following prerequisites before you get started with creating a container image. 

1.1 — Create a Lightsail account, and instance to host your content. 

Already have an account? Log in to your account

1.2 — Install the software required to create containers and then push them to your Lightsail container service. 

For example, you must install and use Docker to create and build your container images that you can then use with your Lightsail container service. 

To create your own container images, and push them to your container service, you must install the following additional software on the same computer on which you plan to create your container images:

  • Docker – Allows you to run, test, and create your own container images that you can then use with your Lightsail container service.
  • AWS Command Line Interface (AWS CLI) – Allows you to specify parameters of the container images you create, and then push them to your Lightsail container service.
  • Lightsail Control (lightsailctl) plugin – Enables the AWS CLI to access the container images that are on the local machine.

For more information, see Installing software to manage container images for your Amazon Lightsail container services.

Step 2: Create a Dockerfile and build a container image

Complete the following procedure to create a Dockerfile, and build a mystaticwebsite Docker container image from it. The container image will be for a simple static website hosted on an Apache webserver on Ubuntu.

2.1 — Create a mystaticwebsite folder on your local machine where you will store your Dockerfile.

 

2.2 — Create a Dockerfile in the folder you just created. The Dockerfile does not use a file extension, such as .TXT

The full file name is Dockerfile.

2.3 — Copy one of the following code blocks depending on how you want to configure your container image, and paste it into your Dockerfile:

2.3a If you want to create a simple static website container image with a Hello World message, then copy the first code block on the right and paste it into your Dockerfile. 

This code sample uses the Ubuntu 18.04 image. The RUN instructions updates the package caches, and installs and configures Apache, and prints a Hello World message to the web server's document root. The EXPOSE instruction exposes port 80 on the container, and the CMD instruction starts the web server.

 

2.3b If you want to use your own set of HTML files for your static website container image, create an html folder in the same folder where you store your Dockerfile. Then put your HTML files in that folder. After your HTML files are in the html folder, copy the second code block on the right and paste into to your Dockerfile. 

This code sample uses the Ubuntu 18.04 image. The RUN instructions updates the package caches, and installs and configures Apache. The COPY instruction copies the contents of the html folder to the web server's document root. The EXPOSE instruction exposes port 80 on the container, and the CMD instruction starts the web server.

FROM ubuntu:18.04

# Install dependencies
RUN apt-get update && \
 apt-get -y install apache2

# Write hello world message
RUN echo 'Hello World!' > /var/www/html/index.html

# Open port 80
EXPOSE 80

# Start Apache service
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
FROM ubuntu:18.04

# Install dependencies
RUN apt-get update && \
 apt-get -y install apache2

# Copy html directory files
COPY html /var/www/html/

# Open port 80
EXPOSE 80

CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]

2.4 — Open a command prompt or terminal window and change the directory to the folder in which you are storing your Dockerfile.

 

2.5 — Enter the following command to build your container image using the Dockerfile in the folder. This command builds a new Docker container image named mystaticwebsite.

Once you submit the command, you should see a message that confirms your image was successfully built.

docker build -t mystaticwebsite .

2.6 — Enter the following command to view the container images on your local machine.

After submitting the command, you should see a result similar to the following screenshot, showing the new container image created.

docker images --filter reference=mystaticwebsite
Configure any steps you'd like your cluster to execute once provisioned

Your newly built container image is ready to be tested by using it to run a new container on your local machine. Continue to the next Step 3: Run your new container image section of this tutorial.

Step 3: Run your new container image

Complete the following steps to run the new container image you created.

3.1 — In a command prompt or terminal window, enter the following command to run the container image that you built in the previous section.  The -p 8080:80 option maps the exposed port 80 on the container to port 8080 on your local machine. The -d option specifies that the container should run in detached mode.

docker container run -d -p 8080:80 --name mystaticwebsite mystaticwebsite:latest

3.2 —Enter the following command to view your running containers.

Once you enter the command, you should see an output to the following screenshot, showing the new container running. 

docker container ls -a
Configure any steps you'd like your cluster to execute once provisioned

3.3 —To confirm that the container is up and running, open a new browser window and browse to http://localhost:8080. You should see a message similar to the following screenshot. This confirms that your container is up and running on your local machine.

Your newly built container image is ready to be pushed to your Lightsail account so that you can deploy it to your Lightsail container service. For more information, see Pushing and managing container images on your Amazon Lightsail container services.

Step 4: Clean up the containers running on your local machine

Now that you've created a container image that you can push to your Lightsail container service, it's time to clean up the containers that are running on your local machine as a result of following the procedures in this guide.

Complete the following steps to clean up the containers running on your local machine:

4.1 — Run the following command to view the containers that are running on your local machine.

You should see a result similar to the following screenshot, which lists the names of the containers running on your local machine.

docker container ls -a
Configure any steps you'd like your cluster to execute once provisioned

4.2 —Run the following command to remove the running container that you created earlier in this guide. This forces the container to be stopped, and permanently deletes it.

In the command, replace <ContainerName> with the name of the container you want to stop, and delete. The second line of code shows an example of this. 

docker container rm <ContainerName> --force
docker container rm mystaticwebsite --force

After you create your container images, push them to your Lightsail container service when you're ready to deploy them. The next step of this tutorial walks you through how to push the container image to your Lightsail container service. 

Step 5: Push container images from your local machine to your container service

Complete the following procedure to push your container images to your container service.

5.1 — Open a command prompt or terminal window.

In the command prompt or terminal window, enter the following command to view the Docker images that are currently on your local machine.

docker images

5.2 —In the result, locate the name (repository name) and tag of the container image that you want to push to your container service. Make a note of it because you will need it in the next step.

5.3 — Enter the following command to push the container image on your local machine to your container service.

In the command, replace:

  • <Region> with the AWS Region in which your container service was created.
  • <ContainerServiceName> with the name of your container service.
  • <ContainerImageLabel> with the label that you want to give your container image when it's stored on your container service. Specify a descriptive label that you can use to track the different versions of your registered container images.
  • The label will be part of the container image name generated by your container service. For example, if your container service name is container-service-1, the container image label is mystaticsite, and this is the first version of the container image you're pushing, then the image name generated by your container service will be :container-service-1.mystaticsite.1.
  • The label will be part of the container image name generated by your container service. For example, if your container service name is container-service-1, the container image label is mystaticsite, and this is the first version of the container image you're pushing, then the image name generated by your container service will be :container-service-1.mystaticsite.1.
  • <LocalContainerImageName> with the name of the container image that you want to push to your container service. You obtained the container image name in the previous step of this procedure.
  • <ImageTag> with the tag of the container image that you want to push to your container service. You obtained the container image tag in the previous step of this procedure.
The second code block demonstrates what your command should look like. 
 
Once that command is entered, you should see a result similar to the screenshot on the right, which confirms that your container image was pushed to your container service.
aws lightsail push-container-image --region <Region> --service-name <ContainerServiceName> --label <ContainerImageLabel> --image <LocalContainerImageName>:<ImageTag>
aws lightsail push-container-image --region us-west-2 --service-name myservice --label mystaticwebsite --image mystaticwebsite:v2
Configure any steps you'd like your cluster to execute once provisioned

Now, complete the following procedure to view container images that were pushed, and are being stored, on your container service.

5.4 — Sign into the Lightsail console, and navigate to the Containers tab. 

5.5 — Choose the name of the container service for which you want to view the current deployment for which you want to view the stored container images.

5.6 —On the container service management page, choose the Images tab.

Note: The Images tab is not displayed if you have not pushed images to your container service. To display the images tab for your container service you must first push container images to your service.

The Images page lists the container images that were pushed to your container service, and are currently being stored on your service. Container images that are being used in a current deployment cannot be deleted and are listed with a grayed-out delete icon.

You have now pushed your container images to Lightsail's container service. You can either delete the images or create deployments of your container images.

Step 6: Create a deployment

Create a deployment when you're ready to launch containers on your Amazon Lightsail container service. A deployment is a set of specifications for the containers that you wish to launch on your service. Your container service can have one running deployment at a time, and a deployment can have up to 10 container entries. You can create a deployment at the same time as you create your container service, or you can create it after your service is up and running.

This section reviews how to create a container service deployment. For more information, please see Creating and managing deployments for your Amazon Lightsail container services.

6.1 — In the Lightsail console, navigate to the Containers tab. Then, select the name of the container service for which you want to create or modify a container service deployment.

6.2 —On the container service management page, choose the Deployments tab. The Deployments page lists your current deployment and deployment versions, if any.

6.3 —Choose one of the following options:

  • If your container service has an existing deployment, choose Modify your deployment.
  • If your container service has not had a deployment, choose Create a deployment.

The deployment form opens, where you can edit existing deployment parameters, or enter new deployment parameters.

6.4 — Enter the parameters of your deployment. 

For more information about the deployment parameters that you can specify, see the Deployment parameters.

6.5 —Choose Add container entry to add more than one container entry to your deployment. You can have up to 10 container entries in your deployment.

6.6 —When you're done entering the parameters of your deployment, choose Save and deploy to create the deployment on your container service.

The status of your container service changes to Deploying while your deployment is being crated. After a few moments, the status of your container service changes to one of the following depending on the status of your deployment:

  • If your deployment succeeds, the status of your container service changes to Running and the status of the deployment changes to Active. If you configured a public endpoint in your deployment, then the container chosen as the public endpoint is available through the default domain of your container service.
  • If your deployment fails, and there is a previous deployment running on your container service, the status of your container service changes to Running and your container service keeps the previous deployment as the active deployment. If there is no previous deployment, the status of your container service changes to Ready with no currently active deployment. View the container logs of the failed deployment to diagnose and troubleshoot what went wrong. For more information, see Viewing the container logs of your Amazon Lightsail container services.

Congratulations

You should now have a container running on your container service. The container was created locally, and then pushed to your Lightsail container service using the AWS CLI.

Amazon Lightsail is a great choice to develop, build, and deploy a variety of applications like WordPress, websites, and blog platforms.

Was this tutorial helpful?

Thank you
Please let us know what you liked.
Close
Sorry to disappoint you
Is something out-of-date, confusing or inaccurate? Please help us improve this tutorial by providing feedback.
Close

View your container logs

View the logs of containers on your container service. Read more here

Monitor utilization metrics

Monitor the utilization metrics of your container service. Read more here

Learn more

Learn more about Amazon Lightsail Containers. Read more here.