Coder Social home page Coder Social logo

docker-for-beginners's Introduction

Purpose

A basic docker tutorial for the absolute beginner.

Why use Docker?

Docker is a light-weight container based deployment system used by product developers to ensure that their product works no matter what system the consumer happens to be using. Its working is similar to that of a virtual machine configuration except for the fact that the operating system percieves it as a normal process. Therefore important resources like memory and the kernel itself do not have to be split for the container. Docker instead uses resource isolation features that allow us to create more than one container for the same instance. For a more in-depth explanation I suggest you go here.

Prerequisites

  • You must first install Docker. For linux users, simply open your terminal and copy the following command: sudo apt install docker

    For downloads on any other operating system please click here. You may check if it is installed by typing docker in the terminal.
  • Next you must create a new working directory in which you can store your Docker related files. I trust you can do that without too much hassle.
  • In this tutorial we'll be using a simple Hello World program written in Java. You can either write it yourself or copy it from here After saving it with the .java extension move a copy of the file over to the working directory you created earlier

Creating the Docker Container

Now we move on to the actual container creation process. Our aim here is to compile and execute the .java file without actually installing Java on our operating system. Ofcourse if you have Java already installed you need not delete it, rather you can use it to compile your Hello World program and debug it in case of errors.

  • Our first step is to create a Dockerfile. This file governs how the system image is created. I recommend you use VSCode to create this file. You can download VSCode from here Copy the following commands into your Dockerfile:
    FROM java:8
    WORKDIR /home/java-docker-app
    COPY . /home/java-docker-app
    RUN javac helloworld.java
    CMD ["/usr/bin/java","helloworld"]
    An indepth explanation of these commands and their significance can be found here. Do not forgot to save the Dockerfile without an extension. It must be stored in the working directory.
  • We must then build the docker image. We can do this by running the following in our terminal:
    docker build .
    Sending build context to Docker daemon 58.88kB
    Step 1/5 : FROM java:8 ---> d23bdf5b1b1b
    Step 2/5 : WORKDIR /home/java-docker-app
    ---> Using cache ---> c3e6f7477902
    Step 3/5 : COPY . /home/java-docker-app ---> 3f785fe05c97
    Step 4/5 : RUN javac helloworld.java ---> Running in d2af1f4f0362 Removing intermediate container d2af1f4f0362 ---> 4992b15c0312
    Step 5/5 : CMD ["/usr/bin/java","helloworld"]
    ---> Running in ef03ebcde1a7 Removing intermediate container ef03ebcde1a7 ---> 0bc24ec77d5c
    Successfully built 0bc24ec77d5c
  • After building the image we must run it.
    docker run <image tag>
    Here the image tag is the hash issued to us at the end of the build output. In our case it is 0bc24ec77d5c. After running the docker run command we should arrive at the following output:
    Hello World

Fin

Congratulations! You've managed to create your first docker container. If you still have any trouble creating a container I recommend taking a look at the official Docker Documentation which you can find here

docker-for-beginners's People

Contributors

thinktanktom avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.