Getting Started with Docker: From Dockerfile to Running Containers

  • Post comments:0 Comments
docker basics

Docker simplifies application deployment by packaging code and dependencies into isolated environments called containers. This tutorial on Docker basics: build and run guides you through creating a Dockerfile, building and tagging Docker images, and running containers.

What You’ll Learn:

  1. What is a Dockerfile?
  2. How to create a Dockerfile.
  3. Building and tagging Docker images.
  4. Running containers from images.
  5. Managing and troubleshooting containers.

1. What is a Dockerfile?

A Dockerfile is a text file containing instructions to assemble a Docker image. It defines the base image, installs dependencies, copies application code, and specifies how the container should run the app.


2. Creating a Dockerfile

Step 1: Set Up Your Project Directory

Create a project folder to store your application and Dockerfile.

mkdir my-docker-app
cd my-docker-app

Let’s say you have a simple Python script (app.py):

# app.py
print("Hello from Docker!")

Step 2: Write the Dockerfile

Create a file named Dockerfile (without any extension) in the project directory.

touch Dockerfile

Now open the Dockerfile and add the following:

# Use an official Python runtime as a base image
FROM python:3.10-slim

# Set the working directory inside the container
WORKDIR /app

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

# Run the app
CMD ["python", "app.py"]

Explanation of Dockerfile Instructions:

  • FROM: Specifies the base image (Python 3.10 in this case).
  • WORKDIR: Sets /app as the working directory inside the container.
  • COPY: Copies files from your host machine to the container.
  • CMD: Defines the command to run when the container starts.

3. Building and Tagging Docker Images

Step 3: Build the Docker Image

Run the following command in the same directory as your Dockerfile:

docker build -t my-docker-app .
  • -t my-docker-app: Tags the image with the name my-docker-app.
  • .: Refers to the current directory (where the Dockerfile is located).

Step 4: Tagging the Docker Image

Tagging helps version your images, especially when pushing to repositories.

docker tag my-docker-app my-docker-app:v1

Now you have two tags:

  • my-docker-app (latest)
  • my-docker-app:v1

4. Running the Docker Container

Step 5: Run the Container

To start a container from your image:

docker run my-docker-app

Expected Output:

Hello from Docker!

Running in Detached Mode

If you want the container to run in the background:

docker run -d --name my-running-app my-docker-app
  • -d: Runs the container in the background.
  • --name: Names the container my-running-app.

View Running Containers

docker ps

To see all containers (including stopped ones):

docker ps -a

5. Managing and Troubleshooting Containers

Stopping and Removing Containers

To stop a running container:

docker stop my-running-app

To remove the container:

docker rm my-running-app

Removing Docker Images

To delete an image:

docker rmi my-docker-app:v1

Bonus: Pushing to Docker Hub

  1. Login to Docker Hub:
   docker login
  1. Tag the Image for Docker Hub: Replace <your-dockerhub-username> with your actual Docker Hub username.
   docker tag my-docker-app <your-dockerhub-username>/my-docker-app:v1
  1. Push the Image:
   docker push <your-dockerhub-username>/my-docker-app:v1

Conclusion

You’ve just created, built, tagged, and run a Docker container! This process is foundational for deploying applications consistently across different environments. As you explore more, try adding more complexity, like multi-stage builds, environment variables, and Docker Compose for multi-container applications.


Let me know in the comments if you have questions or want to dive into more advanced Docker topics!

Leave a Reply