Setting Up Amazon Linux 2 Docker Containers for AWS Development

A Step-by-Step Guide for AWS Developers

Ali Saif
AxOps Academy

--

Image generated by DALL.E 3

For AWS developers seeking to leverage Docker for their development environments, this guide provides a clear and straightforward approach to setting up an Amazon Linux 2 Docker container on a MacOS machine.

This step-by-step tutorial covers the basics, including sharing directories between the host and the container, and offers practical tips for efficient Docker usage.

Let’s get into it 🛠

Step 1: Install Docker

Before setting up your container, ensure Docker is installed on your system. Docker can be downloaded from the Docker website.

Step 2: Pull the Amazon Linux 2 Docker Image

Open your Terminal and execute the following command to pull the Amazon Linux 2 Docker image:

docker pull amazonlinux:2

This step downloads the necessary Docker image to your local machine, setting the stage for container creation.

Step 3: Run the Amazon Linux 2 Docker Image

To create and start a Docker container with the Amazon Linux 2 image, use this command:

docker run -it amazonlinux:2

The -it flag opens the container in interactive mode, allowing you to interact directly with the Amazon Linux 2 environment from your terminal.

Step 4: Setting Up and Customising Your Environment

Once inside the container, you can install necessary packages, configure settings, and modify the environment to fit your development needs.

Step 5: Using Docker Commit for Custom Images

After setting up your environment, you can create a customized image using docker commit. This allows you to save the state of the container as a new image, which can be reused later.

  1. Find Your Container ID: Use docker ps to list running containers and note the ID of your container.
  2. Commit the Container: Execute the following command to create an image from your container:
docker commit [container_id] [new_image_name]

Replace [container_id] with your container's ID and [new_image_name] with a name for your new image.

Additional Tips and Considerations

  • Viewing Containers: To view all your Docker containers (both active and inactive), use the command docker ps -a. This command is useful for managing and monitoring your containers.
  • Restarting and Attaching to Containers: To restart and attach to an existing container, use docker start -a <container_id>. Replace <container_id> with the actual ID of your container.
  • Sharing Directories Between Host and Container: To share a directory between your MacOS host and the Docker container, include the -v option in your docker run command:
docker run -it -v /path/to/host/directory:/path/to/container/directory amazonlinux:2

This command maps a directory from your MacOS filesystem, let’s say (/path/to/host/directory), to a specified path inside the container (/path/to/container/directory).

Important note on inotify events and MacOS

While the -v option facilitates directory sharing, it's crucial to understand its limitations on MacOS. Changes in the shared directory inside the container might not immediately reflect on the host and vice versa.

MacOS does not natively support inotify, a Linux kernel feature for monitoring filesystem events. Instead, MacOS uses a different mechanism called FSEvents for filesystem notifications.

This means that applications or tools designed specifically for Linux and expecting inotify behavior might not work as expected on macOS due to the lack of inotify support.

In the context of Docker, when you mount directories from the host (MacOS) into a Docker container (running Amazon Linux 2), the difference in file system event monitoring systems between the two can affect how changes are detected and synchronized between the host and the container.

In particular, the lack of inotify support on MacOS can impact real-time file synchronization or watching for file changes within Docker containers running Linux-based images. This is important when developing and testing applications that rely on file system events across different operating systems.

Conclusion

This guide provides AWS developers the necessary steps to set up an Amazon Linux 2 Docker container on MacOS. Understanding these foundational elements is crucial for efficient and effective AWS development, especially when working with AWS Lambda functions and other AWS services.

Speaking of AWS Lambda, here’s another post on Building AWS Lambda Layers with Docker and Amazon Linux 2 that you may find useful!

👉 If you found this post useful, please consider hitting the 👏 button 🙏

--

--