Setting Up and Using Docker 🐳

Ashen Wijesingha
6 min readFeb 16, 2023

--

Image From Docker.com
Image from Docker.com

Docker 🐳 is a powerful tool that allows developers to create, deploy, and run applications within isolated environments called containers. By using containers, developers can quickly build, test, and deploy applications consistently and safely. In this article, we will go over the basics of setting up and using Docker, step by step, with code snippets to help you follow along.

from Red Hat Developer Web page

Step 1 — Installing Docker

The first step to using Docker is to install it on your machine. Docker is available for multiple platforms, including Windows, macOS, and various flavours of Linux. To install Docker on your system, follow the instructions for your specific operating system on the official Docker website.

Installing Docker on Windows

  1. Go to the Docker for Windows page.
  2. Click the “Download Docker for Windows” button.
  3. Double-click the downloaded installer to start the installation process.
  4. Follow the prompts to complete the installation.
  5. Once Docker is installed, you can open the Docker Desktop application and start using Docker.

Note: Docker for Windows requires Windows 10 Pro or Enterprise version 14393 to run.

Installing Docker on macOS

  1. Go to the Docker for Mac page.
  2. Click the “Download Docker for Mac” button.
  3. Double-click the downloaded .dmg file to mount the disk image.
  4. Drag the Docker.app file to the Applications folder.
  5. Double-click the Docker.app file to launch Docker.
  6. Once Docker is launched, you can start using Docker.

Note: Docker for Mac requires macOS El Capitan 10.11 or newer to run.

Installing Docker on Linux

The process for installing Docker on Linux can vary depending on the distribution you’re using. In general, you’ll need to follow these steps:

  1. Go to the Docker CE page and select your Linux distribution from the list.
  2. Follow the instructions on the page to install Docker on your system.

Note: Depending on your distribution, you may need to use a package manager to install Docker, or you may need to download and install Docker manually. Be sure to read the instructions carefully for your specific distribution.

Before we move on to Docker, I would like to suggest you get some idea about what is Docker and how it works by reading the documentation.

This is the docker documentation Link. Please spend some time with the documentation first.

and This is the Docker architecture. 😏

Image from JFrog Web Page

Step 2 — Creating a Dockerfile

Once Docker is installed, the next step is to create a Dockerfile. A Dockerfile is a text file that contains instructions on how to build a Docker image. In this example, we will create a simple Python application that prints “Hello, World!” to the console.

Create a new directory for your project, and inside that directory create a new file called Dockerfile with the following command

# Use an official Python runtime as a parent image
FROM python:3.9-slim-buster

# Set the working directory to /app
WORKDIR /app

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

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

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

In this file, we are using an official Python 3.9 slim image as our base image. We set the working directory to /app, copy the contents of our current directory to the container, and install any packages specified in our requirements.txt file, and then run the app.py file.

Step 3 — Creating the Python Application

Now that we have our Dockerfile, we need to create the Python application that it will run. In the same directory as your Dockerfile, create a new file called app.py with the following command.

print("Hello, World!")

This is a simple Python application that prints “Hello, World!” to the console. You can replace this with your application code.

Step 4 — Building the Docker Image

With our Dockerfile and application in place, we can now build our Docker image. Open a terminal window, navigate to the directory where your Dockerfile and app.py files are located and run the following command

docker build -t my-python-app .

This command tells Docker to build an image with the tag my-python-app from the current directory (.). Docker will read the instructions in your Dockerfile and execute them to create the image.

Step 5 — Running the Docker Container

With our Docker image built, we can now run a container using that image. Run the following command to start a container from the my-python-app image.

docker run my-python-app

This will start a container using the my-python-app image and execute the CMD instruction in the Dockerfile, which will run the app.py file and print "Hello, World!" to the console.

How To Use Docker

To use Docker you have to start an instance also known as a container which we use on Docker, You can follow these steps to start a Docker Container.

👊Pull the image:

Before you can create a container, you need to download the Docker image from a repository. You can do this by running the following command in your terminal.

docker pull image_name

Replace “image_name” with the name of the Docker image you want to download.

👊 Run the container:

Once you have downloaded the image, you can run the container using the docker run command. For example, to run a container from the "hello-world" image, you can use the following command:

docker run hello-world

This will start a container, print a “Hello from Docker!” message to your terminal, and then exit.

👊 Interact with the container:

By default, containers are run in isolated environments. To interact with the container, you can use the -it flag to start an interactive session. For example:

docker run -it image_name

Replace “image_name” with the name of the Docker image you want to run.

👊 Customize the container:

You can customize the container by passing additional arguments to the docker run command. For example, to map a port from your local machine to the container, you can use the -p flag. For example:

docker run -p 8080:80 image_name

This will start the container, map port 8080 on your local machine to port 80 in the container, and run the container in the background.

That’s it! You have now started a Docker container. Also to see a list of running containers, you can use the docker ps command. To stop a container, you can use the docker stop command followed by the container ID or name.

In this article, we went over the basics of setting up and using Docker. We started by installing Docker on our machine, then created a Dockerfile to build a Docker image that would run our Python application. We created the Python application itself, built the Docker image using our Dockerfile, and finally ran a Docker container using the image. With these steps.

If you like this article please hit the clap button as many times as you like, as it keeps me motivated to write more articles. Have a great weekend and Happy Hacking 😉.

And Don’t forget to ping me on Linkedin.

--

--

Ashen Wijesingha

I'm reading for my Software Engineering degree program at SLIIT. Also, I'm a driven individual with the ability to adapt to any situation.