Open In App

How to Use Docker For Local Development and Testing?

Last Updated : 22 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Whenever you want to start a project you first check for the requirement i.e. we do the requirement analysis then we look for prerequisites dependencies. We install those dependencies and prerequisites and we simply built our project.
Suppose you are the person who built the project and you want to make your project open source. So there is another person who wants to contribute to your open source project first he/she will need to go through all dependencies and prerequisites for your project and that’s a lot of work.

That’s where docker comes into play, Docker is basically a text file that contains all the needed dependencies and prerequisites for a given project. You will simply share the docker file with your project so people can use that docker file to contribute to your project without even installing the dependencies and prerequisites.

How To Install Docker in Your System? 

  • Visit the official Docker website and simply download the Docker for Desktop latest version
  • Install the Docker desktop on your system 
  • If you facing any issues (Docker not starting, Stuck while loading the start page) download the lower versions of Docker which might fix the issues. If you still facing the issue check the docker official documentation.
  • Now open your IDE (Visual Studio Code is recommended)and install the Docker extension 
  • Once you’ve installed the extension you’ll see the docker icon on the left side which means docker is successfully installed in your IDE

Fundamental Pillars of Docker

  • Docker Image: It’s a simple template that contains the required information for creating a Docker Container.
  • Docker Container: Imagine a container that contains dependencies, packages, and code.
  • Dockerfile: Contains instructions for installing packages and dependencies.

Local Development and Testing Using the Docker

  • Open your project in VS code. 
  •  Project is in Rust language as you can see here running this project inside a container.

jpeg-optimizer_jpeg-optimizer_Capture_optimized.png

  • Press F1 and the below pop-up will occur.
  • Click on Dev Containers and Add Dev Container Configuration Files.

jpeg-optimizer_WhatsApp_Image_2023-04-22_at_14332_PM_optimized.jpeg

  • Click on Show all Definitions i.e. it will show you all the pre-defined Docker Containers for all types of programming languages.

jpeg-optimizer_show_all_definations_optimized.png

  • Now choose the appropriate pre-defined container, I’ll be choosing the Rust dev container

jpeg-optimizer_rust_dev_container_optimized.png

  • Click on bullseye 
  • Select the additional extra features according to your project and click on ‘OK’

jpeg-optimizer_additional_featurs_optimized.png

  • After clicking ‘OK’ it will create a devcontainer.json file and click re-open in the container

2.png

1.png

Now your project is running inside a container. 

Creating a Custom Docker Image

  • Until now we’ve used a pre-defined docker container in VS code let’s implement a custom docker image and run our program inside the container 
  • Create a docker file: Dockerfile is a simple text file that contains dependencies and pre-requisites 

11111111111111.PNG

  • Implement all dependencies on that docker file(I’m using different project which is not running inside a container)

docker-image-2.PNG

  • Create your devcontainer.json file inside the .devcontainer folder in your source directory and run command docker build command

jpeg-optimizer_jpeg-optimizer_devcontainer-(1)-(1)nigg.png

  • Press again F1 on your keyboard and reopen inside a container.
  • Now congrats your program is running inside a custom container.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads