Getting Started with Docker: Your First Container

Installing Docker

Before you can run your first container, you need to install Docker on your machine. Docker is available for Windows, macOS, and various Linux distributions. Here's how you can install Docker:

For Windows and macOS:

  1. Download Docker Desktop from the official Docker website[4].
  2. Run the installer and follow the on-screen instructions.
  3. Once installed, launch Docker Desktop.
  4. Docker will guide you through a tutorial which is optional but recommended for beginners.

For Linux:

  1. Go to the Docker documentation website and choose your Linux distribution.
  2. Follow the step-by-step instructions provided for your specific distribution.
  3. Ensure that your user is added to the docker group so you can execute Docker commands without using sudo.

Running Your First Container

With Docker installed, you can now run your first container. Docker containers are instances of Docker images, which are templates that include the application code, libraries, dependencies, and other files necessary to run the application.

Step 1: Download a Docker Image

Docker images are available on Docker Hub, which is a repository for Docker images. You can download a simple image like hello-world to test your Docker installation:

docker pull hello-world

This command fetches the hello-world image from Docker Hub and saves it on your machine.

Step 2: Run the Container

To run a container using the hello-world image, execute:

docker run hello-world

This command will start a container from the hello-world image. If the image is not found locally, Docker will automatically pull it from Docker Hub. The hello-world container will run, print a message to the console, and then exit.

Step 3: Verify the Container Ran

You can verify that your container ran successfully by listing all the containers, including the stopped ones:

docker ps -a

You should see the hello-world container listed with a status of Exited.

Understanding Docker Commands

Here are some basic Docker commands that you should be familiar with:

  • docker pull: Downloads an image from Docker Hub.
  • docker run: Creates and starts a container from an image.
  • docker ps: Lists running containers.
  • docker ps -a: Lists all containers, including stopped ones.
  • docker stop: Stops a running container.
  • docker rm: Removes a container.
  • docker images: Lists all images on the local machine.
  • docker rmi: Removes an image.

Building and Running a Simple Application

Now that you've run a pre-made image, let's build and run a simple application:

  1. Create a directory for your project and navigate into it.
  2. Create a file named Dockerfile without any extension.
  3. Open the Dockerfile in a text editor and add the following content:
# Use an official Python runtime as a parent image
FROM python:3.8-slim

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy the current directory contents into the container at /usr/src/app
COPY . .

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 80 available to the world outside this container  
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "./app.py"]
  1. Create a requirements.txt file and list any Python dependencies.
  2. Create an app.py file with a simple web server or script.
  3. Build your Docker image:
docker build -t my-python-app .
  1. Run your application in a new container:
docker run -p 4000:80 my-python-app

This command maps port 4000 on your local machine to port 80 on the container, allowing you to access the application.

Congratulations! You've just containerized and run your first application using Docker. As you continue to explore Docker, you'll discover more advanced features and techniques that can help streamline your development workflow.

You have not logged in, please Login to comment.