OneBite.Dev - Coding blog in a bite size

use docker container

Simple docker step by step how to use docker container with explanation

When one is looking to streamline and simplify development, Docker containers are a smart choice to consider. This article will provide a beginner-friendly step-by-step guide on how to use Docker containers.

To start with, Docker containers can be thought of as an isolated system within one’s own system, running its own operating system and software, but using much fewer resources compared to a virtual machine. It allows for seamless transfer of applications from one system to another, making it a popular tool among developers.

Step 1: Installation

Firstly, you’ll need to install Docker. Depending on your operating system visit the Docker’s official website and download the appropriate version. After downloading, run the installer and go through the guided setup and let it finish the installation.

Step 2: Pulling and Running a Docker Image

Once Docker is installed, open a terminal or command prompt window. Here, you can download pre-built Docker images by using the docker pull command. For instance, to download an Ubuntu image, input docker pull ubuntu.

To run the downloaded image, use the command docker run -it ubuntu. The -it flag attaches an interactive terminal to the container.

Step 3: Creating a Dockerfile

To create your own docker image, start by creating a Dockerfile. A Dockerfile is a text document that contains all the commands needed to build a Docker image. In a directory of your choice, create a new file called Dockerfile.

Let’s assume we are creating an image with a Python application. Your Dockerfile might look something like this,

# Use an official Python image
FROM python:3

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /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

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

Step 4: Building the Docker Image

Now build the Docker image by going to the directory that has the Dockerfile and running docker build -t FriendlyName . Replace “FriendlyName” with a name that will be easy for you to remember. This process can take a few minutes, but once completed, your image is ready to use.

Step 5: Running the Docker Image

To run your newly built image, execute docker run -p 4000:80 FriendlyName. The -p command maps the port 4000 on your machine to port 80 on the Docker container.

And that’s it! You now have a functioning Docker container running your application. These steps give a basic understanding of how Docker containers work, but Docker has several other options and functionalities that you might find useful for your circumstances. So do take the time to explore Docker more.

docker