Tag: Docker build Docker run

  • How to run Dockerfile step by step

    How to run Dockerfile step by step

    Dockerfile is a popular tool for creating, deploying, and running applications using containers. A container is a lightweight and isolated environment that runs an application and its dependencies. Containers make it easy to run applications consistently across different platforms and environments.
    To run an application in a container, you need two things: an image and a Dockerfile. An image is a static snapshot of the container that contains the application code and its dependencies. A Dockerfile is a text file that contains the instructions for building the image.
    In this article, we will show you how to run a Dockerfile step by step using a sample Node.js application. You don’t need to know Node.js to follow this guide, but you do need to have Docker installed on your machine.


    Step 1: Get the sample application


    The first step is to get the sample application that we will use for this guide. You can either clone the repository from GitHub or download the zip file.
    To clone the repository, open a terminal and run the following command:

    [bash]
    $ git clone https://github.com/docker/welcome-to-docker
    To download the zip file, go to https://github.com/docker/welcome-to-docker and click on the green “Code” button. Then select “Download ZIP” and save it to your preferred location. Extract the zip file to get the welcome-to-docker folder.


    Step 2: Create a Dockerfile in your project folder


    The next step is to create a Dockerfile in the root directory of your project folder. A Dockerfile is a text file that describes what goes into the container, such as the base image, the working directory, the files to copy, the commands to run, the ports to expose, and the command to start the app.
    To create a Dockerfile, open a text editor or code editor and create a new file called Dockerfile with no file extension. Save it in the welcome-to-docker folder.


    Step 3: Add instructions to your Dockerfile


    Now that you have created a Dockerfile, you need to add some instructions to it. The instructions are written in uppercase and followed by arguments. Each instruction creates a new layer in the image.
    Here are the instructions that we will use for our sample application:

    [Dockerfile]

    syntax=docker/dockerfile:1

    Start your image with a node base image

    FROM node:18-alpine

    Create an application directory

    RUN mkdir -p /app

    Set the /app directory as the working directory for any command that follows

    WORKDIR /app

    Copy the local app package and package-lock.json file to the container

    /COPY package*.json ./

    Copy local directories to the working directory of our docker image (/app)

    /COPY ./src ./src
    COPY ./public ./public

    Install node packages, install serve, build the app, and remove dependencies at the end

    RUN npm install \
    && npm install -g serve \
    && npm run build \
    && rm -fr node_modules

    Specify that the application in the container listens on port 3000

    EXPOSE 3000

    Start the app using serve command

    CMD [ “serve”, “-s”, “build” ]
    Let’s go over each instruction briefly:

    • The FROM instruction specifies the base image from which we are building our image. In this case, we are using node:18-alpine, which is an official image that contains Node.js version 18 and Alpine Linux, a minimal Linux distribution.
    • The RUN instruction executes a command in a new layer on top of the current image. In this case, we are creating an /app directory and setting it as our working directory for any subsequent instruction.
    • The COPY instruction copies files or directories from the local filesystem to the container filesystem. In this case, we are copying our app package files, source code files, and public files to our /app directory.
    • The RUN instruction can also be used to install packages or run scripts. In this case, we are installing node packages, installing serve (a static web server), building our app, and removing dependencies at the end.
    • The EXPOSE instruction informs Docker that the container listens on a specified network port at runtime. In this case, we are exposing port 3000, which is where our app runs.
    • The CMD instruction specifies what command to run when the container starts. In this case, we are running serve with some options to serve our app.

    Step 4: Build your first image


    Now that you have written your Dockerfile, you can build your image by running the following command in your project folder:

    [bash]
    $ docker build -t welcome-to-docker .
    The -t option tags the image with a name, in this case welcome-to-docker. The . indicates the current directory where the Dockerfile is located.
    Building the image may take some time, depending on your network speed and the size of the base image. After your image is built, you can view it in the Images tab in Docker Desktop or by running the following command:

    [bash]
    $ docker images
    You should see something like this:

    [bash]
    REPOSITORY TAG IMAGE ID CREATED SIZE
    welcome-to-docker latest 8a2c9b4f0f0c 10 minutes ago 23.1MB
    node 18-alpine 3b5a9d7e9f6c 2 weeks ago 21.8MB

    Read also: How to Create Storage Account in Azure Portal


    Step 5: Run your container


    The final step is to run your container from your image. You can do this by using the docker run command or by using Docker Desktop.
    To run your container using the docker run command, run the following command:

    [bash]
    $ docker run -p 8089:3000 welcome-to-docker
    The -p option maps a port on the host machine to a port on the container. In this case, we are mapping port 8089 on the host to port 3000 on the container, which is where our app runs.
    To run your container using Docker Desktop, go to the Images tab, and then select Run in the Actions column of your image. When the Optional settings appear, specify the Host port number 8089 and then select Run.


    Step 6: Verify that your container is running


    You can use Docker Desktop to view and access running containers. Go to the Containers tab to view your container and select the link in the Port (s) column or go to http://localhost:8089 to verify that the application is running.
    You should see something like this:
    Welcome to Docker
    Congratulations! You have successfully run a Dockerfile step by step and created a containerized application. So, You can now experiment with different instructions and options in your Dockerfile to customize your image and container. You can also learn more about Docker by reading the official documentation at https://docs.docker.com/. Happy Dockering!