Amazon ECR
User Guide (API Version 2015-09-21)

Docker Basics

Docker is a technology that allows you to build, run, test, and deploy distributed applications that are based on Linux containers. Amazon ECR is a managed AWS Docker registry service. Customers can use the familiar Docker CLI to push, pull, and manage images. For Amazon ECR product details, featured customer case studies, and FAQs, see the Amazon EC2 Container Registry product detail pages.

The documentation in this guide assumes that readers possess a basic understanding of what Docker is and how it works. For more information about Docker, see What is Docker? and the Docker User Guide.

Installing Docker

Docker is available on many different operating systems, including most modern Linux distributions, like Ubuntu, and even Mac OSX and Windows. For more information about how to install Docker on your particular operating system, go to the Docker installation guide.

You don't even need a local development system to use Docker. If you are using Amazon EC2 already, you can launch an Amazon Linux instance and install Docker to get started.

To install Docker on an Amazon Linux instance

  1. Launch an instance with the Amazon Linux AMI. For more information, see Launching an Instance in the Amazon EC2 User Guide for Linux Instances.

  2. Connect to your instance. For more information, see Connect to Your Linux Instance in the Amazon EC2 User Guide for Linux Instances.

  3. Update the installed packages and package cache on your instance.

    sudo yum update -y
  4. Install the most recent Docker Community Edition package.

    sudo yum install -y docker
  5. Start the Docker service.

    sudo service docker start
  6. Add the ec2-user to the docker group so you can execute Docker commands without using sudo.

    sudo usermod -a -G docker ec2-user
  7. Log out and log back in again to pick up the new docker group permissions.

  8. Verify that the ec2-user can run Docker commands without sudo.

    docker info


    Containers: 2 Images: 24 Storage Driver: devicemapper Pool Name: docker-202:1-263460-pool Pool Blocksize: 65.54 kB Data file: /var/lib/docker/devicemapper/devicemapper/data Metadata file: /var/lib/docker/devicemapper/devicemapper/metadata Data Space Used: 702.3 MB Data Space Total: 107.4 GB Metadata Space Used: 1.864 MB Metadata Space Total: 2.147 GB Library Version: 1.02.89-RHEL6 (2014-09-01) Execution Driver: native-0.2 Kernel Version: 3.14.27-25.47.amzn1.x86_64 Operating System: Amazon Linux AMI 2014.09


    In some cases, you may need to reboot your instance to provide permissions for the ec2-user to access the Docker daemon. Try rebooting your instance if you see the following error:

    Cannot connect to the Docker daemon. Is the docker daemon running on this host?

Create a Docker Image

In this section, you create a Docker image of a simple PHP web application, and test it on your local system or EC2 instance.

To create a Docker image of a PHP web application

  1. Install git and use it to clone the simple PHP application from your GitHub repository onto your system.

    1. Install git.

      sudo yum install -y git
    2. Clone the simple PHP application onto your system.

      git clone
  2. Change directories to the ecs-demo-php-simple-app folder.

    cd ecs-demo-php-simple-app
  3. Examine the Dockerfile in this folder. A Dockerfile is a manifest that describes the base image to use for your Docker image and what you want installed and running on it. For more information about Dockerfiles, go to the Dockerfile Reference.

    cat Dockerfile


    FROM ubuntu:12.04 # Install dependencies RUN apt-get update -y RUN apt-get install -y git curl apache2 php5 libapache2-mod-php5 php5-mcrypt php5-mysql # Install app RUN rm -rf /var/www/* ADD src /var/www # Configure apache RUN a2enmod rewrite RUN chown -R www-data:www-data /var/www ENV APACHE_RUN_USER www-data ENV APACHE_RUN_GROUP www-data ENV APACHE_LOG_DIR /var/log/apache2 EXPOSE 80 CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]

    This Dockerfile uses the Ubuntu 12.04 image. The RUN instructions update the package caches, install some software packages for the web server and PHP support, and then add your PHP application to the web server's document root. The EXPOSE instruction exposes port 80 on the container, and the CMD instruction starts the web server.

  4. Build the Docker image from your Dockerfile and tag it as amazon-ecs-sample in your default Amazon ECR registry. Substitute aws_account_id with your AWS account ID.


    Some versions of Docker may require the full path to your Dockerfile in the following command, instead of the relative path shown below.

    docker build -t .
  5. Run docker images to verify that the image was created correctly and that the image name contains a repository that you can push to (in this example, your Amazon ECR registry).

    docker images


    REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE latest 8df953fe88f7 27 minutes ago 260.8 MB ubuntu 12.04 2a7a952931ec 3 weeks ago 136.1 MB

  6. Run the newly built image. The -p 80:80 option maps the exposed port 80 on the container to port 80 on the host system. For more information about docker run, go to the Docker run reference.

    docker run -p 80:80


    Output from the Apache web server is displayed in the terminal window. You can ignore the "Could not reliably determine the server's fully qualified domain name" message.

  7. Open a browser and point to the server that is running Docker and hosting your container.

    • If you are using an EC2 instance, this is the Public DNS value for the server, which is the same address you use to connect to the instance with SSH. Make sure that the security group for your instance allows inbound traffic on port 80.

    • If you are running Docker locally, point your browser to http://localhost/.

    • If you are using docker-machine on a Windows or Mac computer, find the IP address of the VirtualBox VM that is hosting Docker with the docker-machine ip command, substituting machine-name with the name of the docker machine you are using.

      docker-machine ip machine-name

    You should see a web page running the simple PHP app.

                            Simple PHP app
  8. Stop the Docker container by typing Ctrl+c.

Next Steps

Now that you have created and tested your image, you can follow the procedures in Getting Started with Amazon ECR or Using the AWS CLI with Amazon ECR to push the image to your Amazon ECR registry.