AWS Cloud9
User Guide

Docker Sample for AWS Cloud9

This sample shows you how to connect an AWS Cloud9 SSH development environment to a running Docker container inside of an Amazon Linux instance in Amazon EC2. This enables you to use the AWS Cloud9 IDE to work with code and files inside of a Docker container and to run commands on that container. For information about Docker, see What is Docker on the Docker website.

Creating this sample might result in charges to your AWS account. These include possible charges for services such as Amazon EC2. For more information, see Amazon EC2 Pricing.

Prerequisites

  • You should have an Amazon EC2 instance running Amazon Linux. This sample assumes you already have an Amazon EC2 instance running Amazon Linux in your AWS account. To launch an Amazon EC2 instance, see Launch a Linux Virtual Machine. In the Choose an Amazon Machine Image (AMI) page of the wizard, choose an AMI whose display name starts with Amazon Linux AMI.

  • If the Amazon EC2 instance runs within an Amazon VPC, there are additional requirements. See VPC Settings for AWS Cloud9 Development Environments.

  • The Amazon EC2 instance should have at least 8 to 16 GB of free disk space available. This sample uses Docker images that are over 3 GB in size and can use additional increments of 3 GB or more of disk space to build images. If you try to run this sample on a disk that has 8 GB of free space or less, we've found that the Docker image might not build or the Docker container might not run. To check the instance's free disk space, you can run a command such as df -h (for "disk filesystem information in human-readable format") on the instance. To increase an existing instance's disk size, see Modifying a Volume in the Amazon EC2 User Guide for Linux Instances.

Step 1: Install and Run Docker

In this step, you check if Docker is installed on the Amazon EC2 instance, and install Docker if it isn't already installed. After you install Docker, you run it on the instance.

  1. Connect to the running Amazon EC2 instance by using an SSH client such as the ssh utility or PuTTY. To do this, see "Step 3: Connect to Your Instance" in Launch a Linux Virtual Machine.

  2. Check if Docker is installed on the instance. To do this, run the docker command on the instance with the --version option.

    docker --version

    If Docker is installed, the Docker version and build number are displayed. In this case, skip ahead to step 5 later in this procedure.

  3. Install Docker. To do this, run the yum command with the install action, specifying the docker package to install.

    sudo yum install -y docker
  4. Confirm that Docker is installed. To do this, run the docker --version command again. The Docker version and build number are displayed.

  5. Run Docker. To do this, run the service command with the docker service and the start action.

    sudo service docker start
  6. Confirm Docker is running. To do this, run the docker command with the info action.

    sudo docker info

    If Docker is running, information about Docker is displayed.

Step 2: Build the Image

