minte9
LearnRemember




Containerizing

1. Start with your application code and dependencies. 2. Create a Dockerfile that describe the app. 3. Create Docker image with build command. 4. Push the new image to a registry (optional). 5. Run container from Docker image.

1. App

Clone an example app from github.
 
cd /var/www/docker
git clone https://github.com/nigelpoulton/psweb

cd psweb/
ls -l 
    # app.js  circle.yml  Dockerfile  package.json  README.md  test  views

2. Dockerfile

Dockerfile describes an application.
 
# Dockerfile is also a great form of documentation.
# You should treat it like you treat source code.
#
# All Dockerfiles start with a FROM instruction.
# This is the base layer of the image.
#
# Labels are simple key-value pairs.
# The ENTRYPOIN sets the main app that it should run.

cat Dockerfile
    # FROM alpine
    # LABEL maintainer="nigelpoulton@hotmail.com"
    # RUN apk add --update nodejs npm curl
    # COPY . /src
    # WORKDIR /src
    # RUN  npm install
    # EXPOSE 8080
    # ENTRYPOINT ["node", "./app.js"]

3. Build

The following command will build a new image.
 
# Be sure to include the trailing period (.)
# Be sure to run the command from app directory
cd psweb
sudo docker image build -t web.latest .
    # Successfully built 58188456312c
    # Successfully tagged web.latest:latest

# Check that the image exists in Docker host's local repository
docker image ls
    # REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
    # web          latest    58188456312c   26 hours ago   81.9MB
    # alpine       latest    0ac33e5f5afa   4 weeks ago    5.57MB

docker image inspect web:latest
    # The app is containerized! 

4. Hub

Login with you Docker ID on hub.docker.com
 
docker login 
    # Login with your Docker ID to push and pull images from Docker Hub.
    # Username: minte9
    # Password: 
    # Login Succeeded
    
# Before you push an image, 
# you have to tag it in a special way
docker image tag web:latest minte9/web:latest
docker images -a
    # REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
    # <none>       <none>    a101c9391470   27 hours ago   81.9MB
    # web          latest    58188456312c   27 hours ago   81.9MB
    # minte9/web   latest    58188456312c   27 hours ago   81.9MB
    
# Push
docker image push minte9/web:latest
    # The push refers to repository [docker.io/minte9/web]
    # 46c9fd5d5035: Pushed 
    # 5fa0ff1c9a29: Pushed 
    # 0b536982eb25: Pushed 
    # 4fc242d58285: Mounted from library/alpine 

# Pull
docker image pull minte9/web:latest
    # latest: Pulling from minte9/web
    # Digest: sha256:e5527b091fd5c60998622b38e7758a375f1c4cdf8
    # Status: Downloaded newer image for minte9/web:latest
    # docker.io/minte9/web:latest

Run

The containerized app is a web server on port 8080.
 
# Start a new container named web2
docker container run -d --name web2 --publish 8080:8080 minte9/web:latest
    # http://localhost:8080/
    
docker container stop web2
    # http://localhost:8080/ # 404, not found
    
docker container rm web2    
docker container ls -a
    # CONTAINER ID   IMAGE               COMMAND    NAMES
    # 43075e292233   minte9/web:latest   "node ./app.js"    c1



  Last update: 130 days ago