All about Docker

All about Docker

Hello Developers!

By Now you must have heard of Docker and Docker containers, but you might not know what they are yet.

In this blog I will explain exactly what Docker is and teach you the basics in order for you to build you first container


What is Docker ?

Docker is an open source platform for bundling you code projects into containers, which you can run on any device.

What is a Docker container ?

A container is a stand alone unit of software with all its:

  • settings
  • system tools
  • libraries/dependencies

It is an isolated process that runs on any machine.

What is the Docker Engine ?

The Docker engine is the industry standard runtime for containers. It is what runs your Docker containers. The Docker engine enables containerized applications to be run consistently on any infrastructure.

The Dockerfile

Exampe Dockerfile

FROM node:14

WORKDIR /app

COPY package.json ./

RUN npm install

COPY . .

ENV PORT=8080

EXPOSE 8080

CMD ["npm", "start"]

The Dockerfile is a the file that contains the code that enables you to build a Docker image.

Dockerfile intructions

Lets look at the different instructions that go into a Dockerfile

  • FROM

    The FROM instruction indicates a base image for your container. When the Dockerfile is run, the base image will be downloaded from Docker's API. A base image can be anything from an OS ( Ubuntu, Debian... ) to a runtime ( Node, Python ... )

    FROM <base image>
    
    FROM node:14
    
  • WORKDIR

    WORKDIR indicates the working directory of your project/app/program inside of your containers local file system.

    WORKDIR /app
    
    ├── var
    ├── bin
    └── app
      ├── src
      └── index.js
    
  • COPY

    The COPY instruction copies files from our local directory into our Docker image. it takes 2 arguments:

    1. location of the file/folder in your project
    2. location to copy them to in the container
    COPY <local path> <container path>
    

    Example: copying package.json to the working directory

    COPY package.json ./
    
  • RUN

    The RUN instruction runs a command in the container

    RUN <command>
    
    RUN npm install
    
  • ENV

    You can use the ENV instruction to set an environment variable in your container

    ENV <variableName>=<value>
    
    ENV API_KEY=ultratopsecret
    
  • EXPOSE

    This instruction allows you to expose the desired port on your container Note: this port will only be accessible to the device running the container! If you want it to be accessible over the internet, you will need to port forward it.

    EXPOSE <port>
    
    EXPOSE 5000
    
  • CMD

    The CMD instruction defines the default action taken when the container is run from the image. It is preferred to use executive form over command form for this. There should only be one CMD in a Dockerfile .

    CMD ['executable', 'param1', 'param2', ...]
    
    CMD ['npm', 'start']
    

Docker ignore

When creating a container, there may be certain things that you don't want to be copied over if you use the COPY . . To do this you can use a .dockerignore file, which works in the same way as a .gitignore file, you add to it the files/directories that you want to be ignored

Example .dockerignore file

node_modules

/database

Important commands

  • Misc

    1. To list all the images on your system: docker images
    2. List all running containers on your system: docker ps
    3. List ALL the containers on your system: docker ps --all
    4. Access a running containers terminal: docker exec -it containerID sh
  • Building an image

    Once you have finished your writing a Dockerfile, you will probably want to create an image. to do thin, run docker build ., this will build an image from the dockerfile in your current directory .. When you run this commend, docker will build your image and give you an image ID, but an image id can be hard to memorize, so instead you can set a name for your image:

    docker build -t <username>/<buildName>:<tag> .
    

    <username> should be set to your Docker hub username.

    Example: docker build oliveringle/example-app:1.0 .

  • Creating/running a container

    To create a container use the command docker create <imageName or imageID>. This will create your container and

    Then use the command docker start <containerName or containerID> to run that container.

    Alternatively, you can use the docker run <imageName or imageID> to create and run a container.

    A container has its own local set of ports that are only accessible inside of the container. if you want them to be accesible on your system, you need to port forward them with the -p flag:

    docker run -p <containerPort:localPort> <imageID or imageName>
    
  • Stopping a container.

    To stop a running container, use the docker stop <containerID>, this can take some time because docker will do clean-up work. If you want to stop a container instantly, use docker kill <containID or containerName>


Thats it for this blog, I hope you were able to learn something! If you found it interesting, consider going and following me on Twitter .

Thanks for reading!