In this step, you use a Dockerfile to build a Docker image onto the instance. This sample uses an image that includes Node.js and a sample chat server application.

  1. On the instance, create the Dockerfile. To do this, with the SSH client still connected to the instance, in the /tmp directory on the instance, create a file named Dockerfile. For example, run the touch command as follows.

    sudo touch /tmp/Dockerfile
  2. Add the following contents to the Dockerfile file.

    # Build a Docker image based on the cloud9/ws-nodejs Docker image. FROM cloud9/ws-nodejs # Enable the Docker container to communicate with AWS Cloud9 by # installing SSH. RUN apt-get update && apt-get install -y openssh-server # Ensure that Node.js is installed. RUN apt-get install -y nodejs && ln -s /usr/bin/nodejs /usr/bin/node # Disable password authentication by turning off the # Pluggable Authentication Module (PAM). RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config # Add the AWS Cloud9 SSH public key to the Docker container. # This assumes a file named authorized_keys containing the # AWS Cloud9 SSH public key already exists in the same # directory as the Dockerfile. RUN mkdir -p /home/ubuntu/.ssh ADD ./authorized_keys /home/ubuntu/.ssh/authorized_keys RUN chown -R ubuntu /home/ubuntu/.ssh /home/ubuntu/.ssh/authorized_keys && \ chmod 700 /home/ubuntu/.ssh && \ chmod 600 /home/ubuntu/.ssh/authorized_keys # Start SSH in the Docker container. CMD /usr/sbin/sshd -D # Update the password to a random one for the user ubuntu. RUN echo "ubuntu:$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)" | chpasswd

    To add the preceding contents to the Dockerfile file, you could use the vi utility on the instance as follows.

    1. Use vi to open the /tmp/Dockerfile file.

      sudo vi /tmp/Dockerfile
    2. Paste the preceding contents into the Dockerfile file. If you're not sure how to do this, see your SSH client's documentation.

    3. Switch to command mode. To do this, press the Esc key. (-- INSERT -- disappears from the bottom of the window.)

    4. Type :wq (to write to the /tmp/Dockerfile file, save the file, and then exit vi ), and then press Enter.

    Note

    Some additional Docker images are available in the c9/templates repository on the GitHub website. The following table lists the available images and their content and definitions. To use a different Docker image from this repository, replace cloud9/ws-nodejs in the Dockerfile with one of the following image IDs.

    Image ID Contents Definition

    cloud9/workspace

    Ubuntu 14.04.5 with common tools such as Git, Node.js, OpenJDK, Apache Ant, Apache Maven, Nginx, MySQL, PostgreSQL, Ruby, Apache HTTP Server, and PHP.

    Definition

    cloud9/ws-android

    The contents of the cloud9/workspace image with the Android SDK, Gradle, and common Android tools.

    Definition

    cloud9/ws-cpp

    The contents of the cloud9/workspace image with the GNU Compiler Collection (GCC) and sample C and C++ command line applications.

    Definition

    cloud9/ws-default

    The contents of the cloud9/workspace image with a basic readme file.

    Definition

    cloud9/ws-html5

    The contents of the cloud9/workspace image with a sample HTML file.

    Definition

    cloud9/ws-meteor

    The contents of the cloud9/workspace image with Meteor and a sample Meteor application.

    Definition

    cloud9/ws-nodejs (this sample)

    The contents of the cloud9/workspace image with additional versions of Node.js and a sample chat server application.

    Definition

    cloud9/ws-php

    The contents of the cloud9/workspace image with a sample PHP application.

    Definition

    cloud9/ws-python-plain

    The contents of the cloud9/workspace image with several versions of Python and a sample web application.

    Definition

    cloud9/ws-python

    The contents of the cloud9/workspace image with Django.

    Definition

    cloud9/ws-ruby

    The contents of the cloud9/workspace image with several versions of Ruby and Ruby on Rails.

    Definition

    cloud9/ws-wordpress

    The contents of the cloud9/workspace image with WordPress.

    Definition

  3. On the instance, create a file that contains the AWS Cloud9 SSH public key for the Docker container to use. To do this, in the same directory as the Dockerfile file, create a file named authorized_keys, for example, by running the touch command.

    sudo touch /tmp/authorized_keys
  4. Add the AWS Cloud9 SSH public key to the authorized_keys file. To get the AWS Cloud9 SSH public key, do the following:

    1. Open the AWS Cloud9 console at https://console.aws.amazon.com/cloud9/.

    2. In the AWS navigation bar, in the AWS Region selector, choose the AWS Region where you'll want to create the AWS Cloud9 development environment later in this topic.

    3. If a welcome page is displayed, for New AWS Cloud9 environment, choose Create environment. Otherwise, choose Create environment.

    4. On the Name environment page, for Name, type a name for the environment. (The name doesn't matter here. You'll choose a different name later.)

    5. Choose Next step.

    6. For Environment type, choose Connect and run in remote server (SSH).

    7. Expand View public SSH key.

    8. Choose Copy key to clipboard. (This is between View public SSH key and Advanced settings.)

    9. Choose Cancel.

    10. Paste the contents of the clipboard into the authorized_keys file, and then save the file. For example, you can use the vi utility, as described earlier in this step.

  5. Build the image by running the docker command with the build action, adding the tag cloud9-image:latest to the image and specifying the path to the Dockerfile file to use.

    sudo docker build -t cloud9-image:latest /tmp

    If successful, the last two lines of the build output display Successfully built and Successfully tagged.

    To confirm that Docker successfully built the image, run the docker command with the image ls action.

    sudo docker image ls

    If successful, the output displays an entry where the REPOSITORY field is set to cloud9-image and the TAG field is set to latest.

  6. Make a note of the Amazon EC2 instance's public IP address. You'll need it for Step 4: Create the Environment. If you're not sure what the public IP address of the instance is, you can run the following command on the instance to get it.

    curl http://169.254.169.254/latest/meta-data/public-ipv4

Step 3: Run the Container

In this step, you run a Docker container on the instance. This container is based on the image you built in the previous step.

  1. To run the Docker container, run the docker command on the instance with the run action and the following options.

    sudo docker run -d -it --expose 9090 -p 0.0.0.0:9090:22 --name cloud9 cloud9-image:latest
    • -d runs the container in detached mode, exiting whenever the root process that is used to run the container (in this sample, the SSH client) exits.

    • -it runs the container with an allocated pseudo-TTY and keeps STDIN open, even if the container is not attached.

    • --expose makes the specified port (in this sample, port 9090) available from the container.

    • -p makes the specified port available internally to the Amazon EC2 instance over the specified IP address and port. In this sample, port 9090 on the container can be accessed internally through port 22 on the Amazon EC2 instance.

    • --name is a human-readable name for the container (in this sample, cloud9).

    • cloud9-image:latest is the human-readable name of the built image to use to run the container.

    To confirm that Docker is successfully running the container, run the docker command with the container ls action.

    sudo docker container ls

    If successful, the output displays an entry where the IMAGE field is set to cloud9-image:latest and the NAMES field is set to cloud9.

  2. Log in to the running container. To do this, run the docker command with the exec action and the following options.

    sudo docker exec -it cloud9 bash
    • -it runs the container with an allocated pseudo-TTY and keeps STDIN open, even if the container isn't attached.

    • cloud9 is the human-readable name of the running container.

    • bash starts the standard shell in the running container.

    If successful, the terminal prompt changes to display the logged-in user's name for the container and the ID of the container.

    Note

    If you ever want to log out of the running container, run the exit command. The terminal prompt changes back to display the logged-in user's name for the instance and the private DNS of the instance. The container should still be running.

  3. For the directory on the running container that you want AWS Cloud9 to start from after it logs in, set its access permissions to rwxr-xr-x . This means read-write-execute permissions for the owner, read-execute permissions for the group, and read-execute permissions for others. For example, if the directory's path is ~, you can set these permissions on the directory by running the chmod command in the running container as follows.

    sudo chmod u=rwx,g=rx,o=rx ~
  4. Make a note of the path to the directory on the running container that contains the Node.js binary, as you'll need it for Step 4: Create the Environment. If you're not sure what this path is, run the following command on the running container to get it.

    which node

