General

Some useful Docker stuff

Some common commands necessary to build and run own docker image in a container:

Command Description Example
docker pull [image_name] Pulls image from default docker registry: //hub.docker.com/ docker pull ubuntu
docker ps [options] List running containers.
  • Add -a for all available container.
docker ps -a
docker container create [options] [image_name] [comand]

Creates container with pulled image.

  • Add –name to provide desired name for container. Although it is optional, random name would be generated.
  • Add –interactive to keep open standard input
  • Add –tty to add psedudo termintal with STDIN and STDOUT
  • Add command /bin/bash to have terminal working
docker container create –interactive –tty –name my-ubuntu ubuntu /bin/bash
docker start [container_name] Starts container docker start my-ubuntu
docker rm [container_name] Removes container docker rm my-ubuntu
docker attach [container_name] Connects local STDIN, STDOUT, STDERR to container. So we can view can view an output of container or control it via commands. docker attach my-ubuntu
docker build [options] [path]

Build docker image against Dockerfile in provided path.

  • Add -t to provide name for image
  • . stands for location of current folder
docker build -t my-ubuntu-image .
docker run [options] [image_name]

Combines two commands: docker container create and docker start. It remains created after stopping container. May start it later again.

  • Add -p [local_port]:[docker_port] for mapping local environment port to one which is open in docker.
  • Add –name to provide name for container which will be created
docker run -p 3000:8080 my-ubuntu-image

 

Sample of creating docker image with NodeJs application running in it

 

  1. Create super tiny server app in any local filesystem folder. Name it as app.js.
    const http = require('http');
    
    const hostname = '0.0.0.0';
    const port = 3009;
    
    const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');
      res.end('Hello World');
    });
    
    server.listen(port, hostname, () => {
      console.log(`Server running at //${hostname}:${port}/`);
    });
    Note: it is important here to specify hostname as ‘0.0.0.0’ which tells server to listen on all hosts, which will further allow us to connect to it from local mashine. If you would specify i.e. 127.0.0.1 which basically means localhost or loopback, it will listen only to request from internal docker OS (ubuntu in our case).
  2. Create a Dockerfile in the same folder.
    FROM ubuntu:latest
    
    RUN apt-get update
    RUN apt-get install -y nodejs
    RUN apt-get install -y npm
    
    RUN mkdir -p /usr/src/myapp
    COPY . /usr/src/myapp
    WORKDIR /usr/src/myapp
    
    RUN npm install
    
    EXPOSE 3009
    
    CMD node ./app.js
    Here we have:
    • FROM ubuntu:latest – tells docker to pull latest version of Ubuntu OS from docker hub (//hub.docker.com)
    • RUN apt-get update – refreshing ubuntu apt-get database
    • RUN apt-get install -y nodejs – installing NodeJs
    • RUN apt-get install -y npm – installing Node package manager
    • RUN mkdir -p /usr/src/myapp – creating directory for our new app
    • COPY . /usr/src/myapp – copy all contents (in our particular case just app.js is relevant) from local current folder to the one we dedicated on ubuntu
    • WORKDIR /usr/src/myapp – tell docker to use this directory as current working one
    • RUN npm install – build NodeJs application
    • EXPOSE 3009 – make port 3009 accessible from outside
    • CMD node ./app.js – instruction to run main app file of our application when container is started. The difference between RUN and CMD instructions is, that the first one is executed on image build and the second one on container start.

      Note: installing node and npm via apt-get could be skipped here if we choose to build our own docker image from the one which already has embedded needed software. It basically means we can build new image on any other previously made by us or by others.
  3. Build docker image
    docker build -t my-node-app .
  4. Run docker image
    docker run  –name my-node-app-container -p 3009:3009 my-node-app

Here we go. We should be able to access new app from local machine via port 3009 on localhost: //localhost:3009

Hello World! 🙂


Advertisement

About Danas Tarnauskas

Leave a Reply

Your email address will not be published.