Skip to main content

Command Palette

Search for a command to run...

Docker Container Introduction and Setup

Published
3 min read
M

As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021.

With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences.

In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS.

I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.

Welcome Back

Imagine running your application code on your local machine in the exact same way it will run in the cloud, in production. Wouldn't that be great? This setup ensures consistency across development and deployment environments, eliminating the "works on my machine" problem.

We aim for our project to function seamlessly across various operating systems and runtime versions. This is where Docker containers revolutionize the development landscape. By packaging our application along with its dependencies into a container, Docker ensures it runs uniformly across different environments, be it your laptop, a cloud server, or a data center.

What are Docker Containers?

Docker containers are lightweight, stand-alone, and executable software packages. Each container includes everything needed to run a piece of software: code, runtime, system tools, libraries, and settings. Unlike virtual machines, Docker containers share the host system’s kernel, making them more efficient in terms of resources and startup time.

How Containers Differ from Virtual Machines

Virtual machines (VMs) require a hypervisor and a full guest OS, adding overhead and latency. Containers, on the other hand, use the host OS directly. For example, instead of running a full Windows environment on macOS, a container runs the necessary application environment while leveraging macOS's kernel. This approach reduces the footprint from gigabytes to megabytes and startup times from minutes to seconds.

While VMs offer stronger isolation, which is crucial for multi-tenant environments like cloud providers, containers provide sufficient isolation for most use cases with significantly less overhead. In practice, companies often use a combination of VMs and containers to balance isolation and efficiency.

Deploying Docker on AWS

To get started with Docker, let's move towards deploying it on AWS EC2, combining the agility of Docker containers with the scalability of AWS infrastructure.

Installing Docker

Installing Docker is straightforward:

  1. Navigate to Docker’s Website: Visit Docker and follow the "Getting Started" link.

  2. Download Docker Desktop: Choose the version suitable for your OS. For Mac and Windows, Docker Desktop is the recommended option. It includes a user-friendly interface and Docker Compose.

    • Mac and Windows: Download and install Docker Desktop. Follow the installation prompts to complete the setup.

    • Linux: Select the Docker Engine version compatible with your distribution (e.g., Ubuntu). Follow the installation instructions specific to your distribution.

  3. Verify Installation: Open a terminal and type docker --version to check if Docker is installed correctly. Run docker to see a list of available commands.

Creating Your First Docker Container

Now, let’s create our first Docker container. Here’s the step-by-step process:

  1. Understand Docker Hub: Docker Hub is a repository of container images. Think of it as GitHub for Docker images. Popular images include Node.js, MongoDB, and PostgreSQL. Visit Docker Hub to explore available images.

  2. Run a Docker Container: Use the docker run command to create and start a container. For example, to run the "hello-world" container:

     docker run hello-world
    

    This command downloads the image (if not already present) and runs it.

  3. Mapping Ports: When running a web server in a container, map a container port to a host port using the -p flag. For example:

     docker run -p 80:80 docker/getting-started
    

    This maps port 80 in the container to port 80 on your machine.

  4. Access the Application: Open a browser and navigate to localhost:80 to see your application in action.

Congratulations!

You’ve successfully created and run your first Docker container. This is just the beginning. With Docker, you can build, share, and run applications anywhere. In the next steps, we’ll dive deeper into building custom Docker images and deploying them on AWS.

Stay tuned for more hands-on Docker adventures!