Step 4: Create the Environment

In this step, you use AWS Cloud9 to create an AWS Cloud9 SSH development environment and connect it to the running Docker container. After AWS Cloud9 creates the environment, it displays the AWS Cloud9 IDE so that you can start working with the files and code in the container.

  1. Open the AWS Cloud9 console, if it isn't already open, at https://console.aws.amazon.com/cloud9/.

  2. In the AWS navigation bar, in the AWS Region selector, choose the AWS Region where you want to create the SSH environment.

  3. If a welcome page is displayed, for New AWS Cloud9 environment, choose Create environment. Otherwise, choose Create environment.

  4. On the Name environment page, for Name, type a name for the environment.

  5. To add a description to the environment, type it in Description.

  6. Choose Next step.

  7. For Environment type:, choose Connect and run in remote server (SSH).

  8. For User, type ubuntu.

  9. For Host, type the pubic IP address of the Amazon EC2 instance, which you noted earlier.

  10. For Port, type 9090.

  11. Expand Advanced settings.

  12. For Environment path, type the path to the directory on the running container that you want AWS Cloud9 to start from after it logs in.

  13. For Node.js binary path, type the path to the directory on the running container that contains the Node.js binary, which you noted earlier.

  14. Choose Next step.

  15. Choose Create environment.

  16. When the AWS Cloud9 Installer dialog box appears, choose Next.

  17. In the list of components to be installed, clear the c9.ide.lambda.docker check box, and then choose Next. This is because AWS Cloud9 cannot run Docker inside of Docker.

  18. When the AWS Cloud9 Installer dialog box displays Installation Completed, choose Next, and then choose Finish. The AWS Cloud9 IDE appears for the running container, and you can start working with the container's files and code.

Note

If the container stops running, you can no longer use the IDE to access the container until you start running the container again. To do this, go back to Step 3: Run the Container.

Step 5: Run the Code

In this step, you use the AWS Cloud9 IDE to run a sample application inside the running Docker container.

  1. With the AWS Cloud9 IDE displayed for the running container, start the sample chat server. To do this, in the Environment window, right-click the sample workspace/server.js file, and then choose Run.

  2. Preview the sample application. To do this, in the Environment window, open the the workspace/client/index.html file. Then, on the menu bar, choose Tools, Preview, Preview Running Application.

  3. On the application preview tab, for Your Name, type your name. For Message, type a message. Then choose Send. The chat server adds your name and message to the list.

Step 6: Clean Up

In this step, you delete the environment and remove AWS Cloud9 and Docker support files from the Amazon EC2 instance. Also, to prevent ongoing charges to your AWS account after you're done using this sample, you should terminate the Amazon EC2 instance that is running Docker.

Step 6.1: Delete the Environment

To delete the environment, see Deleting an Environment.

Step 6.2: Remove AWS Cloud9 Support Files from the Container

After you delete the environment, some AWS Cloud9 support files still remain in the container. If you want to keep using the container but no longer need these support files, delete the .c9 folder from the directory on the container that you specified AWS Cloud9 to start from after it logs in. For example, if the directory is ~, run the rm command with the -r option as follows.

sudo rm -r ~/.c9

Step 6.3: Remove Docker Support Files from the Instance

If you no longer want to keep the Docker container, the Docker image, and Docker on the Amazon EC2 instance, but you want to keep the instance, you can remove these Docker support files as follows.

  1. Remove the Docker container from the instance. To do this, run the docker command on the instance with the stop and rm stop actions and the human-readable name of the container.

    sudo docker stop cloud9 sudo docker rm cloud9
  2. Remove the Docker image from the instance. To do this, run the docker command on the instance with the image rm action and the image's tag.

    sudo docker image rm cloud9-image:latest
  3. Remove any additional Docker support files that might still exit. To do this, run the docker command on the instance with the system prune action.

    sudo docker system prune -a
  4. Uninstall Docker. To do this, run the yum command on the instance with the remove action, specifying the docker package to uninstall.

    sudo yum -y remove docker

    You can also remove the Dockerfile and authorized_keys files you created earlier. For example, run the rm command on the instance.

    sudo rm /tmp/Dockerfile sudo rm /tmp/authorized_keys

Step 6.4: Terminate the Instance

To terminate the Amazon EC2 instance, see Terminate Your Instance in the Amazon EC2 User Guide for Linux Instances.